Gabe Black has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/49391 )
Change subject: python: Pull most of the logic in marshal.cc into python.
......................................................................
python: Pull most of the logic in marshal.cc into python.
Put most of the logic in python, and turn the c++ parts into a very
generic wrapper which could meaningfully run any python code under the
interpreter which will be embedded in gem5.
Change-Id: I3f6e50839490eaf0db9a6bd242efbcb8de1b6e24
---
M src/python/marshal.cc
1 file changed, 23 insertions(+), 11 deletions(-)
diff --git a/src/python/marshal.cc b/src/python/marshal.cc
index 7e05c3a..99305b2 100644
--- a/src/python/marshal.cc
+++ b/src/python/marshal.cc
@@ -37,19 +37,23 @@
#include <pybind11/embed.h>
-#include <iostream>
-
namespace py = pybind11;
-using namespace pybind11::literals;
constexpr auto MarshalScript = R"(
import marshal
+import sys
+if len(sys.argv) < 2:
+ print(f"Usage: {sys.argv[0]} PYSOURCE", file=sys.stderr)
+ sys.exit(1)
+
+source = sys.argv[1]
with open(source, 'r') as f:
src = f.read()
compiled = compile(src, source, 'exec')
marshalled = marshal.dumps(compiled)
+sys.stdout.buffer.write(marshalled)
)";
int
@@ -57,17 +61,25 @@
{
py::scoped_interpreter guard;
- if (argc != 2) {
- std::cerr << "Usage: marshal PYSOURCE" << std::endl;
- exit(1);
+ // Embedded python doesn't set up sys.argv, so we'll do that ourselves.
+ py::list py_argv;
+ auto sys = py::module::import("sys");
+ if (py::hasattr(sys, "argv")) {
+ // sys.argv already exists, so grab that.
+ py_argv = sys.attr("argv");
+ } else {
+ // sys.argv doesn't exist, so create it.
+ sys.add_object("argv", py_argv);
}
+ // Clear out argv just in case it has something in it.
+ py_argv.attr("clear")();
- auto locals = py::dict("source"_a=argv[1]);
+ // Fill it with our argvs.
+ for (int i = 0; i < argc; i++)
+ py_argv.append(argv[i]);
- py::exec(MarshalScript, py::globals(), locals);
-
- auto marshalled = locals["marshalled"].cast<std::string>();
- std::cout << marshalled;
+ // Actually call the script.
+ py::exec(MarshalScript, py::globals());
return 0;
}
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/49391
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I3f6e50839490eaf0db9a6bd242efbcb8de1b6e24
Gerrit-Change-Number: 49391
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <gabe.bl...@gmail.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s