| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | # This script will generate the Resources interface for Python. | 
					
						
							|  |  |  | # It uses the "bgen" package to generate C code. | 
					
						
							|  |  |  | # It execs the file resgen.py which contain the function definitions | 
					
						
							|  |  |  | # (resgen.py was generated by resscan.py, scanning the <Resources.h> header file). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from macsupport import * | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResMixIn: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	def checkit(self): | 
					
						
							| 
									
										
										
										
											2002-03-18 15:31:08 +00:00
										 |  |  | 		if self.returntype.__class__ != OSErrType: | 
					
						
							|  |  |  | 			OutLbrace() | 
					
						
							|  |  |  | 			Output("OSErr _err = ResError();") | 
					
						
							|  |  |  | 			Output("if (_err != noErr) return PyMac_Error(_err);") | 
					
						
							|  |  |  | 			OutRbrace() | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | 		FunctionGenerator.checkit(self) # XXX | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-22 14:16:39 +00:00
										 |  |  | class ResFunction(ResMixIn, OSErrWeakLinkFunctionGenerator): pass | 
					
						
							|  |  |  | class ResMethod(ResMixIn, OSErrWeakLinkMethodGenerator): pass | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-12 22:10:21 +00:00
										 |  |  | RsrcChainLocation = Type("RsrcChainLocation", "h") | 
					
						
							| 
									
										
										
										
											2002-01-01 22:43:13 +00:00
										 |  |  | FSCatalogInfoBitmap = FakeType("0") # Type("FSCatalogInfoBitmap", "l") | 
					
						
							|  |  |  | FSCatalogInfo_ptr = FakeType("(FSCatalogInfo *)0") | 
					
						
							| 
									
										
										
										
											2000-12-12 22:10:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | # includestuff etc. are imported from macsupport | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | includestuff = includestuff + """
 | 
					
						
							| 
									
										
										
										
											2002-11-18 15:26:43 +00:00
										 |  |  | #ifndef PyDoc_STR | 
					
						
							|  |  |  | #define PyDoc_STR(x) (x) | 
					
						
							|  |  |  | #endif | 
					
						
							| 
									
										
										
										
											2001-05-22 21:56:42 +00:00
										 |  |  | #ifdef WITHOUT_FRAMEWORKS | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | #include <Resources.h> | 
					
						
							| 
									
										
										
										
											1997-08-15 14:36:45 +00:00
										 |  |  | #include <string.h> | 
					
						
							| 
									
										
										
										
											2001-05-22 21:56:42 +00:00
										 |  |  | #else | 
					
						
							|  |  |  | #include <Carbon/Carbon.h> | 
					
						
							|  |  |  | #endif | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-05-17 21:58:34 +00:00
										 |  |  | #ifdef USE_TOOLBOX_OBJECT_GLUE | 
					
						
							|  |  |  | extern PyObject *_ResObj_New(Handle); | 
					
						
							|  |  |  | extern int _ResObj_Convert(PyObject *, Handle *); | 
					
						
							|  |  |  | extern PyObject *_OptResObj_New(Handle); | 
					
						
							|  |  |  | extern int _OptResObj_Convert(PyObject *, Handle *); | 
					
						
							|  |  |  | #define ResObj_New _ResObj_New | 
					
						
							|  |  |  | #define ResObj_Convert _ResObj_Convert | 
					
						
							|  |  |  | #define OptResObj_New _OptResObj_New | 
					
						
							|  |  |  | #define OptResObj_Convert _OptResObj_Convert | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-08 16:58:15 +00:00
										 |  |  | /* Function to dispose a resource, with a "normal" calling sequence */ | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | PyMac_AutoDisposeHandle(Handle h) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	DisposeHandle(h); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | finalstuff = finalstuff + """
 | 
					
						
							| 
									
										
										
										
											1995-08-17 14:29:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Alternative version of ResObj_New, which returns None for null argument */ | 
					
						
							| 
									
										
										
										
											2001-05-22 21:56:42 +00:00
										 |  |  | PyObject *OptResObj_New(Handle itself) | 
					
						
							| 
									
										
										
										
											1995-08-17 14:29:23 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (itself == NULL) { | 
					
						
							|  |  |  | 		Py_INCREF(Py_None); | 
					
						
							|  |  |  | 		return Py_None; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ResObj_New(itself); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-05 10:31:52 +00:00
										 |  |  | int OptResObj_Convert(PyObject *v, Handle *p_itself) | 
					
						
							| 
									
										
										
										
											1995-12-12 15:02:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1999-12-12 22:57:29 +00:00
										 |  |  | 	PyObject *tmp; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-12-12 15:02:03 +00:00
										 |  |  | 	if ( v == Py_None ) { | 
					
						
							|  |  |  | 		*p_itself = NULL; | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1999-12-12 22:57:29 +00:00
										 |  |  | 	if (ResObj_Check(v)) | 
					
						
							| 
									
										
										
										
											1995-12-12 15:02:03 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											1999-12-12 22:57:29 +00:00
										 |  |  | 		*p_itself = ((ResourceObject *)v)->ob_itself; | 
					
						
							|  |  |  | 		return 1; | 
					
						
							| 
									
										
										
										
											1995-12-12 15:02:03 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1999-12-12 22:57:29 +00:00
										 |  |  | 	/* If it isn't a resource yet see whether it is convertible */ | 
					
						
							|  |  |  | 	if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) { | 
					
						
							|  |  |  | 		*p_itself = ((ResourceObject *)tmp)->ob_itself; | 
					
						
							|  |  |  | 		Py_DECREF(tmp); | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	PyErr_Clear(); | 
					
						
							|  |  |  | 	PyErr_SetString(PyExc_TypeError, "Resource required"); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											1995-12-12 15:02:03 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | initstuff = initstuff + """
 | 
					
						
							| 
									
										
										
										
											2001-05-22 21:56:42 +00:00
										 |  |  | 	PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, ResObj_New); | 
					
						
							|  |  |  | 	PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, ResObj_Convert); | 
					
						
							|  |  |  | 	PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, OptResObj_New); | 
					
						
							|  |  |  | 	PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, OptResObj_Convert); | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-23 13:51:46 +00:00
										 |  |  | module = MacModule('_Res', 'Res', includestuff, finalstuff, initstuff) | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-05 23:26:38 +00:00
										 |  |  | class ResDefinition(PEP253Mixin, GlobalObjectDefinition): | 
					
						
							| 
									
										
										
										
											2002-11-29 23:40:48 +00:00
										 |  |  | 	getsetlist = [ | 
					
						
							|  |  |  | 		('data', | 
					
						
							|  |  |  | 		"""
 | 
					
						
							|  |  |  | 		PyObject *res; | 
					
						
							|  |  |  | 		char state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		state = HGetState(self->ob_itself); | 
					
						
							|  |  |  | 		HLock(self->ob_itself); | 
					
						
							|  |  |  | 		res = PyString_FromStringAndSize( | 
					
						
							|  |  |  | 			*self->ob_itself, | 
					
						
							|  |  |  | 			GetHandleSize(self->ob_itself)); | 
					
						
							|  |  |  | 		HUnlock(self->ob_itself); | 
					
						
							|  |  |  | 		HSetState(self->ob_itself, state); | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 		""",
 | 
					
						
							|  |  |  | 		"""
 | 
					
						
							|  |  |  | 		char *data; | 
					
						
							|  |  |  | 		long size; | 
					
						
							| 
									
										
										
										
											1995-06-18 20:20:27 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-11-29 23:40:48 +00:00
										 |  |  | 		if ( v == NULL ) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		if ( !PyString_Check(v) ) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		size = PyString_Size(v); | 
					
						
							|  |  |  | 		data = PyString_AsString(v); | 
					
						
							|  |  |  | 		/* XXXX Do I need the GetState/SetState calls? */ | 
					
						
							|  |  |  | 		SetHandleSize(self->ob_itself, size); | 
					
						
							|  |  |  | 		if ( MemError()) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		HLock(self->ob_itself); | 
					
						
							|  |  |  | 		memcpy((char *)*self->ob_itself, data, size); | 
					
						
							|  |  |  | 		HUnlock(self->ob_itself); | 
					
						
							|  |  |  | 		/* XXXX Should I do the Changed call immedeately? */ | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 		""",
 | 
					
						
							|  |  |  | 		'The resource data' | 
					
						
							|  |  |  | 		), ( | 
					
						
							|  |  |  | 		'size', | 
					
						
							|  |  |  | 		'return PyInt_FromLong(GetHandleSize(self->ob_itself));', | 
					
						
							|  |  |  | 		None, | 
					
						
							|  |  |  | 		'The length of the resource data' | 
					
						
							|  |  |  | 		)] | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	def outputCheckNewArg(self): | 
					
						
							|  |  |  | 		Output("if (itself == NULL) return PyMac_Error(resNotFound);") | 
					
						
							| 
									
										
										
										
											1999-12-12 22:57:29 +00:00
										 |  |  | 		 | 
					
						
							|  |  |  | 	def outputCheckConvertArg(self): | 
					
						
							|  |  |  | 		# if it isn't a resource we may be able to coerce it | 
					
						
							|  |  |  | 		Output("if (!%s_Check(v))", self.prefix) | 
					
						
							|  |  |  | 		OutLbrace() | 
					
						
							|  |  |  | 		Output("PyObject *tmp;") | 
					
						
							|  |  |  | 		Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )') | 
					
						
							|  |  |  | 		OutLbrace() | 
					
						
							|  |  |  | 		Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;") | 
					
						
							|  |  |  | 		Output("Py_DECREF(tmp);") | 
					
						
							|  |  |  | 		Output("return 1;") | 
					
						
							|  |  |  | 		OutRbrace() | 
					
						
							|  |  |  | 		Output("PyErr_Clear();") | 
					
						
							|  |  |  | 		OutRbrace() | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-08 16:58:15 +00:00
										 |  |  | 	def outputStructMembers(self): | 
					
						
							|  |  |  | 		GlobalObjectDefinition.outputStructMembers(self) | 
					
						
							|  |  |  | 		Output("void (*ob_freeit)(%s ptr);", self.itselftype) | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	def outputInitStructMembers(self): | 
					
						
							|  |  |  | 		GlobalObjectDefinition.outputInitStructMembers(self) | 
					
						
							|  |  |  | 		Output("it->ob_freeit = NULL;") | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	def outputCleanupStructMembers(self): | 
					
						
							|  |  |  | 		Output("if (self->ob_freeit && self->ob_itself)") | 
					
						
							|  |  |  | 		OutLbrace() | 
					
						
							|  |  |  | 		Output("self->ob_freeit(self->ob_itself);") | 
					
						
							|  |  |  | 		OutRbrace() | 
					
						
							|  |  |  | 		Output("self->ob_itself = NULL;") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-05 23:26:38 +00:00
										 |  |  | 	def output_tp_newBody(self): | 
					
						
							|  |  |  | 		Output("PyObject *self;") | 
					
						
							|  |  |  | 		Output | 
					
						
							|  |  |  | 		Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;") | 
					
						
							|  |  |  | 		Output("((%s *)self)->ob_itself = NULL;", self.objecttype) | 
					
						
							|  |  |  | 		Output("((%s *)self)->ob_freeit = NULL;", self.objecttype) | 
					
						
							|  |  |  | 		Output("return self;") | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	def output_tp_initBody(self): | 
					
						
							|  |  |  | 		Output("char *srcdata = NULL;") | 
					
						
							|  |  |  | 		Output("int srclen = 0;") | 
					
						
							|  |  |  | 		Output("%s itself;", self.itselftype); | 
					
						
							|  |  |  | 		Output("char *kw[] = {\"itself\", 0};") | 
					
						
							|  |  |  | 		Output() | 
					
						
							|  |  |  | 		Output("if (PyArg_ParseTupleAndKeywords(args, kwds, \"O&\", kw, %s_Convert, &itself))", | 
					
						
							|  |  |  | 			self.prefix); | 
					
						
							|  |  |  | 		OutLbrace() | 
					
						
							|  |  |  | 		Output("((%s *)self)->ob_itself = itself;", self.objecttype) | 
					
						
							|  |  |  | 		Output("return 0;") | 
					
						
							|  |  |  | 		OutRbrace() | 
					
						
							|  |  |  | 		Output("PyErr_Clear();") | 
					
						
							|  |  |  | 		Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"|s#\", kw, &srcdata, &srclen)) return -1;") | 
					
						
							|  |  |  | 		Output("if ((itself = NewHandle(srclen)) == NULL)") | 
					
						
							|  |  |  | 		OutLbrace() | 
					
						
							|  |  |  | 		Output("PyErr_NoMemory();") | 
					
						
							|  |  |  | 		Output("return 0;") | 
					
						
							|  |  |  | 		OutRbrace() | 
					
						
							|  |  |  | 		Output("((%s *)self)->ob_itself = itself;", self.objecttype) | 
					
						
							|  |  |  | # XXXX		Output("((%s *)self)->ob_freeit = PyMac_AutoDisposeHandle;") | 
					
						
							|  |  |  | 		Output("if (srclen && srcdata)") | 
					
						
							|  |  |  | 		OutLbrace() | 
					
						
							|  |  |  | 		Output("HLock(itself);") | 
					
						
							|  |  |  | 		Output("memcpy(*itself, srcdata, srclen);") | 
					
						
							|  |  |  | 		Output("HUnlock(itself);") | 
					
						
							|  |  |  | 		OutRbrace() | 
					
						
							|  |  |  | 		Output("return 0;") | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-08 16:58:15 +00:00
										 |  |  | resobject = ResDefinition('Resource', 'ResObj', 'Handle') | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | module.addobject(resobject) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | functions = [] | 
					
						
							|  |  |  | resmethods = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | execfile('resgen.py') | 
					
						
							| 
									
										
										
										
											1995-02-05 16:54:27 +00:00
										 |  |  | execfile('resedit.py') | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | for f in functions: module.add(f) | 
					
						
							|  |  |  | for f in resmethods: resobject.add(f) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-23 13:51:46 +00:00
										 |  |  | SetOutputFileName('_Resmodule.c') | 
					
						
							| 
									
										
										
										
											1995-01-30 11:53:55 +00:00
										 |  |  | module.generate() |