Skip to content

Commit 701c125

Browse files
committed
Fix python2's initialization
In order to be possible to successfully initialize python we must perform some changes: - Enable some python modules in our Setup.dist file, otherwise we will get import errors. - Adapt the source code to embed python to the new python2's build mechanism (start.c). - Adapt CythonRecipe to the new python2's build system (this should have been fixed in commit 02151be)
1 parent 138b38d commit 701c125

File tree

4 files changed

+95
-20
lines changed

4 files changed

+95
-20
lines changed

pythonforandroid/bootstraps/sdl2/build/jni/src/start.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ int main(int argc, char *argv[]) {
139139
#if PY_MAJOR_VERSION >= 3
140140
wchar_t *wchar_paths = Py_DecodeLocale(paths, NULL);
141141
Py_SetPath(wchar_paths);
142-
#else
143-
char *wchar_paths = paths;
144-
LOGP("Can't Py_SetPath in python2, so crystax python2 doesn't work yet");
145-
exit(1);
146142
#endif
147143

148144
LOGP("set wchar paths...");
@@ -156,6 +152,9 @@ int main(int argc, char *argv[]) {
156152
Py_Initialize();
157153

158154
#if PY_MAJOR_VERSION < 3
155+
// Can't Py_SetPath in python2 but we can set PySys_SetPath, which must
156+
// be applied after Py_Initialize rather than before like Py_SetPath
157+
PySys_SetPath(paths);
159158
PySys_SetArgv(argc, argv);
160159
#endif
161160

@@ -177,18 +176,6 @@ int main(int argc, char *argv[]) {
177176
* replace sys.path with our path
178177
*/
179178
PyRun_SimpleString("import sys, posix\n");
180-
if (dir_exists("lib")) {
181-
/* If we built our own python, set up the paths correctly */
182-
LOGP("Setting up python from ANDROID_PRIVATE");
183-
PyRun_SimpleString("private = posix.environ['ANDROID_APP_PATH']\n"
184-
"argument = posix.environ['ANDROID_ARGUMENT']\n"
185-
"sys.path[:] = [ \n"
186-
" private + '/lib/python27.zip', \n"
187-
" private + '/lib/python2.7/', \n"
188-
" private + '/lib/python2.7/lib-dynload/', \n"
189-
" private + '/lib/python2.7/site-packages/', \n"
190-
" argument ]\n");
191-
}
192179

193180
char add_site_packages_dir[256];
194181
if (dir_exists(crystax_python_dir)) {

pythonforandroid/recipe.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,7 @@ def get_recipe_env(self, arch, with_flags_in_cc=True):
990990
env['LDFLAGS'] = (env['LDFLAGS'] +
991991
' -L{}'.format(join(self.ctx.bootstrap.build_dir, 'libs', arch.arch)))
992992

993-
if self.ctx.python_recipe.from_crystax or self.ctx.python_recipe.name == 'python3':
994-
env['LDSHARED'] = env['CC'] + ' -shared'
995-
else:
996-
env['LDSHARED'] = join(self.ctx.root_dir, 'tools', 'liblink.sh')
993+
env['LDSHARED'] = env['CC'] + ' -shared'
997994
# shprint(sh.whereis, env['LDSHARED'], _env=env)
998995
env['LIBLINK'] = 'NOTNONE'
999996
env['NDKPLATFORM'] = self.ctx.ndk_platform

pythonforandroid/recipes/python2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Python2Recipe(TargetPythonRecipe):
2424
# new 2.7.15 patches
2525
# ('patches/Python-2.7.15-fix-api-minor-than-21.patch',
2626
# is_api_lt(21)), # todo: this should be tested
27+
'patches/fix-modules-initialization.patch',
2728
'patches/Python_{version}-ctypes-libffi-fix-configure.patch',
2829
'patches/ffi-config.sub-{version}.patch',
2930
'patches/fix-locale-{version}.patch',
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
--- Python-2.7.15/Modules/Setup.dist.orig 2018-04-30 00:47:33.000000000 +0200
2+
+++ Python-2.7.15/Modules/Setup.dist 2018-11-13 20:25:38.096160189 +0100
3+
@@ -168,31 +168,31 @@
4+
5+
# Modules that should always be present (non UNIX dependent):
6+
7+
-#array arraymodule.c # array objects
8+
+array arraymodule.c # array objects
9+
#cmath cmathmodule.c _math.c # -lm # complex math library functions
10+
-#math mathmodule.c _math.c # -lm # math library functions, e.g. sin()
11+
-#_struct _struct.c # binary structure packing/unpacking
12+
-#time timemodule.c # -lm # time operations and variables
13+
-#operator operator.c # operator.add() and similar goodies
14+
+math mathmodule.c _math.c # -lm # math library functions, e.g. sin()
15+
+_struct _struct.c # binary structure packing/unpacking
16+
+time timemodule.c # -lm # time operations and variables
17+
+operator operator.c # operator.add() and similar goodies
18+
#_testcapi _testcapimodule.c # Python C API test module
19+
-#_random _randommodule.c # Random number generator
20+
-#_collections _collectionsmodule.c # Container types
21+
+_random _randommodule.c # Random number generator
22+
+_collections _collectionsmodule.c # Container types
23+
#_heapq _heapqmodule.c # Heapq type
24+
-#itertools itertoolsmodule.c # Functions creating iterators for efficient looping
25+
+itertools itertoolsmodule.c # Functions creating iterators for efficient looping
26+
#strop stropmodule.c # String manipulations
27+
-#_functools _functoolsmodule.c # Tools for working with functions and callable objects
28+
+_functools _functoolsmodule.c # Tools for working with functions and callable objects
29+
#_elementtree -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c # elementtree accelerator
30+
#_pickle _pickle.c # pickle accelerator
31+
#datetime datetimemodule.c # date/time type
32+
-#_bisect _bisectmodule.c # Bisection algorithms
33+
+_bisect _bisectmodule.c # Bisection algorithms
34+
35+
-#unicodedata unicodedata.c # static Unicode character database
36+
+unicodedata unicodedata.c # static Unicode character database
37+
38+
# access to ISO C locale support
39+
#_locale _localemodule.c # -lintl
40+
41+
# Standard I/O baseline
42+
-#_io -I$(srcdir)/Modules/_io _io/bufferedio.c _io/bytesio.c _io/fileio.c _io/iobase.c _io/_iomodule.c _io/stringio.c _io/textio.c
43+
+_io -I$(srcdir)/Modules/_io _io/bufferedio.c _io/bytesio.c _io/fileio.c _io/iobase.c _io/_iomodule.c _io/stringio.c _io/textio.c
44+
45+
46+
# Modules with some UNIX dependencies -- on by default:
47+
@@ -252,14 +252,14 @@
48+
# Message-Digest Algorithm, described in RFC 1321. The necessary files
49+
# md5.c and md5.h are included here.
50+
51+
-#_md5 md5module.c md5.c
52+
+_md5 md5module.c md5.c
53+
54+
55+
# The _sha module implements the SHA checksum algorithms.
56+
# (NIST's Secure Hash Algorithms.)
57+
-#_sha shamodule.c
58+
-#_sha256 sha256module.c
59+
-#_sha512 sha512module.c
60+
+_sha shamodule.c
61+
+_sha256 sha256module.c
62+
+_sha512 sha512module.c
63+
64+
65+
# SGI IRIX specific modules -- off by default.
66+
@@ -434,13 +434,13 @@
67+
68+
69+
# Helper module for various ascii-encoders
70+
-#binascii binascii.c
71+
+binascii binascii.c
72+
73+
# Fred Drake's interface to the Python parser
74+
#parser parsermodule.c
75+
76+
# cStringIO and cPickle
77+
-#cStringIO cStringIO.c
78+
+cStringIO cStringIO.c
79+
#cPickle cPickle.c
80+
81+
82+
@@ -464,7 +464,7 @@
83+
# Andrew Kuchling's zlib module.
84+
# This require zlib 1.1.3 (or later).
85+
# See http://www.gzip.org/zlib/
86+
-#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
87+
+zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
88+
89+
# Interface to the Expat XML parser
90+
#

0 commit comments

Comments
 (0)