Skip to content

Commit

Permalink
https://github.com/kbengine/kbengine/issues/612
Browse files Browse the repository at this point in the history
  • Loading branch information
kebiao committed Jan 23, 2019
1 parent 2533fcf commit 8ea7bc1
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions kbe/src/lib/entitydef/py_entitydef.cpp
Expand Up @@ -53,6 +53,7 @@ struct DefContext

propertyFlags = "";
propertyIndex = "";
propertyDefaultVal = "";

implementedBy = "";

Expand Down Expand Up @@ -82,6 +83,7 @@ struct DefContext

std::string propertyFlags;
std::string propertyIndex;
std::string propertyDefaultVal;

std::string implementedBy;

Expand Down Expand Up @@ -350,6 +352,14 @@ static PyObject* __py_def_parse(PyObject *self, PyObject* args)
DefContext defContext;
defContext.optionName = cc.optionName;

if (!args || PyTuple_Size(args) < 1)
{
PyErr_Format(PyExc_AssertionError, "Def.__py_def_call(Def.%s): error!\n", defContext.optionName.c_str());
PY_RETURN_ERROR;
}

PyObject* pyFunc = PyTuple_GET_ITEM(args, 0);

if (defContext.optionName == "method")
{
static char * keywords[] =
Expand Down Expand Up @@ -424,6 +434,39 @@ static PyObject* __py_def_parse(PyObject *self, PyObject* args)

if (pyDatabaseLength)
defContext.databaseLength = (int)PyLong_AsLong(pyDatabaseLength);

// 对于属性, 我们需要获得返回值作为默认值
PyObject* pyRet = PyObject_CallFunction(pyFunc,
const_cast<char*>("(O)"), Py_None);

if (!pyRet)
return NULL;

if (pyRet != Py_None)
{
PyObject* pyStrResult = PyObject_Str(pyRet);
defContext.propertyDefaultVal = PyUnicode_AsUTF8AndSize(pyStrResult, NULL);
Py_DECREF(pyStrResult);

// 验证这个字符串是否可以还原成对象
if (defContext.propertyDefaultVal.size() > 0)
{
PyObject* module = PyImport_AddModule("__main__");
if (module == NULL)
return NULL;

PyObject* mdict = PyModule_GetDict(module); // Borrowed reference.
PyObject* result = PyRun_String(const_cast<char*>(defContext.propertyDefaultVal.c_str()),
Py_eval_input, mdict, mdict);

if (result == NULL)
return NULL;

Py_DECREF(result);
}
}

Py_DECREF(pyRet);
}
else if (defContext.optionName == "entity")
{
Expand Down Expand Up @@ -501,14 +544,6 @@ static PyObject* __py_def_parse(PyObject *self, PyObject* args)
PY_RETURN_ERROR;
}

if (!args || PyTuple_Size(args) < 1)
{
PyErr_Format(PyExc_AssertionError, "Def.__py_def_call(Def.%s): error!\n", defContext.optionName.c_str());
PY_RETURN_ERROR;
}

PyObject* pyFunc = PyTuple_GET_ITEM(args, 0);

PyObject* pyQualname = PyObject_GetAttrString(pyFunc, "__qualname__");
if (!pyQualname)
{
Expand Down

0 comments on commit 8ea7bc1

Please sign in to comment.