@@ -612,6 +612,48 @@ static PyGetSetDef ResObj_getsetlist[] = {
612612#define ResObj_repr NULL
613613
614614#define ResObj_hash NULL
615+ static int ResObj_tp_init (PyObject * self , PyObject * args , PyObject * kwds )
616+ {
617+ char * srcdata = NULL ;
618+ int srclen = 0 ;
619+ Handle itself ;
620+ char * kw [] = {"itself" , 0 };
621+
622+ if (PyArg_ParseTupleAndKeywords (args , kwds , "O&" , kw , ResObj_Convert , & itself ))
623+ {
624+ ((ResourceObject * )self )-> ob_itself = itself ;
625+ return 0 ;
626+ }
627+ PyErr_Clear ();
628+ if (!PyArg_ParseTupleAndKeywords (args , kwds , "|s#" , kw , & srcdata , & srclen )) return -1 ;
629+ if ((itself = NewHandle (srclen )) == NULL )
630+ {
631+ PyErr_NoMemory ();
632+ return 0 ;
633+ }
634+ ((ResourceObject * )self )-> ob_itself = itself ;
635+ if (srclen && srcdata )
636+ {
637+ HLock (itself );
638+ memcpy (* itself , srcdata , srclen );
639+ HUnlock (itself );
640+ }
641+ return 0 ;
642+ }
643+
644+ #define ResObj_tp_alloc PyType_GenericAlloc
645+
646+ static PyObject * ResObj_tp_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
647+ {
648+ PyObject * self ;
649+ if ((self = type -> tp_alloc (type , 0 )) == NULL ) return NULL ;
650+ ((ResourceObject * )self )-> ob_itself = NULL ;
651+ ((ResourceObject * )self )-> ob_freeit = NULL ;
652+ return self ;
653+ }
654+
655+ #define ResObj_tp_free PyObject_Del
656+
615657
616658PyTypeObject Resource_Type = {
617659 PyObject_HEAD_INIT (NULL )
@@ -634,19 +676,27 @@ PyTypeObject Resource_Type = {
634676 0 , /*tp_str*/
635677 PyObject_GenericGetAttr , /*tp_getattro*/
636678 PyObject_GenericSetAttr , /*tp_setattro */
637- 0 , /*outputHook_tp_as_buffer */
638- 0 , /*outputHook_tp_flags */
639- 0 , /*outputHook_tp_doc */
640- 0 , /*outputHook_tp_traverse */
641- 0 , /*outputHook_tp_clear */
642- 0 , /*outputHook_tp_richcompare */
643- 0 , /*outputHook_tp_weaklistoffset */
644- 0 , /*outputHook_tp_iter */
645- 0 , /*outputHook_tp_iternext */
679+ 0 , /*tp_as_buffer */
680+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE , /* tp_flags */
681+ 0 , /*tp_doc */
682+ 0 , /*tp_traverse */
683+ 0 , /*tp_clear */
684+ 0 , /*tp_richcompare */
685+ 0 , /*tp_weaklistoffset */
686+ 0 , /*tp_iter */
687+ 0 , /*tp_iternext */
646688 ResObj_methods , /* tp_methods */
647- 0 , /*outputHook_tp_members */
689+ 0 , /*tp_members */
648690 ResObj_getsetlist , /*tp_getset*/
649- 0 , /*outputHook_tp_base*/
691+ 0 , /*tp_base*/
692+ 0 , /*tp_dict*/
693+ 0 , /*tp_descr_get*/
694+ 0 , /*tp_descr_set*/
695+ 0 , /*tp_dictoffset*/
696+ ResObj_tp_init , /* tp_init */
697+ ResObj_tp_alloc , /* tp_alloc */
698+ ResObj_tp_new , /* tp_new */
699+ ResObj_tp_free , /* tp_free */
650700};
651701
652702/* -------------------- End object type Resource -------------------- */
@@ -1694,29 +1744,6 @@ static PyObject *Res_FSOpenResourceFile(PyObject *_self, PyObject *_args)
16941744}
16951745#endif
16961746
1697- static PyObject * Res_Resource (PyObject * _self , PyObject * _args )
1698- {
1699- PyObject * _res = NULL ;
1700-
1701- char * buf ;
1702- int len ;
1703- Handle h ;
1704-
1705- if (!PyArg_ParseTuple (_args , "s#" , & buf , & len ))
1706- return NULL ;
1707- h = NewHandle (len );
1708- if ( h == NULL ) {
1709- PyErr_NoMemory ();
1710- return NULL ;
1711- }
1712- HLock (h );
1713- memcpy (* h , buf , len );
1714- HUnlock (h );
1715- _res = ResObj_New (h );
1716- return _res ;
1717-
1718- }
1719-
17201747static PyObject * Res_Handle (PyObject * _self , PyObject * _args )
17211748{
17221749 PyObject * _res = NULL ;
@@ -1871,8 +1898,6 @@ static PyMethodDef Res_methods[] = {
18711898 {"FSOpenResourceFile" , (PyCFunction )Res_FSOpenResourceFile , 1 ,
18721899 PyDoc_STR ("(FSRef ref, Buffer forkNameLength, SignedByte permissions) -> (SInt16 refNum)" )},
18731900#endif
1874- {"Resource" , (PyCFunction )Res_Resource , 1 ,
1875- PyDoc_STR ("Convert a string to a resource object.\n\nThe created resource object is actually just a handle,\napply AddResource() to write it to a resource file.\nSee also the Handle() docstring.\n" )},
18761901 {"Handle" , (PyCFunction )Res_Handle , 1 ,
18771902 PyDoc_STR ("Convert a string to a Handle object.\n\nResource() and Handle() are very similar, but objects created with Handle() are\nby default automatically DisposeHandle()d upon object cleanup. Use AutoDispose()\nto change this.\n" )},
18781903 {NULL , NULL , 0 }
@@ -1937,8 +1962,10 @@ void init_Res(void)
19371962 return ;
19381963 Resource_Type .ob_type = & PyType_Type ;
19391964 Py_INCREF (& Resource_Type );
1940- if (PyDict_SetItemString (d , "ResourceType" , (PyObject * )& Resource_Type ) != 0 )
1941- Py_FatalError ("can't initialize ResourceType" );
1965+ PyModule_AddObject (m , "Resource" , (PyObject * )& Resource_Type );
1966+ /* Backward-compatible name */
1967+ Py_INCREF (& Resource_Type );
1968+ PyModule_AddObject (m , "ResourceType" , (PyObject * )& Resource_Type );
19421969}
19431970
19441971/* ======================== End module _Res ========================= */
0 commit comments