sim: Prepare C++ side for Python 3
authorAndreas Sandberg <andreas.sandberg@arm.com>
Fri, 25 Jan 2019 11:13:38 +0000 (11:13 +0000)
committerAndreas Sandberg <andreas.sandberg@arm.com>
Thu, 31 Jan 2019 17:31:54 +0000 (17:31 +0000)
Python 3 uses wide strings instead of ordinary strings for many
APIs. Add the necessary conversions to comply with the new API.

Change-Id: I6f45c9c532537d50d54b542f34eb8fd8cb375874
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/15977
Reviewed-by: Gabe Black <gabeblack@google.com>
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
src/sim/init.cc
src/sim/main.cc

index 50612895b360cffcb0007bbc35b76dda0c612f0b..66ec408959e2b595c4fbf34fbb5b3a866064d640 100644 (file)
@@ -51,6 +51,7 @@
 #include <iostream>
 #include <list>
 #include <string>
+#include <vector>
 
 #include "base/cprintf.hh"
 #include "base/logging.hh"
@@ -249,7 +250,7 @@ const char * __attribute__((weak)) m5MainCommands[] = {
  * main function.
  */
 int
-m5Main(int argc, char **argv)
+m5Main(int argc, char **_argv)
 {
 #if HAVE_PROTOBUF
     // Verify that the version of the protobuf library that we linked
@@ -258,6 +259,23 @@ m5Main(int argc, char **argv)
     GOOGLE_PROTOBUF_VERIFY_VERSION;
 #endif
 
+
+#if PY_MAJOR_VERSION >= 3
+    typedef std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> WArgUPtr;
+    std::vector<WArgUPtr> v_argv;
+    std::vector<wchar_t *> vp_argv;
+    v_argv.reserve(argc);
+    vp_argv.reserve(argc);
+    for (int i = 0; i < argc; i++) {
+        v_argv.emplace_back(Py_DecodeLocale(_argv[i], NULL), &PyMem_RawFree);
+        vp_argv.emplace_back(v_argv.back().get());
+    }
+
+    wchar_t **argv = vp_argv.data();
+#else
+    char **argv = _argv;
+#endif
+
     PySys_SetArgv(argc, argv);
 
     // We have to set things up in the special __main__ module
index 48b1592638d2f3c378ec9d3c9de340d9ab70247c..a77c5f578f73c8f800556fecd919ed4043b6d50b 100644 (file)
@@ -45,7 +45,14 @@ main(int argc, char **argv)
     // Initialize m5 special signal handling.
     initSignals();
 
+#if PY_MAJOR_VERSION >= 3
+    std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> program(
+        Py_DecodeLocale(argv[0], NULL),
+        &PyMem_RawFree);
+    Py_SetProgramName(program.get());
+#else
     Py_SetProgramName(argv[0]);
+#endif
 
     // initialize embedded Python interpreter
     Py_Initialize();