You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_bootlocale imports locale at startup on Android, causing test_site to fail #71115
The problem is caused by the fact that android does not have HAVE_LANGINFO_H and CODESET set, hence in the _bootlocale module, the statement '_locale.CODESET' raises AttributeError and the locale module is imported upon interpreter startup. The locale module imports re.
See issue bpo-19205 for why we would rather not import re and locale on startup.
This seems difficult to fix without either skipping most part of the test as it is done with Mac OS X, or having a specific sys.platform for android to handle the AttributeError in _bootlocale by having the getpreferredencoding() fuction returning 'UTF-8' ('UTF-8' is the file system encoding on android).
xdegaye commented on May 5, 2016
xdegayemannequin
MannequinAuthor
More actions
BTW the test runs fine on android when the AttributeError in _bootlocale is hard-coded with a getpreferredencoding() fuction returning 'UTF-8' and not importing locale.
xdegaye commented on May 21, 2016
xdegayemannequin
MannequinAuthor
More actions
Sorry for the confusion, the file system encoding is not the locale encoding.
In issue bpo-9548, Antoine proposed a patch that avoids the import of the re, collections and functools modules by the _io module on startup, by refactoring and moving code from locale to _bootlocale. The attached refactor_locale.patch does that for python 3.6. The reasons for why Antoine patch has not been pushed still apply to this patch :(
The patch does fix the problem for Android though.
changed the title [-]android: test_site fails[/-][+]_bootlocale imports locale at startup on Android[/+]on May 21, 2016
xdegaye commented on May 21, 2016
xdegayemannequin
MannequinAuthor
More actions
An improvement to Python startup time on Android (Android does not have nl_langinfo()) is to have _bootlocale.getpreferredencoding() return 'ascii' without importing locale, when none of the locale environment variables is set. With patch no-locale-envvar.patch, test_site runs ok on android-21-x86 emulator when the locale environment variables are not set.
Committing this patch while leaving the current issue open would also allow removing this issue from the dependencies of the Android meta-issue bpo-26865.
changed the title [-]_bootlocale imports locale at startup on Android[/-][+]_bootlocale imports locale at startup on Android, causing test_site to fail[/+]on May 21, 2016
xdegaye commented on Nov 3, 2016
xdegayemannequin
MannequinAuthor
More actions
This patch fixes test_startup_imports when the platform does not have langinfo.h.
Entered new bpo-28596: "on Android _bootlocale on startup relies on too many library modules".
If it is not possible to change the locale, it makes sense to hardcode utf8.
Note: to avoid mojibake, it's better if sys.getfilesystemencoding() and
locale.getpreferredencoding(False) are equal. I understand that both must
be utf8.
Closing as invalid, it is useful to have the test failing on platforms that do not have CODESET and detect that too many modules are imported on startup. For Android, this problem is fixed in bpo-28596.
Activity
xdegaye commentedon May 3, 2016
One test of test_site fails on an android emulator running an x86 system image at API level 21.
See the attached test_output.txt file.
xdegaye commentedon May 5, 2016
The problem is caused by the fact that android does not have HAVE_LANGINFO_H and CODESET set, hence in the _bootlocale module, the statement '_locale.CODESET' raises AttributeError and the locale module is imported upon interpreter startup. The locale module imports re.
See issue bpo-19205 for why we would rather not import re and locale on startup.
This seems difficult to fix without either skipping most part of the test as it is done with Mac OS X, or having a specific sys.platform for android to handle the AttributeError in _bootlocale by having the getpreferredencoding() fuction returning 'UTF-8' ('UTF-8' is the file system encoding on android).
xdegaye commentedon May 5, 2016
BTW the test runs fine on android when the AttributeError in _bootlocale is hard-coded with a getpreferredencoding() fuction returning 'UTF-8' and not importing locale.
xdegaye commentedon May 21, 2016
Sorry for the confusion, the file system encoding is not the locale encoding.
In issue bpo-9548, Antoine proposed a patch that avoids the import of the re, collections and functools modules by the _io module on startup, by refactoring and moving code from locale to _bootlocale. The attached refactor_locale.patch does that for python 3.6. The reasons for why Antoine patch has not been pushed still apply to this patch :(
The patch does fix the problem for Android though.
[-]android: test_site fails[/-][+]_bootlocale imports locale at startup on Android[/+]xdegaye commentedon May 21, 2016
An improvement to Python startup time on Android (Android does not have nl_langinfo()) is to have _bootlocale.getpreferredencoding() return 'ascii' without importing locale, when none of the locale environment variables is set. With patch no-locale-envvar.patch, test_site runs ok on android-21-x86 emulator when the locale environment variables are not set.
Committing this patch while leaving the current issue open would also allow removing this issue from the dependencies of the Android meta-issue bpo-26865.
[-]_bootlocale imports locale at startup on Android[/-][+]_bootlocale imports locale at startup on Android, causing test_site to fail[/+]xdegaye commentedon Nov 3, 2016
This patch fixes test_startup_imports when the platform does not have langinfo.h.
Entered new bpo-28596: "on Android _bootlocale on startup relies on too many library modules".
xdegaye commentedon Nov 18, 2016
Patch that follows closely the conditionals in the __bootlocale module.
vstinner commentedon Nov 18, 2016
Hum, is it possible to get the locale encoding by another way?
If not, what is the locale encoding?
Does Android provide mbstowcs() and wcstombs() functions?
yan12125 commentedon Nov 18, 2016
Seems Android/BioniC always uses UTF-8: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/mbrtoc32.cpp#83
vstinner commentedon Nov 18, 2016
If it is not possible to change the locale, it makes sense to hardcode utf8.
Note: to avoid mojibake, it's better if sys.getfilesystemencoding() and
locale.getpreferredencoding(False) are equal. I understand that both must
be utf8.
yan12125 commentedon Nov 18, 2016
There are some locale strings supported in setlocale(): https://android.googlesource.com/platform/bionic/+/master/libc/bionic/locale.cpp#104. However, seems mbstowcs just ignores such a setting on Android. Here's an example:
On Linux (glibc 2.24) the result is:
On Android (6.0 Marshmallow) the result is:
shell@ASUS_Z00E_2:/ $ /data/local/tmp/a.out
setlocale: 1
mbstowcs: 2
dest: 4e2d 6587
setlocale: 1
mbstowcs: 2
dest: 4e2d 6587
A quick search indicates setlocale() affects *scanf functions only, so I guess it's safe to force UTF-8 in CPython.
vstinner commentedon Nov 18, 2016
In Python, the most important functions are Py_DecodeLocale() and
Py_EncodeLocale() which use mbstowcs() and wvstombs().
yan12125 commentedon Nov 18, 2016
Submitted a patch to bpo-28596
xdegaye commentedon Dec 17, 2016
Closing as invalid, it is useful to have the test failing on platforms that do not have CODESET and detect that too many modules are imported on startup. For Android, this problem is fixed in bpo-28596.