From 68840c846ca7ffff6a99ae9b5d3f156c8623a373 Mon Sep 17 00:00:00 2001 From: mattip Date: Tue, 24 Nov 2015 21:50:04 +0200 Subject: update and cleanup more demos --- demo/pwuid.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'demo/pwuid.py') 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 -#include -""") -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) -- cgit v1.2.1