@@ -53,6 +53,8 @@ these can be preceded by a decimal repeat count:\n\
5353 l:long; L:unsigned long; f:float; d:double.\n\
5454Special cases (preceding decimal count indicates length):\n\
5555 s:string (array of char); p: pascal string (w. count byte).\n\
56+ Special case (only available in native format):\n\
57+ P:an integer type that is wide enough to hold a pointer.\n\
5658Whitespace between formats is ignored.\n\
5759\n\
5860The variable struct.error is an exception raised on errors." ;
@@ -86,12 +88,14 @@ typedef struct { char c; int x; } s_int;
8688typedef struct { char c ; long x ; } s_long ;
8789typedef struct { char c ; float x ; } s_float ;
8890typedef struct { char c ; double x ; } s_double ;
91+ typedef struct { char c ; void * x ; } s_void_p ;
8992
9093#define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
9194#define INT_ALIGN (sizeof(s_int) - sizeof(int))
9295#define LONG_ALIGN (sizeof(s_long) - sizeof(long))
9396#define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
9497#define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
98+ #define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void *))
9599
96100#ifdef __powerc
97101#pragma options align=reset
@@ -523,6 +527,14 @@ nu_double(p, f)
523527 return PyFloat_FromDouble (x );
524528}
525529
530+ static PyObject *
531+ nu_void_p (p , f )
532+ const char * p ;
533+ const formatdef * f ;
534+ {
535+ return PyLong_FromVoidPtr (* (void * * )p );
536+ }
537+
526538static int
527539np_byte (p , v , f )
528540 char * p ;
@@ -648,6 +660,24 @@ np_double(p, v, f)
648660 return 0 ;
649661}
650662
663+ static int
664+ np_void_p (p , v , f )
665+ char * p ;
666+ PyObject * v ;
667+ const formatdef * f ;
668+ {
669+ void * x = PyLong_AsVoidPtr (v );
670+ if (x == NULL && PyErr_Occurred ()) {
671+ /* ### hrm. PyLong_AsVoidPtr raises SystemError */
672+ if (PyErr_ExceptionMatches (PyExc_TypeError ))
673+ PyErr_SetString (StructError ,
674+ "required argument is not an integer" );
675+ return -1 ;
676+ }
677+ * (void * * )p = x ;
678+ return 0 ;
679+ }
680+
651681static formatdef native_table [] = {
652682 {'x' , sizeof (char ), 0 , NULL },
653683 {'b' , sizeof (char ), 0 , nu_byte , np_byte },
@@ -663,6 +693,7 @@ static formatdef native_table[] = {
663693 {'L' , sizeof (long ), LONG_ALIGN , nu_ulong , np_ulong },
664694 {'f' , sizeof (float ), FLOAT_ALIGN , nu_float , np_float },
665695 {'d' , sizeof (double ), DOUBLE_ALIGN , nu_double , np_double },
696+ {'P' , sizeof (void * ), VOID_P_ALIGN , nu_void_p , np_void_p },
666697 {0 }
667698};
668699
0 commit comments