Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d3b93b

Browse files
author
Victor Stinner
committed
Use the new Unicode API
* Replace PyUnicode_FromUnicode(NULL, 0) by PyUnicode_New(0, 0) * Replce PyUnicode_FromUnicode(str, len) by PyUnicode_FromWideChar(str, len) * Replace Py_UNICODE by wchar_t * posix_putenv() uses PyUnicode_FromFormat() to create the string, instead of PyUnicode_FromUnicode() + _snwprintf()
1 parent b84d723 commit 9d3b93b

10 files changed

Lines changed: 38 additions & 41 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4255,7 +4255,7 @@ Array_subscript(PyObject *_self, PyObject *item)
42554255
wchar_t *dest;
42564256

42574257
if (slicelen <= 0)
4258-
return PyUnicode_FromUnicode(NULL, 0);
4258+
return PyUnicode_New(0, 0);
42594259
if (step == 1) {
42604260
return PyUnicode_FromWideChar(ptr + start,
42614261
slicelen);
@@ -4930,7 +4930,7 @@ Pointer_subscript(PyObject *_self, PyObject *item)
49304930
wchar_t *dest;
49314931

49324932
if (len <= 0)
4933-
return PyUnicode_FromUnicode(NULL, 0);
4933+
return PyUnicode_New(0, 0);
49344934
if (step == 1) {
49354935
return PyUnicode_FromWideChar(ptr + start,
49364936
len);

Modules/posixmodule.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,7 +2599,7 @@ posix_listdir(PyObject *self, PyObject *args)
25992599
/* Skip over . and .. */
26002600
if (wcscmp(wFileData.cFileName, L".") != 0 &&
26012601
wcscmp(wFileData.cFileName, L"..") != 0) {
2602-
v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2602+
v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName));
26032603
if (v == NULL) {
26042604
Py_DECREF(d);
26052605
d = NULL;
@@ -2967,7 +2967,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
29672967
result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
29682968
}
29692969
if (result)
2970-
v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2970+
v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
29712971
else
29722972
v = win32_error_object("GetFullPathNameW", po);
29732973
if (woutbufp != woutbuf)
@@ -3054,7 +3054,7 @@ posix__getfinalpathname(PyObject *self, PyObject *args)
30543054
return win32_error_object("CloseHandle", po);
30553055

30563056
target_path[result_length] = 0;
3057-
result = PyUnicode_FromUnicode(target_path, result_length);
3057+
result = PyUnicode_FromWideChar(target_path, result_length);
30583058
free(target_path);
30593059
return result;
30603060

@@ -7750,7 +7750,7 @@ static PyObject *
77507750
posix_putenv(PyObject *self, PyObject *args)
77517751
{
77527752
#ifdef MS_WINDOWS
7753-
wchar_t *s1, *s2;
7753+
PyObject *s1, *s2;
77547754
wchar_t *newenv;
77557755
#else
77567756
PyObject *os1, *os2;
@@ -7762,7 +7762,7 @@ posix_putenv(PyObject *self, PyObject *args)
77627762

77637763
#ifdef MS_WINDOWS
77647764
if (!PyArg_ParseTuple(args,
7765-
"uu:putenv",
7765+
"UU:putenv",
77667766
&s1, &s2))
77677767
return NULL;
77687768
#else
@@ -7799,26 +7799,26 @@ posix_putenv(PyObject *self, PyObject *args)
77997799
/* len includes space for a trailing \0; the size arg to
78007800
PyBytes_FromStringAndSize does not count that */
78017801
#ifdef MS_WINDOWS
7802-
len = wcslen(s1) + wcslen(s2) + 2;
7803-
newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
7804-
#else
7805-
len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
7806-
newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
7807-
#endif
7802+
newstr = PyUnicode_FromFormat("%U=%U", s1, s2);
78087803
if (newstr == NULL) {
78097804
PyErr_NoMemory();
78107805
goto error;
78117806
}
7812-
#ifdef MS_WINDOWS
78137807
newenv = PyUnicode_AsUnicode(newstr);
78147808
if (newenv == NULL)
78157809
goto error;
7816-
_snwprintf(newenv, len, L"%s=%s", s1, s2);
78177810
if (_wputenv(newenv)) {
78187811
posix_error();
78197812
goto error;
78207813
}
78217814
#else
7815+
len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
7816+
newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
7817+
if (newstr == NULL) {
7818+
PyErr_NoMemory();
7819+
goto error;
7820+
}
7821+
78227822
newenv = PyBytes_AS_STRING(newstr);
78237823
PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
78247824
if (putenv(newenv)) {

Modules/socketmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3900,7 +3900,7 @@ socket_gethostname(PyObject *self, PyObject *unused)
39003900
PyObject *result;
39013901

39023902
if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
3903-
return PyUnicode_FromUnicode(buf, size);
3903+
return PyUnicode_FromWideChar(buf, size);
39043904

39053905
if (GetLastError() != ERROR_MORE_DATA)
39063906
return PyErr_SetFromWindowsErr(0);

Objects/abstract.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
701701

702702
/* If no format_spec is provided, use an empty string */
703703
if (format_spec == NULL) {
704-
empty = PyUnicode_FromUnicode(NULL, 0);
704+
empty = PyUnicode_New(0, 0);
705705
format_spec = empty;
706706
}
707707

PC/_msi.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static PyObject*
1818
uuidcreate(PyObject* obj, PyObject*args)
1919
{
2020
UUID result;
21-
unsigned short *cresult;
21+
wchar_t *cresult;
2222
PyObject *oresult;
2323

2424
/* May return ok, local only, and no address.
@@ -35,7 +35,7 @@ uuidcreate(PyObject* obj, PyObject*args)
3535
return NULL;
3636
}
3737

38-
oresult = PyUnicode_FromUnicode(cresult, wcslen(cresult));
38+
oresult = PyUnicode_FromWideChar(cresult, wcslen(cresult));
3939
RpcStringFreeW(&cresult);
4040
return oresult;
4141

@@ -379,7 +379,7 @@ record_getstring(msiobj* record, PyObject* args)
379379
}
380380
if (status != ERROR_SUCCESS)
381381
return msierror((int) status);
382-
string = PyUnicode_FromUnicode(res, size);
382+
string = PyUnicode_FromWideChar(res, size);
383383
if (buf != res)
384384
free(res);
385385
return string;
@@ -401,7 +401,7 @@ record_setstring(msiobj* record, PyObject *args)
401401
{
402402
int status;
403403
int field;
404-
Py_UNICODE *data;
404+
wchar_t *data;
405405

406406
if (!PyArg_ParseTuple(args, "iu:SetString", &field, &data))
407407
return NULL;
@@ -418,7 +418,7 @@ record_setstream(msiobj* record, PyObject *args)
418418
{
419419
int status;
420420
int field;
421-
Py_UNICODE *data;
421+
wchar_t *data;
422422

423423
if (!PyArg_ParseTuple(args, "iu:SetStream", &field, &data))
424424
return NULL;

PC/_subprocess.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,14 @@ sp_CreateProcess(PyObject* self, PyObject* args)
419419
PyObject* environment;
420420
wchar_t *wenvironment;
421421

422-
Py_UNICODE* application_name;
423-
Py_UNICODE* command_line;
422+
wchar_t* application_name;
423+
wchar_t* command_line;
424424
PyObject* process_attributes; /* ignored */
425425
PyObject* thread_attributes; /* ignored */
426426
int inherit_handles;
427427
int creation_flags;
428428
PyObject* env_mapping;
429-
Py_UNICODE* current_directory;
429+
wchar_t* current_directory;
430430
PyObject* startup_info;
431431

432432
if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess",
@@ -454,24 +454,21 @@ sp_CreateProcess(PyObject* self, PyObject* args)
454454
if (PyErr_Occurred())
455455
return NULL;
456456

457-
if (env_mapping == Py_None)
458-
environment = NULL;
459-
else {
457+
if (env_mapping != Py_None) {
460458
environment = getenvironment(env_mapping);
461459
if (! environment)
462460
return NULL;
463-
}
464-
465-
if (environment) {
466461
wenvironment = PyUnicode_AsUnicode(environment);
467462
if (wenvironment == NULL)
468463
{
469464
Py_XDECREF(environment);
470465
return NULL;
471466
}
472467
}
473-
else
468+
else {
469+
environment = NULL;
474470
wenvironment = NULL;
471+
}
475472

476473
Py_BEGIN_ALLOW_THREADS
477474
result = CreateProcessW(application_name,

PC/msvcrtmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ cannot be read with this function.");
211211
static PyObject *
212212
msvcrt_getwch(PyObject *self, PyObject *args)
213213
{
214-
Py_UNICODE ch;
214+
wchar_t ch;
215215

216216
if (!PyArg_ParseTuple(args, ":getwch"))
217217
return NULL;
@@ -254,7 +254,7 @@ a printable character.");
254254
static PyObject *
255255
msvcrt_getwche(PyObject *self, PyObject *args)
256256
{
257-
Py_UNICODE ch;
257+
wchar_t ch;
258258

259259
if (!PyArg_ParseTuple(args, ":getwche"))
260260
return NULL;

PC/winreg.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,8 @@ PyEnumValue(PyObject *self, PyObject *args)
12271227
static PyObject *
12281228
PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
12291229
{
1230-
Py_UNICODE *retValue = NULL;
1231-
Py_UNICODE *src;
1230+
wchar_t *retValue = NULL;
1231+
wchar_t *src;
12321232
DWORD retValueSize;
12331233
DWORD rc;
12341234
PyObject *o;
@@ -1241,7 +1241,7 @@ PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
12411241
return PyErr_SetFromWindowsErrWithFunction(retValueSize,
12421242
"ExpandEnvironmentStrings");
12431243
}
1244-
retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1244+
retValue = (wchar_t *)PyMem_Malloc(retValueSize * sizeof(wchar_t));
12451245
if (retValue == NULL) {
12461246
return PyErr_NoMemory();
12471247
}
@@ -1252,7 +1252,7 @@ PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
12521252
return PyErr_SetFromWindowsErrWithFunction(retValueSize,
12531253
"ExpandEnvironmentStrings");
12541254
}
1255-
o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1255+
o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
12561256
PyMem_Free(retValue);
12571257
return o;
12581258
}
@@ -1537,7 +1537,7 @@ PySetValueEx(PyObject *self, PyObject *args)
15371537
{
15381538
HKEY hKey;
15391539
PyObject *obKey;
1540-
Py_UNICODE *valueName;
1540+
wchar_t *valueName;
15411541
PyObject *obRes;
15421542
PyObject *value;
15431543
BYTE *data;

PC/winsound.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ PyDoc_STRVAR(sound_module_doc,
7272
static PyObject *
7373
sound_playsound(PyObject *s, PyObject *args)
7474
{
75-
Py_UNICODE *wsound;
75+
wchar_t *wsound;
7676
int flags;
7777
int ok;
7878

Python/dynload_win.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
249249
"DLL load failed: ");
250250

251251
PyUnicode_AppendAndDel(&message,
252-
PyUnicode_FromUnicode(
252+
PyUnicode_FromWideChar(
253253
theInfo,
254254
theLength));
255255
}

0 commit comments

Comments
 (0)