Make iterators work with interrogate by mapping None to NULL for the iternext method. Not the best solution, but is easy and fits most use cases.

This commit is contained in:
rdb 2014-03-27 15:15:58 +00:00
parent 44b4afe898
commit d5d5de1d7e
2 changed files with 26 additions and 2 deletions

View File

@ -507,9 +507,9 @@ get_slotted_function_def(Object *obj, Function *func, SlottedFunctionDef &def) {
return true;
}
if (method_name == "next") {
if (method_name == "next" || method_name == "__next__") {
def._answer_location = "tp_iternext";
def._wrapper_type = WT_no_params;
def._wrapper_type = WT_iter_next;
return true;
}
}
@ -1700,6 +1700,29 @@ write_module_class(ostream &out, Object *obj) {
}
break;
case WT_iter_next:
// PyObject *func(PyObject *self)
// However, returns NULL instead of None
{
Function *func = rfi->first;
out << "//////////////////\n";
out << "// A wrapper function to satisfy Python's internal calling conventions.\n";
out << "// " << ClassName << " ..." << rfi->second._answer_location << " = " << methodNameFromCppName(func, export_class_name, false) << "\n";
out << "//////////////////\n";
out << "static PyObject *" << func->_name << methodNameFromCppName(func, export_class_name, false) << "(PyObject *self) {\n";
out << " PyObject *args = Py_BuildValue(\"()\");\n";
out << " PyObject *result = " << func->_name << "(self, args, NULL);\n";
out << " Py_DECREF(args);\n";
out << " if (result == Py_None) {\n";
out << " Py_DECREF(Py_None);\n";
out << " return NULL;\n";
out << " } else {\n";
out << " return result;\n";
out << " }\n";
out << "}\n\n";
}
break;
case WT_none:
break;
}

View File

@ -76,6 +76,7 @@ private:
WT_inquiry,
WT_getbuffer,
WT_releasebuffer,
WT_iter_next,
};
class SlottedFunctionDef {