Dies ist die Support Website des Buches:: Das Python Praxisbuch Der große Profi-Leitfaden für Programmierer Farid Hajji Addison Wesley / Pearson Education ISBN 978-3-8273-2543-3 (Sep 2008), 1298 Seiten. ********************** 11. Python und C/C++ ********************** ctypes ====== Die ctypes-Datentypwrapper -------------------------- Komplexe Datentypen ------------------- Die Funktionen ``assctime`` und ``erand48``: .. code-block:: c char * asctime(const struct tm *); /* */ double erand48(unsigned short[3]); /* */ ctypes-Pointer ^^^^^^^^^^^^^^ ctypes-Strukturen und -Unions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Die C-Structur ``tm``: .. code-block:: c struct tm { int tm_sec; /* seconds after the minute [0-60] */ int tm_min; /* minutes after the hour [0-59] */ int tm_hour; /* hours since midnight [0-23] */ int tm_mday; /* day of the month [1-31] */ int tm_mon; /* months since January [0-11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday [0-6] */ int tm_yday; /* days since January 1 [0-365] */ int tm_isdst; /* Daylight Savings Time flag */ long tm_gmtoff; /* offset from UTC in seconds */ char *tm_zone; /* timezone abbreviation */ }; .. literalinclude:: ../att/pythonc/time_tm.py `time_tm.py `_ ctypes-Arrays ^^^^^^^^^^^^^ Die Klasse ``XZY``:: class XZY(Structure): _fields_ = [ ("point", c_ushort * 3), ("rgb", c_ubyte * 3) ] C-Funktionen aufrufen --------------------- Funktionen aus Bibliotheken laden ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ URLs: * `Manpage der C-Funktion sleep `_ * `Manpage der C-Funktion printf `_ Screenshots: * `API der Funktion MessageBox `_ * `Wie sieht eine MessageBoxA aus? `_ * `Wie sieht eine MessageBoxW aus? `_ Aus der Unix ``libc`` laden:: from ctypes import * libc = cdll.LoadLibrary("libc.so") libc.sleep(5) Aus Windows DLLs laden:: from ctypes import * kernel32 = windll.kernel32 user32 = windll.user32 msvcrt = cdll.msvcrt MessageBoxW = user32.MessageBoxW MessageBoxW(0, u'A Unicode Message: ÄÖÜäöüß', u'A Unicode Greeter', 0) Hier hatte ``MessageBox`` (ANSI und Wide) die Signatur: .. code-block:: c int MessageBox( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType ); Und ``printf`` ruft man so aus:: >>> from ctypes import * >>> printf = cdll.msvcrt.printf >>> result = printf("Hello, %s. You are %d years old.\n", ... "John", 39) Hello, John. You are 39 years old. >>> result 35 Zwei einfache Funktionen: time und sleep ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ URLs: * `Manpage der Funktion time `_ * `Manpage der C-Funktion sleep `_ Screenshots: * `API der Funktion Sleep `_ Argument- und Rückgabewerte spezifizieren: cos ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ URLs: * `Manpage der C-Funktion cos `_ Funktionen, die Strings auslesen: getenv, setenv ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: c char *getenv(const char *name); int setenv(const char *name, const char *value, int overwrite); Die nötigen Vorbereitungen:: from ctypes import * libc = cdll.LoadLibrary("libc.so") getenv = libc.getenv setenv = libc.setenv getenv.argtypes = [c_char_p] getenv.restype = c_char_p setenv.argtypes = [c_char_p, c_char_p, c_int] setenv.restype = c_int URLs: * `Manpage der C-Funktionen getenv, setenv `_ Funktionen, die in einen Puffer schreiben ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: c int gethostname(char *name, size_t namelen); /* */ Die nötigen Vorbereitungen:: from ctypes import * libc = cdll.LoadLibrary("libc.so") gethostname = libc.gethostname gethostname.argtypes = [c_char * 255, c_uint] gethostname.restype = c_int buf = create_string_buffer(255) URLS: * `Manpage der C-Funktionen gethostname, sethostname `_ .. code-block:: c DWORD WINAPI GetEnvironmentVariable( __in_opt LPCTSTR lpName, __out_opt LPTSTR lpBuffer, __in DWORD nSize ); Screenshots: * `API der Funktion GetEnvironmentVariable `_ Funktionen mit struct und pointer: gmtime, asctime ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: c time_t time(time_t *tloc); /* */ struct tm * gmtime(const time_t *clock); /* */ char * asctime(const struct tm *tm); /* */ URLs: * `Manpage der C-Funktion time `_ * `Manpage der C-Funktionen gmtime, asctime `_ SWIG ==== URLs: * `Homepage von SWIG `_ * `SWIG Dokumentation `_ SWIG installieren ----------------- URLs: * http://prdownloads.sourceforge.net/swig/ (Neueste Versionen von swig, macswig und swigwin) SWIG unter Unix installieren ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SWIG unter Windows installieren ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Screenshots: * `SWIG in einer DOS-Box `_ SWIG aufrufen ------------- .. literalinclude:: ../att/pythonc/sleeper.i :language: c `sleeper.i `_ .. literalinclude:: ../att/pythonc/sleeper_setup.py `sleeper_setup.py `_ C-Code mitkompilieren ^^^^^^^^^^^^^^^^^^^^^ .. literalinclude:: ../att/pythonc/deepthought.h :language: c `deepthought.h `_ .. literalinclude:: ../att/pythonc/deepthought.c :language: c `deepthought.c `_ .. literalinclude:: ../att/pythonc/deepthought.i :language: c `deepthought.i `_ .. literalinclude:: ../att/pythonc/deepthought_setup.py `deepthought_setup.py `_ Screenshots: * `deepthought.i mit SWIG in der DOS-Box `_ * `deepthought.i in der DOS-Box ausgeführt `_ Konstanten und Variablen ------------------------ Konstanten ^^^^^^^^^^ .. literalinclude:: ../att/pythonc/math_constants.h :language: c `math_constants.h `_ .. literalinclude:: ../att/pythonc/math_constants.i :language: c `math_constants.i `_ .. literalinclude:: ../att/pythonc/math_constants_setup.py `math_constants_setup.py `_ Globale Variablen: cvar ^^^^^^^^^^^^^^^^^^^^^^^ .. literalinclude:: ../att/pythonc/globvar.h :language: c `globvar.h `_ .. literalinclude:: ../att/pythonc/globvar.c :language: c `globvar.c `_ .. literalinclude:: ../att/pythonc/globvar.i :language: c `globvar.i `_ .. literalinclude:: ../att/pythonc/globvar_setup.py `globvar_setup.py `_ .. literalinclude:: ../att/pythonc/globvar_ro.i :language: c `globvar_ro.i `_ .. literalinclude:: ../att/pythonc/globvar_ro_setup.py `globvar_ro_setup.py `_ .. literalinclude:: ../att/pythonc/globvar_noglobals.i :language: c `globvar_noglobals.i `_ .. literalinclude:: ../att/pythonc/globvar_noglobals_setup.py `globvar_noglobals_setup.py `_ Stringmanipulationen -------------------- C-Funktionen, die Strings lesen ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: c char *getenv(const char *name); int setenv(const char *name, const char *value, int overwrite); URLs: * `Manpage der C-Funktionen getenv, setenv `_ .. literalinclude:: ../att/pythonc/environ.i :language: c `environ.i `_ `environ_setup.py `_ C-Funktionen, die Strings verändern ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: c int gethostname(char *name, size_t namelen); /* */ URLS: * `Manpage der C-Funktionen gethostname, sethostname `_ .. literalinclude:: ../att/pythonc/hostname.i :language: c `hostname.i `_ `hostname_setup.py `_ Strukturen ---------- .. code-block:: c time_t time(time_t *tloc); /* */ struct tm * gmtime(const time_t *clock); /* */ char * asctime(const struct tm *tm); /* */ URLs: * `Manpage der C-Funktion time `_ * `Manpage der C-Funktionen gmtime, asctime `_ .. literalinclude:: ../att/pythonc/tm.i :language: c `tm.i `_ `tm_setup.py `_ .. literalinclude:: ../att/pythonc/tm_verbose.i :language: c `tm_verbose.i `_ `tm_verbose_setup.py `_ C++ Klassen ----------- .. literalinclude:: ../att/pythonc/person.h :language: c++ `person.h `_ .. literalinclude:: ../att/pythonc/person.cxx :language: c++ `person.cxx `_ .. literalinclude:: ../att/pythonc/person_test.cxx :language: c++ `person_test.cxx `_ .. literalinclude:: ../att/pythonc/person.i :language: c++ `person.i `_ .. literalinclude:: ../att/pythonc/person_setup.py `person_setup.py `_ Unbekannte Datentypen sind Pointer ---------------------------------- .. code-block:: c #include gid_t getegid(void); uid_t geteuid(void); URLs: * `Manpage der C-Funktion getegid `_ * `Manpage der C-Funktion geteuid `_ .. literalinclude:: ../att/pythonc/uid_err.i :language: c `uid_err.i `_ `uid_setup.py `_ .. literalinclude:: ../att/pythonc/uid.i :language: c `uid.i `_ .. literalinclude:: ../att/pythonc/fileio.i :language: c `fileio.i `_ `fileio_setup.py `_ .. literalinclude:: ../att/pythonc/filecopy.py `filecopy.py `_ URLs zum Verständnis von :file:`filecopy.py`: * `Manpage der C-Funktion fopen `_ * `Manpage der C-Funktionen fread, fwrite `_ * `Manpage der C-Funktion fclose `_ * `Manpage der C-Funktionen malloc, free `_ Wie geht's von hier aus weiter? ------------------------------- Boost.Python ============ URLs: * http://www.boost.org/libs/python/doc/ * http://www.boost.org/libs/python/doc/tutorial/doc/html/index.html * http://www.boost-consulting.com/writing/bpl.html * http://wiki.python.org/moin/boost.python * http://www.language-binding.net/pyplusplus/pyplusplus.html Low-level Python/C-API ====================== URLs: * `Extending and Embedding `_ * `Python/C-API `_ Zusammenfassung =============== URLs: * `Cython `_ * `Pyrex `_