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

Skip to content

Commit fc93ec5

Browse files
committed
merge heads
2 parents c7a8a21 + e262377 commit fc93ec5

3 files changed

Lines changed: 51 additions & 10 deletions

File tree

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ What's New in Python 3.4.0 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #16416: OS data are now always encoded/decoded to/from
14+
UTF-8/surrogateescape, instead of the locale encoding (which may be ASCII if
15+
no locale environment variable is set), to avoid inconsistencies with
16+
os.fsencode() and os.fsdecode() functions which are already using
17+
UTF-8/surrogateescape.
18+
1319
- Issue #16453: Fix equality testing of dead weakref objects.
1420

1521
- Issue #9535: Fix pending signals that have been received but not yet

Modules/python.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ wmain(int argc, wchar_t **argv)
1515
}
1616
#else
1717

18-
#ifdef __APPLE__
19-
extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
20-
#endif
21-
2218
int
2319
main(int argc, char **argv)
2420
{
@@ -45,11 +41,7 @@ main(int argc, char **argv)
4541
oldloc = strdup(setlocale(LC_ALL, NULL));
4642
setlocale(LC_ALL, "");
4743
for (i = 0; i < argc; i++) {
48-
#ifdef __APPLE__
49-
argv_copy[i] = _Py_DecodeUTF8_surrogateescape(argv[i], strlen(argv[i]));
50-
#else
5144
argv_copy[i] = _Py_char2wchar(argv[i], NULL);
52-
#endif
5345
if (!argv_copy[i]) {
5446
free(oldloc);
5547
fprintf(stderr, "Fatal Python error: "

Python/fileutils.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include <langinfo.h>
99
#endif
1010

11+
#ifdef __APPLE__
12+
extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
13+
#endif
14+
1115
PyObject *
1216
_Py_device_encoding(int fd)
1317
{
@@ -60,6 +64,15 @@ _Py_device_encoding(int fd)
6064
wchar_t*
6165
_Py_char2wchar(const char* arg, size_t *size)
6266
{
67+
#ifdef __APPLE__
68+
wchar_t *wstr;
69+
wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
70+
if (wstr == NULL)
71+
return NULL;
72+
if (size != NULL)
73+
*size = wcslen(wstr);
74+
return wstr;
75+
#else
6376
wchar_t *res;
6477
#ifdef HAVE_BROKEN_MBSTOWCS
6578
/* Some platforms have a broken implementation of
@@ -145,7 +158,7 @@ _Py_char2wchar(const char* arg, size_t *size)
145158
argsize -= converted;
146159
out++;
147160
}
148-
#else
161+
#else /* HAVE_MBRTOWC */
149162
/* Cannot use C locale for escaping; manually escape as if charset
150163
is ASCII (i.e. escape all bytes > 128. This will still roundtrip
151164
correctly in the locale's charset, which must be an ASCII superset. */
@@ -160,14 +173,15 @@ _Py_char2wchar(const char* arg, size_t *size)
160173
else
161174
*out++ = 0xdc00 + *in++;
162175
*out = 0;
163-
#endif
176+
#endif /* HAVE_MBRTOWC */
164177
if (size != NULL)
165178
*size = out - res;
166179
return res;
167180
oom:
168181
if (size != NULL)
169182
*size = (size_t)-1;
170183
return NULL;
184+
#endif /* __APPLE__ */
171185
}
172186

173187
/* Encode a (wide) character string to the locale encoding with the
@@ -184,6 +198,34 @@ _Py_char2wchar(const char* arg, size_t *size)
184198
char*
185199
_Py_wchar2char(const wchar_t *text, size_t *error_pos)
186200
{
201+
#ifdef __APPLE__
202+
Py_ssize_t len;
203+
PyObject *unicode, *bytes = NULL;
204+
char *cpath;
205+
206+
unicode = PyUnicode_FromWideChar(text, wcslen(text));
207+
if (unicode == NULL) {
208+
Py_DECREF(unicode);
209+
return NULL;
210+
}
211+
212+
bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
213+
Py_DECREF(unicode);
214+
if (bytes == NULL) {
215+
PyErr_Clear();
216+
return NULL;
217+
}
218+
219+
len = PyBytes_GET_SIZE(bytes);
220+
cpath = PyMem_Malloc(len+1);
221+
if (cpath == NULL) {
222+
Py_DECREF(bytes);
223+
return NULL;
224+
}
225+
memcpy(cpath, PyBytes_AsString(bytes), len + 1);
226+
Py_DECREF(bytes);
227+
return cpath;
228+
#else /* __APPLE__ */
187229
const size_t len = wcslen(text);
188230
char *result = NULL, *bytes = NULL;
189231
size_t i, size, converted;
@@ -243,6 +285,7 @@ _Py_wchar2char(const wchar_t *text, size_t *error_pos)
243285
bytes = result;
244286
}
245287
return result;
288+
#endif /* __APPLE__ */
246289
}
247290

248291
/* In principle, this should use HAVE__WSTAT, and _wstat

0 commit comments

Comments
 (0)