[gem5-dev] Change in gem5/gem5[master]: python: Fix native module initialisation on Python 3

2019-02-12 Thread Andreas Sandberg (Gerrit)
Andreas Sandberg has submitted this change and it was merged. (  
https://gem5-review.googlesource.com/c/public/gem5/+/15979 )


Change subject: python: Fix native module initialisation on Python 3
..

python: Fix native module initialisation on Python 3

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 
Reviewed-on: https://gem5-review.googlesource.com/c/15979
Reviewed-by: Jason Lowe-Power 
Reviewed-by: Gabe Black 
---
M src/sim/init.cc
M src/sim/init.hh
M src/sim/main.cc
3 files changed, 24 insertions(+), 14 deletions(-)

Approvals:
  Jason Lowe-Power: Looks good to me, approved
  Gabe Black: Looks good to me, but someone else must approve
  Andreas Sandberg: Looks good to me, approved



diff --git a/src/sim/init.cc b/src/sim/init.cc
index 66ec408..5a49f36 100644
--- a/src/sim/init.cc
+++ b/src/sim/init.cc
@@ -191,7 +191,11 @@
 return objs;
 }

+#if PY_MAJOR_VERSION >= 3
+PyObject *
+#else
 void
+#endif
 EmbeddedPyBind::initAll()
 {
 std::list pending;
@@ -226,13 +230,18 @@
 }
 }
 }
+
+#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 @@

 return 0;
 }
-
-PyMODINIT_FUNC
-initm5(void)
-{
-initM5Python();
-PyImport_ImportModule(PyCC("m5"));
-}
diff --git a/src/sim/init.hh b/src/sim/init.hh
index de6b44d..40ff9ae 100644
--- a/src/sim/init.hh
+++ b/src/sim/init.hh
@@ -90,7 +90,11 @@
 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 @@
 static std::map &getMap();
 };

-int initM5Python();
+void registerNativeModules();
+
 int m5Main(int argc, char **argv);
-PyMODINIT_FUNC initm5(void);

 #endif // __SIM_INIT_HH__
diff --git a/src/sim/main.cc b/src/sim/main.cc
index a77c5f5..168e439 100644
--- a/src/sim/main.cc
+++ b/src/sim/main.cc
@@ -54,11 +54,15 @@
 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

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/15979
To unsubscribe, or for help writing mail filters, visit  
https://gem5-review.googlesource.com/settings


Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: I961f8a33993c621473732faeaab955a882769a4b
Gerrit-Change-Number: 15979
Gerrit-PatchSet: 4
Gerrit-Owner: Andreas Sandberg 
Gerrit-Reviewer: Andreas Sandberg 
Gerrit-Reviewer: Gabe Black 
Gerrit-Reviewer: Giacomo Travaglini 
Gerrit-Reviewer: Jason Lowe-Power 
Gerrit-MessageType: merged
___
gem5-dev mailing list
gem5-dev@gem5.org
http://m5sim.org/mailman/listinfo/gem5-dev

[gem5-dev] Change in gem5/gem5[master]: python: Fix native module initialisation on Python 3

2019-01-28 Thread Andreas Sandberg (Gerrit)
Andreas Sandberg has uploaded this change for review. (  
https://gem5-review.googlesource.com/c/public/gem5/+/15979



Change subject: python: Fix native module initialisation on Python 3
..

python: Fix native module initialisation on Python 3

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 
---
M src/sim/init.cc
M src/sim/init.hh
M src/sim/main.cc
3 files changed, 24 insertions(+), 14 deletions(-)



diff --git a/src/sim/init.cc b/src/sim/init.cc
index 66ec408..5a49f36 100644
--- a/src/sim/init.cc
+++ b/src/sim/init.cc
@@ -191,7 +191,11 @@
 return objs;
 }

+#if PY_MAJOR_VERSION >= 3
+PyObject *
+#else
 void
+#endif
 EmbeddedPyBind::initAll()
 {
 std::list pending;
@@ -226,13 +230,18 @@
 }
 }
 }
+
+#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 @@

 return 0;
 }
-
-PyMODINIT_FUNC
-initm5(void)
-{
-initM5Python();
-PyImport_ImportModule(PyCC("m5"));
-}
diff --git a/src/sim/init.hh b/src/sim/init.hh
index de6b44d..40ff9ae 100644
--- a/src/sim/init.hh
+++ b/src/sim/init.hh
@@ -90,7 +90,11 @@
 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 @@
 static std::map &getMap();
 };

-int initM5Python();
+void registerNativeModules();
+
 int m5Main(int argc, char **argv);
-PyMODINIT_FUNC initm5(void);

 #endif // __SIM_INIT_HH__
diff --git a/src/sim/main.cc b/src/sim/main.cc
index a77c5f5..168e439 100644
--- a/src/sim/main.cc
+++ b/src/sim/main.cc
@@ -54,11 +54,15 @@
 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

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/15979
To unsubscribe, or for help writing mail filters, visit  
https://gem5-review.googlesource.com/settings


Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: I961f8a33993c621473732faeaab955a882769a4b
Gerrit-Change-Number: 15979
Gerrit-PatchSet: 1
Gerrit-Owner: Andreas Sandberg 
Gerrit-MessageType: newchange
___
gem5-dev mailing list
gem5-dev@gem5.org
http://m5sim.org/mailman/listinfo/gem5-dev