weimingtom / python-for-android

Automatically exported from code.google.com/p/python-for-android
Apache License 2.0
1 stars 0 forks source link

sys.getfilesystemencoding() returns None #35

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Open Python shell on an android device.
2. Enter 'import sys; print sys.filesystemencoding()'

What is the expected output? What do you see instead?

A string "UTF-8" or "mbcs" expected, but sees None.
This causes unicode exception on many os module functions accessing the file 
system with filename/path containing non ascii char.

What version of the product are you using? On what operating system?

Tested Android (2.2, 2.3, 3.1). Used PythonForAndroid_r5.apk

Please provide any additional information below.

See linked ASE issue: 
http://code.google.com/p/android-scripting/issues/detail?id=575

Original issue reported on code.google.com by anthony....@gmail.com on 28 Nov 2011 at 10:26

GoogleCodeExporter commented 9 years ago
The fix seems in Python-2.6.2/Python/bltinmodule.c to change:

#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
const char *Py_FileSystemDefaultEncoding = "mbcs";
#elif defined(__APPLE__)
const char *Py_FileSystemDefaultEncoding = "utf-8";
#else
const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
#endif

To:

#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
const char *Py_FileSystemDefaultEncoding = "mbcs";
#elif defined(__APPLE__)
const char *Py_FileSystemDefaultEncoding = "utf-8";
#else
const char *Py_FileSystemDefaultEncoding = "utf-8"; /* use default */
#endif

Source: 
https://github.com/kivy/python-for-android/commit/c3b84199cdf7952d6b3cedb54130a1
988b099107

Original comment by anthony....@gmail.com on 30 Mar 2012 at 9:48

GoogleCodeExporter commented 9 years ago
I try this patch and rebuid Python 2.6.2, not fixing.

Original comment by anthony....@gmail.com on 18 Apr 2012 at 2:57

GoogleCodeExporter commented 9 years ago
Have you tried encoding/decoding paths in your own code, e.g. 
some_string.encode('utf-8') ?

Please post some example code that shows the errors you are referring to.

Original comment by brian.le...@gmail.com on 21 Apr 2012 at 3:06

GoogleCodeExporter commented 9 years ago
Hello,

I'm having the same problem. With an application for parsing a xml, I got 
UnicodeEncodeError when picking a non-ascii character.

print sys.getfilesystemencoding() gives me also "None" on SL4A, while on 
Windows gives me "mbcs" (python 2.6 and 2.7) and Linux "UTF-8" (python 2.7).

I leave here an example (sorry if the code is not very clean, but I'm just 
starting to work with python):
http://goo.gl/XWqfj 
http://goo.gl/ylwtS 

Both in Linux and Windows, works without problems. With SL4A and Python 2.6.2:
UnicodeEncodeError: 'ascii' codec can not encode character u '\ XE7' in 
position 98: ordinal not in range (128)

I have also tried a few workarounds, such as distrito2.encode('utf8') or 
repr(distrito2), but without success.

There is already a solution?

Thank you very much!

Best regards,
Pedro

Original comment by pedrongv...@gmail.com on 17 Aug 2012 at 11:29

GoogleCodeExporter commented 9 years ago
Locale is broken so you would need to wrap with smth like:

_ENCODING = sys.getfilesystemencoding() or locale.getdefaultlocale()[1] or 
'utf-8'

try:
  my_stuff(string)
except (UnicodeEncodeError, UnicodeDecodeError):
  my_stuff(string.encode(_ENCODING))

Original comment by anthony....@gmail.com on 17 Aug 2012 at 8:35

GoogleCodeExporter commented 9 years ago
Hello Anthony,

It worked with _ENCODING = 'utf-8'

Thank you very much!

Best regards,
Pedro

Original comment by pedrongv...@gmail.com on 17 Aug 2012 at 11:35

GoogleCodeExporter commented 9 years ago
>I try this patch and rebuid Python 2.6.2, not fixing.

I probably mistaked there as I rebuilt Python 2.7.2 with this patch and it 
fixed.

You can build Python 2.7.2 with this patch (IPv6 patch too) there: 
http://code.google.com/p/android-python27/source/browse/#hg%2Fpython-build ; or 
you can use directly the already built version: 
http://code.google.com/p/android-python27/source/browse/#hg%2Fapk%2Fres%2Fraw

And use it with SL4A, package.sh builds python_27.zip and python_extras_27.zip 
(replacement for python_r16.zip and python_extras_r14.zip)

Original comment by anthony....@gmail.com on 18 Aug 2012 at 1:26