Skip to content

Commit d1d0c91

Browse files
committed
Ported ctypes fix from old-p4a (but may not work)
1 parent 51c068d commit d1d0c91

File tree

8 files changed

+162
-4
lines changed

8 files changed

+162
-4
lines changed

pythonforandroid/bootstraps/pygame/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def run_distribute(self):
7979

8080
info('Deleting some other stuff not used on android')
8181
# To quote the original distribute.sh, 'well...'
82-
shprint(sh.rm, '-rf', 'ctypes')
82+
# shprint(sh.rm, '-rf', 'ctypes')
8383
shprint(sh.rm, '-rf', 'lib2to3')
8484
shprint(sh.rm, '-rf', 'idlelib')
8585
for filename in glob.glob('config/libpython*.a'):

pythonforandroid/bootstraps/sdl2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def run_distribute(self):
7171

7272
info('Deleting some other stuff not used on android')
7373
# To quote the original distribute.sh, 'well...'
74-
shprint(sh.rm, '-rf', 'ctypes')
74+
# shprint(sh.rm, '-rf', 'ctypes')
7575
shprint(sh.rm, '-rf', 'lib2to3')
7676
shprint(sh.rm, '-rf', 'idlelib')
7777
for filename in glob.glob('config/libpython*.a'):

pythonforandroid/bootstraps/sdl2/build/src/org/kivy/android/PythonActivity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public void loadLibraries() {
7171

7272
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_io.so");
7373
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/unicodedata.so");
74+
75+
try {
76+
System.loadLibrary("ctypes");
77+
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_ctypes.so");
78+
} catch(UnsatisfiedLinkError e) {
79+
Log.v(TAG, "Unsatisfied linker when loading ctypes");
80+
}
7481

7582
Log.v(TAG, "Loaded everything!");
7683
}

pythonforandroid/recipes/python2/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pythonforandroid.toolchain import Recipe, shprint, get_directory, current_directory, ArchAndroid
33
from os.path import exists, join
44
from os import uname
5+
import glob
56
import sh
67

78
class Python2Recipe(Recipe):
@@ -17,6 +18,7 @@ def prebuild_armeabi(self):
1718
print('Python2 already patched, skipping.')
1819
return
1920
self.apply_patch(join('patches', 'Python-{}-xcompile.patch'.format(self.version)))
21+
self.apply_patch(join('patches', 'Python-{}-ctypes-disable-wchar.patch'.format(self.version)))
2022
self.apply_patch(join('patches', 'disable-modules.patch'))
2123
self.apply_patch(join('patches', 'fix-locale.patch'))
2224
self.apply_patch(join('patches', 'fix-gethostbyaddr.patch'))
@@ -28,6 +30,7 @@ def prebuild_armeabi(self):
2830
self.apply_patch(join('patches', 'fix-remove-corefoundation.patch'))
2931
self.apply_patch(join('patches', 'fix-dynamic-lookup.patch'))
3032
self.apply_patch(join('patches', 'fix-dlfcn.patch'))
33+
self.apply_patch(join('patches', 'ctypes-find-library.patch'))
3134

3235
if uname()[0] == 'Linux':
3336
self.apply_patch(join('patches', 'fix-configure-darwin.patch'))
@@ -46,6 +49,8 @@ def build_armeabi(self):
4649
hostpython = join(self.get_build_dir('armeabi'), 'hostpython')
4750
hostpgen = join(self.get_build_dir('armeabi'), 'hostpython')
4851

52+
# ctypes: Need to set buildarch?
53+
4954
if exists(join(self.get_build_dir('armeabi'), 'libpython2.7.so')):
5055
print('libpython2.7.so already exists, skipping python build.')
5156
self.ctx.hostpython = join(self.ctx.build_dir, 'python-install',
@@ -106,6 +111,8 @@ def build_armeabi(self):
106111

107112
print('Ready to copy .so for python arm')
108113
shprint(sh.cp, 'libpython2.7.so', self.ctx.libs_dir)
114+
for filen in glob.glob('build/lib.*-2.7/_ctypes*.so'):
115+
shprint(sh.cp, '-a', filen, self.ctx.libs_dir)
109116

110117
print('Copying hostpython binary to targetpython folder')
111118
shprint(sh.cp, self.ctx.hostpython,
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
diff -ur '--exclude=*~' Python-2.7.2.orig/Lib/ctypes/__init__.py Python-2.7.2/Lib/ctypes/__init__.py
2+
--- Python-2.7.2.orig/Lib/ctypes/__init__.py 2011-06-11 16:46:24.000000000 +0100
3+
+++ Python-2.7.2/Lib/ctypes/__init__.py 2015-03-19 12:32:45.747723687 +0000
4+
@@ -272,31 +272,34 @@
5+
else:
6+
set_conversion_mode("ascii", "strict")
7+
8+
- class c_wchar_p(_SimpleCData):
9+
- _type_ = "Z"
10+
-
11+
- class c_wchar(_SimpleCData):
12+
- _type_ = "u"
13+
-
14+
- POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param
15+
-
16+
- def create_unicode_buffer(init, size=None):
17+
- """create_unicode_buffer(aString) -> character array
18+
- create_unicode_buffer(anInteger) -> character array
19+
- create_unicode_buffer(aString, anInteger) -> character array
20+
- """
21+
- if isinstance(init, (str, unicode)):
22+
- if size is None:
23+
- size = len(init)+1
24+
- buftype = c_wchar * size
25+
- buf = buftype()
26+
- buf.value = init
27+
- return buf
28+
- elif isinstance(init, (int, long)):
29+
- buftype = c_wchar * init
30+
- buf = buftype()
31+
- return buf
32+
- raise TypeError(init)
33+
+# The wchar stuff causes a crash on Android (the bionic C library doesn't
34+
+# implement wchar_t anyway)
35+
+#
36+
+# class c_wchar_p(_SimpleCData):
37+
+# _type_ = "Z"
38+
+#
39+
+# class c_wchar(_SimpleCData):
40+
+# _type_ = "u"
41+
+#
42+
+# POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param
43+
+#
44+
+# def create_unicode_buffer(init, size=None):
45+
+# """create_unicode_buffer(aString) -> character array
46+
+# create_unicode_buffer(anInteger) -> character array
47+
+# create_unicode_buffer(aString, anInteger) -> character array
48+
+# """
49+
+# if isinstance(init, (str, unicode)):
50+
+# if size is None:
51+
+# size = len(init)+1
52+
+# buftype = c_wchar * size
53+
+# buf = buftype()
54+
+# buf.value = init
55+
+# return buf
56+
+# elif isinstance(init, (int, long)):
57+
+# buftype = c_wchar * init
58+
+# buf = buftype()
59+
+# return buf
60+
+# raise TypeError(init)
61+
62+
POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param
63+
64+
diff -ur '--exclude=*~' Python-2.7.2.orig/Modules/_ctypes/callproc.c Python-2.7.2/Modules/_ctypes/callproc.c
65+
--- Python-2.7.2.orig/Modules/_ctypes/callproc.c 2015-03-19 11:56:40.668159317 +0000
66+
+++ Python-2.7.2/Modules/_ctypes/callproc.c 2015-03-19 11:45:45.898288000 +0000
67+
@@ -1423,7 +1423,7 @@
68+
mode |= RTLD_NOW;
69+
handle = ctypes_dlopen(name, mode);
70+
if (!handle) {
71+
- char *errmsg = ctypes_dlerror();
72+
+ const char *errmsg = ctypes_dlerror();
73+
if (!errmsg)
74+
errmsg = "dlopen() error";
75+
PyErr_SetString(PyExc_OSError,
76+
diff -ur '--exclude=*~' Python-2.7.2.orig/Modules/_ctypes/libffi/src/dlmalloc.c Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c
77+
--- Python-2.7.2.orig/Modules/_ctypes/libffi/src/dlmalloc.c 2015-03-19 13:26:58.928438829 +0000
78+
+++ Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c 2015-03-19 15:32:19.042396376 +0000
79+
@@ -614,18 +614,6 @@
80+
#include "/usr/include/malloc.h"
81+
#else /* HAVE_USR_INCLUDE_MALLOC_H */
82+
83+
-struct mallinfo {
84+
- MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */
85+
- MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */
86+
- MALLINFO_FIELD_TYPE smblks; /* always 0 */
87+
- MALLINFO_FIELD_TYPE hblks; /* always 0 */
88+
- MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */
89+
- MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */
90+
- MALLINFO_FIELD_TYPE fsmblks; /* always 0 */
91+
- MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
92+
- MALLINFO_FIELD_TYPE fordblks; /* total free space */
93+
- MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
94+
-};
95+
96+
#endif /* HAVE_USR_INCLUDE_MALLOC_H */
97+
#endif /* NO_MALLINFO */
98+
@@ -966,7 +954,7 @@
99+
p = malloc(n);
100+
assert(malloc_usable_size(p) >= 256);
101+
*/
102+
-size_t dlmalloc_usable_size(void*);
103+
+size_t dlmalloc_usable_size(const void*);
104+
105+
/*
106+
malloc_stats();
107+
@@ -4384,7 +4372,7 @@
108+
internal_malloc_stats(gm);
109+
}
110+
111+
-size_t dlmalloc_usable_size(void* mem) {
112+
+size_t dlmalloc_usable_size(const void* mem) {
113+
if (mem != 0) {
114+
mchunkptr p = mem2chunk(mem);
115+
if (cinuse(p))
116+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
diff -ur Python-2.7.2.orig/Lib/ctypes/util.py Python-2.7.2/Lib/ctypes/util.py
2+
--- Python-2.7.2.orig/Lib/ctypes/util.py 2011-06-11 16:46:24.000000000 +0100
3+
+++ Python-2.7.2/Lib/ctypes/util.py 2015-05-10 15:50:18.906203529 +0100
4+
@@ -71,7 +71,21 @@
5+
def find_library(name):
6+
return name
7+
8+
-if os.name == "posix" and sys.platform == "darwin":
9+
+# this test is for android specifically shoudl match here and ignore any
10+
+# of the other platform tests below
11+
+if os.name == "posix":
12+
+ def find_library(name):
13+
+ """ hack to find librarys for kivy and android
14+
+ split the path and get the first parts which will give us
15+
+ the app path something like /data/data/org.app.foo/"""
16+
+ app_root = os.path.abspath('./').split(os.path.sep)[0:4]
17+
+ lib_search = os.path.sep.join(app_root) + os.path.sep + 'lib'
18+
+ for filename in os.listdir(lib_search):
19+
+ if filename.endswith('.so') and name in filename:
20+
+ return lib_search + os.path.sep + filename
21+
+ return None
22+
+
23+
+elif os.name == "posix" and sys.platform == "darwin":
24+
from ctypes.macholib.dyld import dyld_find as _dyld_find
25+
def find_library(name):
26+
possible = ['lib%s.dylib' % name,
27+
Only in Python-2.7.2/Lib/ctypes: util.py.save
28+
Only in Python-2.7.2/Lib/ctypes: util.py.save.1

pythonforandroid/recipes/python2/patches/disable-modules.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# This global variable is used to hold the list of modules to be disabled.
77
-disabled_module_list = []
8-
+disabled_module_list = ['spwd', '_ctypes','bz2','ossaudiodev','_curses','_curses_panel','readline','_locale','_bsddb','gdbm','dbm','nis','linuxaudiodev','crypt','_multiprocessing']
8+
+disabled_module_list = ['spwd','bz2','ossaudiodev','_curses','_curses_panel','readline','_locale','_bsddb','gdbm','dbm','nis','linuxaudiodev','crypt','_multiprocessing']
99

1010
def add_dir_to_list(dirlist, dir):
1111
"""Add the directory 'dir' to the list 'dirlist' (at the front) if

pythonforandroid/toolchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,7 @@ def run_pymodules_install(ctx, modules):
18841884
info('Installing Python modules with pip')
18851885

18861886
# AND: This doesn't work yet
1887-
shprint(sh.bash, '-c', '''"source venv/bin/activate && env CC=/bin/false CXX=/bin/false pip install --target '{}' -r requirements.txt"'''.format(ctx.get_site_packages_dir()))
1887+
shprint(sh.bash, '-c', '''"source venv/bin/activate && env CC=/bin/false CXX=/bin/false PYTHONPATH= pip install --target '{}' -r requirements.txt"'''.format(ctx.get_site_packages_dir()))
18881888

18891889

18901890

0 commit comments

Comments
 (0)