summaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
authormattip <matti.picus@gmail.com>2015-11-24 21:50:04 +0200
committermattip <matti.picus@gmail.com>2015-11-24 21:50:04 +0200
commit68840c846ca7ffff6a99ae9b5d3f156c8623a373 (patch)
treecb275e08d6357d4a28e3a29b3f6a5a239247a0bd /demo
parent5043f478b1ff4b3a1d03d0b9869ea32fbb4c61c5 (diff)
downloadcffi-68840c846ca7ffff6a99ae9b5d3f156c8623a373.tar.gz
update and cleanup more demos
Diffstat (limited to 'demo')
-rw-r--r--demo/gmp.py5
-rw-r--r--demo/pwuid.py32
-rw-r--r--demo/pwuid_build.py18
-rw-r--r--demo/readdir2.py12
-rw-r--r--demo/winclipboard.py62
-rw-r--r--demo/winclipboard_build.py36
6 files changed, 110 insertions, 55 deletions
diff --git a/demo/gmp.py b/demo/gmp.py
index 8cec6f7..af96be3 100644
--- 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
index c4d7201..650b9da 100644
--- 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
index 0000000..bfbfa55
--- /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
index 5927a77..64fc60f 100644
--- 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
index 79dc166..6b226f4 100644
--- a/demo/winclipboard.py
+++ b/demo/winclipboard.py
@@ -1,60 +1,44 @@
__author__ = "Israel Fruchter <israel.fruchter@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
index 0000000..6ad55b2
--- /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()