summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-09-10 23:49:04 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-09-10 23:49:04 (GMT)
commitcb04352e8cb1f81a4bc8b741734713c2877b3d47 (patch)
tree05e163a2768b55b3293af83900d4f49465aabe34
parent1017ae525321c89492e8f8e54ce49b8197acdf4f (diff)
downloadcpython-cb04352e8cb1f81a4bc8b741734713c2877b3d47.zip
cpython-cb04352e8cb1f81a4bc8b741734713c2877b3d47.tar.gz
cpython-cb04352e8cb1f81a4bc8b741734713c2877b3d47.tar.bz2
Issue #9579, #9580: Fix os.confstr() for value longer than 255 bytes and encode
the value with filesystem encoding and surrogateescape (instead of utf-8 in strict mode).
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/posixmodule.c42
2 files changed, 26 insertions, 20 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 579fc04..007629f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,10 @@ Core and Builtins
Library
-------
+- Issue #9579, #9580: Fix os.confstr() for value longer than 255 bytes and
+ encode the value with filesystem encoding and surrogateescape (instead of
+ utf-8 in strict mode).
+
- Issue #9632: Remove sys.setfilesystemencoding() function: use
PYTHONFSENCODING environment variable to set the filesystem encoding at
Python startup. sys.setfilesystemencoding() creates inconsistencies because
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 58b1206..82bbd91 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -6721,32 +6721,34 @@ posix_confstr(PyObject *self, PyObject *args)
{
PyObject *result = NULL;
int name;
- char buffer[256];
-
- if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
+ char buffer[255];
int len;
- errno = 0;
- len = confstr(name, buffer, sizeof(buffer));
- if (len == 0) {
- if (errno) {
- posix_error();
- }
- else {
- result = Py_None;
- Py_INCREF(Py_None);
- }
+ if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
+ return NULL;
+
+ errno = 0;
+ len = confstr(name, buffer, sizeof(buffer));
+ if (len == 0) {
+ if (errno) {
+ posix_error();
+ return NULL;
}
else {
- if ((unsigned int)len >= sizeof(buffer)) {
- result = PyUnicode_FromStringAndSize(NULL, len-1);
- if (result != NULL)
- confstr(name, _PyUnicode_AsString(result), len);
- }
- else
- result = PyUnicode_FromStringAndSize(buffer, len-1);
+ Py_RETURN_NONE;
}
}
+
+ if ((unsigned int)len >= sizeof(buffer)) {
+ char *buf = PyMem_Malloc(len);
+ if (buf == NULL)
+ return PyErr_NoMemory();
+ confstr(name, buf, len);
+ result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
+ PyMem_Free(buf);
+ }
+ else
+ result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
return result;
}
#endif