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

Skip to content

Commit 8387af6

Browse files
committed
If a control has no refcon pointing back to the Python object we create a new
Python object. This needs a new bgenObjectDefinition.py, which implements compare and hash functions.
1 parent d5138ca commit 8387af6

2 files changed

Lines changed: 75 additions & 11 deletions

File tree

Mac/Modules/ctl/Ctlmodule.c

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,34 @@ static PyObject *CtlObj_getattr(self, name)
10161016

10171017
#define CtlObj_setattr NULL
10181018

1019+
static int CtlObj_compare(self, other)
1020+
ControlObject *self, *other;
1021+
{
1022+
unsigned long v, w;
1023+
1024+
if (!CtlObj_Check((PyObject *)other))
1025+
{
1026+
v=(unsigned long)self;
1027+
w=(unsigned long)other;
1028+
}
1029+
else
1030+
{
1031+
v=(unsigned long)self->ob_itself;
1032+
w=(unsigned long)other->ob_itself;
1033+
}
1034+
if( v < w ) return -1;
1035+
if( v > w ) return 1;
1036+
return 0;
1037+
}
1038+
1039+
#define CtlObj_repr NULL
1040+
1041+
static long CtlObj_hash(self)
1042+
ControlObject *self;
1043+
{
1044+
return (long)self->ob_itself;
1045+
}
1046+
10191047
PyTypeObject Control_Type = {
10201048
PyObject_HEAD_INIT(&PyType_Type)
10211049
0, /*ob_size*/
@@ -1027,6 +1055,12 @@ PyTypeObject Control_Type = {
10271055
0, /*tp_print*/
10281056
(getattrfunc) CtlObj_getattr, /*tp_getattr*/
10291057
(setattrfunc) CtlObj_setattr, /*tp_setattr*/
1058+
(cmpfunc) CtlObj_compare, /*tp_compare*/
1059+
(reprfunc) CtlObj_repr, /*tp_repr*/
1060+
(PyNumberMethods *)0, /* tp_as_number */
1061+
(PySequenceMethods *)0, /* tp_as_sequence */
1062+
(PyMappingMethods *)0, /* tp_as_mapping */
1063+
(hashfunc) CtlObj_hash, /*tp_hash*/
10301064
};
10311065

10321066
/* -------------------- End object type Control --------------------- */
@@ -1388,18 +1422,33 @@ static PyMethodDef Ctl_methods[] = {
13881422

13891423

13901424

1425+
PyObject *CtlObj_NewUnmanaged(itself)
1426+
ControlHandle itself;
1427+
{
1428+
ControlObject *it;
1429+
if (itself == NULL) return PyMac_Error(resNotFound);
1430+
it = PyObject_NEW(ControlObject, &Control_Type);
1431+
if (it == NULL) return NULL;
1432+
it->ob_itself = itself;
1433+
return (PyObject *)it;
1434+
}
1435+
13911436
PyObject *
13921437
CtlObj_WhichControl(ControlHandle c)
13931438
{
13941439
PyObject *it;
13951440

1396-
/* XXX What if we find a control belonging to some other package? */
13971441
if (c == NULL)
1398-
it = NULL;
1399-
else
1400-
it = (PyObject *) GetControlReference(c);
1401-
if (it == NULL || ((ControlObject *)it)->ob_itself != c)
14021442
it = Py_None;
1443+
else {
1444+
it = (PyObject *) GetControlReference(c);
1445+
/*
1446+
** If the refcon is zero or doesn't point back to the Python object
1447+
** the control is not ours. Return a temporary object.
1448+
*/
1449+
if (it == NULL || ((ControlObject *)it)->ob_itself != c)
1450+
return CtlObj_NewUnmanaged(c);
1451+
}
14031452
Py_INCREF(it);
14041453
return it;
14051454
}

Mac/Modules/ctl/ctlsupport.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,33 @@
8989
"""
9090

9191
finalstuff = finalstuff + """
92+
PyObject *CtlObj_NewUnmanaged(itself)
93+
ControlHandle itself;
94+
{
95+
ControlObject *it;
96+
if (itself == NULL) return PyMac_Error(resNotFound);
97+
it = PyObject_NEW(ControlObject, &Control_Type);
98+
if (it == NULL) return NULL;
99+
it->ob_itself = itself;
100+
return (PyObject *)it;
101+
}
102+
92103
PyObject *
93104
CtlObj_WhichControl(ControlHandle c)
94105
{
95106
PyObject *it;
96107
97-
/* XXX What if we find a control belonging to some other package? */
98108
if (c == NULL)
99-
it = NULL;
100-
else
101-
it = (PyObject *) GetControlReference(c);
102-
if (it == NULL || ((ControlObject *)it)->ob_itself != c)
103109
it = Py_None;
110+
else {
111+
it = (PyObject *) GetControlReference(c);
112+
/*
113+
** If the refcon is zero or doesn't point back to the Python object
114+
** the control is not ours. Return a temporary object.
115+
*/
116+
if (it == NULL || ((ControlObject *)it)->ob_itself != c)
117+
return CtlObj_NewUnmanaged(c);
118+
}
104119
Py_INCREF(it);
105120
return it;
106121
}
@@ -147,7 +162,7 @@
147162
mytracker_upp = NewControlActionProc(mytracker);
148163
"""
149164

150-
class MyObjectDefinition(GlobalObjectDefinition):
165+
class MyObjectDefinition(ObjectIdentityMixin, GlobalObjectDefinition):
151166
def outputCheckNewArg(self):
152167
Output("if (itself == NULL) return PyMac_Error(resNotFound);")
153168
def outputInitStructMembers(self):

0 commit comments

Comments
 (0)