Back to index

python3.2  3.2.2
Classes | Defines | Functions | Variables
cfield.c File Reference
#include "Python.h"
#include <ffi.h>
#include "ctypes.h"

Go to the source code of this file.

Classes

struct  s_char
struct  s_short
struct  s_int
struct  s_long
struct  s_float
struct  s_double
struct  s_long_double
struct  s_char_p
struct  s_void_p

Defines

#define CTYPES_CFIELD_CAPSULE_NAME_PYMEM   "_ctypes/cfield.c pymem"
#define NO_BITFIELD   0
#define NEW_BITFIELD   1
#define CONT_BITFIELD   2
#define EXPAND_BITFIELD   3
#define LOW_BIT(x)   ((x) & 0xFFFF)
#define NUM_BITS(x)   ((x) >> 16)
#define BIT_MASK(size)   ((1LL << NUM_BITS(size))-1)
#define GET_BITFIELD(v, size)
#define SET(x, v, size)
#define SWAP_2(v)
#define SWAP_4(v)
#define SWAP_8(v)
#define SWAP_INT   SWAP_4
#define _RET(X)   Py_INCREF(Py_None); return Py_None
#define BOOL_TYPE   char
#define SIZEOF__BOOL   1
#define FLOAT_ALIGN   (sizeof(s_float) - sizeof(float))
#define DOUBLE_ALIGN   (sizeof(s_double) - sizeof(double))
#define LONGDOUBLE_ALIGN   (sizeof(s_long_double) - sizeof(long double))
#define VOID_P_ALIGN   (sizeof(s_void_p) - sizeof(void*))

Functions

static PyObjectPyCField_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObjectPyCField_FromDesc (PyObject *desc, Py_ssize_t index, Py_ssize_t *pfield_size, int bitsize, int *pbitofs, Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign, int pack, int big_endian)
static int PyCField_set (CFieldObject *self, PyObject *inst, PyObject *value)
static PyObjectPyCField_get (CFieldObject *self, PyObject *inst, PyTypeObject *type)
static PyObjectPyCField_get_offset (PyObject *self, void *data)
static PyObjectPyCField_get_size (PyObject *self, void *data)
static int PyCField_traverse (CFieldObject *self, visitproc visit, void *arg)
static int PyCField_clear (CFieldObject *self)
static void PyCField_dealloc (PyObject *self)
static PyObjectPyCField_repr (CFieldObject *self)
static int get_long (PyObject *v, long *p)
static int get_ulong (PyObject *v, unsigned long *p)
static PyObjectb_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectb_get (void *ptr, Py_ssize_t size)
static PyObjectB_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectB_get (void *ptr, Py_ssize_t size)
static PyObjecth_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjecth_set_sw (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjecth_get (void *ptr, Py_ssize_t size)
static PyObjecth_get_sw (void *ptr, Py_ssize_t size)
static PyObjectH_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectH_set_sw (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectH_get (void *ptr, Py_ssize_t size)
static PyObjectH_get_sw (void *ptr, Py_ssize_t size)
static PyObjecti_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjecti_set_sw (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjecti_get (void *ptr, Py_ssize_t size)
static PyObjecti_get_sw (void *ptr, Py_ssize_t size)
static PyObjectbool_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectbool_get (void *ptr, Py_ssize_t size)
static PyObjectI_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectI_set_sw (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectI_get (void *ptr, Py_ssize_t size)
static PyObjectI_get_sw (void *ptr, Py_ssize_t size)
static PyObjectl_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectl_set_sw (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectl_get (void *ptr, Py_ssize_t size)
static PyObjectl_get_sw (void *ptr, Py_ssize_t size)
static PyObjectL_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectL_set_sw (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectL_get (void *ptr, Py_ssize_t size)
static PyObjectL_get_sw (void *ptr, Py_ssize_t size)
static PyObjectg_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectg_get (void *ptr, Py_ssize_t size)
static PyObjectd_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectd_get (void *ptr, Py_ssize_t size)
static PyObjectd_set_sw (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectd_get_sw (void *ptr, Py_ssize_t size)
static PyObjectf_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectf_get (void *ptr, Py_ssize_t size)
static PyObjectf_set_sw (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectf_get_sw (void *ptr, Py_ssize_t size)
static PyObjectO_get (void *ptr, Py_ssize_t size)
static PyObjectO_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectc_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectc_get (void *ptr, Py_ssize_t size)
static PyObjects_get (void *ptr, Py_ssize_t size)
static PyObjects_set (void *ptr, PyObject *value, Py_ssize_t length)
static PyObjectz_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectz_get (void *ptr, Py_ssize_t size)
static PyObjectP_set (void *ptr, PyObject *value, Py_ssize_t size)
static PyObjectP_get (void *ptr, Py_ssize_t size)
struct fielddesc_ctypes_get_fielddesc (const char *fmt)

Variables

static PyGetSetDef PyCField_getset []
PyTypeObject PyCField_Type
static struct fielddesc []
ffi_type ffi_type_void = { 1, 1, FFI_TYPE_VOID }
ffi_type ffi_type_uint8 = { 1, 1, FFI_TYPE_UINT8 }
ffi_type ffi_type_sint8 = { 1, 1, FFI_TYPE_SINT8 }
ffi_type ffi_type_uint16 = { 2, 2, FFI_TYPE_UINT16 }
ffi_type ffi_type_sint16 = { 2, 2, FFI_TYPE_SINT16 }
ffi_type ffi_type_uint32 = { 4, 4, FFI_TYPE_UINT32 }
ffi_type ffi_type_sint32 = { 4, 4, FFI_TYPE_SINT32 }
ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 }
ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 }
ffi_type ffi_type_float = { sizeof(float), FLOAT_ALIGN, FFI_TYPE_FLOAT }
ffi_type ffi_type_double = { sizeof(double), DOUBLE_ALIGN, FFI_TYPE_DOUBLE }
ffi_type ffi_type_longdouble
ffi_type ffi_type_pointer = { sizeof(void *), VOID_P_ALIGN, FFI_TYPE_POINTER }

Class Documentation

struct s_char

Definition at line 1669 of file cfield.c.

Class Members
char c
char x
struct s_short

Definition at line 1670 of file cfield.c.

Class Members
char c
short x
struct s_int

Definition at line 1671 of file cfield.c.

Class Members
char c
int x
struct s_long

Definition at line 1672 of file cfield.c.

Class Members
char c
long x
struct s_float

Definition at line 1673 of file cfield.c.

Class Members
char c
float x
struct s_double

Definition at line 1674 of file cfield.c.

Class Members
char c
double x
struct s_long_double

Definition at line 1675 of file cfield.c.

Class Members
char c
long double x
struct s_char_p

Definition at line 1676 of file cfield.c.

Class Members
char c
char * x
struct s_void_p

Definition at line 1677 of file cfield.c.

Class Members
char c
void * x

Define Documentation

#define _RET (   X)    Py_INCREF(Py_None); return Py_None

Definition at line 512 of file cfield.c.

#define BIT_MASK (   size)    ((1LL << NUM_BITS(size))-1)

Definition at line 437 of file cfield.c.

#define BOOL_TYPE   char

Definition at line 729 of file cfield.c.

#define CONT_BITFIELD   2
#define CTYPES_CFIELD_CAPSULE_NAME_PYMEM   "_ctypes/cfield.c pymem"

Definition at line 10 of file cfield.c.

#define DOUBLE_ALIGN   (sizeof(s_double) - sizeof(double))

Definition at line 1686 of file cfield.c.

#define EXPAND_BITFIELD   3
#define FLOAT_ALIGN   (sizeof(s_float) - sizeof(float))

Definition at line 1685 of file cfield.c.

#define GET_BITFIELD (   v,
  size 
)
Value:
if (NUM_BITS(size)) {                                               \
        v <<= (sizeof(v)*8 - LOW_BIT(size) - NUM_BITS(size));           \
        v >>= (sizeof(v)*8 - NUM_BITS(size));                           \
    }

Definition at line 443 of file cfield.c.

#define LONGDOUBLE_ALIGN   (sizeof(s_long_double) - sizeof(long double))

Definition at line 1687 of file cfield.c.

#define LOW_BIT (   x)    ((x) & 0xFFFF)

Definition at line 430 of file cfield.c.

#define NEW_BITFIELD   1
#define NO_BITFIELD   0
#define NUM_BITS (   x)    ((x) >> 16)

Definition at line 431 of file cfield.c.

#define SET (   x,
  v,
  size 
)
Value:
(NUM_BITS(size) ?                                                   \
     ( ( x & ~(BIT_MASK(size) << LOW_BIT(size)) ) | ( (v & BIT_MASK(size)) << LOW_BIT(size) ) ) \
     : v)

Definition at line 450 of file cfield.c.

#define SIZEOF__BOOL   1

Definition at line 731 of file cfield.c.

#define SWAP_2 (   v)
Value:
( ( (v >> 8) & 0x00FF) |                    \
      ( (v << 8) & 0xFF00) )

Definition at line 456 of file cfield.c.

#define SWAP_4 (   v)
Value:
( ( (v & 0x000000FF) << 24 ) |  \
      ( (v & 0x0000FF00) <<  8 ) |  \
      ( (v & 0x00FF0000) >>  8 ) |  \
      ( ((v >> 24) & 0xFF)) )

Definition at line 460 of file cfield.c.

#define SWAP_8 (   v)
Value:
( ( (v & 0x00000000000000FFLL) << 56 ) |  \
      ( (v & 0x000000000000FF00LL) << 40 ) |  \
      ( (v & 0x0000000000FF0000LL) << 24 ) |  \
      ( (v & 0x00000000FF000000LL) <<  8 ) |  \
      ( (v & 0x000000FF00000000LL) >>  8 ) |  \
      ( (v & 0x0000FF0000000000LL) >> 24 ) |  \
      ( (v & 0x00FF000000000000LL) >> 40 ) |  \
      ( ((v >> 56) & 0xFF)) )

Definition at line 477 of file cfield.c.

#define SWAP_INT   SWAP_4

Definition at line 488 of file cfield.c.

#define VOID_P_ALIGN   (sizeof(s_void_p) - sizeof(void*))

Definition at line 1690 of file cfield.c.


Function Documentation

struct fielddesc* _ctypes_get_fielddesc ( const char *  fmt) [read]

Definition at line 1645 of file cfield.c.

{
    static int initialized = 0;
    struct fielddesc *table = formattable;

    if (!initialized) {
        initialized = 1;
#ifdef CTYPES_UNICODE
        if (sizeof(wchar_t) == sizeof(short))
            _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort;
        else if (sizeof(wchar_t) == sizeof(int))
            _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint;
        else if (sizeof(wchar_t) == sizeof(long))
            _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong;
#endif
    }

    for (; table->code; ++table) {
        if (table->code == fmt[0])
            return table;
    }
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* b_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 531 of file cfield.c.

{
    signed char val = *(signed char *)ptr;
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* B_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 551 of file cfield.c.

{
    unsigned char val = *(unsigned char *)ptr;
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* b_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 520 of file cfield.c.

{
    long val;
    if (get_long(value, &val) < 0)
        return NULL;
    *(signed char *)ptr = (signed char)SET(*(signed char *)ptr, (signed char)val, size);
    _RET(value);
}

Here is the call graph for this function:

static PyObject* B_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 539 of file cfield.c.

{
    unsigned long val;
    if (get_ulong(value, &val) < 0)
        return NULL;
    *(unsigned char *)ptr = (unsigned char)SET(*(unsigned char*)ptr,
                                               (unsigned short)val, size);
    _RET(value);
}

Here is the call graph for this function:

static PyObject* bool_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 750 of file cfield.c.

{
    return PyBool_FromLong((long)*(BOOL_TYPE *)ptr);
}

Here is the call graph for this function:

static PyObject* bool_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 735 of file cfield.c.

{
    switch (PyObject_IsTrue(value)) {
    case -1:
        return NULL;
    case 0:
        *(BOOL_TYPE *)ptr = 0;
        _RET(value);
    default:
        *(BOOL_TYPE *)ptr = 1;
        _RET(value);
    }
}

Here is the call graph for this function:

static PyObject* c_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1197 of file cfield.c.

{
    return PyBytes_FromStringAndSize((char *)ptr, 1);
}

Here is the call graph for this function:

static PyObject* c_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1171 of file cfield.c.

{
    if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) {
        *(char *)ptr = PyBytes_AS_STRING(value)[0];
        _RET(value);
    }
    if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) {
        *(char *)ptr = PyByteArray_AS_STRING(value)[0];
        _RET(value);
    }
    if (PyLong_Check(value))
    {
        long longval = PyLong_AS_LONG(value);
        if (longval < 0 || longval >= 256)
            goto error;
        *(char *)ptr = (char)longval;
        _RET(value);
    }
  error:
    PyErr_Format(PyExc_TypeError,
                 "one character string expected");
    return NULL;
}

Here is the call graph for this function:

static PyObject* d_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1040 of file cfield.c.

{
    double val;
    memcpy(&val, ptr, sizeof(val));
    return PyFloat_FromDouble(val);
}

Here is the call graph for this function:

static PyObject* d_get_sw ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1070 of file cfield.c.

{
#ifdef WORDS_BIGENDIAN
    return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 1));
#else
    return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 0));
#endif
}

Here is the call graph for this function:

static PyObject* d_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1024 of file cfield.c.

{
    double x;

    x = PyFloat_AsDouble(value);
    if (x == -1 && PyErr_Occurred()) {
        PyErr_Format(PyExc_TypeError,
                     " float expected instead of %s instance",
                     value->ob_type->tp_name);
        return NULL;
    }
    memcpy(ptr, &x, sizeof(double));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* d_set_sw ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1048 of file cfield.c.

{
    double x;

    x = PyFloat_AsDouble(value);
    if (x == -1 && PyErr_Occurred()) {
        PyErr_Format(PyExc_TypeError,
                     " float expected instead of %s instance",
                     value->ob_type->tp_name);
        return NULL;
    }
#ifdef WORDS_BIGENDIAN
    if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1))
        return NULL;
#else
    if (_PyFloat_Pack8(x, (unsigned char *)ptr, 0))
        return NULL;
#endif
    _RET(value);
}

Here is the call graph for this function:

static PyObject* f_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1096 of file cfield.c.

{
    float val;
    memcpy(&val, ptr, sizeof(val));
    return PyFloat_FromDouble(val);
}

Here is the call graph for this function:

static PyObject* f_get_sw ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1126 of file cfield.c.

{
#ifdef WORDS_BIGENDIAN
    return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 1));
#else
    return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 0));
#endif
}

Here is the call graph for this function:

static PyObject* f_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1080 of file cfield.c.

{
    float x;

    x = (float)PyFloat_AsDouble(value);
    if (x == -1 && PyErr_Occurred()) {
        PyErr_Format(PyExc_TypeError,
                     " float expected instead of %s instance",
                     value->ob_type->tp_name);
        return NULL;
    }
    memcpy(ptr, &x, sizeof(x));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* f_set_sw ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1104 of file cfield.c.

{
    float x;

    x = (float)PyFloat_AsDouble(value);
    if (x == -1 && PyErr_Occurred()) {
        PyErr_Format(PyExc_TypeError,
                     " float expected instead of %s instance",
                     value->ob_type->tp_name);
        return NULL;
    }
#ifdef WORDS_BIGENDIAN
    if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1))
        return NULL;
#else
    if (_PyFloat_Pack4(x, (unsigned char *)ptr, 0))
        return NULL;
#endif
    _RET(value);
}

Here is the call graph for this function:

static PyObject* g_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1016 of file cfield.c.

{
    long double val;
    memcpy(&val, ptr, sizeof(long double));
    return PyFloat_FromDouble(val);
}

Here is the call graph for this function:

static PyObject* g_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1000 of file cfield.c.

{
    long double x;

    x = PyFloat_AsDouble(value);
    if (x == -1 && PyErr_Occurred()) {
        PyErr_Format(PyExc_TypeError,
                     " float expected instead of %s instance",
                     value->ob_type->tp_name);
        return NULL;
    }
    memcpy(ptr, &x, sizeof(long double));
    _RET(value);
}

Here is the call graph for this function:

static int get_long ( PyObject v,
long p 
) [static]

Definition at line 350 of file cfield.c.

{
    long x;

    if (PyFloat_Check(v)) {
        PyErr_SetString(PyExc_TypeError,
                        "int expected instead of float");
        return -1;
    }
    x = PyLong_AsUnsignedLongMask(v);
    if (x == -1 && PyErr_Occurred())
        return -1;
    *p = x;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int get_ulong ( PyObject v,
unsigned long p 
) [static]

Definition at line 369 of file cfield.c.

{
    unsigned long x;

    if (PyFloat_Check(v)) {
        PyErr_SetString(PyExc_TypeError,
                        "int expected instead of float");
        return -1;
    }
    x = PyLong_AsUnsignedLongMask(v);
    if (x == (unsigned long)-1 && PyErr_Occurred())
        return -1;
    *p = x;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* h_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 588 of file cfield.c.

{
    short val;
    memcpy(&val, ptr, sizeof(val));
    GET_BITFIELD(val, size);
    return PyLong_FromLong((long)val);
}

Here is the call graph for this function:

static PyObject* H_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 636 of file cfield.c.

{
    unsigned short val;
    memcpy(&val, ptr, sizeof(val));
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* h_get_sw ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 597 of file cfield.c.

{
    short val;
    memcpy(&val, ptr, sizeof(val));
    val = SWAP_2(val);
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* H_get_sw ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 645 of file cfield.c.

{
    unsigned short val;
    memcpy(&val, ptr, sizeof(val));
    val = SWAP_2(val);
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* h_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 559 of file cfield.c.

{
    long val;
    short x;
    if (get_long(value, &val) < 0)
        return NULL;
    memcpy(&x, ptr, sizeof(x));
    x = SET(x, (short)val, size);
    memcpy(ptr, &x, sizeof(x));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* H_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 607 of file cfield.c.

{
    unsigned long val;
    unsigned short x;
    if (get_ulong(value, &val) < 0)
        return NULL;
    memcpy(&x, ptr, sizeof(x));
    x = SET(x, (unsigned short)val, size);
    memcpy(ptr, &x, sizeof(x));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* h_set_sw ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 573 of file cfield.c.

{
    long val;
    short field;
    if (get_long(value, &val) < 0)
        return NULL;
    memcpy(&field, ptr, sizeof(field));
    field = SWAP_2(field);
    field = SET(field, (short)val, size);
    field = SWAP_2(field);
    memcpy(ptr, &field, sizeof(field));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* H_set_sw ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 620 of file cfield.c.

{
    unsigned long val;
    unsigned short field;
    if (get_ulong(value, &val) < 0)
        return NULL;
    memcpy(&field, ptr, sizeof(field));
    field = SWAP_2(field);
    field = SET(field, (unsigned short)val, size);
    field = SWAP_2(field);
    memcpy(ptr, &field, sizeof(field));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* i_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 684 of file cfield.c.

{
    int val;
    memcpy(&val, ptr, sizeof(val));
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* I_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 784 of file cfield.c.

{
    unsigned int val;
    memcpy(&val, ptr, sizeof(val));
    GET_BITFIELD(val, size);
    return PyLong_FromUnsignedLong(val);
}

Here is the call graph for this function:

static PyObject* i_get_sw ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 693 of file cfield.c.

{
    int val;
    memcpy(&val, ptr, sizeof(val));
    val = SWAP_INT(val);
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* I_get_sw ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 793 of file cfield.c.

{
    unsigned int val;
    memcpy(&val, ptr, sizeof(val));
    val = SWAP_INT(val);
    GET_BITFIELD(val, size);
    return PyLong_FromUnsignedLong(val);
}

Here is the call graph for this function:

static PyObject* i_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 655 of file cfield.c.

{
    long val;
    int x;
    if (get_long(value, &val) < 0)
        return NULL;
    memcpy(&x, ptr, sizeof(x));
    x = SET(x, (int)val, size);
    memcpy(ptr, &x, sizeof(x));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* I_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 756 of file cfield.c.

{
    unsigned long val;
    unsigned int x;
    if (get_ulong(value, &val) < 0)
        return  NULL;
    memcpy(&x, ptr, sizeof(x));
    x = SET(x, (unsigned int)val, size);
    memcpy(ptr, &x, sizeof(x));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* i_set_sw ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 668 of file cfield.c.

{
    long val;
    int field;
    if (get_long(value, &val) < 0)
        return NULL;
    memcpy(&field, ptr, sizeof(field));
    field = SWAP_INT(field);
    field = SET(field, (int)val, size);
    field = SWAP_INT(field);
    memcpy(ptr, &field, sizeof(field));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* I_set_sw ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 769 of file cfield.c.

{
    unsigned long val;
    unsigned int field;
    if (get_ulong(value, &val) < 0)
        return  NULL;
    memcpy(&field, ptr, sizeof(field));
    field = (unsigned int)SET(field, (unsigned int)val, size);
    field = SWAP_INT(field);
    memcpy(ptr, &field, sizeof(field));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* l_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 832 of file cfield.c.

{
    long val;
    memcpy(&val, ptr, sizeof(val));
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* L_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 880 of file cfield.c.

{
    unsigned long val;
    memcpy(&val, ptr, sizeof(val));
    GET_BITFIELD(val, size);
    return PyLong_FromUnsignedLong(val);
}

Here is the call graph for this function:

static PyObject* l_get_sw ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 841 of file cfield.c.

{
    long val;
    memcpy(&val, ptr, sizeof(val));
    val = SWAP_LONG(val);
    GET_BITFIELD(val, size);
    return PyLong_FromLong(val);
}

Here is the call graph for this function:

static PyObject* L_get_sw ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 889 of file cfield.c.

{
    unsigned long val;
    memcpy(&val, ptr, sizeof(val));
    val = SWAP_LONG(val);
    GET_BITFIELD(val, size);
    return PyLong_FromUnsignedLong(val);
}

Here is the call graph for this function:

static PyObject* l_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 803 of file cfield.c.

{
    long val;
    long x;
    if (get_long(value, &val) < 0)
        return NULL;
    memcpy(&x, ptr, sizeof(x));
    x = SET(x, val, size);
    memcpy(ptr, &x, sizeof(x));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* L_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 851 of file cfield.c.

{
    unsigned long val;
    unsigned long x;
    if (get_ulong(value, &val) < 0)
        return  NULL;
    memcpy(&x, ptr, sizeof(x));
    x = SET(x, val, size);
    memcpy(ptr, &x, sizeof(x));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* l_set_sw ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 816 of file cfield.c.

{
    long val;
    long field;
    if (get_long(value, &val) < 0)
        return NULL;
    memcpy(&field, ptr, sizeof(field));
    field = SWAP_LONG(field);
    field = (long)SET(field, val, size);
    field = SWAP_LONG(field);
    memcpy(ptr, &field, sizeof(field));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* L_set_sw ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 864 of file cfield.c.

{
    unsigned long val;
    unsigned long field;
    if (get_ulong(value, &val) < 0)
        return  NULL;
    memcpy(&field, ptr, sizeof(field));
    field = SWAP_LONG(field);
    field = (unsigned long)SET(field, val, size);
    field = SWAP_LONG(field);
    memcpy(ptr, &field, sizeof(field));
    _RET(value);
}

Here is the call graph for this function:

static PyObject* O_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1146 of file cfield.c.

{
    PyObject *ob = *(PyObject **)ptr;
    if (ob == NULL) {
        if (!PyErr_Occurred())
            /* Set an error if not yet set */
            PyErr_SetString(PyExc_ValueError,
                            "PyObject is NULL");
        return NULL;
    }
    Py_INCREF(ob);
    return ob;
}

Here is the call graph for this function:

static PyObject* O_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1161 of file cfield.c.

{
    /* Hm, does the memory block need it's own refcount or not? */
    *(PyObject **)ptr = value;
    Py_INCREF(value);
    return value;
}
static PyObject* P_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1573 of file cfield.c.

{
    if (*(void **)ptr == NULL) {
        Py_INCREF(Py_None);
        return Py_None;
    }
    return PyLong_FromVoidPtr(*(void **)ptr);
}

Here is the call graph for this function:

static PyObject* P_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1540 of file cfield.c.

{
    void *v;
    if (value == Py_None) {
        *(void **)ptr = NULL;
        _RET(value);
    }

    if (!PyLong_Check(value) && !PyLong_Check(value)) {
        PyErr_SetString(PyExc_TypeError,
                        "cannot be converted to pointer");
        return NULL;
    }

#if SIZEOF_VOID_P <= SIZEOF_LONG
    v = (void *)PyLong_AsUnsignedLongMask(value);
#else
#ifndef HAVE_LONG_LONG
#   error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
#elif SIZEOF_LONG_LONG < SIZEOF_VOID_P
#   error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
    v = (void *)PyLong_AsUnsignedLongLongMask(value);
#endif

    if (PyErr_Occurred())
        return NULL;

    *(void **)ptr = v;
    _RET(value);
}

Here is the call graph for this function:

static int PyCField_clear ( CFieldObject self) [static]

Definition at line 263 of file cfield.c.

{
    Py_CLEAR(self->proto);
    return 0;
}

Here is the caller graph for this function:

static void PyCField_dealloc ( PyObject self) [static]

Definition at line 270 of file cfield.c.

{
    PyCField_clear((CFieldObject *)self);
    self->ob_type->tp_free((PyObject *)self);
}

Here is the call graph for this function:

PyObject* PyCField_FromDesc ( PyObject desc,
Py_ssize_t  index,
Py_ssize_t pfield_size,
int  bitsize,
int pbitofs,
Py_ssize_t psize,
Py_ssize_t poffset,
Py_ssize_t palign,
int  pack,
int  big_endian 
)

Definition at line 48 of file cfield.c.

{
    CFieldObject *self;
    PyObject *proto;
    Py_ssize_t size, align, length;
    SETFUNC setfunc = NULL;
    GETFUNC getfunc = NULL;
    StgDictObject *dict;
    int fieldtype;
#define NO_BITFIELD 0
#define NEW_BITFIELD 1
#define CONT_BITFIELD 2
#define EXPAND_BITFIELD 3

    self = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type,
                                               NULL);
    if (self == NULL)
        return NULL;
    dict = PyType_stgdict(desc);
    if (!dict) {
        PyErr_SetString(PyExc_TypeError,
                        "has no _stginfo_");
        Py_DECREF(self);
        return NULL;
    }
    if (bitsize /* this is a bitfield request */
        && *pfield_size /* we have a bitfield open */
#ifdef MS_WIN32
        /* MSVC, GCC with -mms-bitfields */
        && dict->size * 8 == *pfield_size
#else
        /* GCC */
        && dict->size * 8 <= *pfield_size
#endif
        && (*pbitofs + bitsize) <= *pfield_size) {
        /* continue bit field */
        fieldtype = CONT_BITFIELD;
#ifndef MS_WIN32
    } else if (bitsize /* this is a bitfield request */
        && *pfield_size /* we have a bitfield open */
        && dict->size * 8 >= *pfield_size
        && (*pbitofs + bitsize) <= dict->size * 8) {
        /* expand bit field */
        fieldtype = EXPAND_BITFIELD;
#endif
    } else if (bitsize) {
        /* start new bitfield */
        fieldtype = NEW_BITFIELD;
        *pbitofs = 0;
        *pfield_size = dict->size * 8;
    } else {
        /* not a bit field */
        fieldtype = NO_BITFIELD;
        *pbitofs = 0;
        *pfield_size = 0;
    }

    size = dict->size;
    length = dict->length;
    proto = desc;

    /*  Field descriptors for 'c_char * n' are be scpecial cased to
        return a Python string instead of an Array object instance...
    */
    if (PyCArrayTypeObject_Check(proto)) {
        StgDictObject *adict = PyType_stgdict(proto);
        StgDictObject *idict;
        if (adict && adict->proto) {
            idict = PyType_stgdict(adict->proto);
            if (!idict) {
                PyErr_SetString(PyExc_TypeError,
                                "has no _stginfo_");
                Py_DECREF(self);
                return NULL;
            }
            if (idict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
                struct fielddesc *fd = _ctypes_get_fielddesc("s");
                getfunc = fd->getfunc;
                setfunc = fd->setfunc;
            }
#ifdef CTYPES_UNICODE
            if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
                struct fielddesc *fd = _ctypes_get_fielddesc("U");
                getfunc = fd->getfunc;
                setfunc = fd->setfunc;
            }
#endif
        }
    }

    self->setfunc = setfunc;
    self->getfunc = getfunc;
    self->index = index;

    Py_INCREF(proto);
    self->proto = proto;

    switch (fieldtype) {
    case NEW_BITFIELD:
        if (big_endian)
            self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
        else
            self->size = (bitsize << 16) + *pbitofs;
        *pbitofs = bitsize;
        /* fall through */
    case NO_BITFIELD:
        if (pack)
            align = min(pack, dict->align);
        else
            align = dict->align;
        if (align && *poffset % align) {
            Py_ssize_t delta = align - (*poffset % align);
            *psize += delta;
            *poffset += delta;
        }

        if (bitsize == 0)
            self->size = size;
        *psize += size;

        self->offset = *poffset;
        *poffset += size;

        *palign = align;
        break;

    case EXPAND_BITFIELD:
        *poffset += dict->size - *pfield_size/8;
        *psize += dict->size - *pfield_size/8;

        *pfield_size = dict->size * 8;

        if (big_endian)
            self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
        else
            self->size = (bitsize << 16) + *pbitofs;

        self->offset = *poffset - size; /* poffset is already updated for the NEXT field */
        *pbitofs += bitsize;
        break;

    case CONT_BITFIELD:
        if (big_endian)
            self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
        else
            self->size = (bitsize << 16) + *pbitofs;

        self->offset = *poffset - size; /* poffset is already updated for the NEXT field */
        *pbitofs += bitsize;
        break;
    }

    return (PyObject *)self;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* PyCField_get ( CFieldObject self,
PyObject inst,
PyTypeObject type 
) [static]

Definition at line 224 of file cfield.c.

{
    CDataObject *src;
    if (inst == NULL) {
        Py_INCREF(self);
        return (PyObject *)self;
    }
    assert(CDataObject_Check(inst));
    src = (CDataObject *)inst;
    return PyCData_get(self->proto, self->getfunc, inst,
                     self->index, self->size, src->b_ptr + self->offset);
}

Here is the call graph for this function:

static PyObject* PyCField_get_offset ( PyObject self,
void data 
) [static]

Definition at line 238 of file cfield.c.

{
    return PyLong_FromSsize_t(((CFieldObject *)self)->offset);
}
static PyObject* PyCField_get_size ( PyObject self,
void data 
) [static]

Definition at line 244 of file cfield.c.

{
    return PyLong_FromSsize_t(((CFieldObject *)self)->size);
}
static PyObject* PyCField_new ( PyTypeObject type,
PyObject args,
PyObject kwds 
) [static]

Definition at line 28 of file cfield.c.

{
    CFieldObject *obj;
    obj = (CFieldObject *)type->tp_alloc(type, 0);
    return (PyObject *)obj;
}
static PyObject* PyCField_repr ( CFieldObject self) [static]

Definition at line 277 of file cfield.c.

{
    PyObject *result;
    Py_ssize_t bits = self->size >> 16;
    Py_ssize_t size = self->size & 0xFFFF;
    const char *name;

    name = ((PyTypeObject *)self->proto)->tp_name;

    if (bits)
        result = PyUnicode_FromFormat(
            "<Field type=%s, ofs=%zd:%zd, bits=%zd>",
            name, self->offset, size, bits);
    else
        result = PyUnicode_FromFormat(
            "<Field type=%s, ofs=%zd, size=%zd>",
            name, self->offset, size);
    return result;
}
static int PyCField_set ( CFieldObject self,
PyObject inst,
PyObject value 
) [static]

Definition at line 207 of file cfield.c.

{
    CDataObject *dst;
    char *ptr;
    assert(CDataObject_Check(inst));
    dst = (CDataObject *)inst;
    ptr = dst->b_ptr + self->offset;
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError,
                        "can't delete attribute");
        return -1;
    }
    return PyCData_set(inst, self->proto, self->setfunc, value,
                     self->index, self->size, ptr);
}

Here is the call graph for this function:

static int PyCField_traverse ( CFieldObject self,
visitproc  visit,
void arg 
) [static]

Definition at line 256 of file cfield.c.

{
    Py_VISIT(self->proto);
    return 0;
}
static PyObject* s_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1302 of file cfield.c.

{
    Py_ssize_t i;
    char *p;

    p = (char *)ptr;
    for (i = 0; i < size; ++i) {
        if (*p++ == '\0')
            break;
    }

    return PyBytes_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
}

Here is the call graph for this function:

static PyObject* s_set ( void ptr,
PyObject value,
Py_ssize_t  length 
) [static]

Definition at line 1317 of file cfield.c.

{
    char *data;
    Py_ssize_t size;

    if(PyBytes_Check(value)) {
        Py_INCREF(value);
    } else {
        PyErr_Format(PyExc_TypeError,
                     "expected string, %s found",
                     value->ob_type->tp_name);
        return NULL;
    }

    data = PyBytes_AS_STRING(value);
    if (!data)
        return NULL;
    size = strlen(data); /* XXX Why not Py_SIZE(value)? */
    if (size < length) {
        /* This will copy the leading NUL character
         * if there is space for it.
         */
        ++size;
    } else if (size > length) {
        PyErr_Format(PyExc_ValueError,
                     "string too long (%zd, maximum length %zd)",
                     size, length);
        Py_DECREF(value);
        return NULL;
    }
    /* Also copy the terminating NUL character if there is space */
    memcpy((char *)ptr, data, size);

    Py_DECREF(value);
    _RET(value);
}

Here is the call graph for this function:

static PyObject* z_get ( void ptr,
Py_ssize_t  size 
) [static]

Definition at line 1381 of file cfield.c.

{
    /* XXX What about invalid pointers ??? */
    if (*(void **)ptr) {
#if defined(MS_WIN32) && !defined(_WIN32_WCE)
        if (IsBadStringPtrA(*(char **)ptr, -1)) {
            PyErr_Format(PyExc_ValueError,
                         "invalid string pointer %p",
                         *(char **)ptr);
            return NULL;
        }
#endif
        return PyBytes_FromStringAndSize(*(char **)ptr,
                                         strlen(*(char **)ptr));
    } else {
        Py_INCREF(Py_None);
        return Py_None;
    }
}

Here is the call graph for this function:

static PyObject* z_set ( void ptr,
PyObject value,
Py_ssize_t  size 
) [static]

Definition at line 1355 of file cfield.c.

{
    if (value == Py_None) {
        *(char **)ptr = NULL;
        Py_INCREF(value);
        return value;
    }
    if (PyBytes_Check(value)) {
        *(char **)ptr = PyBytes_AsString(value);
        Py_INCREF(value);
        return value;
    } else if (PyLong_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
        *(char **)ptr = (char *)PyLong_AsUnsignedLongLongMask(value);
#else
        *(char **)ptr = (char *)PyLong_AsUnsignedLongMask(value);
#endif
        _RET(value);
    }
    PyErr_Format(PyExc_TypeError,
                 "string or integer address expected instead of %s instance",
                 value->ob_type->tp_name);
    return NULL;
}

Here is the call graph for this function:


Variable Documentation

Definition at line 1733 of file cfield.c.

Definition at line 1732 of file cfield.c.

Initial value:
 { sizeof(long double), LONGDOUBLE_ALIGN,
                                 FFI_TYPE_LONGDOUBLE }

Definition at line 1739 of file cfield.c.

Definition at line 1742 of file cfield.c.

Definition at line 1724 of file cfield.c.

Definition at line 1727 of file cfield.c.

ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 }

Definition at line 1730 of file cfield.c.

Definition at line 1721 of file cfield.c.

Definition at line 1723 of file cfield.c.

Definition at line 1726 of file cfield.c.

ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 }

Definition at line 1729 of file cfield.c.

Definition at line 1720 of file cfield.c.

Definition at line 1718 of file cfield.c.

struct fielddesc[] [static]

Definition at line 1582 of file cfield.c.

Initial value:
 {
    { "offset", PyCField_get_offset, NULL, "offset in bytes of this field" },
    { "size", PyCField_get_size, NULL, "size in bytes of this field" },
    { NULL, NULL, NULL, NULL },
}

Definition at line 249 of file cfield.c.

Definition at line 297 of file cfield.c.