Programming Python - examples from the book work, no?

Thomas S. Strinnhed thstr at serop.abb.se
Mon Jun 7 03:45:48 EDT 1999


Hi

Doing everything in MSVC++ on NT I try to run the Example 14-2 (p534-540
) from "Programming Python" by Mark Lutz. I run it just like it comes on
the CD included with the book (CD-DRIVE:/Examples/DOS/Extend/Stacktyp.c)
trying to extend Python with this Stack-type. 
BUT: I get an I-don't-know-what-this-is-about kind of compiler error, 
complaing about this call: PyObject_HEAD_INIT(&PyType_Type)  
with message "initializer is not a constant".

PyObject_HEAD_INIT is in object.h as
#define PyObject_HEAD_INIT(type) 1, type,

and

PyType_Type is (also in object.h) 
extern DL_IMPORT(PyTypeObject) PyType_Type; /* The type of type objects
*/

What's missing in this picture? Is it a version clash??
My sys.version
'1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)]'
(goes for everything but stacktyp.c, which I think is v1.3)

Is it some declaration/definition missing that I need to add?
Has anyone else had problems with this example code?
...and solved it?
maybe even with some MS-developer-environment-point-of-view??

If it is an obvious mistake from my side, please point it out to me,
otherwise just please point it out to me.

Many Thanks
 -- Thomas S. Strinnhed, thstr at serop.abb.se

(Below is the complete initialization of the Stacktype from stacktyp.c,
near the eind of the file.)
...
static PyTypeObject Stacktype = {      /* main python type-descriptor */
  /* type header */                    /* shared by all instances */
      PyObject_HEAD_INIT(&PyType_Type)         
      0,                               /* ob_size */
      "stack",                         /* tp_name */
      sizeof(stackobject),             /* tp_basicsize */
      0,                               /* tp_itemsize */

  /* standard methods */
      (destructor)  stack_dealloc,   /* tp_dealloc  ref-count==0  */
      (printfunc)   stack_print,     /* tp_print    "print x"     */
      (getattrfunc) stack_getattr,   /* tp_getattr  "x.attr"      */
      (setattrfunc) 0,               /* tp_setattr  "x.attr=v"    */
      (cmpfunc)     stack_compare,   /* tp_compare  "x > y"       */
      (reprfunc)    0,               /* tp_repr     `x`, print x  */

  /* type categories */
      0,                             /* tp_as_number  
+,-,*,/,%,&,>>,pow...*/
      &stack_as_sequence,            /* tp_as_sequence +,[i],[i:j],len,
...*/
      0,                             /* tp_as_mapping  [key], len, ...*/

  /* more methods */
      (hashfunc)   0,                /* tp_hash    "dict[x]" */
      (binaryfunc) 0,                /* tp_call    "x()"     */
      (reprfunc)   0,                /* tp_str     "str(x)"  */

};  /* plus others: see Include/object.h */
...




More information about the Python-list mailing list