Differences between revisions 8 and 9
Revision 8 as of 2007-07-14 14:19:20
Size: 3061
Editor: c-67-188-135-108
Comment: point to the FFI page rather than the ffi page
Revision 9 as of 2007-07-14 14:23:32
Size: 3027
Editor: c-67-188-135-108
Comment: format code samples as code samples using the wiki syntax of three curly braces
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
{{{
Line 12: Line 13:
Line 14: Line 14:
}}}
Line 17: Line 18:
{{{
Line 18: Line 20:
Line 20: Line 21:
Line 22: Line 22:
}}}
Line 31: Line 32:
{{{
Line 32: Line 34:
Line 34: Line 35:

''Someone more Wiki literate than I should kill the false hot link at MaxByteString above.''
}}}
Line 39: Line 39:
{{{
Line 40: Line 41:
}}}
Line 45: Line 47:
{{{
Line 47: Line 50:
}}}
Line 52: Line 56:
{{{
Line 53: Line 58:
}}}
Line 56: Line 62:
{{{
Line 57: Line 64:
}}}

"ctypes is an advanced ["FFI"] ... package for Python ...", according to its [http://www.python.net/crew/theller/ctypes/ home page].

["Python 2.5"] includes ctypes.

CTypes FAQs

FAQ: How do I copy bytes to Python from a ctypes.Structure?

def send(self):
        return buffer(self)[:]

FAQ: How do I copy bytes to a ctypes.Structure from Python?

def receiveSome(self, bytes):
        fit = min(len(bytes), ctypes.sizeof(self))
        ctypes.memmove(ctypes.addressof(self), bytes, fit)

FAQ: Why should I fear using ctypes.memmove?

ctypes.memmove emulates the memmove of C with complete faithfulness. If you tell memmove to copy more bytes than sizeof(self), you will overwrite memory that you do not own, with indeterminate consequences, arbitrarily delayed.

FAQ: How do I change the byte length of a ctypes.Structure?

Declare the max length and allocate that much memory, but then copy less than all of the memory allocated. You change the byte length that you use, not the byte length that ctypes.sizeof reports. For example:

class MaxByteString(ctypes.Structure):
        _fields_ = [('Bytes'), 0xFF * ctypes.c_ubyte)]

FAQ: How do I say memcpy?

memmove

FAQ: How do I say offsetof?

If the field is a member of an instance of a struct class or an array class, then:

def offsetof(self, field):
        return ctypes.addressof(field) - ctypes.addressof(self)

Otherwise you have to sum up the aligned sizes of the _fields_ that precede the fieldname. If _pack_ is 1, then the aligned size of each field is merely its ctypes.sizeof, otherwise you have to work harder.

FAQ: How do I say uchar?

ctypes.c_ubyte

FAQ: How do I say ((void *) -1)?

INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value

FAQ: How do I contribute to this CTypes FAQ?

1. Learn how to edit this Wiki page at:

http://wiki.python.org/moin/WikiSandBox

2. Post newbie CTypes questions into comp.lang.python, per the remarkable invitation of:

from: http://wiki.python.org/moin/MovingToPythonFromOtherLanguages

Don't be surprised to have your questions answered by the original ... The best thing about comp.lang.python is how "newbie-friendly" the mail group is. You can ask any question and never get a "RTFM" thrown back at you.

FAQ: How do I learn Python CTypes, after learning C and Python?

1. Read a version of the CTypes tutorial:

The search http://www.google.com/search?q=site%3Adocs.python.org+ctypes once upon a time did find http://docs.python.org/lib/module-ctypes.html

2. Download and locally search a version of the CTypes tutorial.

The search http://www.google.com/search?q=ctypes+tutorial once upon a time did find http://python.net/crew/theller/ctypes/tutorial.html

3. Read other CTypes wiki's:

The search http://www.google.com/search?q=ctypes+python+wiki once upon a time did find http://starship.python.net/crew/theller/wiki

ctypes (last edited 2011-11-30 00:28:21 by webproxy3)

Unable to edit the page? See the FrontPage for instructions.