From 7cb42f4368e873e5930cf84bc2494a8e8172a046 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Tue, 5 Jun 2018 21:03:06 +0200 Subject: Initial GUI work --- common/pybindings.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'common/pybindings.cc') diff --git a/common/pybindings.cc b/common/pybindings.cc index 8bae4e51..4986c549 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -18,6 +18,7 @@ * */ + #include "design.h" #include "chip.h" #include "pybindings.h" @@ -26,6 +27,9 @@ #define PASTER(x, y) x ## _ ## y #define EVALUATOR(x, y) PASTER(x,y) #define MODULE_NAME EVALUATOR(nextpnrpy, ARCHNAME) +#define PYINIT_MODULE_NAME EVALUATOR(&PyInit_nextpnrpy, ARCHNAME) +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) // Architecture-specific bindings should be created in the below function, which must be implemented in all // architectures @@ -39,3 +43,41 @@ BOOST_PYTHON_MODULE (MODULE_NAME) { arch_wrap_python(); } + +void arch_appendinittab() +{ + PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); +} + +void execute_python_file(const char *executable, const char* python_file) +{ + wchar_t *program = Py_DecodeLocale(executable, NULL); + if (program == NULL) { + fprintf(stderr, "Fatal error: cannot decode executable filename\n"); + exit(1); + } + try + { + PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); + Py_SetProgramName(program); + Py_Initialize(); + + FILE* fp = fopen(python_file, "r"); + if (fp == NULL) { + fprintf(stderr, "Fatal error: file not found %s\n",python_file); + exit(1); + } + PyRun_SimpleFile(fp , python_file); + fclose(fp); + + Py_Finalize(); + PyMem_RawFree(program); + } + catch(boost::python::error_already_set const &) + { + // Parse and output the exception + std::string perror_str = parse_python_exception(); + std::cout << "Error in Python: " << perror_str << std::endl; + } +} + -- cgit v1.2.3