@@ -9,9 +9,8 @@ def __init__(self, name, prefix, itselftype):
99 self .name = name
1010 self .itselftype = itselftype
1111 self .objecttype = name + 'Object'
12- self .typename = self .prefix + '_' + \
13- string .upper (name [:1 ]) + \
14- string .lower (name [1 :]) + '_Type'
12+ self .typename = name + '_Type'
13+ self .argref = "" # set to "*" if arg to <type>_New should be pointer
1514
1615 def add (self , g ):
1716 g .setselftype (self .objecttype , self .itselftype )
@@ -29,87 +28,76 @@ def generate(self):
2928 Output ("staticforward PyTypeObject %s;" , self .typename )
3029 Output ()
3130
32- Output ("#define is_%sobject (x) ((x)->ob_type == %s)" ,
33- self .name , self .typename )
31+ Output ("#define %s_Check (x) ((x)->ob_type == & %s)" ,
32+ self .prefix , self .typename )
3433 Output ()
3534
3635 Output ("typedef struct {" )
3736 IndentLevel ()
38- Output ("OB_HEAD " )
37+ Output ("PyObject_HEAD " )
3938 Output ("%s ob_itself;" , self .itselftype )
4039 DedentLevel ()
4140 Output ("} %s;" , self .objecttype )
4241 Output ()
4342
44- Output ("static %s *new%s(itself)" , self .objecttype , self .objecttype )
43+ self .outputNew ()
44+
45+ self .outputConvert ()
46+
47+ self .outputDealloc ()
48+
49+ GeneratorGroup .generate (self )
50+
51+ self .outputGetattr ()
52+
53+ self .outputSetattr ()
54+
55+ self .outputTypeObject ()
56+
57+ OutHeader2 ("End object type " + self .name )
58+
59+ def outputNew (self ):
60+ Output ("static %s *%s_New(itself)" , self .objecttype , self .prefix )
4561 IndentLevel ()
46- Output ("%s itself ;" , self .itselftype )
62+ Output ("const %s %sitself ;" , self .itselftype , self . argref )
4763 DedentLevel ()
4864 OutLbrace ()
4965 Output ("%s *it;" , self .objecttype )
5066 Output ("it = PyObject_NEW(%s, &%s);" , self .objecttype , self .typename )
5167 Output ("if (it == NULL) return NULL;" )
52- Output ("it->ob_itself = itself;" )
68+ Output ("it->ob_itself = %sitself;" , self . argref )
5369 Output ("return it;" )
5470 OutRbrace ()
5571 Output ()
5672
73+ def outputConvert (self ):
74+ Output ("""\
75+ static int %(prefix)s_Convert(v, p_itself)
76+ PyObject *v;
77+ %(itselftype)s *p_itself;
78+ {
79+ if (v == NULL || !%(prefix)s_Check(v)) {
80+ PyErr_SetString(PyExc_TypeError, "%(name)s required");
81+ return 0;
82+ }
83+ *p_itself = ((%(objecttype)s *)v)->ob_itself;
84+ return 1;
85+ }
86+ """ % self .__dict__ )
87+
88+ def outputDealloc (self ):
5789 Output ("static void %s_dealloc(self)" , self .prefix )
5890 IndentLevel ()
5991 Output ("%s *self;" , self .objecttype )
6092 DedentLevel ()
6193 OutLbrace ()
62- ## Output("if (self->ob_itself != NULL)")
63- ## OutLbrace()
64- ## self.outputFreeIt("self->ob_itself")
65- ## OutRbrace()
66- Output ("DEL(self);" )
94+ self .outputFreeIt ("self->ob_itself" )
95+ Output ("PyMem_DEL(self);" )
6796 OutRbrace ()
6897 Output ()
6998
70- ## Output("static int %s_converter(p_itself, p_it)", self.prefix)
71- ## IndentLevel()
72- ## Output("%s *p_itself;", self.itselftype)
73- ## Output("%s **p_it;", self.objecttype)
74- ## DedentLevel()
75- ## OutLbrace()
76- ## Output("if (**p_it == NULL)")
77- ## OutLbrace()
78- ## Output("*p_it = new%s(*p_itself);", self.objecttype)
79- ## OutRbrace()
80- ## Output("else")
81- ## OutLbrace()
82- ## Output("*p_itself = (*p_it)->ob_itself;")
83- ## OutRbrace()
84- ## Output("return 1;")
85- ## OutRbrace()
86- ## Output()
87-
88- GeneratorGroup .generate (self )
89-
90- self .outputGetattr ()
91-
92- self .outputSetattr ()
93-
94- Output ("static PyTypeObject %s = {" , self .typename )
95- IndentLevel ()
96- Output ("PyObject_HEAD_INIT(&PyType_Type)" )
97- Output ("0, /*ob_size*/" )
98- Output ("\" %s\" , /*tp_name*/" , self .name )
99- Output ("sizeof(%s), /*tp_basicsize*/" , self .objecttype )
100- Output ("0, /*tp_itemsize*/" )
101- Output ("/* methods */" )
102- Output ("(destructor) %s_dealloc, /*tp_dealloc*/" , self .prefix )
103- Output ("0, /*tp_print*/" )
104- Output ("(getattrfunc) %s_getattr, /*tp_getattr*/" , self .prefix )
105- Output ("(setattrfunc) %s_setattr, /*tp_setattr*/" , self .prefix )
106- DedentLevel ()
107- Output ("};" )
108-
109- OutHeader2 ("End object type " + self .name )
110-
11199 def outputFreeIt (self , name ):
112- Output ("DEL(%s); /* XXX */" , name )
100+ Output ("/* Cleanup of %s goes here */" , name )
113101
114102 def outputGetattr (self ):
115103 Output ("static PyObject *%s_getattr(self, name)" , self .prefix )
@@ -123,8 +111,28 @@ def outputGetattr(self):
123111 Output ()
124112
125113 def outputGetattrBody (self ):
126- Output ("return findmethod(self, %s_methods, name);" , self .prefix )
114+ self .outputGetattrHook ()
115+ Output ("return Py_FindMethod(%s_methods, (PyObject *)self, name);" , self .prefix )
116+
117+ def outputGetattrHook (self ):
118+ pass
127119
128120 def outputSetattr (self ):
129121 Output ("#define %s_setattr NULL" , self .prefix )
130122 Output ()
123+
124+ def outputTypeObject (self ):
125+ Output ("static PyTypeObject %s = {" , self .typename )
126+ IndentLevel ()
127+ Output ("PyObject_HEAD_INIT(&PyType_Type)" )
128+ Output ("0, /*ob_size*/" )
129+ Output ("\" %s\" , /*tp_name*/" , self .name )
130+ Output ("sizeof(%s), /*tp_basicsize*/" , self .objecttype )
131+ Output ("0, /*tp_itemsize*/" )
132+ Output ("/* methods */" )
133+ Output ("(destructor) %s_dealloc, /*tp_dealloc*/" , self .prefix )
134+ Output ("0, /*tp_print*/" )
135+ Output ("(getattrfunc) %s_getattr, /*tp_getattr*/" , self .prefix )
136+ Output ("(setattrfunc) %s_setattr, /*tp_setattr*/" , self .prefix )
137+ DedentLevel ()
138+ Output ("};" )
0 commit comments