[pypy-commit] cffi demo-cleanup: update and cleanup more demos

mattip noreply at buildbot.pypy.org
Tue Nov 24 15:36:39 EST 2015


Author: mattip <matti.picus at gmail.com>
Branch: demo-cleanup
Changeset: r2439:c7cec8d17b33
Date: 2015-11-24 21:50 +0200
http://bitbucket.org/cffi/cffi/changeset/c7cec8d17b33/

Log:	update and cleanup more demos

diff --git a/demo/gmp.py b/demo/gmp.py
--- a/demo/gmp.py
+++ b/demo/gmp.py
@@ -1,4 +1,9 @@
 import sys
+#
+# This is only a demo based on the GMP library.
+# There is a rather more complete (but perhaps outdated) version available at:
+# http://bazaar.launchpad.net/~tolot-solar-empire/+junk/gmpy_cffi/files
+#
 
 # If the build script was run immediately before this script, the cffi module
 # ends up in the current directory. Make sure we can import it.
diff --git a/demo/pwuid.py b/demo/pwuid.py
--- a/demo/pwuid.py
+++ b/demo/pwuid.py
@@ -1,14 +1,18 @@
-from cffi import FFI
-ffi = FFI()
-ffi.cdef("""     // some declarations from the man page
-    struct passwd {
-        char *pw_name;
-        ...; 
-    };
-    struct passwd *getpwuid(int uid);
-""")
-C = ffi.verify("""   // passed to the real C compiler
-#include <sys/types.h>
-#include <pwd.h>
-""")
-print ffi.string(C.getpwuid(0).pw_name)
+import sys, os
+
+# If the build script was run immediately before this script, the cffi module
+# ends up in the current directory. Make sure we can import it.
+sys.path.append('.')
+
+try:
+    from _pwuid import ffi, lib
+except ImportError:
+    print 'run pwuid_build first, then make sure the shared object is on sys.path'
+    sys.exit(-1)
+
+# ffi "knows" about the declared variables and functions from the
+#     cdef parts of the module xclient_build created,
+# lib "knows" how to call the functions from the set_source parts
+#     of the module.
+
+print ffi.string(lib.getpwuid(0).pw_name)
diff --git a/demo/pwuid_build.py b/demo/pwuid_build.py
new file mode 100644
--- /dev/null
+++ b/demo/pwuid_build.py
@@ -0,0 +1,18 @@
+from cffi import FFI
+ffi = FFI()
+ffi.cdef("""     // some declarations from the man page
+    struct passwd {
+        char *pw_name;
+        ...; 
+    };
+    struct passwd *getpwuid(int uid);
+""")
+
+ffi.set_source('_pwuid', """   // passed to the real C compiler
+#include <sys/types.h>
+#include <pwd.h>
+""")
+
+
+if __name__ == '__main__':
+    ffi.compile()
diff --git a/demo/readdir2.py b/demo/readdir2.py
--- a/demo/readdir2.py
+++ b/demo/readdir2.py
@@ -1,11 +1,19 @@
-# A Linux-only demo, using verify() instead of hard-coding the exact layouts
+# A Linux-only demo, using set_source() instead of hard-coding the exact layouts
 #
 import sys
-from _readdir2 import ffi, lib
 
 if not sys.platform.startswith('linux'):
     raise Exception("Linux-only demo")
 
+# If the build script was run immediately before this script, the cffi module
+# ends up in the current directory. Make sure we can import it.
+sys.path.append('.')
+
+try:
+    from _readdir2 import ffi, lib
+except ImportError:
+    print 'run readdir2_build first, then make sure the shared object is on sys.path'
+    sys.exit(-1)
 
 def walk(basefd, path):
     print '{', path
diff --git a/demo/winclipboard.py b/demo/winclipboard.py
--- a/demo/winclipboard.py
+++ b/demo/winclipboard.py
@@ -1,60 +1,44 @@
 __author__ = "Israel Fruchter <israel.fruchter at gmail.com>"
 
-from cffi import FFI
+import sys, os
 
-ffi = FFI()
-ffi.cdef('''
-    typedef void * HANDLE;
-    typedef HANDLE HWND;
-    typedef int BOOL;
-    typedef unsigned int UINT;
-    typedef int SIZE_T;
-    typedef char * LPTSTR;
-    typedef HANDLE HGLOBAL;
-    typedef HANDLE LPVOID;
+if not sys.platform == 'win32':
+    raise Exception("Windows-only demo")
 
-    HWND GetConsoleWindow(void);
+# If the build script was run immediately before this script, the cffi module
+# ends up in the current directory. Make sure we can import it.
+sys.path.append('.')
 
-    LPVOID GlobalLock( HGLOBAL hMem );
-    BOOL GlobalUnlock( HGLOBAL hMem );
-    HGLOBAL GlobalAlloc(UINT uFlags, SIZE_T dwBytes);
+try:
+    from _winclipboard import ffi, lib
+except ImportError:
+    print 'run winclipboard_build first, then make sure the shared object is on sys.path'
+    sys.exit(-1)
 
-    BOOL  OpenClipboard(HWND hWndNewOwner);
-    BOOL  CloseClipboard(void);
-    BOOL  EmptyClipboard(void);
-    HANDLE  SetClipboardData(UINT uFormat, HANDLE hMem);
-
-    #define CF_TEXT ...
-    #define GMEM_MOVEABLE ...
-
-    void * memcpy(void * s1, void * s2, int n);
-    ''')
-
-lib = ffi.verify('''
-    #include <windows.h>
-''', libraries=["user32"])
-
-globals().update(lib.__dict__)
+# ffi "knows" about the declared variables and functions from the
+#     cdef parts of the module xclient_build created,
+# lib "knows" how to call the functions from the set_source parts
+#     of the module.
 
 def CopyToClipboard(string):
     '''
         use win32 api to copy `string` to the clipboard
     '''
-    hWnd = GetConsoleWindow()
+    hWnd = lib.GetConsoleWindow()
   
-    if OpenClipboard(hWnd):
+    if lib.OpenClipboard(hWnd):
         cstring = ffi.new("char[]", string)
         size = ffi.sizeof(cstring)
         
         # make it a moveable memory for other processes
-        hGlobal = GlobalAlloc(GMEM_MOVEABLE, size)
-        buffer = GlobalLock(hGlobal)
+        hGlobal = lib.GlobalAlloc(lib.GMEM_MOVEABLE, size)
+        buffer = lib.GlobalLock(hGlobal)
         memcpy(buffer, cstring, size)
-        GlobalUnlock(hGlobal)
+        lib.GlobalUnlock(hGlobal)
         
-        res = EmptyClipboard()
-        res = SetClipboardData(CF_TEXT, buffer)
+        res = lib.EmptyClipboard()
+        res = lib.SetClipboardData(lib.CF_TEXT, buffer)
  
-        CloseClipboard()
+        lib.CloseClipboard()
         
 CopyToClipboard("hello world from cffi")
diff --git a/demo/winclipboard_build.py b/demo/winclipboard_build.py
new file mode 100644
--- /dev/null
+++ b/demo/winclipboard_build.py
@@ -0,0 +1,36 @@
+from cffi import FFI
+
+ffi = FFI()
+ffi.cdef('''
+    typedef void * HANDLE;
+    typedef HANDLE HWND;
+    typedef int BOOL;
+    typedef unsigned int UINT;
+    typedef int SIZE_T;
+    typedef char * LPTSTR;
+    typedef HANDLE HGLOBAL;
+    typedef HANDLE LPVOID;
+
+    HWND GetConsoleWindow(void);
+
+    LPVOID GlobalLock( HGLOBAL hMem );
+    BOOL GlobalUnlock( HGLOBAL hMem );
+    HGLOBAL GlobalAlloc(UINT uFlags, SIZE_T dwBytes);
+
+    BOOL  OpenClipboard(HWND hWndNewOwner);
+    BOOL  CloseClipboard(void);
+    BOOL  EmptyClipboard(void);
+    HANDLE  SetClipboardData(UINT uFormat, HANDLE hMem);
+
+    #define CF_TEXT ...
+    #define GMEM_MOVEABLE ...
+
+    void * memcpy(void * s1, void * s2, int n);
+    ''')
+
+ffi.set_source('_winclipboard', '''
+    #include <windows.h>
+''', libraries=["user32"])
+
+if __name__ == '__main__':
+    ffi.compile()


More information about the pypy-commit mailing list