Skip to content

Commit 397c546

Browse files
committed
https://github.com/kbengine/kbengine/issues/133
key in globalData|entities方法支持。
1 parent 5605152 commit 397c546

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

‎kbe/src/lib/entitydef/entities.hpp‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
113131
TEMPLATE_SCRIPT_METHOD_DECLARE_BEGIN(template<typename T>, Entities<T>, Entities)
114132
SCRIPT_METHOD_DECLARE("has_key", pyHas_key, METH_VARARGS, 0)
115133
SCRIPT_METHOD_DECLARE("keys", pyKeys, METH_VARARGS, 0)
@@ -124,7 +142,7 @@ SCRIPT_MEMBER_DECLARE_END()
124142
TEMPLATE_SCRIPT_GETSET_DECLARE_BEGIN(template<typename T>, Entities<T>, Entities)
125143
SCRIPT_GET_DECLARE("garbages", pyGarbages, 0, 0)
126144
SCRIPT_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

‎kbe/src/lib/entitydef/entity_garbages.hpp‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class EntityGarbages : public script::ScriptObject
9292
static int mp_length(PyObject* self);
9393

9494
static PyMappingMethods mappingMethods;
95+
static PySequenceMethods mappingSequenceMethods;
9596

9697
ENTITYS_MAP& getEntities(void){ return _entities; }
9798

@@ -117,6 +118,23 @@ PyMappingMethods EntityGarbages<T>::mappingMethods =
117118
NULL // mp_ass_subscript
118119
};
119120

121+
// 参考 objects/dictobject.c
122+
// Hack to implement "key in dict"
123+
template<typename T>
124+
PySequenceMethods EntityGarbages<T>::mappingSequenceMethods =
125+
{
126+
0, /* sq_length */
127+
0, /* sq_concat */
128+
0, /* sq_repeat */
129+
0, /* sq_item */
130+
0, /* sq_slice */
131+
0, /* sq_ass_item */
132+
0, /* sq_ass_slice */
133+
PyMapping_HasKey, /* sq_contains */
134+
0, /* sq_inplace_concat */
135+
0, /* sq_inplace_repeat */
136+
};
137+
120138
TEMPLATE_SCRIPT_METHOD_DECLARE_BEGIN(template<typename T>, EntityGarbages<T>, EntityGarbages)
121139
SCRIPT_METHOD_DECLARE("has_key", pyHas_key, METH_VARARGS, 0)
122140
SCRIPT_METHOD_DECLARE("keys", pyKeys, METH_VARARGS, 0)
@@ -130,8 +148,7 @@ SCRIPT_MEMBER_DECLARE_END()
130148

131149
TEMPLATE_SCRIPT_GETSET_DECLARE_BEGIN(template<typename T>, EntityGarbages<T>, EntityGarbages)
132150
SCRIPT_GETSET_DECLARE_END()
133-
TEMPLATE_SCRIPT_INIT(template<typename T>, EntityGarbages<T>, EntityGarbages, 0, 0, &EntityGarbages<T>::mappingMethods, 0, 0)
134-
151+
TEMPLATE_SCRIPT_INIT(template<typename T>, EntityGarbages<T>, EntityGarbages, 0, &EntityGarbages<T>::mappingSequenceMethods, &EntityGarbages<T>::mappingMethods, 0, 0)
135152

136153
//-------------------------------------------------------------------------------------
137154
template<typename T>
@@ -159,7 +176,7 @@ PyObject* EntityGarbages<T>::mp_subscript(PyObject* self, PyObject* key /*entity
159176
if(pyEntity == NULL)
160177
{
161178
PyErr_Format(PyExc_KeyError, "%d", entityID);
162-
PyErr_PrintEx(0);
179+
//PyErr_PrintEx(0);
163180
return NULL;
164181
}
165182

‎kbe/src/lib/pyscript/map.cpp‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ PyMappingMethods Map::mappingMethods =
3030
(objobjargproc)Map::mp_ass_subscript // mp_ass_subscript
3131
};
3232

33+
// 参考 objects/dictobject.c
34+
// Hack to implement "key in dict"
35+
PySequenceMethods Map::mappingSequenceMethods =
36+
{
37+
0, /* sq_length */
38+
0, /* sq_concat */
39+
0, /* sq_repeat */
40+
0, /* sq_item */
41+
0, /* sq_slice */
42+
0, /* sq_ass_item */
43+
0, /* sq_ass_slice */
44+
PyMapping_HasKey, /* sq_contains */
45+
0, /* sq_inplace_concat */
46+
0, /* sq_inplace_repeat */
47+
};
3348

3449
SCRIPT_METHOD_DECLARE_BEGIN(Map)
3550
SCRIPT_METHOD_DECLARE("has_key", has_key, METH_VARARGS, 0)
@@ -45,7 +60,7 @@ SCRIPT_MEMBER_DECLARE_END()
4560

4661
SCRIPT_GETSET_DECLARE_BEGIN(Map)
4762
SCRIPT_GETSET_DECLARE_END()
48-
SCRIPT_INIT(Map, 0, 0, &Map::mappingMethods, 0, 0)
63+
SCRIPT_INIT(Map, 0, &Map::mappingSequenceMethods, &Map::mappingMethods, 0, 0)
4964

5065
//-------------------------------------------------------------------------------------
5166
Map::Map(PyTypeObject* pyType, bool isInitialised):

‎kbe/src/lib/pyscript/map.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Map : public ScriptObject
3232
INSTANCE_SCRIPT_HREADER(Map, ScriptObject)
3333
public:
3434
static PyMappingMethods mappingMethods;
35+
static PySequenceMethods mappingSequenceMethods;
3536

3637
Map(PyTypeObject* pyType, bool isInitialised = false);
3738
virtual ~Map();
@@ -45,7 +46,7 @@ class Map : public ScriptObject
4546
static PyObject* __py_items(PyObject* self, PyObject* args);
4647
static PyObject* __py_update(PyObject* self, PyObject* args);
4748
static PyObject* __py_get(PyObject* self, PyObject* args);
48-
49+
static int __py_contains(PyObject* self, PyObject* args);
4950
/**
5051
map操作函数相关
5152
*/

0 commit comments

Comments
 (0)