Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f61a05

Browse files
committed
Fixed so the Res.Resource() accepts either another resource, a string
or no argument (giving an empty resource).
1 parent dd888a6 commit 5f61a05

3 files changed

Lines changed: 129 additions & 66 deletions

File tree

Mac/Modules/res/_Resmodule.c

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

616658
PyTypeObject 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-
17201747
static 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 ========================= */

Mac/Modules/res/resedit.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
resource_body = """
2-
char *buf;
3-
int len;
4-
Handle h;
5-
6-
if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
7-
return NULL;
8-
h = NewHandle(len);
9-
if ( h == NULL ) {
10-
PyErr_NoMemory();
11-
return NULL;
12-
}
13-
HLock(h);
14-
memcpy(*h, buf, len);
15-
HUnlock(h);
16-
_res = ResObj_New(h);
17-
return _res;
18-
"""
19-
20-
f = ManualGenerator("Resource", resource_body)
21-
f.docstring = lambda: """Convert a string to a resource object.
22-
23-
The created resource object is actually just a handle,
24-
apply AddResource() to write it to a resource file.
25-
See also the Handle() docstring.
26-
"""
27-
functions.append(f)
1+
##resource_body = """
2+
##char *buf;
3+
##int len;
4+
##Handle h;
5+
##
6+
##if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
7+
## return NULL;
8+
##h = NewHandle(len);
9+
##if ( h == NULL ) {
10+
## PyErr_NoMemory();
11+
## return NULL;
12+
##}
13+
##HLock(h);
14+
##memcpy(*h, buf, len);
15+
##HUnlock(h);
16+
##_res = ResObj_New(h);
17+
##return _res;
18+
##"""
19+
##
20+
##f = ManualGenerator("Resource", resource_body)
21+
##f.docstring = lambda: """Convert a string to a resource object.
22+
##
23+
##The created resource object is actually just a handle,
24+
##apply AddResource() to write it to a resource file.
25+
##See also the Handle() docstring.
26+
##"""
27+
##functions.append(f)
2828

2929
handle_body = """
3030
char *buf;

Mac/Modules/res/ressupport.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class ResMethod(ResMixIn, OSErrWeakLinkMethodGenerator): pass
100100

101101
module = MacModule('_Res', 'Res', includestuff, finalstuff, initstuff)
102102

103-
class ResDefinition(PEP252Mixin, GlobalObjectDefinition):
103+
class ResDefinition(PEP253Mixin, GlobalObjectDefinition):
104104
getsetlist = [
105105
('data',
106106
"""
@@ -176,6 +176,42 @@ def outputCleanupStructMembers(self):
176176
OutRbrace()
177177
Output("self->ob_itself = NULL;")
178178

179+
def output_tp_newBody(self):
180+
Output("PyObject *self;")
181+
Output
182+
Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
183+
Output("((%s *)self)->ob_itself = NULL;", self.objecttype)
184+
Output("((%s *)self)->ob_freeit = NULL;", self.objecttype)
185+
Output("return self;")
186+
187+
def output_tp_initBody(self):
188+
Output("char *srcdata = NULL;")
189+
Output("int srclen = 0;")
190+
Output("%s itself;", self.itselftype);
191+
Output("char *kw[] = {\"itself\", 0};")
192+
Output()
193+
Output("if (PyArg_ParseTupleAndKeywords(args, kwds, \"O&\", kw, %s_Convert, &itself))",
194+
self.prefix);
195+
OutLbrace()
196+
Output("((%s *)self)->ob_itself = itself;", self.objecttype)
197+
Output("return 0;")
198+
OutRbrace()
199+
Output("PyErr_Clear();")
200+
Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"|s#\", kw, &srcdata, &srclen)) return -1;")
201+
Output("if ((itself = NewHandle(srclen)) == NULL)")
202+
OutLbrace()
203+
Output("PyErr_NoMemory();")
204+
Output("return 0;")
205+
OutRbrace()
206+
Output("((%s *)self)->ob_itself = itself;", self.objecttype)
207+
# XXXX Output("((%s *)self)->ob_freeit = PyMac_AutoDisposeHandle;")
208+
Output("if (srclen && srcdata)")
209+
OutLbrace()
210+
Output("HLock(itself);")
211+
Output("memcpy(*itself, srcdata, srclen);")
212+
Output("HUnlock(itself);")
213+
OutRbrace()
214+
Output("return 0;")
179215

180216
resobject = ResDefinition('Resource', 'ResObj', 'Handle')
181217
module.addobject(resobject)

0 commit comments

Comments
 (0)