Ctypes

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Group

    Ctypes - Presentation Transcript

    1. ctypes from C import * Steven Mohr <steven.mohr@dlr.de> TechTalk, DLR Köln, 27. Januar 2009
    2. Gliederung
      • Was ist CTypes ?
      • Libraries
      • Datentypen
        • Einfache Datentypen
        • Pointer
        • Arrays
        • Strukturierte Datentypen
      • Funktionen
      • Automatische Codegenerierung
      • Quellen
    3. 1. Was ist ctypes?
      • Erlaubt es C-Bibliotheken zu laden und zu benutzen (Foreign function interface)
      • Erstellung „C-kompatibler“ Pythonobjekte
      • Gehört zur Python-Standardbibliothek
    4. 2. Bibliotheken
      • Laden der Bibliotheken:
      • Erstellen eines CDLL oder WinDLL-Objektes
      • libc = cdll.LoadLibrary(„libc.so.6“)
      • libc = CDLL(„libc.so.6“)
      • Art des Ladens:
      • now: alle Symbole werden sofort aufgelöst
      • lazy: alle Symbole werden bei Bedarf aufgelöst
      • global: die Symbole stehen zur Auflösung von Symbolen anderer Bibliotheken bereit
      • local: die Symbole stehen nicht zur Auflösung bereit
    5. 3. Datentypen
    6. 3.1 Einfache Datentypen
      • ctypes bietet Objekte für alle C-Standarddatentypen
      • Bespiele:
      Python-Typ C-Typ ctypes Datentyp float float c_float int unsigned int c_uint int short c_short 1-character string char c_char
    7. 3.2 Pointer
      • Erzeugung von Pointern:
      • Mittels der pointer-Funktion
      • i = c_int(42)
      • pi = pointer(i)
      • Zugriff auf Werte:
      • >>> pi.contents
      • c_int(42)
      • Ändern der Referenz:
      • >>> pi.contents = c_int(60)
      • >>> pi.contents
      • c_int(42)
    8. 3.3 Arrays
      • Arraytyp erzeugen:
      • FiveIntArray = c_int * 5
      • Instanzierung:
      • array1 = FiveIntArray()
      • array2 = FiveIntArray(1, 2, 3, 4, 5)
      • Zugriff:
      • >>> array1[2]
      • 0
      • >>> array2[2]
      • 3
    9. 3.4 Strukturierte Datentypen
      • ctypes stellt Union und Structure als Objekte zur Verfügung
      • Alle C-Strukturen erben hiervon
      • Aufbau der Struktur wird in Attribut _fields_ bestimmt => Liste von 2-Tupeln mit Name und Typ
      • Einfache Initialisierung: Constructor weist übergebene Werte der Reihe nach zu oder direkte Angabe als Key-Value-Paar
      • >>> from ctypes import *
      • >>> class POINT(Structure):
      • ... _fields_ = [(&quot;x&quot;, c_int), (&quot;y&quot;, c_int)]
      • >>> point = POINT(10, 20)
      • >>> print point.x, point.y
      • 10 20
    10. 4. Funktionen
      • Zugriff auf Funktionen:
      • Funktionen sind Elemente des Bibliotheken-Objektes
      • libc.printf(„Ich bin eine C-Funktion“)
      • None, integers, longs, byte strings und unicode strings können direkt an C-Funktionen übergeben werden
      • Übergabe von Parametern:
      • Python- oder ctypes-Objekte
      • By reference: Pointer-Objekt oder über byref()
      • Über das Attribut argtypes können die erlaubten Übergabewerte gesetzt werden
    11. 4. Funktionen
      • Rückgabewerte:
      • Standard: int
      • Sonst: Setzen über das Attribut restype des Funktionsobjektes
      • restype kann auch eine Pythonfunktion sein
      • Bedingung: eigentlicher Rückgabewert: Integer
      • Funktionen mit Pythonobjekten aufrufen:
      • Ctypes sucht ein Atrribut namens _as_parameter_ und nutzt dieses
    12. 5. Automatische Codegenerierung
      • CTypesLib mit GCCXML
      • 2 Skripte: h2xml.py und xml2py.py
      • Generieren das Python-Wrappermodul
      • Python-orientierte Wrapper sind vielfach sinnvoll
      • Nachteile:
      • CTypesLib stark an einzelne GCCXML-Versionen gekoppelt
      • Konfiguration dieser Toolchain sehr komplex bzw. undurchsichtig
      • alledings: CTypesLib noch in der Entwicklung, frühes Stadium
    13. 6. Quellen
      • ctypes-Doku: http://docs.python.org/library/ctypes.html#module-ctypes
      • Guy K. Kloß: Automatic C Library Wrapping - Ctypes from the Trenches
      • The Python Papers, Vol. 3, No. 3 (2008) http://ojs.pythonpapers.org/index.php/tpp/issue/view/10
    SlideShare Zeitgeist 2009

    + Andreas SchreiberAndreas Schreiber Nominate

    custom

    571 views, 0 favs, 0 embeds more stats

    TechTalk von Steven Mohr (27.01.2009)

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 571
      • 571 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events