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

Skip to content

Commit 76febd0

Browse files
author
Xavier de Gaye
committed
Issue #26919: On Android, operating system data is now always encoded/decoded
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with os.fsencode() and os.fsdecode() which are already using UTF-8.
1 parent 3d3f264 commit 76febd0

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
import subprocess
1010
import tempfile
11-
from test.support import script_helper
11+
from test.support import script_helper, is_android
1212
from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
1313
assert_python_failure)
1414

@@ -178,15 +178,16 @@ def test_undecodable_code(self):
178178
if not stdout.startswith(pattern):
179179
raise AssertionError("%a doesn't start with %a" % (stdout, pattern))
180180

181-
@unittest.skipUnless(sys.platform == 'darwin', 'test specific to Mac OS X')
182-
def test_osx_utf8(self):
181+
@unittest.skipUnless((sys.platform == 'darwin' or
182+
is_android), 'test specific to Mac OS X and Android')
183+
def test_osx_android_utf8(self):
183184
def check_output(text):
184185
decoded = text.decode('utf-8', 'surrogateescape')
185186
expected = ascii(decoded).encode('ascii') + b'\n'
186187

187188
env = os.environ.copy()
188189
# C locale gives ASCII locale encoding, but Python uses UTF-8
189-
# to parse the command line arguments on Mac OS X
190+
# to parse the command line arguments on Mac OS X and Android.
190191
env['LC_ALL'] = 'C'
191192

192193
p = subprocess.Popen(

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.6.1 release candidate 1
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #26919: On Android, operating system data is now always encoded/decoded
14+
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
15+
os.fsencode() and os.fsdecode() which are already using UTF-8.
16+
1317
- Issue #28147: Fix a memory leak in split-table dictionaries: setattr()
1418
must not convert combined table into split table. Patch written by INADA
1519
Naoki.

Objects/unicodeobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,10 +5083,10 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
50835083
return NULL;
50845084
}
50855085

5086-
#ifdef __APPLE__
5086+
#if defined(__APPLE__) || defined(__ANDROID__)
50875087

50885088
/* Simplified UTF-8 decoder using surrogateescape error handler,
5089-
used to decode the command line arguments on Mac OS X.
5089+
used to decode the command line arguments on Mac OS X and Android.
50905090
50915091
Return a pointer to a newly allocated wide character string (use
50925092
PyMem_RawFree() to free the memory), or NULL on memory allocation error. */
@@ -5137,7 +5137,7 @@ _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size)
51375137
return unicode;
51385138
}
51395139

5140-
#endif /* __APPLE__ */
5140+
#endif /* __APPLE__ or __ANDROID__ */
51415141

51425142
/* Primary internal function which creates utf8 encoded bytes objects.
51435143

Python/fileutils.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern int winerror_to_errno(int);
2020
#include <fcntl.h>
2121
#endif /* HAVE_FCNTL_H */
2222

23-
#ifdef __APPLE__
23+
#if defined(__APPLE__) || defined(__ANDROID__)
2424
extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
2525
#endif
2626

@@ -273,7 +273,7 @@ decode_ascii_surrogateescape(const char *arg, size_t *size)
273273
wchar_t*
274274
Py_DecodeLocale(const char* arg, size_t *size)
275275
{
276-
#ifdef __APPLE__
276+
#if defined(__APPLE__) || defined(__ANDROID__)
277277
wchar_t *wstr;
278278
wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
279279
if (size != NULL) {
@@ -406,7 +406,7 @@ Py_DecodeLocale(const char* arg, size_t *size)
406406
if (size != NULL)
407407
*size = (size_t)-1;
408408
return NULL;
409-
#endif /* __APPLE__ */
409+
#endif /* __APPLE__ or __ANDROID__ */
410410
}
411411

412412
/* Encode a wide character string to the locale encoding with the
@@ -424,7 +424,7 @@ Py_DecodeLocale(const char* arg, size_t *size)
424424
char*
425425
Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
426426
{
427-
#ifdef __APPLE__
427+
#if defined(__APPLE__) || defined(__ANDROID__)
428428
Py_ssize_t len;
429429
PyObject *unicode, *bytes = NULL;
430430
char *cpath;
@@ -522,7 +522,7 @@ Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
522522
bytes = result;
523523
}
524524
return result;
525-
#endif /* __APPLE__ */
525+
#endif /* __APPLE__ or __ANDROID__ */
526526
}
527527

528528

0 commit comments

Comments
 (0)