Py BGE API

Python dir(ob) for game types now includes attributes names,
* Use "__dict__" rather then "__methods__" attribute to be Python 3.0 compatible
* Added _getattr_dict() for getting the method and attribute names from a PyObject, rather then building it in the macro.
* Added place holder *::Attribute array, needed for the _getattr_up macro.
This commit is contained in:
2009-02-26 09:04:06 +00:00
parent 936a6eeda8
commit c785532bec
47 changed files with 206 additions and 29 deletions

View File

@@ -232,7 +232,9 @@ PyMethodDef CListValue::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef CListValue::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* CListValue::_getattr(const char *attr) {
_getattr_up(CValue);

View File

@@ -707,5 +707,35 @@ PyObject *PyObjectPlus::Py_isA(PyObject *value) // Python wrapper for isA
Py_RETURN_FALSE;
}
/* Utility function called by the macro _getattr_up()
* for getting ob.__dict__() values from our PyObject
* this is used by python for doing dir() on an object, so its good
* if we return a list of attributes and methods.
*
* Other then making dir() useful the value returned from __dict__() is not useful
* since every value is a Py_None
* */
PyObject *_getattr_dict(PyObject *pydict, PyMethodDef *meth, PyAttributeDef *attrdef)
{
if(pydict==NULL) { /* incase calling __dict__ on the parent of this object raised an error */
PyErr_Clear();
pydict = PyDict_New();
}
if(meth) {
for (; meth->ml_name != NULL; meth++) {
PyDict_SetItemString(pydict, meth->ml_name, Py_None);
}
}
if(attrdef) {
for (; attrdef->m_name != NULL; attrdef++) {
PyDict_SetItemString(pydict, attrdef->m_name, Py_None);
}
}
return pydict;
}
#endif //NO_EXP_PYTHON_EMBEDDING

View File

@@ -89,35 +89,22 @@ static inline void Py_Fatal(const char *M) {
virtual PyParentObject *GetParents(void) {return Parents;}
// This defines the _getattr_up macro
// which allows attribute and method calls
// to be properly passed up the hierarchy.
#define _getattr_up(Parent) \
PyObject *rvalue = NULL; \
if (!strcmp(attr, "__methods__")) { \
PyObject *_attr_string = NULL; \
PyMethodDef *meth = Methods; \
rvalue = Parent::_getattr(attr); \
if (rvalue==NULL) { \
PyErr_Clear(); \
rvalue = PyList_New(0); \
} \
if (meth) { \
for (; meth->ml_name != NULL; meth++) { \
_attr_string = PyString_FromString(meth->ml_name); \
PyList_Append(rvalue, _attr_string); \
Py_DECREF(_attr_string); \
} \
PyObject *rvalue = Py_FindMethod(Methods, this, attr); \
\
if (rvalue == NULL) { \
PyErr_Clear(); \
rvalue = Parent::_getattr(attr); \
} \
} else { \
rvalue = Py_FindMethod(Methods, this, attr); \
if (rvalue == NULL) { \
PyErr_Clear(); \
rvalue = Parent::_getattr(attr); \
} \
} \
return rvalue; \
if ((rvalue == NULL) && !strcmp(attr, "__dict__")) {\
PyErr_Clear(); \
rvalue = _getattr_dict(Parent::_getattr(attr), Methods, Attributes); \
} \
return rvalue; \
/**
* These macros are helpfull when embedding Python routines. The second
@@ -398,6 +385,8 @@ public:
}
};
PyObject *_getattr_dict(PyObject *pydict, PyMethodDef *meth, PyAttributeDef *attrdef);
#endif // _adr_py_lib_h_
#endif //NO_EXP_PYTHON_EMBEDDING

View File

@@ -673,6 +673,10 @@ static PyMethodDef CValueMethods[] =
{ NULL,NULL} // Sentinel
};
PyAttributeDef CValue::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* CValue::_getattr(const char *attr)
{

View File

@@ -113,6 +113,9 @@ PyMethodDef SCA_2DFilterActuator::Methods[] = {
{NULL,NULL}
};
PyAttributeDef SCA_2DFilterActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_2DFilterActuator::_getattr(const char *attr) {
_getattr_up(SCA_IActuator);

View File

@@ -137,6 +137,10 @@ PyMethodDef SCA_ANDController::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_ANDController::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_ANDController::_getattr(const char *attr) {
_getattr_up(SCA_IController);
}

View File

@@ -135,6 +135,10 @@ PyMethodDef SCA_AlwaysSensor::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_AlwaysSensor::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_AlwaysSensor::_getattr(const char *attr) {
_getattr_up(SCA_ISensor);
}

View File

@@ -407,6 +407,9 @@ PyMethodDef SCA_IObject::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_IObject::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_IObject::_getattr(const char *attr) {

View File

@@ -137,6 +137,10 @@ PyMethodDef SCA_NANDController::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_NANDController::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_NANDController::_getattr(const char *attr) {
_getattr_up(SCA_IController);
}

View File

@@ -137,6 +137,10 @@ PyMethodDef SCA_NORController::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_NORController::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_NORController::_getattr(const char *attr) {
_getattr_up(SCA_IController);
}

View File

@@ -129,6 +129,11 @@ PyMethodDef SCA_ORController::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_ORController::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_ORController::_getattr(const char *attr) {
_getattr_up(SCA_IController);
}

View File

@@ -264,6 +264,9 @@ PyMethodDef SCA_PythonController::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_PythonController::Attributes[] = {
{ NULL } //Sentinel
};
bool SCA_PythonController::Compile()
{

View File

@@ -141,6 +141,10 @@ PyMethodDef SCA_XNORController::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_XNORController::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_XNORController::_getattr(const char *attr) {
_getattr_up(SCA_IController);
}

View File

@@ -141,6 +141,10 @@ PyMethodDef SCA_XORController::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_XORController::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* SCA_XORController::_getattr(const char *attr) {
_getattr_up(SCA_IController);
}

View File

@@ -767,6 +767,9 @@ PyMethodDef BL_Shader::Methods[] =
{NULL,NULL} //Sentinel
};
PyAttributeDef BL_Shader::Attributes[] = {
{ NULL } //Sentinel
};
PyTypeObject BL_Shader::Type = {
PyObject_HEAD_INIT(&PyType_Type)

View File

@@ -143,6 +143,10 @@ PyMethodDef KX_NetworkMessageActuator::Methods[] = {
{NULL,NULL} // Sentinel
};
PyAttributeDef KX_NetworkMessageActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_NetworkMessageActuator::_getattr(const char *attr) {
_getattr_up(SCA_IActuator);
}

View File

@@ -213,6 +213,10 @@ PyMethodDef KX_NetworkMessageSensor::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_NetworkMessageSensor::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_NetworkMessageSensor::_getattr(const char *attr) {
_getattr_up(SCA_ISensor); // implicit return!
}

View File

@@ -748,6 +748,9 @@ PyMethodDef KX_BlenderMaterial::Methods[] =
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_BlenderMaterial::Attributes[] = {
{ NULL } //Sentinel
};
PyTypeObject KX_BlenderMaterial::Type = {
PyObject_HEAD_INIT(&PyType_Type)

View File

@@ -197,7 +197,9 @@ PyMethodDef KX_CDActuator::Methods[] = {
{NULL,NULL,NULL,NULL} //Sentinel
};
PyAttributeDef KX_CDActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_CDActuator::_getattr(const char *attr)
{

View File

@@ -483,6 +483,10 @@ PyMethodDef KX_Camera::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_Camera::Attributes[] = {
{ NULL } //Sentinel
};
char KX_Camera::doc[] = "Module KX_Camera\n\n"
"Constants:\n"
"\tINSIDE\n"

View File

@@ -612,6 +612,10 @@ PyMethodDef KX_ConstraintActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_ConstraintActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_ConstraintActuator::_getattr(const char *attr) {
_getattr_up(SCA_IActuator);
}

View File

@@ -130,3 +130,7 @@ PyMethodDef KX_ConstraintWrapper::Methods[] = {
{"getConstraintId",(PyCFunction) KX_ConstraintWrapper::sPyGetConstraintId, METH_VARARGS},
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_ConstraintWrapper::Attributes[] = {
{ NULL } //Sentinel
};

View File

@@ -246,6 +246,10 @@ PyMethodDef KX_GameActuator::Methods[] =
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_GameActuator::Attributes[] = {
{ NULL } //Sentinel
};
/* getFile */
const char KX_GameActuator::GetFile_doc[] =
"getFile()\n"

View File

@@ -1033,6 +1033,9 @@ PyMethodDef KX_GameObject::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_GameObject::Attributes[] = {
{ NULL } //Sentinel
};
/*

View File

@@ -457,6 +457,10 @@ PyMethodDef KX_IpoActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_IpoActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_IpoActuator::_getattr(const char *attr) {
_getattr_up(SCA_IActuator);
}

View File

@@ -301,6 +301,10 @@ PyMethodDef KX_LightObject::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_LightObject::Attributes[] = {
{ NULL } //Sentinel
};
char KX_LightObject::doc[] = "Module KX_LightObject\n\n"
"Constants:\n"
"\tSPOT\n"

View File

@@ -86,6 +86,10 @@ KX_PYMETHODTABLE(KX_MeshProxy, reinstancePhysicsMesh),
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_MeshProxy::Attributes[] = {
{ NULL } //Sentinel
};
void KX_MeshProxy::SetMeshModified(bool v)
{
m_meshobj->SetMeshModified(v);

View File

@@ -332,6 +332,10 @@ PyMethodDef KX_MouseFocusSensor::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_MouseFocusSensor::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_MouseFocusSensor::_getattr(const char *attr) {
_getattr_up(SCA_MouseSensor);
}

View File

@@ -318,6 +318,9 @@ PyMethodDef KX_NearSensor::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_NearSensor::Attributes[] = {
{ NULL } //Sentinel
};
PyObject*
KX_NearSensor::_getattr(const char *attr)

View File

@@ -332,6 +332,10 @@ PyMethodDef KX_ObjectActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_ObjectActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_ObjectActuator::_getattr(const char *attr) {
_getattr_up(SCA_IActuator);
};

View File

@@ -172,6 +172,10 @@ PyMethodDef KX_ParentActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_ParentActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_ParentActuator::_getattr(const char *attr) {
if (!strcmp(attr, "object")) {

View File

@@ -77,6 +77,10 @@ PyMethodDef KX_PolyProxy::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_PolyProxy::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_PolyProxy::_getattr(const char *attr)
{
if (!strcmp(attr, "matname"))

View File

@@ -180,6 +180,9 @@ PyMethodDef KX_PolygonMaterial::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_PolygonMaterial::Attributes[] = {
{ NULL } //Sentinel
};
PyTypeObject KX_PolygonMaterial::Type = {
PyObject_HEAD_INIT(&PyType_Type)

View File

@@ -230,6 +230,10 @@ PyMethodDef KX_RadarSensor::Methods[] = {
{NULL,NULL,NULL,NULL} //Sentinel
};
PyAttributeDef KX_RadarSensor::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_RadarSensor::_getattr(const char *attr) {
_getattr_up(KX_TouchSensor);
}

View File

@@ -343,6 +343,10 @@ PyMethodDef KX_RaySensor::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_RaySensor::Attributes[] = {
{ NULL } //Sentinel
};
const char KX_RaySensor::GetHitObject_doc[] =
"getHitObject()\n"
"\tReturns the name of the object that was hit by this ray.\n";

View File

@@ -204,6 +204,9 @@ PyMethodDef KX_SCA_AddObjectActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_SCA_AddObjectActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_SCA_AddObjectActuator::_getattr(const char *attr)
{

View File

@@ -85,6 +85,9 @@ PyMethodDef KX_SCA_DynamicActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_SCA_DynamicActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_SCA_DynamicActuator::_getattr(const char *attr)

View File

@@ -127,6 +127,9 @@ PyMethodDef KX_SCA_EndObjectActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_SCA_EndObjectActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_SCA_EndObjectActuator::_getattr(const char *attr)
{

View File

@@ -89,7 +89,9 @@ PyMethodDef KX_SCA_ReplaceMeshActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_SCA_ReplaceMeshActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_SCA_ReplaceMeshActuator::_getattr(const char *attr)
{

View File

@@ -1525,6 +1525,10 @@ PyMethodDef KX_Scene::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_Scene::Attributes[] = {
{ NULL } //Sentinel
};
PyTypeObject KX_Scene::Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,

View File

@@ -268,7 +268,9 @@ PyMethodDef KX_SceneActuator::Methods[] =
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_SceneActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_SceneActuator::_getattr(const char *attr)
{

View File

@@ -285,7 +285,9 @@ PyMethodDef KX_SoundActuator::Methods[] = {
{NULL,NULL,NULL,NULL} //Sentinel
};
PyAttributeDef KX_SoundActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_SoundActuator::_getattr(const char *attr)
{

View File

@@ -146,6 +146,10 @@ KX_StateActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_StateActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_StateActuator::_getattr(const char *attr)
{
_getattr_up(SCA_IActuator);

View File

@@ -468,6 +468,9 @@ PyMethodDef KX_TrackToActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_TrackToActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_TrackToActuator::_getattr(const char *attr)

View File

@@ -382,3 +382,6 @@ PyMethodDef KX_VehicleWrapper::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_VehicleWrapper::Attributes[] = {
{ NULL } //Sentinel
};

View File

@@ -78,6 +78,10 @@ PyMethodDef KX_VertexProxy::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_VertexProxy::Attributes[] = {
{ NULL } //Sentinel
};
PyObject*
KX_VertexProxy::_getattr(const char *attr)
{

View File

@@ -126,6 +126,10 @@ KX_VisibilityActuator::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef KX_VisibilityActuator::Attributes[] = {
{ NULL } //Sentinel
};
PyObject* KX_VisibilityActuator::_getattr(const char *attr)
{
_getattr_up(SCA_IActuator);