[Python-checkins] python/dist/src/Tools/bgen/bgen bgenObjectDefinition.py,1.19,1.20

jackjansen@users.sourceforge.net jackjansen@users.sourceforge.net
2002年12月03日 15:35:24 -0800


Update of /cvsroot/python/python/dist/src/Tools/bgen/bgen
In directory sc8-pr-cvs1:/tmp/cvs-serv10163
Modified Files:
	bgenObjectDefinition.py 
Log Message:
Added PEP253 support.
Index: bgenObjectDefinition.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/bgen/bgen/bgenObjectDefinition.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** bgenObjectDefinition.py	28 Nov 2002 23:23:14 -0000	1.19
--- bgenObjectDefinition.py	3 Dec 2002 23:35:22 -0000	1.20
***************
*** 5,8 ****
--- 5,10 ----
 	"Spit out code that together defines a new Python object type"
 	basechain = "NULL"
+ 	tp_flags = "Py_TPFLAGS_DEFAULT"
+ 	basetype = None
 
 	def __init__(self, name, prefix, itselftype):
***************
*** 46,50 ****
 		Output("%sPyTypeObject %s;", sf, self.typename)
 		Output()
! 		Output("#define %s_Check(x) ((x)->ob_type == &%s)",
 		 self.prefix, self.typename)
 		Output()
--- 48,56 ----
 		Output("%sPyTypeObject %s;", sf, self.typename)
 		Output()
! 		if self.basetype:
! 			Output("#define %s_Check(x) ((x)->ob_type == &%s || PyObject_TypeCheck((x), %s)",
! 		 self.prefix, self.typename, self.typename)
! 		else:
! 			Output("#define %s_Check(x) ((x)->ob_type == &%s)",
 		 self.prefix, self.typename)
 		Output()
***************
*** 77,80 ****
--- 83,88 ----
 		self.outputHash()
 		
+ 		self.outputPEP253Hooks()
+ 		
 		self.outputTypeObject()
 
***************
*** 97,100 ****
--- 105,110 ----
 		Output("it = PyObject_NEW(%s, &%s);", self.objecttype, self.typename)
 		Output("if (it == NULL) return NULL;")
+ 		if self.basetype:
+ 			Output("/* XXXX Should we tp_init or tp_new our basetype? */")
 		self.outputInitStructMembers()
 		Output("return (PyObject *)it;")
***************
*** 129,133 ****
 		OutLbrace()
 		self.outputCleanupStructMembers()
! 		Output("PyObject_Del(self);")
 		OutRbrace()
 
--- 139,146 ----
 		OutLbrace()
 		self.outputCleanupStructMembers()
! 		if self.basetype:
! 			Output("%s.tp_dealloc(self)", self.basetype)
! 		else:
! 			Output("PyObject_Del(self);")
 		OutRbrace()
 
***************
*** 198,209 ****
 	def outputTypeObjectInitializer(self):
 		Output("""%s.ob_type = &PyType_Type;""", self.typename);
 		Output("""Py_INCREF(&%s);""", self.typename);
! 		Output("""if (PyDict_SetItemString(d, "%sType", (PyObject *)&%s) != 0)""",
! 			self.name, self.typename);
! 		IndentLevel()
! 		Output("""Py_FatalError("can\'t initialize %sType");""",
! 		 self.name)
! 		DedentLevel()
 
 class PEP252Mixin:
 	getsetlist = []
--- 211,225 ----
 	def outputTypeObjectInitializer(self):
 		Output("""%s.ob_type = &PyType_Type;""", self.typename);
+ 		if self.basetype:
+ 			Output("%s.tp_base = %s;", self.typename, self.basetype)
+ 		Output("""Py_INCREF(&%s);""", self.typename)
+ 		Output("PyModule_AddObject(m, \"%s\", (PyObject *)&%s);", self.name, self.typename);
+ 		Output("/* Backward-compatible name */")
 		Output("""Py_INCREF(&%s);""", self.typename);
! 		Output("PyModule_AddObject(m, \"%sType\", (PyObject *)&%s);", self.name, self.typename);
 
+ 	def outputPEP253Hooks(self):
+ 		pass
+ 		
 class PEP252Mixin:
 	getsetlist = []
***************
*** 238,242 ****
 			func()
 		else:
! 			Output("0, /*%s*/", methodname)
 	
 	def outputTypeObject(self):
--- 254,258 ----
 			func()
 		else:
! 			Output("0, /*%s*/", name)
 	
 	def outputTypeObject(self):
***************
*** 267,271 ****
 		
 		Output("(hashfunc) %s_hash, /*tp_hash*/", self.prefix)
! 		Output("0, /*tp_call*/")
 		Output("0, /*tp_str*/")
 		Output("PyObject_GenericGetAttr, /*tp_getattro*/")
--- 283,287 ----
 		
 		Output("(hashfunc) %s_hash, /*tp_hash*/", self.prefix)
! 		self.outputHook("tp_call")
 		Output("0, /*tp_str*/")
 		Output("PyObject_GenericGetAttr, /*tp_getattro*/")
***************
*** 273,277 ****
 		
 		self.outputHook("tp_as_buffer")
! 		self.outputHook("tp_flags")
 		self.outputHook("tp_doc")
 		self.outputHook("tp_traverse")
--- 289,293 ----
 		
 		self.outputHook("tp_as_buffer")
! 		Output("%s, /* tp_flags */", self.tp_flags)
 		self.outputHook("tp_doc")
 		self.outputHook("tp_traverse")
***************
*** 285,288 ****
--- 301,312 ----
 		Output("%s_getsetlist, /*tp_getset*/", self.prefix)
 		self.outputHook("tp_base")
+ 		self.outputHook("tp_dict")
+ 		self.outputHook("tp_descr_get")
+ 		self.outputHook("tp_descr_set")
+ 		self.outputHook("tp_dictoffset")
+ 		self.outputHook("tp_init")
+ 		self.outputHook("tp_alloc")
+ 		self.outputHook("tp_new")
+ 		self.outputHook("tp_free")
 		DedentLevel()
 		Output("};")
***************
*** 334,337 ****
--- 358,440 ----
 		OutRbrace()
 		Output()
+ 		
+ class PEP253Mixin(PEP252Mixin):
+ 	tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE"
+ 	
+ 	def outputHook_tp_init(self):
+ 		Output("%s_tp_init, /* tp_init */", self.prefix)
+ 		
+ 	def outputHook_tp_alloc(self):
+ 		Output("%s_tp_alloc, /* tp_alloc */", self.prefix)
+ 	
+ 	def outputHook_tp_new(self):
+ 		Output("%s_tp_new, /* tp_new */", self.prefix)
+ 		
+ 	def outputHook_tp_free(self):
+ 		Output("%s_tp_free, /* tp_free */", self.prefix)
+ 		
+ 	output_tp_initBody = None
+ 	
+ 	def output_tp_init(self):
+ 		if self.output_tp_initBody:
+ 			Output("static int %s_init(PyObject *self, PyObject *args, PyObject *kwds)", self.prefix)
+ 			OutLbrace()
+ 			self.output_tp_initBody()
+ 			OutRbrace()
+ 		else:
+ 			Output("#define %s_tp_init 0", self.prefix)
+ 		Output()
+ 		
+ 	output_tp_allocBody = None
+ 	
+ 	def output_tp_alloc(self):
+ 		if self.output_tp_allocBody:
+ 			Output("static PyObject *%s_tp_alloc(PyTypeObject *type, int nitems)",
+ 				self.prefix)
+ 			OutLbrace()
+ 			self.output_tp_allocBody()
+ 			OutRbrace()
+ 		else:
+ 			Output("#define %s_tp_alloc PyType_GenericAlloc", self.prefix)
+ 		Output()
+ 		
+ 	def output_tp_newBody(self):
+ 		Output("PyObject *self;");
+ 		Output("%s itself;", self.itselftype);
+ 		Output("char *kw[] = {\"itself\", 0};")
+ 		Output()
+ 		Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"O&\", kw, %s_Convert, &itself)) return NULL;",
+ 			self.prefix);
+ 		Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
+ 		Output("((%s *)self)->ob_itself = itself;", self.objecttype)
+ 		Output("return self;")
+ 	
+ 	def output_tp_new(self):
+ 		if self.output_tp_newBody:
+ 			Output("static PyObject *%s_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)", self.prefix)
+ 			OutLbrace()
+ 			self.output_tp_newBody()
+ 			OutRbrace()
+ 		else:
+ 			Output("#define %s_tp_new PyType_GenericNew", self.prefix)
+ 		Output()
+ 	
+ 	output_tp_freeBody = None
+ 	
+ 	def output_tp_free(self):
+ 		if self.output_tp_freeBody:
+ 			Output("static void %s_tp_free(PyObject *self)", self.prefix)
+ 			OutLbrace()
+ 			self.output_tp_freeBody()
+ 			OutRbrace()
+ 		else:
+ 			Output("#define %s_tp_free PyObject_Del", self.prefix)
+ 		Output()
+ 		
+ 	def outputPEP253Hooks(self):
+ 		self.output_tp_init()
+ 		self.output_tp_alloc()
+ 		self.output_tp_new()
+ 		self.output_tp_free()
 
 class GlobalObjectDefinition(ObjectDefinition):

AltStyle によって変換されたページ (->オリジナル) /