diff options
author | ZipCPU <dgisselq@ieee.org> | 2018-06-06 07:55:18 -0400 |
---|---|---|
committer | ZipCPU <dgisselq@ieee.org> | 2018-06-06 07:55:18 -0400 |
commit | d0ee08aeb12a8fb7237b31083666d9b165f13f69 (patch) | |
tree | e09d84b0389462f2c1959ce59438d88a7b5f9ed2 /common/pybindings.cc | |
parent | 2e6d0b752ab2d269f822bfd3ea029b100ecf4233 (diff) | |
parent | d3f19cc27ea4634a64821688e9adec6046f4d7de (diff) | |
download | nextpnr-d0ee08aeb12a8fb7237b31083666d9b165f13f69.tar.gz nextpnr-d0ee08aeb12a8fb7237b31083666d9b165f13f69.tar.bz2 nextpnr-d0ee08aeb12a8fb7237b31083666d9b165f13f69.zip |
Merge branch 'master' into gqtech
Diffstat (limited to 'common/pybindings.cc')
-rw-r--r-- | common/pybindings.cc | 42 |
1 files changed, 42 insertions, 0 deletions
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; + } +} + |