@@ -77,6 +77,7 @@ class Entities : public script::ScriptObject
7777 static int mp_length (PyObject * self);
7878
7979 static PyMappingMethods mappingMethods;
80+ static PySequenceMethods mappingSequenceMethods;
8081
8182 ENTITYS_MAP & getEntities (void ){ return _entities; }
8283
@@ -110,6 +111,23 @@ PyMappingMethods Entities<T>::mappingMethods =
110111 NULL // mp_ass_subscript
111112};
112113
114+ // 参考 objects/dictobject.c
115+ // Hack to implement "key in dict"
116+ template <typename T>
117+ PySequenceMethods Entities<T>::mappingSequenceMethods =
118+ {
119+ 0 , /* sq_length */
120+ 0 , /* sq_concat */
121+ 0 , /* sq_repeat */
122+ 0 , /* sq_item */
123+ 0 , /* sq_slice */
124+ 0 , /* sq_ass_item */
125+ 0 , /* sq_ass_slice */
126+ PyMapping_HasKey, /* sq_contains */
127+ 0 , /* sq_inplace_concat */
128+ 0 , /* sq_inplace_repeat */
129+ };
130+
113131TEMPLATE_SCRIPT_METHOD_DECLARE_BEGIN (template <typename T>, Entities<T>, Entities)
114132SCRIPT_METHOD_DECLARE (" has_key" , pyHas_key, METH_VARARGS , 0 )
115133SCRIPT_METHOD_DECLARE (" keys" , pyKeys, METH_VARARGS , 0 )
@@ -124,7 +142,7 @@ SCRIPT_MEMBER_DECLARE_END()
124142TEMPLATE_SCRIPT_GETSET_DECLARE_BEGIN (template <typename T>, Entities<T>, Entities)
125143SCRIPT_GET_DECLARE (" garbages" , pyGarbages, 0 , 0 )
126144SCRIPT_GETSET_DECLARE_END ()
127- TEMPLATE_SCRIPT_INIT (template <typename T>, Entities<T>, Entities, 0 , 0 , &Entities<T>::mappingMethods, 0 , 0 )
145+ TEMPLATE_SCRIPT_INIT (template <typename T>, Entities<T>, Entities, 0 , &Entities<T>::mappingSequenceMethods , &Entities<T>::mappingMethods, 0 , 0 )
128146
129147
130148// -------------------------------------------------------------------------------------
@@ -153,7 +171,7 @@ PyObject * Entities<T>::mp_subscript(PyObject* self, PyObject* key /*entityID*/)
153171 if (pyEntity == NULL )
154172 {
155173 PyErr_Format (PyExc_KeyError, " %d" , entityID);
156- PyErr_PrintEx (0 );
174+ // PyErr_PrintEx(0);
157175 return NULL ;
158176 }
159177
0 commit comments