Skip to content

Commit a36dc6e

Browse files
committed
Merge pull request kivy#343 from olymk2/feature/enable-ctypes
Feature/enable ctypes
2 parents 52e1861 + 6c352dd commit a36dc6e

File tree

6 files changed

+163
-5
lines changed

6 files changed

+163
-5
lines changed

distribute.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ function run_distribute() {
815815
try find . | grep -E '*\.(py|pyc|so\.o|so\.a|so\.libs)$' | xargs rm
816816

817817
# we are sure that all of theses will be never used on android (well...)
818-
try rm -rf ctypes
819818
try rm -rf lib2to3
820819
try rm -rf idlelib
821820
try rm -rf config/libpython*.a
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

recipes/python/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

recipes/python/recipe.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function prebuild_python() {
2020
fi
2121

2222
try patch -p1 < $RECIPE_python/patches/Python-$VERSION_python-xcompile.patch
23+
try patch -p1 < $RECIPE_python/patches/Python-$VERSION_python-ctypes-disable-wchar.patch
2324
try patch -p1 < $RECIPE_python/patches/disable-modules.patch
2425
try patch -p1 < $RECIPE_python/patches/fix-locale.patch
2526
try patch -p1 < $RECIPE_python/patches/fix-gethostbyaddr.patch
@@ -31,6 +32,7 @@ function prebuild_python() {
3132
try patch -p1 < $RECIPE_python/patches/fix-remove-corefoundation.patch
3233
try patch -p1 < $RECIPE_python/patches/fix-dynamic-lookup.patch
3334
try patch -p1 < $RECIPE_python/patches/fix-dlfcn.patch
35+
try patch -p1 < $RECIPE_python/patches/ctypes-find-library.patch
3436

3537
system=$(uname -s)
3638
if [ "X$system" == "XDarwin" ]; then
@@ -92,14 +94,19 @@ function build_python() {
9294
export LDFLAGS="$LDFLAGS -L$SRC_PATH/obj/local/$ARCH/"
9395
fi
9496

95-
try ./configure --host=arm-eabi OPT=$OFLAG --prefix="$BUILD_PATH/python-install" --enable-shared --disable-toolbox-glue --disable-framework
96-
echo ./configure --host=arm-eabi OPT=$OFLAG --prefix="$BUILD_PATH/python-install" --enable-shared --disable-toolbox-glue --disable-framework
97+
# CFLAGS for python ctypes library
98+
#export CFLAGS="$CFLAGS -DNO_MALLINFO"
99+
export BUILDARCH=x86_64-linux-gnu
100+
export HOSTARCH=arm-eabi
101+
102+
try ./configure --host=$HOSTARCH --build=$BUILDARCH OPT=$OFLAG --prefix="$BUILD_PATH/python-install" --enable-shared --disable-toolbox-glue --disable-framework
103+
echo ./configure --host=$HOSTARCH --build=$BUILDARCH OPT=$OFLAG --prefix="$BUILD_PATH/python-install" --enable-shared --disable-toolbox-glue --disable-framework
97104
echo $MAKE HOSTPYTHON=$BUILD_python/hostpython HOSTPGEN=$BUILD_python/hostpgen CROSS_COMPILE_TARGET=yes INSTSONAME=libpython2.7.so
98105
cp HOSTPYTHON=$BUILD_python/hostpython python
99106

100107
# FIXME, the first time, we got a error at:
101108
# python$EXE ../../Tools/scripts/h2py.py -i '(u_long)' /usr/include/netinet/in.h
102-
# /home/tito/code/python-for-android/build/python/Python-2.7.2/python: 1: Syntax error: word unexpected (expecting ")")
109+
# /home/tito/code/python-for-android/build/python/Python-2.7.2/python: 1: Syntax error: word unexpected (expecting ")")
103110
# because at this time, python is arm, not x86. even that, why /usr/include/netinet/in.h is used ?
104111
# check if we can avoid this part.
105112

@@ -117,6 +124,7 @@ function build_python() {
117124
fi
118125
try cp $BUILD_hostpython/hostpython $HOSTPYTHON
119126
try cp libpython2.7.so $LIBS_PATH/
127+
try cp -a build/lib.linux-x86_64-2.7/_ctypes*.so $LIBS_PATH
120128

121129
# reduce python
122130
rm -rf "$BUILD_PATH/python-install/lib/python2.7/test"

src/src/org/renpy/android/PythonService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,16 @@ public void run(){
8585
System.loadLibrary("python2.7");
8686
System.loadLibrary("application");
8787
System.loadLibrary("sdl_main");
88+
8889

8990
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_io.so");
9091
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/unicodedata.so");
92+
93+
try {
94+
System.loadLibrary("ctypes");
95+
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_ctypes.so");
96+
} catch(UnsatisfiedLinkError e) {
97+
}
9198

9299
try {
93100
System.loadLibrary("sqlite3");

0 commit comments

Comments
 (0)