HellerCommaA / android-scripting

Automatically exported from code.google.com/p/android-scripting
0 stars 0 forks source link

ctypes module for python ASE #317

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. import ctypes
2. error
3. unable to import ctypes stdlib module

What is the expected output? What do you see instead?
ctypes module for python correctly imported

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

Please provide any additional information below.
x

Original issue reported on code.google.com by kpo...@gmail.com on 30 Apr 2010 at 4:58

GoogleCodeExporter commented 9 years ago
Issue 324 has been merged into this issue.

Original comment by damonkoh...@gmail.com on 2 May 2010 at 5:54

GoogleCodeExporter commented 9 years ago
more specifically:

>>> import ctypes
import ctypes
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/home/damonkohler/ase_src/python/src/android/python/lib/python2.6/ctypes/__init
__.py",
line 10, in <module>
ImportError: No module named _ctypes
>>> import _ctypes

Original comment by kpo...@gmail.com on 2 May 2010 at 5:57

GoogleCodeExporter commented 9 years ago

Original comment by damonkoh...@gmail.com on 2 May 2010 at 3:17

GoogleCodeExporter commented 9 years ago
This is part of python stdlib ! not an enhancement ! :)

Original comment by kpo...@gmail.com on 3 May 2010 at 1:57

GoogleCodeExporter commented 9 years ago
can we support Structures /unions first?

Original comment by feifan....@gmail.com on 27 Sep 2010 at 11:40

GoogleCodeExporter commented 9 years ago
I've received a patch to make ctypes work:

1. wchar_t is not properly supported on Android, and so Python-2.6.2/pyconfig.h 
must be configured appropriately by #undef'ing HAVE_USABLE_WCHAR_T and 
HAVE_WCHAR_H

2. ctypes expects dlopen(NULL) to return a handle to the python application; 
the Android linker doesn't support this. I think a proper solution to this 
(aside from patching the linker, which won't help people with current builds) 
involves building python in two parts: executable and .so, and patching 
ctypes/__init__.py to load the .so, as it does with cygwin. In the meantime, 
here is the workaround I am using:

--- Lib/ctypes/__init__.py    2010-10-20 13:03:54.000000000 -0700
+++ /Users/bluremployee/Desktop/Python-2.6.2/Lib/ctypes/__init__.py    
2008-08-14 12:10:48.000000000 -0700
@@ -438,10 +438,7 @@
 elif _sys.platform == "cygwin":
     pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
 else:
-    # On Android, dlopen(NULL) doesn't return a handle to the application
-    # as this code expects.
-    # pythonapi = PyDLL(None)
-    pass
+    pythonapi = PyDLL(None)

 if _os.name in ("nt", "ce"):

Attached is an archive with a hacked together tree that builds a working 
_ctypes.so. It builds under the Android NDK. Also attached is the full version 
of ctypes/__init__.py for Python 2.6.2.

And here's a sample interactive session on the device:

# PYTHONHOME=/data/data/com.googlecode.pythonforandroid/files/python/ 
PYTHONPATH="/sdcard/sl4a/scripts:/sdcard/com.googlecode.pythonforandroid/extras/
python:/data/data/com.googlecode.pythonforandroid/files/python/lib/python26.zip:
/data/data/com.googlecode.pythonforandroid/files/python/lib/python2.6:/data/data
/com.googlecode.pythonforandroid/files/python/lib/python2.6/plat-linux2:/data/da
ta/com.googlecode.pythonforandroid/files/python/lib/python2.6/lib-tk:/data/data/
com.googlecode.pythonforandroid/files/python/lib/python2.6/lib-old:/data/data/co
m.googlecode.pythonforandroid/files/python/lib/python2.6/lib-dynload" 
/data/data/com.googlecode.pythonforandroid/files/python/bin/python  
Python 2.6.2 (r262:71600, Sep 19 2009, 11:03:28) 
[GCC 4.2.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> liblog=ctypes.cdll.LoadLibrary('liblog.so')
>>> liblog.__android_log_write(3, 'sl4a', 'ctypes works!')
20
>>> libc=ctypes.cdll.LoadLibrary('libc.so')
>>> libc.printf('hello %s\n', 'world')
hello world
12

Original comment by damonkoh...@gmail.com on 6 Nov 2010 at 6:07

Attachments: