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).
This commit is contained in:
Victor Stinner 2010-09-10 23:49:04 +00:00
parent 1017ae5253
commit cb04352e8c
2 changed files with 26 additions and 20 deletions

View file

@ -29,6 +29,10 @@ Core and Builtins
Library 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 - Issue #9632: Remove sys.setfilesystemencoding() function: use
PYTHONFSENCODING environment variable to set the filesystem encoding at PYTHONFSENCODING environment variable to set the filesystem encoding at
Python startup. sys.setfilesystemencoding() creates inconsistencies because Python startup. sys.setfilesystemencoding() creates inconsistencies because

View file

@ -6721,32 +6721,34 @@ posix_confstr(PyObject *self, PyObject *args)
{ {
PyObject *result = NULL; PyObject *result = NULL;
int name; int name;
char buffer[256]; char buffer[255];
if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
int len; int len;
if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
return NULL;
errno = 0; errno = 0;
len = confstr(name, buffer, sizeof(buffer)); len = confstr(name, buffer, sizeof(buffer));
if (len == 0) { if (len == 0) {
if (errno) { if (errno) {
posix_error(); posix_error();
return NULL;
} }
else { else {
result = Py_None; Py_RETURN_NONE;
Py_INCREF(Py_None);
} }
} }
else {
if ((unsigned int)len >= sizeof(buffer)) { if ((unsigned int)len >= sizeof(buffer)) {
result = PyUnicode_FromStringAndSize(NULL, len-1); char *buf = PyMem_Malloc(len);
if (result != NULL) if (buf == NULL)
confstr(name, _PyUnicode_AsString(result), len); return PyErr_NoMemory();
confstr(name, buf, len);
result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
PyMem_Free(buf);
} }
else else
result = PyUnicode_FromStringAndSize(buffer, len-1); result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
}
}
return result; return result;
} }
#endif #endif