python: Fix native module initialisation on Python 3
authorAndreas Sandberg <andreas.sandberg@arm.com>
Mon, 28 Jan 2019 16:14:41 +0000 (16:14 +0000)
committerAndreas Sandberg <andreas.sandberg@arm.com>
Tue, 12 Feb 2019 09:38:12 +0000 (09:38 +0000)
The approach we currently use to register our native modules doesn't
work on Python 3. Convert the code to use the Python inittab instead
of the old ad-hoc method.

Change-Id: I961f8a33993c621473732faeaab955a882769a4b
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/15979
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-by: Gabe Black <gabeblack@google.com>
src/sim/init.cc
src/sim/init.hh
src/sim/main.cc

index 66ec408959e2b595c4fbf34fbb5b3a866064d640..5a49f360abd896466770dcd4fe8e11e37c209e14 100644 (file)
@@ -191,7 +191,11 @@ EmbeddedPyBind::getMap()
     return objs;
 }
 
+#if PY_MAJOR_VERSION >= 3
+PyObject *
+#else
 void
+#endif
 EmbeddedPyBind::initAll()
 {
     std::list<EmbeddedPyBind *> pending;
@@ -226,13 +230,18 @@ EmbeddedPyBind::initAll()
             }
         }
     }
+
+#if PY_MAJOR_VERSION >= 3
+    return m_m5.ptr();
+#endif
 }
 
-int
-initM5Python()
+void
+registerNativeModules()
 {
-    EmbeddedPyBind::initAll();
-    return EmbeddedPython::initAll();
+    auto result = PyImport_AppendInittab("_m5", EmbeddedPyBind::initAll);
+    if (result == -1)
+        panic("Failed to add _m5 to Python's inittab\n");
 }
 
 /*
@@ -307,10 +316,3 @@ m5Main(int argc, char **_argv)
 
     return 0;
 }
-
-PyMODINIT_FUNC
-initm5(void)
-{
-    initM5Python();
-    PyImport_ImportModule(PyCC("m5"));
-}
index de6b44de4f4debd24ab13ed166559624c42e9f34..40ff9aea03e11f2f7f44aa54dd1d5aa76c1ae6be 100644 (file)
@@ -90,7 +90,11 @@ class EmbeddedPyBind
     EmbeddedPyBind(const char *_name,
                    void (*init_func)(pybind11::module &));
 
+#if PY_MAJOR_VERSION >= 3
+    static PyObject *initAll();
+#else
     static void initAll();
+#endif
 
   private:
     void (*initFunc)(pybind11::module &);
@@ -105,8 +109,8 @@ class EmbeddedPyBind
     static std::map<std::string, EmbeddedPyBind *> &getMap();
 };
 
-int initM5Python();
+void registerNativeModules();
+
 int m5Main(int argc, char **argv);
-PyMODINIT_FUNC initm5(void);
 
 #endif // __SIM_INIT_HH__
index a77c5f578f73c8f800556fecd919ed4043b6d50b..168e4390f9fd8aed3b18831d78d17a9fb7d2226d 100644 (file)
@@ -54,11 +54,15 @@ main(int argc, char **argv)
     Py_SetProgramName(argv[0]);
 #endif
 
+    // Register native modules with Python's init system before
+    // initializing the interpreter.
+    registerNativeModules();
+
     // initialize embedded Python interpreter
     Py_Initialize();
 
     // Initialize the embedded m5 python library
-    ret = initM5Python();
+    ret = EmbeddedPython::initAll();
 
     if (ret == 0) {
         // start m5