aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorDavid Shah <davey1576@gmail.com>2018-06-07 08:56:54 +0200
committerDavid Shah <davey1576@gmail.com>2018-06-07 08:56:54 +0200
commitbdd93135823ae1ef293d4be3eb50d0ca6bdeee9e (patch)
tree286f8f2ead78b5b20afd8aa8feaffe7e97df9f7f /common
parentc18f0b1c2286d737f3ace47d4f843ef523ba4b97 (diff)
downloadnextpnr-bdd93135823ae1ef293d4be3eb50d0ca6bdeee9e.tar.gz
nextpnr-bdd93135823ae1ef293d4be3eb50d0ca6bdeee9e.tar.bz2
nextpnr-bdd93135823ae1ef293d4be3eb50d0ca6bdeee9e.zip
Allow loading and running Python files before GUI starts
Signed-off-by: David Shah <davey1576@gmail.com>
Diffstat (limited to 'common')
-rw-r--r--common/pybindings.cc74
-rw-r--r--common/pybindings.h6
2 files changed, 46 insertions, 34 deletions
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);
+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);
+}
- 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 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 <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
-
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<t##Range>().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();