python: Minor cleanups in the marshal program source.
Fix some minor style issues, use a "raw" string constant to make the marshal script more readable, get rid of a redundant \n in the help text, and make argv const. Change-Id: I1dc3181a67b50286e3a0b833bb7251b7efd01978 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/48382 Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Jason Lowe-Power <power.jg@gmail.com> Maintainer: Jason Lowe-Power <power.jg@gmail.com>
This commit is contained in:
@@ -42,23 +42,29 @@
|
||||
namespace py = pybind11;
|
||||
using namespace pybind11::literals;
|
||||
|
||||
constexpr auto MarshalScript = R"(
|
||||
import marshal
|
||||
|
||||
with open(source, 'r') as f:
|
||||
src = f.read()
|
||||
|
||||
compiled = compile(src, source, 'exec')
|
||||
marshalled = marshal.dumps(compiled)
|
||||
)";
|
||||
|
||||
int
|
||||
main(int argc, char **argv) {
|
||||
py::scoped_interpreter guard{};
|
||||
main(int argc, const char **argv)
|
||||
{
|
||||
py::scoped_interpreter guard;
|
||||
|
||||
if (argc != 2) {
|
||||
std::cerr << "Usage: marshal PYSOURCE\n" << std::endl;
|
||||
std::cerr << "Usage: marshal PYSOURCE" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
auto locals = py::dict("source"_a=argv[1]);
|
||||
|
||||
py::exec(
|
||||
"import marshal\n"
|
||||
"with open(source, 'r') as f: src = f.read()\n"
|
||||
"compiled = compile(src, source, 'exec')\n"
|
||||
"marshalled = marshal.dumps(compiled)\n",
|
||||
py::globals(), locals);
|
||||
py::exec(MarshalScript, py::globals(), locals);
|
||||
|
||||
auto marshalled = locals["marshalled"].cast<std::string>();
|
||||
std::cout << marshalled;
|
||||
|
||||
Reference in New Issue
Block a user