Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r1680:3fba04d45c38
Date: 2015-04-08 08:58 +0200
http://bitbucket.org/cffi/cffi/changeset/3fba04d45c38/
Log: Quick and dirty hack to get started
diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -371,6 +371,31 @@
self._libraries.append(lib)
return lib
+ def csource(self, modulename, source='', **kwargs):
+ if hasattr(self, '_csource'):
+ raise ValueError("can't call csource() more than once")
+ self._csource = (modulename, source, kwargs)
+
+ def recompile(self):
+ from .verifier import Verifier
+ if self._windows_unicode:
+ self._apply_windows_unicode(kwargs)
+ modulename, source, kwargs = self._csource
+ verifier = Verifier(self, source, modulename=modulename+'_ffi',
+ tmpdir='.', **kwargs)
+ verifier.compile_module()
+ f = open(modulename + '.py', 'w')
+ print >> f, 'import os, cffi as _cffi'
+ print >> f, 'ffi = _cffi.FFI()'
+ for cdef in self._cdefsources:
+ print >> f
+ print >> f, 'ffi.cdef(%r)' % (cdef,)
+ print >> f
+ print >> f, 'lib = ffi.verify('
+ print >> f, ' tmpdir=os.path.dirname(__file__), modulename=%r)' % (
+ modulename + '_ffi',)
+ f.close()
+
def _get_errno(self):
return self._backend.get_errno()
def _set_errno(self, errno):
diff --git a/demo/bsdopendirtype.py b/demo/bsdopendirtype.py
--- a/demo/bsdopendirtype.py
+++ b/demo/bsdopendirtype.py
@@ -1,22 +1,4 @@
-from cffi import FFI
-
-ffi = FFI()
-ffi.cdef("""
- typedef ... DIR;
- struct dirent {
- unsigned char d_type; /* type of file */
- char d_name[]; /* filename */
- ...;
- };
- DIR *opendir(const char *name);
- int closedir(DIR *dirp);
- struct dirent *readdir(DIR *dirp);
- static const int DT_BLK, DT_CHR, DT_DIR, DT_FIFO, DT_LNK, DT_REG, DT_SOCK;
-""")
-lib = ffi.verify("""
- #include <sys/types.h>
- #include <dirent.h>
-""")
+from _bsdopendirtype import ffi, lib
def _posix_error():
@@ -60,3 +42,7 @@
yield name, smode
finally:
lib.closedir(dirp)
+
+if __name__ == '__main__':
+ for name, smode in opendir('/tmp'):
+ print hex(smode), name
diff --git a/demo/bsdopendirtype_setup.py b/demo/bsdopendirtype_setup.py
new file mode 100644
--- /dev/null
+++ b/demo/bsdopendirtype_setup.py
@@ -0,0 +1,22 @@
+from cffi import FFI
+
+ffi = FFI()
+ffi.csource("_bsdopendirtype", """
+ #include <sys/types.h>
+ #include <dirent.h>
+""")
+ffi.cdef("""
+ typedef ... DIR;
+ struct dirent {
+ unsigned char d_type; /* type of file */
+ char d_name[]; /* filename */
+ ...;
+ };
+ DIR *opendir(const char *name);
+ int closedir(DIR *dirp);
+ struct dirent *readdir(DIR *dirp);
+ static const int DT_BLK, DT_CHR, DT_DIR, DT_FIFO, DT_LNK, DT_REG, DT_SOCK;
+""")
+
+if __name__ == '__main__':
+ ffi.recompile()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit