mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Implement PEP 393.
This commit is contained in:
parent
48d49497c5
commit
d63a3b8beb
102 changed files with 8153 additions and 5431 deletions
|
@ -329,12 +329,9 @@ gethandle(PyObject* obj, char* name)
|
|||
static PyObject*
|
||||
getenvironment(PyObject* environment)
|
||||
{
|
||||
int i;
|
||||
Py_ssize_t envsize;
|
||||
PyObject* out = NULL;
|
||||
PyObject* keys;
|
||||
PyObject* values;
|
||||
Py_UNICODE* p;
|
||||
Py_ssize_t i, envsize, totalsize;
|
||||
Py_UCS4 *buffer = NULL, *p, *end;
|
||||
PyObject *keys, *values, *res;
|
||||
|
||||
/* convert environment dictionary to windows enviroment string */
|
||||
if (! PyMapping_Check(environment)) {
|
||||
|
@ -350,14 +347,8 @@ getenvironment(PyObject* environment)
|
|||
if (!keys || !values)
|
||||
goto error;
|
||||
|
||||
out = PyUnicode_FromUnicode(NULL, 2048);
|
||||
if (! out)
|
||||
goto error;
|
||||
|
||||
p = PyUnicode_AS_UNICODE(out);
|
||||
|
||||
totalsize = 1; /* trailing null character */
|
||||
for (i = 0; i < envsize; i++) {
|
||||
Py_ssize_t ksize, vsize, totalsize;
|
||||
PyObject* key = PyList_GET_ITEM(keys, i);
|
||||
PyObject* value = PyList_GET_ITEM(values, i);
|
||||
|
||||
|
@ -366,36 +357,42 @@ getenvironment(PyObject* environment)
|
|||
"environment can only contain strings");
|
||||
goto error;
|
||||
}
|
||||
ksize = PyUnicode_GET_SIZE(key);
|
||||
vsize = PyUnicode_GET_SIZE(value);
|
||||
totalsize = (p - PyUnicode_AS_UNICODE(out)) + ksize + 1 +
|
||||
vsize + 1 + 1;
|
||||
if (totalsize > PyUnicode_GET_SIZE(out)) {
|
||||
Py_ssize_t offset = p - PyUnicode_AS_UNICODE(out);
|
||||
PyUnicode_Resize(&out, totalsize + 1024);
|
||||
p = PyUnicode_AS_UNICODE(out) + offset;
|
||||
}
|
||||
Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(key), ksize);
|
||||
p += ksize;
|
||||
totalsize += PyUnicode_GET_LENGTH(key) + 1; /* +1 for '=' */
|
||||
totalsize += PyUnicode_GET_LENGTH(value) + 1; /* +1 for '\0' */
|
||||
}
|
||||
|
||||
buffer = PyMem_Malloc(totalsize * sizeof(Py_UCS4));
|
||||
if (! buffer)
|
||||
goto error;
|
||||
p = buffer;
|
||||
end = buffer + totalsize;
|
||||
|
||||
for (i = 0; i < envsize; i++) {
|
||||
PyObject* key = PyList_GET_ITEM(keys, i);
|
||||
PyObject* value = PyList_GET_ITEM(values, i);
|
||||
if (!PyUnicode_AsUCS4(key, p, end - p, 0))
|
||||
goto error;
|
||||
p += PyUnicode_GET_LENGTH(key);
|
||||
*p++ = '=';
|
||||
Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(value), vsize);
|
||||
p += vsize;
|
||||
if (!PyUnicode_AsUCS4(value, p, end - p, 0))
|
||||
goto error;
|
||||
p += PyUnicode_GET_LENGTH(value);
|
||||
*p++ = '\0';
|
||||
}
|
||||
|
||||
/* add trailing null byte */
|
||||
*p++ = '\0';
|
||||
PyUnicode_Resize(&out, p - PyUnicode_AS_UNICODE(out));
|
||||
|
||||
/* PyObject_Print(out, stdout, 0); */
|
||||
assert(p == end);
|
||||
|
||||
Py_XDECREF(keys);
|
||||
Py_XDECREF(values);
|
||||
|
||||
return out;
|
||||
res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, p - buffer);
|
||||
PyMem_Free(buffer);
|
||||
return res;
|
||||
|
||||
error:
|
||||
Py_XDECREF(out);
|
||||
PyMem_Free(buffer);
|
||||
Py_XDECREF(keys);
|
||||
Py_XDECREF(values);
|
||||
return NULL;
|
||||
|
@ -609,7 +606,7 @@ sp_GetModuleFileName(PyObject* self, PyObject* args)
|
|||
if (! result)
|
||||
return PyErr_SetFromWindowsErr(GetLastError());
|
||||
|
||||
return PyUnicode_FromUnicode(filename, Py_UNICODE_strlen(filename));
|
||||
return PyUnicode_FromWideChar(filename, wcslen(filename));
|
||||
}
|
||||
|
||||
static PyMethodDef sp_functions[] = {
|
||||
|
|
|
@ -93,7 +93,7 @@ _PyWin_FindRegisteredModule(PyObject *moduleName,
|
|||
}
|
||||
if (fdp->suffix == NULL)
|
||||
return NULL;
|
||||
path = PyUnicode_FromUnicode(pathBuf, wcslen(pathBuf));
|
||||
path = PyUnicode_FromWideChar(pathBuf, wcslen(pathBuf));
|
||||
if (path == NULL)
|
||||
return NULL;
|
||||
fp = _Py_fopen(path, fdp->mode);
|
||||
|
|
|
@ -212,7 +212,6 @@ static PyObject *
|
|||
msvcrt_getwch(PyObject *self, PyObject *args)
|
||||
{
|
||||
Py_UNICODE ch;
|
||||
Py_UNICODE u[1];
|
||||
|
||||
if (!PyArg_ParseTuple(args, ":getwch"))
|
||||
return NULL;
|
||||
|
@ -220,8 +219,7 @@ msvcrt_getwch(PyObject *self, PyObject *args)
|
|||
Py_BEGIN_ALLOW_THREADS
|
||||
ch = _getwch();
|
||||
Py_END_ALLOW_THREADS
|
||||
u[0] = ch;
|
||||
return PyUnicode_FromUnicode(u, 1);
|
||||
return PyUnicode_FromOrdinal(ch);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(getwch_doc,
|
||||
|
@ -257,7 +255,6 @@ static PyObject *
|
|||
msvcrt_getwche(PyObject *self, PyObject *args)
|
||||
{
|
||||
Py_UNICODE ch;
|
||||
Py_UNICODE s[1];
|
||||
|
||||
if (!PyArg_ParseTuple(args, ":getwche"))
|
||||
return NULL;
|
||||
|
@ -265,8 +262,7 @@ msvcrt_getwche(PyObject *self, PyObject *args)
|
|||
Py_BEGIN_ALLOW_THREADS
|
||||
ch = _getwche();
|
||||
Py_END_ALLOW_THREADS
|
||||
s[0] = ch;
|
||||
return PyUnicode_FromUnicode(s, 1);
|
||||
return PyUnicode_FromOrdinal(ch);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(getwche_doc,
|
||||
|
|
|
@ -550,10 +550,6 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
|
|||
/* Define if you want to use the GNU readline library */
|
||||
/* #define WITH_READLINE 1 */
|
||||
|
||||
/* Define as the size of the unicode type. */
|
||||
/* This is enough for unicodeobject.h to do the "right thing" on Windows. */
|
||||
#define Py_UNICODE_SIZE 2
|
||||
|
||||
/* Use Python's own small-block memory-allocator. */
|
||||
#define WITH_PYMALLOC 1
|
||||
|
||||
|
|
|
@ -882,7 +882,7 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
|
|||
retDataSize -= 2;
|
||||
if (retDataSize <= 0)
|
||||
data = L"";
|
||||
obData = PyUnicode_FromUnicode(data, retDataSize/2);
|
||||
obData = PyUnicode_FromWideChar(data, retDataSize/2);
|
||||
break;
|
||||
}
|
||||
case REG_MULTI_SZ:
|
||||
|
@ -913,7 +913,7 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
|
|||
}
|
||||
PyList_SetItem(obData,
|
||||
index,
|
||||
PyUnicode_FromUnicode(str[index], len));
|
||||
PyUnicode_FromWideChar(str[index], len));
|
||||
}
|
||||
free(str);
|
||||
|
||||
|
@ -1123,7 +1123,7 @@ PyEnumKey(PyObject *self, PyObject *args)
|
|||
if (rc != ERROR_SUCCESS)
|
||||
return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
|
||||
|
||||
retStr = PyUnicode_FromUnicode(tmpbuf, len);
|
||||
retStr = PyUnicode_FromWideChar(tmpbuf, len);
|
||||
return retStr; /* can be NULL */
|
||||
}
|
||||
|
||||
|
@ -1394,7 +1394,7 @@ PyQueryValue(PyObject *self, PyObject *args)
|
|||
"RegQueryValue");
|
||||
}
|
||||
|
||||
retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
|
||||
retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
|
||||
PyMem_Free(retBuf);
|
||||
return retStr;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue