Re: FAQ: how to vary the byte offset of a field of a ctypes.Structure



p.lavarre@xxxxxxxx schrieb:
How do I vary the byte offset of a field of a ctypes.Structure?

How do I "use the dynamic nature of Python, and (re-)define the data
type after the required size is already known, on a case by case
basis"?

\\\

For example, suppose sometimes I receive the value '\x03hi' + \x04bye'
for the struct:

class Struct34(ctypes.Structure):
_pack_ = 1
_fields_ = [('first', 3 * ctypes.c_ubyte),
('second', 4 * ctypes.c_ubyte)]

but then sometimes instead I receive the value '\x05left' + \x06right'
for the struct:

class Struct56(ctypes.Structure):
_pack_ = 1
_fields_ = [('first', 5 * ctypes.c_ubyte),
('second', 6 * ctypes.c_ubyte)]

Thus in general I receive (0xFF ** 2) possible combinations of field
lengths.

///

How do I declare all those hugely many simply regular combinations as
one CTypes.structure?

I also need to do series of 3 or 4 or 5 strings, not just 2 strings.
But always the byte offsets of the subsequent fields vary when the
byte sizes of the preceding fields vary. The byte size of the
enclosing packed struct varies as the length of the packed bytes it
contains.

Often it helps to ask yourself the question: How would I do this in C?

IMO, the answer to this question, applied to your problem, would be:
*Not* by using a structure. A structure is fine if the definition is fixed,
or at most has *one* variable sized field at the very end. Nothing
is true for your problem.

Thomas

.



Relevant Pages

  • Re: FAQ: how to vary the byte offset of a field of a ctypes.Structure
    ... for the struct: ... But always the byte offsets of the subsequent fields vary when the ... enclosing packed struct varies as the length of the packed bytes it ... AttributeError: '_fields_' must be a sequence of pairs ...
    (comp.lang.python)
  • Re: Official definition of call-by-value (Re: Finding the instance reference...)
    ... I cannot say what defines a Python object in a generic way, but I do know what defines a CPython object. ... Most functions in the CPython API receive, return, or handle PyObject pointers everywhere. ... The PyObject struct is very small and contains nothing apart from the ob_type field. ... We refer to this dictionary as the '__dict__' attribute in Python code, but it is just another field in the object struct, as anything else. ...
    (comp.lang.python)
  • Re: C structure in the Python extension
    ... Python code can't work with C structs directly, ... PyCObject is also pretty evil if abused, ... in a memory leak if the PyCObject holds the only pointer to the struct). ...
    (comp.lang.python)
  • Re: Need help with C extension module
    ... Using the example in Programming Python, I did get the easier of the two functions working--only takes a string parameter. ... I'm stuck now on the other function and not sure how to wrap it, ... (only primitive data types) ... struct Out ...
    (comp.lang.python)
  • Re: Retain reference to a struct
    ... > in the Python code. ... > instance of the struct until a certain function is called. ... > I can get the pointer into the python code, but whenever I try to use ...
    (comp.lang.python)