Re: [Pyobjc-dev] Re: Structs, tuples and instances
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2003-11-03 15:46:40
|
On 31 okt 2003, at 18:07, Ronald Oussoren wrote: >>> I'd love that... As I said I could put it to good use for the >>> various Mac toolbox objects. > > But such a patch would not be useable by PyObjC until Python 2.4 gets > out :-(. That said, I'll work on such a patch. I will also work on a > custom version for PyObjC ;-). > The patch for Python is easy enough, but there is one major downside of this patch: writeable structseqs are not hashable, which makes it impossible to use these objects as keys in dictionaries. Making them hashable would be easy, it would even decrease the size of the patch, but would make it possible to create invalid dicts: d = {} p = NSPoint(x=1, y=2) d[p] = 1 p.x = 3 d[d.keys()[0]] will fail, as will d[NSPoint(x=3, y=2)] unless NSPoint(1,2) happens to have the same hash as NSPoint(3,2). A completely untested patch is included below, I haven't even tried to compile this. Ronald Index: Include/structseq.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/structseq.h,v retrieving revision 1.4 diff -c -r1.4 structseq.h *** Include/structseq.h 17 Oct 2002 19:48:27 -0000 1.4 --- Include/structseq.h 3 Nov 2003 15:39:46 -0000 *************** *** 23,28 **** --- 23,30 ---- PyAPI_FUNC(void) PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc); + PyAPI_FUNC(void) PyStructSequence_InitTypeRW(PyTypeObject *type, + PyStructSequence_Desc *desc); PyAPI_FUNC(PyObject *) PyStructSequence_New(PyTypeObject* type); Index: Objects/structseq.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/structseq.c,v retrieving revision 1.12 diff -c -r1.12 structseq.c *** Objects/structseq.c 1 Feb 2003 02:16:37 -0000 1.12 --- Objects/structseq.c 3 Nov 2003 15:39:47 -0000 *************** *** 342,349 **** structseq_new, /* tp_new */ }; ! void ! PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) { PyObject *dict; PyMemberDef* members; --- 342,349 ---- structseq_new, /* tp_new */ }; ! static void ! _inittype(PyTypeObject* type, PyStructSequence_Desc* desc, int readWrite) { PyObject *dict; PyMemberDef* members; *************** *** 373,384 **** members[k].type = T_OBJECT; members[k].offset = offsetof(PyStructSequence, ob_item) + i * sizeof(PyObject*); ! members[k].flags = READONLY; members[k].doc = desc->fields[i].doc; k++; } members[k].name = NULL; type->tp_members = members; if (PyType_Ready(type) < 0) --- 373,393 ---- members[k].type = T_OBJECT; members[k].offset = offsetof(PyStructSequence, ob_item) + i * sizeof(PyObject*); ! ! if (readWrite) { ! members[k].flags = 0; ! } else { ! members[k].flags = READONLY; ! } members[k].doc = desc->fields[i].doc; k++; } members[k].name = NULL; + if (readWrite) { + type->tp_hash = NULL; + } + type->tp_members = members; if (PyType_Ready(type) < 0) *************** *** 392,395 **** --- 401,416 ---- PyInt_FromLong((long) n_members)); PyDict_SetItemString(dict, unnamed_fields_key, PyInt_FromLong((long) n_unnamed_members)); + } + + void + PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) + { + _inittype(type, desc, 0); + } + + void + PyStructSequence_InitTypeRW(PyTypeObject *type, PyStructSequence_Desc *desc) + { + _inittype(type, desc, 1); } |