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()
This commit is contained in:
Victor Stinner 2011-11-22 02:27:30 +01:00
parent b84d723509
commit 9d3b93ba30
10 changed files with 38 additions and 41 deletions

View file

@ -4255,7 +4255,7 @@ Array_subscript(PyObject *_self, PyObject *item)
wchar_t *dest; wchar_t *dest;
if (slicelen <= 0) if (slicelen <= 0)
return PyUnicode_FromUnicode(NULL, 0); return PyUnicode_New(0, 0);
if (step == 1) { if (step == 1) {
return PyUnicode_FromWideChar(ptr + start, return PyUnicode_FromWideChar(ptr + start,
slicelen); slicelen);
@ -4930,7 +4930,7 @@ Pointer_subscript(PyObject *_self, PyObject *item)
wchar_t *dest; wchar_t *dest;
if (len <= 0) if (len <= 0)
return PyUnicode_FromUnicode(NULL, 0); return PyUnicode_New(0, 0);
if (step == 1) { if (step == 1) {
return PyUnicode_FromWideChar(ptr + start, return PyUnicode_FromWideChar(ptr + start,
len); len);

View file

@ -2599,7 +2599,7 @@ posix_listdir(PyObject *self, PyObject *args)
/* Skip over . and .. */ /* Skip over . and .. */
if (wcscmp(wFileData.cFileName, L".") != 0 && if (wcscmp(wFileData.cFileName, L".") != 0 &&
wcscmp(wFileData.cFileName, L"..") != 0) { wcscmp(wFileData.cFileName, L"..") != 0) {
v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName));
if (v == NULL) { if (v == NULL) {
Py_DECREF(d); Py_DECREF(d);
d = NULL; d = NULL;
@ -2967,7 +2967,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
} }
if (result) if (result)
v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
else else
v = win32_error_object("GetFullPathNameW", po); v = win32_error_object("GetFullPathNameW", po);
if (woutbufp != woutbuf) if (woutbufp != woutbuf)
@ -3054,7 +3054,7 @@ posix__getfinalpathname(PyObject *self, PyObject *args)
return win32_error_object("CloseHandle", po); return win32_error_object("CloseHandle", po);
target_path[result_length] = 0; target_path[result_length] = 0;
result = PyUnicode_FromUnicode(target_path, result_length); result = PyUnicode_FromWideChar(target_path, result_length);
free(target_path); free(target_path);
return result; return result;
@ -7750,7 +7750,7 @@ static PyObject *
posix_putenv(PyObject *self, PyObject *args) posix_putenv(PyObject *self, PyObject *args)
{ {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
wchar_t *s1, *s2; PyObject *s1, *s2;
wchar_t *newenv; wchar_t *newenv;
#else #else
PyObject *os1, *os2; PyObject *os1, *os2;
@ -7762,7 +7762,7 @@ posix_putenv(PyObject *self, PyObject *args)
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
if (!PyArg_ParseTuple(args, if (!PyArg_ParseTuple(args,
"uu:putenv", "UU:putenv",
&s1, &s2)) &s1, &s2))
return NULL; return NULL;
#else #else
@ -7799,26 +7799,26 @@ posix_putenv(PyObject *self, PyObject *args)
/* len includes space for a trailing \0; the size arg to /* len includes space for a trailing \0; the size arg to
PyBytes_FromStringAndSize does not count that */ PyBytes_FromStringAndSize does not count that */
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
len = wcslen(s1) + wcslen(s2) + 2; newstr = PyUnicode_FromFormat("%U=%U", s1, s2);
newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
#else
len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
#endif
if (newstr == NULL) { if (newstr == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
goto error; goto error;
} }
#ifdef MS_WINDOWS
newenv = PyUnicode_AsUnicode(newstr); newenv = PyUnicode_AsUnicode(newstr);
if (newenv == NULL) if (newenv == NULL)
goto error; goto error;
_snwprintf(newenv, len, L"%s=%s", s1, s2);
if (_wputenv(newenv)) { if (_wputenv(newenv)) {
posix_error(); posix_error();
goto error; goto error;
} }
#else #else
len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
if (newstr == NULL) {
PyErr_NoMemory();
goto error;
}
newenv = PyBytes_AS_STRING(newstr); newenv = PyBytes_AS_STRING(newstr);
PyOS_snprintf(newenv, len, "%s=%s", s1, s2); PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
if (putenv(newenv)) { if (putenv(newenv)) {

View file

@ -3900,7 +3900,7 @@ socket_gethostname(PyObject *self, PyObject *unused)
PyObject *result; PyObject *result;
if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
return PyUnicode_FromUnicode(buf, size); return PyUnicode_FromWideChar(buf, size);
if (GetLastError() != ERROR_MORE_DATA) if (GetLastError() != ERROR_MORE_DATA)
return PyErr_SetFromWindowsErr(0); return PyErr_SetFromWindowsErr(0);

View file

@ -701,7 +701,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
/* If no format_spec is provided, use an empty string */ /* If no format_spec is provided, use an empty string */
if (format_spec == NULL) { if (format_spec == NULL) {
empty = PyUnicode_FromUnicode(NULL, 0); empty = PyUnicode_New(0, 0);
format_spec = empty; format_spec = empty;
} }

View file

@ -18,7 +18,7 @@ static PyObject*
uuidcreate(PyObject* obj, PyObject*args) uuidcreate(PyObject* obj, PyObject*args)
{ {
UUID result; UUID result;
unsigned short *cresult; wchar_t *cresult;
PyObject *oresult; PyObject *oresult;
/* May return ok, local only, and no address. /* May return ok, local only, and no address.
@ -35,7 +35,7 @@ uuidcreate(PyObject* obj, PyObject*args)
return NULL; return NULL;
} }
oresult = PyUnicode_FromUnicode(cresult, wcslen(cresult)); oresult = PyUnicode_FromWideChar(cresult, wcslen(cresult));
RpcStringFreeW(&cresult); RpcStringFreeW(&cresult);
return oresult; return oresult;
@ -379,7 +379,7 @@ record_getstring(msiobj* record, PyObject* args)
} }
if (status != ERROR_SUCCESS) if (status != ERROR_SUCCESS)
return msierror((int) status); return msierror((int) status);
string = PyUnicode_FromUnicode(res, size); string = PyUnicode_FromWideChar(res, size);
if (buf != res) if (buf != res)
free(res); free(res);
return string; return string;
@ -401,7 +401,7 @@ record_setstring(msiobj* record, PyObject *args)
{ {
int status; int status;
int field; int field;
Py_UNICODE *data; wchar_t *data;
if (!PyArg_ParseTuple(args, "iu:SetString", &field, &data)) if (!PyArg_ParseTuple(args, "iu:SetString", &field, &data))
return NULL; return NULL;
@ -418,7 +418,7 @@ record_setstream(msiobj* record, PyObject *args)
{ {
int status; int status;
int field; int field;
Py_UNICODE *data; wchar_t *data;
if (!PyArg_ParseTuple(args, "iu:SetStream", &field, &data)) if (!PyArg_ParseTuple(args, "iu:SetStream", &field, &data))
return NULL; return NULL;

View file

@ -419,14 +419,14 @@ sp_CreateProcess(PyObject* self, PyObject* args)
PyObject* environment; PyObject* environment;
wchar_t *wenvironment; wchar_t *wenvironment;
Py_UNICODE* application_name; wchar_t* application_name;
Py_UNICODE* command_line; wchar_t* command_line;
PyObject* process_attributes; /* ignored */ PyObject* process_attributes; /* ignored */
PyObject* thread_attributes; /* ignored */ PyObject* thread_attributes; /* ignored */
int inherit_handles; int inherit_handles;
int creation_flags; int creation_flags;
PyObject* env_mapping; PyObject* env_mapping;
Py_UNICODE* current_directory; wchar_t* current_directory;
PyObject* startup_info; PyObject* startup_info;
if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess", if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess",
@ -454,15 +454,10 @@ sp_CreateProcess(PyObject* self, PyObject* args)
if (PyErr_Occurred()) if (PyErr_Occurred())
return NULL; return NULL;
if (env_mapping == Py_None) if (env_mapping != Py_None) {
environment = NULL;
else {
environment = getenvironment(env_mapping); environment = getenvironment(env_mapping);
if (! environment) if (! environment)
return NULL; return NULL;
}
if (environment) {
wenvironment = PyUnicode_AsUnicode(environment); wenvironment = PyUnicode_AsUnicode(environment);
if (wenvironment == NULL) if (wenvironment == NULL)
{ {
@ -470,8 +465,10 @@ sp_CreateProcess(PyObject* self, PyObject* args)
return NULL; return NULL;
} }
} }
else else {
environment = NULL;
wenvironment = NULL; wenvironment = NULL;
}
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
result = CreateProcessW(application_name, result = CreateProcessW(application_name,

View file

@ -211,7 +211,7 @@ cannot be read with this function.");
static PyObject * static PyObject *
msvcrt_getwch(PyObject *self, PyObject *args) msvcrt_getwch(PyObject *self, PyObject *args)
{ {
Py_UNICODE ch; wchar_t ch;
if (!PyArg_ParseTuple(args, ":getwch")) if (!PyArg_ParseTuple(args, ":getwch"))
return NULL; return NULL;
@ -254,7 +254,7 @@ a printable character.");
static PyObject * static PyObject *
msvcrt_getwche(PyObject *self, PyObject *args) msvcrt_getwche(PyObject *self, PyObject *args)
{ {
Py_UNICODE ch; wchar_t ch;
if (!PyArg_ParseTuple(args, ":getwche")) if (!PyArg_ParseTuple(args, ":getwche"))
return NULL; return NULL;

View file

@ -1227,8 +1227,8 @@ PyEnumValue(PyObject *self, PyObject *args)
static PyObject * static PyObject *
PyExpandEnvironmentStrings(PyObject *self, PyObject *args) PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
{ {
Py_UNICODE *retValue = NULL; wchar_t *retValue = NULL;
Py_UNICODE *src; wchar_t *src;
DWORD retValueSize; DWORD retValueSize;
DWORD rc; DWORD rc;
PyObject *o; PyObject *o;
@ -1241,7 +1241,7 @@ PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
return PyErr_SetFromWindowsErrWithFunction(retValueSize, return PyErr_SetFromWindowsErrWithFunction(retValueSize,
"ExpandEnvironmentStrings"); "ExpandEnvironmentStrings");
} }
retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE)); retValue = (wchar_t *)PyMem_Malloc(retValueSize * sizeof(wchar_t));
if (retValue == NULL) { if (retValue == NULL) {
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
@ -1252,7 +1252,7 @@ PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
return PyErr_SetFromWindowsErrWithFunction(retValueSize, return PyErr_SetFromWindowsErrWithFunction(retValueSize,
"ExpandEnvironmentStrings"); "ExpandEnvironmentStrings");
} }
o = PyUnicode_FromUnicode(retValue, wcslen(retValue)); o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
PyMem_Free(retValue); PyMem_Free(retValue);
return o; return o;
} }
@ -1537,7 +1537,7 @@ PySetValueEx(PyObject *self, PyObject *args)
{ {
HKEY hKey; HKEY hKey;
PyObject *obKey; PyObject *obKey;
Py_UNICODE *valueName; wchar_t *valueName;
PyObject *obRes; PyObject *obRes;
PyObject *value; PyObject *value;
BYTE *data; BYTE *data;

View file

@ -72,7 +72,7 @@ PyDoc_STRVAR(sound_module_doc,
static PyObject * static PyObject *
sound_playsound(PyObject *s, PyObject *args) sound_playsound(PyObject *s, PyObject *args)
{ {
Py_UNICODE *wsound; wchar_t *wsound;
int flags; int flags;
int ok; int ok;

View file

@ -249,7 +249,7 @@ dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
"DLL load failed: "); "DLL load failed: ");
PyUnicode_AppendAndDel(&message, PyUnicode_AppendAndDel(&message,
PyUnicode_FromUnicode( PyUnicode_FromWideChar(
theInfo, theInfo,
theLength)); theLength));
} }