summaryrefslogtreecommitdiffstats
path: root/PC/_subprocess.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2011-09-28 05:41:54 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2011-09-28 05:41:54 (GMT)
commitd63a3b8beb4a0841cb59fb3515347ccaab34b733 (patch)
tree3b4e3cc63151c5a5a910c3550a190aefaea96ad4 /PC/_subprocess.c
parent48d49497c50e79d14e9df9527d766ca3a0a38be5 (diff)
downloadcpython-d63a3b8beb4a0841cb59fb3515347ccaab34b733.zip
cpython-d63a3b8beb4a0841cb59fb3515347ccaab34b733.tar.gz
cpython-d63a3b8beb4a0841cb59fb3515347ccaab34b733.tar.bz2
Implement PEP 393.
Diffstat (limited to 'PC/_subprocess.c')
-rw-r--r--PC/_subprocess.c61
1 files changed, 29 insertions, 32 deletions
diff --git a/PC/_subprocess.c b/PC/_subprocess.c
index f11e3e3..f0ad559 100644
--- a/PC/_subprocess.c
+++ b/PC/_subprocess.c
@@ -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[] = {