From 7bc152ecafe1c2925c2c33b7a49727abaa6d2f5e Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 4 Jun 2015 22:46:08 +0100 Subject: [PATCH 1/2] ffilib: Initial version of wrapper for ffi module. --- ffilib/ffilib.py | 32 ++++++++++++++++++++++++++++++++ ffilib/metadata.txt | 7 +++++++ ffilib/setup.py | 18 ++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 ffilib/ffilib.py create mode 100644 ffilib/metadata.txt create mode 100644 ffilib/setup.py diff --git a/ffilib/ffilib.py b/ffilib/ffilib.py new file mode 100644 index 000000000..6806baa17 --- /dev/null +++ b/ffilib/ffilib.py @@ -0,0 +1,32 @@ +import ffi + +_cache = {} + +def open(name, maxver=10, extra=()): + try: + return _cache[name] + except KeyError: + pass + err = None + for n in ("%s.so" % name, "%s.dylib" % name): + try: + l = ffi.open(n) + _cache[name] = l + return l + except OSError as e: + err = e + for i in range(maxver): + try: + l = ffi.open("%s.so.%u" % (name, i)) + _cache[name] = l + return l + except OSError as e: + err = e + for n in extra: + try: + l = ffi.open(n) + _cache[name] = l + return l + except OSError as e: + err = e + raise err diff --git a/ffilib/metadata.txt b/ffilib/metadata.txt new file mode 100644 index 000000000..aeaccb0bd --- /dev/null +++ b/ffilib/metadata.txt @@ -0,0 +1,7 @@ +dist_name = ffilib +srctype = micropython-lib +type = module +version = 0.1.0 +author = Damien George +desc = MicroPython FFI helper module +long_desc = MicroPython FFI helper module to easily interface with underlying shared libraries diff --git a/ffilib/setup.py b/ffilib/setup.py new file mode 100644 index 000000000..491269a8c --- /dev/null +++ b/ffilib/setup.py @@ -0,0 +1,18 @@ +import sys +# Remove current dir from sys.path, otherwise setuptools will peek up our +# module instead of system. +sys.path.pop(0) +from setuptools import setup + + +setup(name='micropython-libc', + version='0.1.0', + description='MicroPython FFI helper module', + long_description='MicroPython FFI helper module to easily interface with underlying shared libraries', + url='https://github.com/micropython/micropython/issues/405', + author='Damien George', + author_email='micro-python@googlegroups.com', + maintainer='MicroPython Developers', + maintainer_email='micro-python@googlegroups.com', + license='MIT', + py_modules=['ffilib']) From 1cecb8ca5f6193026e371e271d40ddea39a14172 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 Jun 2015 10:33:17 +0100 Subject: [PATCH 2/2] ffilib: Reduce code duplicatin by using generator; fix setup.py. --- ffilib/ffilib.py | 27 ++++++++++++--------------- ffilib/setup.py | 2 +- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/ffilib/ffilib.py b/ffilib/ffilib.py index 6806baa17..df9222ca3 100644 --- a/ffilib/ffilib.py +++ b/ffilib/ffilib.py @@ -1,3 +1,4 @@ +import sys import ffi _cache = {} @@ -7,22 +8,18 @@ def open(name, maxver=10, extra=()): return _cache[name] except KeyError: pass + def libs(): + if sys.platform == "linux": + yield '%s.so' % name + for i in range(maxver, -1, -1): + yield '%s.so.%u' % (name, i) + else: + for ext in ('dylib', 'dll'): + yield '%s.%s' % (name, ext) + for n in extra: + yield n err = None - for n in ("%s.so" % name, "%s.dylib" % name): - try: - l = ffi.open(n) - _cache[name] = l - return l - except OSError as e: - err = e - for i in range(maxver): - try: - l = ffi.open("%s.so.%u" % (name, i)) - _cache[name] = l - return l - except OSError as e: - err = e - for n in extra: + for n in libs(): try: l = ffi.open(n) _cache[name] = l diff --git a/ffilib/setup.py b/ffilib/setup.py index 491269a8c..eb83f4acc 100644 --- a/ffilib/setup.py +++ b/ffilib/setup.py @@ -5,7 +5,7 @@ from setuptools import setup -setup(name='micropython-libc', +setup(name='micropython-ffilib', version='0.1.0', description='MicroPython FFI helper module', long_description='MicroPython FFI helper module to easily interface with underlying shared libraries',