Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5894ba7

Browse files
committed
Fixed a bug in PyUnicode_DecodeFSDefault. strcmp() returns 0 on success.
Added PyUnicode_DecodeFSDefaultAndSize Fixed a problem with the sys.path code that caused a segfault on Windows when the path contains non ASCII chars. The code for sys.executable, exec_prefix and prefix should be fixed, too.
1 parent 9c1257e commit 5894ba7

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

Include/unicodeobject.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
155155
# define PyUnicode_DecodeCharmap PyUnicodeUCS2_DecodeCharmap
156156
# define PyUnicode_DecodeLatin1 PyUnicodeUCS2_DecodeLatin1
157157
# define PyUnicode_DecodeFSDefault PyUnicodeUCS2_DecodeFSDefault
158+
# define PyUnicode_DecodeFSDefaultAndSize PyUnicodeUCS2_DecodeFSDefaultAndSize
158159
# define PyUnicode_DecodeRawUnicodeEscape PyUnicodeUCS2_DecodeRawUnicodeEscape
159160
# define PyUnicode_DecodeUTF32 PyUnicodeUCS2_DecodeUTF32
160161
# define PyUnicode_DecodeUTF32Stateful PyUnicodeUCS2_DecodeUTF32Stateful
@@ -247,6 +248,7 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
247248
# define PyUnicode_DecodeCharmap PyUnicodeUCS4_DecodeCharmap
248249
# define PyUnicode_DecodeLatin1 PyUnicodeUCS4_DecodeLatin1
249250
# define PyUnicode_DecodeFSDefault PyUnicodeUCS4_DecodeFSDefault
251+
# define PyUnicode_DecodeFSDefaultAndSize PyUnicodeUCS4_DecodeFSDefaultAndSize
250252
# define PyUnicode_DecodeRawUnicodeEscape PyUnicodeUCS4_DecodeRawUnicodeEscape
251253
# define PyUnicode_DecodeUTF32 PyUnicodeUCS4_DecodeUTF32
252254
# define PyUnicode_DecodeUTF32Stateful PyUnicodeUCS4_DecodeUTF32Stateful
@@ -657,6 +659,12 @@ PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefault(
657659
const char *s /* encoded string */
658660
);
659661

662+
PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefaultAndSize(
663+
const char *s, /* encoded string */
664+
Py_ssize_t size /* size */
665+
);
666+
667+
660668
/* Return a char* holding the UTF-8 encoded value of the
661669
Unicode object.
662670

Objects/unicodeobject.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,22 +1263,26 @@ PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
12631263
}
12641264

12651265
PyObject*
1266-
PyUnicode_DecodeFSDefault(const char *s)
1267-
{
1266+
PyUnicode_DecodeFSDefault(const char *s) {
12681267
Py_ssize_t size = (Py_ssize_t)strlen(s);
1268+
return PyUnicode_DecodeFSDefaultAndSize(s, size);
1269+
}
12691270

1271+
PyObject*
1272+
PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
1273+
{
12701274
/* During the early bootstrapping process, Py_FileSystemDefaultEncoding
12711275
can be undefined. If it is case, decode using UTF-8. The following assumes
12721276
that Py_FileSystemDefaultEncoding is set to a built-in encoding during the
12731277
bootstrapping process where the codecs aren't ready yet.
12741278
*/
12751279
if (Py_FileSystemDefaultEncoding) {
12761280
#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
1277-
if (strcmp(Py_FileSystemDefaultEncoding, "mbcs")) {
1281+
if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) {
12781282
return PyUnicode_DecodeMBCS(s, size, "replace");
12791283
}
12801284
#elif defined(__APPLE__)
1281-
if (strcmp(Py_FileSystemDefaultEncoding, "utf-8")) {
1285+
if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) {
12821286
return PyUnicode_DecodeUTF8(s, size, "replace");
12831287
}
12841288
#endif

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ makepathobject(const char *path, int delim)
11501150
p = strchr(path, delim);
11511151
if (p == NULL)
11521152
p = strchr(path, '\0'); /* End of string */
1153-
w = PyUnicode_FromStringAndSize(path, (Py_ssize_t) (p - path));
1153+
w = PyUnicode_DecodeFSDefaultAndSize(path, (Py_ssize_t) (p - path));
11541154
if (w == NULL) {
11551155
Py_DECREF(v);
11561156
return NULL;

0 commit comments

Comments
 (0)