Browse Source

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.

rdb 11 years ago
parent
commit
d5d5de1d7e

+ 25 - 2
dtool/src/interrogate/interfaceMakerPythonNative.cxx

@@ -507,9 +507,9 @@ get_slotted_function_def(Object *obj, Function *func, SlottedFunctionDef &def) {
       return true;
       return true;
     }
     }
 
 
-    if (method_name == "next") {
+    if (method_name == "next" || method_name == "__next__") {
       def._answer_location = "tp_iternext";
       def._answer_location = "tp_iternext";
-      def._wrapper_type = WT_no_params;
+      def._wrapper_type = WT_iter_next;
       return true;
       return true;
     }
     }
   }
   }
@@ -1700,6 +1700,29 @@ write_module_class(ostream &out, Object *obj) {
         }
         }
         break;
         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:
       case WT_none:
         break;
         break;
       }
       }

+ 1 - 0
dtool/src/interrogate/interfaceMakerPythonNative.h

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