diff --git a/common/pybindings.cc b/common/pybindings.cc index 556e838e..7a542dc1 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -22,6 +22,7 @@ #include "design.h" #include "chip.h" #include "pybindings.h" +#include "emb.h" // Required to determine concatenated module name (which differs for different archs) #define PASTER(x, y) x ## _ ## y @@ -36,43 +37,52 @@ void arch_wrap_python(); BOOST_PYTHON_MODULE (MODULE_NAME) { - arch_wrap_python(); + arch_wrap_python(); } -void arch_appendinittab() -{ - PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); +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(); +static wchar_t *program; - 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; - } +void init_python(const char *executable) { + 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); + emb::append_inittab(); + Py_SetProgramName(program); + Py_Initialize(); + } 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; + } +} + +void deinit_python() { + Py_Finalize(); + PyMem_RawFree(program); +} + +void execute_python_file(const char *python_file) { + try { + 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); + } + 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; + } } diff --git a/common/pybindings.h b/common/pybindings.h index f594784c..84280908 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -26,7 +26,6 @@ #include #include #include - using namespace boost::python; /* A wrapper for a Pythonised nextpnr Iterator. The actual class wrapped is a @@ -79,7 +78,10 @@ struct range_wrapper { #define WRAP_RANGE(t) range_wrapper().wrap(#t "Range", #t "Iterator") -void execute_python_file(const char *executable, const char* python_file); +void init_python(const char *executable); +void deinit_python(); + +void execute_python_file(const char* python_file); std::string parse_python_exception(); void arch_appendinittab(); diff --git a/gui/mainwindow.cc b/gui/mainwindow.cc index 4404fc5a..b72a0851 100644 --- a/gui/mainwindow.cc +++ b/gui/mainwindow.cc @@ -11,9 +11,6 @@ MainWindow::MainWindow(QWidget *parent) : ui(new Ui::MainWindow) { ui->setupUi(this); - emb::append_inittab(); - arch_appendinittab(); - Py_Initialize(); PyImport_ImportModule("emb"); write = [this] (std::string s) { @@ -27,7 +24,6 @@ MainWindow::MainWindow(QWidget *parent) : MainWindow::~MainWindow() { - Py_Finalize(); delete ui; } diff --git a/ice40/main.cc b/ice40/main.cc index 9295bde4..3d3312c4 100644 --- a/ice40/main.cc +++ b/ice40/main.cc @@ -45,7 +45,7 @@ void svg_dump_el(const GraphicElement &el) int main(int argc, char *argv[]) { namespace po = boost::program_options; - + int rc = 0; std::string str; po::options_description options("Allowed options"); @@ -122,15 +122,7 @@ int main(int argc, char *argv[]) chipArgs.type = ChipArgs::UP5K; Design design(chipArgs); - - if (vm.count("gui")) - { - QApplication a(argc, argv); - MainWindow w; - w.show(); - - return a.exec(); - } + init_python(argv[0]); if (vm.count("test")) { @@ -216,8 +208,17 @@ int main(int argc, char *argv[]) if (vm.count("file")) { std::string filename = vm["file"].as(); - execute_python_file(argv[0],filename.c_str()); - } - - return 0; + execute_python_file(filename.c_str()); + } + + if (vm.count("gui")) + { + QApplication a(argc, argv); + MainWindow w; + w.show(); + + rc = a.exec(); + } + deinit_python(); + return rc; } diff --git a/python/functions.py b/python/functions.py new file mode 100644 index 00000000..4f005456 --- /dev/null +++ b/python/functions.py @@ -0,0 +1,2 @@ +def test_function(): + print("Hello World!")