Skip to content

Commit

Permalink
cleanup python modules, remove py3c
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-dev committed Aug 26, 2021
1 parent fa4efdd commit ec6ed34
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 743 deletions.
28 changes: 2 additions & 26 deletions src/emc/pythonplugin/python_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,12 @@ namespace bp = boost::python;

static const char *strstore(const char *s);

// boost python versions from 1.58 to 1.61 (the latest at the time of
// writing) all have a bug in boost::python::execfile that results in a
// double free. Work around it by using the Python implementation of
// execfile instead.
// The bug was introduced at https://github.com/boostorg/python/commit/fe24ab9dd5440562e27422cd38f7de03356bfd16
bp::object working_execfile(const char *filename, bp::object globals, bp::object locals) {
#if PY_MAJOR_VERSION >=3
return bp::exec_file(filename,globals,locals);
#else
return bp::import("__builtin__").attr("execfile")(filename, globals, locals);
#endif
}

int PythonPlugin::run_string(const char *cmd, bp::object &retval, bool as_file)
{
reload();
try {
if (as_file)
retval = working_execfile(cmd, main_namespace, main_namespace);
retval = bp::exec_file(cmd, main_namespace, main_namespace);
else
retval = bp::exec(cmd, main_namespace, main_namespace);
status = PLUGIN_OK;
Expand Down Expand Up @@ -273,9 +260,7 @@ int PythonPlugin::initialize()
main_namespace[inittab_entries[i]] = bp::import(inittab_entries[i].c_str());
}
if (toplevel) // only execute a file if there's one configured.
bp::object result = working_execfile(abs_path,
main_namespace,
main_namespace);
bp::object result = bp::exec_file(abs_path, main_namespace, main_namespace);
status = PLUGIN_OK;
}
catch (bp::error_already_set &) {
Expand Down Expand Up @@ -306,7 +291,6 @@ PythonPlugin::PythonPlugin(struct _inittab *inittab) :
abs_path(0),
log_level(0)
{
#if PY_MAJOR_VERSION >= 3
if (abs_path) {
wchar_t *program = Py_DecodeLocale(abs_path, NULL);
Py_SetProgramName(program);
Expand Down Expand Up @@ -338,14 +322,6 @@ PythonPlugin::PythonPlugin(struct _inittab *inittab) :
}
}
Py_UnbufferedStdioFlag = 1;
#else
Py_SetProgramName((char *) abs_path);
if ((inittab != NULL) && PyImport_ExtendInittab(inittab)) {
logPP(-1, "cant extend inittab");
status = PLUGIN_INITTAB_FAILED;
return;
}
#endif
Py_Initialize();
initialize();
}
Expand Down
53 changes: 16 additions & 37 deletions src/emc/rs274ngc/gcodemodule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <Python.h>
#include "py3c/py3c.h"
#include <structmember.h>

#include "rs274ngc.hh"
Expand All @@ -30,8 +29,6 @@ int _task = 0; // control preview behaviour when remapping

char _parameter_file_name[LINELEN];

#if PY_MAJOR_VERSION >=3

extern "C" PyObject* PyInit_emctask(void);
extern "C" PyObject* PyInit_interpreter(void);
extern "C" PyObject* PyInit_emccanon(void);
Expand All @@ -42,23 +39,11 @@ struct _inittab builtin_modules[] = {
{ NULL, NULL }
};

#else

extern "C" void initinterpreter();
extern "C" void initemccanon();
extern "C" struct _inittab builtin_modules[];
struct _inittab builtin_modules[] = {
{ (char *) "interpreter", initinterpreter },
{ (char *) "emccanon", initemccanon },
// any others...
{ NULL, NULL }
};
#endif

static PyObject *int_array(int *arr, int sz) {
PyObject *res = PyTuple_New(sz);
for(int i = 0; i < sz; i++) {
PyTuple_SET_ITEM(res, i, PyInt_FromLong(arr[i]));
PyTuple_SET_ITEM(res, i, PyLong_FromLong(arr[i]));
}
return res;
}
Expand Down Expand Up @@ -544,7 +529,7 @@ void SET_PARAMETER_FILE_NAME(const char *name)
void GET_EXTERNAL_PARAMETER_FILE_NAME(char *name, int max_size) {
PyObject *result = PyObject_GetAttrString(callback, "parameter_file");
if(!result) { name[0] = 0; return; }
char *s = (char*)PyStr_AsString(result);
char *s = (char*)PyUnicode_AsUTF8(result);
if(!s) { name[0] = 0; return; }
memset(name, 0, max_size);
strncpy(name, s, max_size - 1);
Expand Down Expand Up @@ -622,8 +607,8 @@ int GET_EXTERNAL_AXIS_MASK() {
PyObject *result =
callmethod(callback, "get_axis_mask", "");
if(!result) { interp_error ++; return 7 /* XYZABC */; }
if(!PyInt_Check(result)) { interp_error ++; return 7 /* XYZABC */; }
int mask = PyInt_AsLong(result);
if(!PyLong_Check(result)) { interp_error ++; return 7 /* XYZABC */; }
int mask = PyLong_AsLong(result);
Py_DECREF(result);
return mask;
}
Expand Down Expand Up @@ -656,8 +641,8 @@ double GET_EXTERNAL_TOOL_LENGTH_WOFFSET() {
return tool_offset.w;
}

static bool PyInt_CheckAndError(const char *func, PyObject *p) {
if(PyInt_Check(p)) return true;
static bool PyLong_CheckAndError(const char *func, PyObject *p) {
if(PyLong_Check(p)) return true;
PyErr_Format(PyExc_TypeError,
"%s: Expected int, got %s", func, Py_TYPE(p)->tp_name);
return false;
Expand Down Expand Up @@ -773,7 +758,7 @@ static PyObject *parse_file(PyObject *self, PyObject *args) {
{
PyObject *item = PyList_GetItem(initcodes, i);
if(!item) return NULL;
const char *code = PyStr_AsString(item);
const char *code = PyUnicode_AsUTF8(item);
if(!code) return NULL;
result = pinterp->read(code);
if(!RESULT_OK) goto out_error;
Expand Down Expand Up @@ -827,8 +812,8 @@ static PyObject *parse_file(PyObject *self, PyObject *args) {
maybe_new_line();
if(PyErr_Occurred()) { interp_error = 1; goto out_error; }
PyObject *retval = PyTuple_New(2);
PyTuple_SetItem(retval, 0, PyInt_FromLong(result));
PyTuple_SetItem(retval, 1, PyInt_FromLong(last_sequence_number + error_line_offset));
PyTuple_SetItem(retval, 0, PyLong_FromLong(result));
PyTuple_SetItem(retval, 1, PyLong_FromLong(last_sequence_number + error_line_offset));
return retval;
}

Expand All @@ -840,7 +825,7 @@ static PyObject *rs274_strerror(PyObject *s, PyObject *o) {
int err;
if(!PyArg_ParseTuple(o, "i", &err)) return nullptr;
pinterp->error_text(err, savedError, LINELEN);
return PyStr_FromString(savedError);
return PyUnicode_FromString(savedError);
}

static PyObject *rs274_calc_extents(PyObject *self, PyObject *args) {
Expand Down Expand Up @@ -906,17 +891,10 @@ static PyObject *rs274_calc_extents(PyObject *self, PyObject *args) {
min_xt, min_yt, min_zt, max_xt, max_yt, max_zt);
}

#if PY_VERSION_HEX < 0x02050000
#define PyObject_GetAttrString(o,s) \
PyObject_GetAttrString((o),const_cast<char*>((s)))
#define PyArg_VaParse(o,f,a) \
PyArg_VaParse((o),const_cast<char*>((f)),(a))
#endif

static bool get_attr(PyObject *o, const char *attr_name, int *v) {
PyObject *attr = PyObject_GetAttrString(o, attr_name);
if(attr && PyInt_CheckAndError(attr_name, attr)) {
*v = PyInt_AsLong(attr);
if(attr && PyLong_CheckAndError(attr_name, attr)) {
*v = PyLong_AsLong(attr);
Py_DECREF(attr);
return true;
}
Expand Down Expand Up @@ -1082,15 +1060,16 @@ static struct PyModuleDef gcode_moduledef = {
gcode_methods /* m_methods */
};

MODULE_INIT_FUNC(gcode)
PyMODINIT_FUNC PyInit_gcode(void);
PyMODINIT_FUNC PyInit_gcode(void)
{

PyObject *m = PyModule_Create(&gcode_moduledef);
PyType_Ready(&LineCodeType);
PyModule_AddObject(m, "linecode", (PyObject*)&LineCodeType);
PyObject_SetAttrString(m, "MAX_ERROR", PyInt_FromLong(maxerror));
PyObject_SetAttrString(m, "MAX_ERROR", PyLong_FromLong(maxerror));
PyObject_SetAttrString(m, "MIN_ERROR",
PyInt_FromLong(INTERP_MIN_ERROR));
PyLong_FromLong(INTERP_MIN_ERROR));
return m;
}
// vim:ts=8:sts=4:sw=4:et:
7 changes: 3 additions & 4 deletions src/emc/rs274ngc/interp_namedparams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#define _GNU_SOURCE
#endif

#include "py3c/py3c.h"
#define BOOST_PYTHON_MAX_ARITY 4
#include "python_plugin.hh"
#include <boost/python/dict.hpp>
Expand Down Expand Up @@ -369,13 +368,13 @@ int Interp::find_named_param(
"named param - pycall(%s):\n%s", nameBuf,
python_plugin->last_exception().c_str());
CHKS(retval.ptr() == Py_None, "Python namedparams.%s returns no value", nameBuf);
if (PyStr_Check(retval.ptr())) {
if (PyUnicode_Check(retval.ptr())) {
// returning a string sets the interpreter error message and aborts
*status = 0;
char *msg = bp::extract<char *>(retval);
ERS("%s", msg);
}
if (PyInt_Check(retval.ptr())) { // widen
if (PyLong_Check(retval.ptr())) { // widen
*value = (double) bp::extract<int>(retval);
*status = 1;
return INTERP_OK;
Expand All @@ -391,7 +390,7 @@ int Interp::find_named_param(
Py_XDECREF(res_str);
ERS("Python call %s.%s returned '%s' - expected double, int or string, got %s",
NAMEDPARAMS_MODULE, nameBuf,
PyStr_AsString(res_str),
PyUnicode_AsUTF8(res_str),
retval.ptr()->ob_type->tp_name);
} else {
*value = pv->value;
Expand Down
15 changes: 5 additions & 10 deletions src/emc/rs274ngc/interp_python.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "py3c/py3c.h"
#define BOOST_PYTHON_MAX_ARITY 4
#include "python_plugin.hh"
#include "interp_python.hh"
Expand Down Expand Up @@ -195,22 +194,18 @@ int Interp::pycall(setup_pointer settings,

// a generator was returned. This must have been the first time call to a handler
// which contains a yield. Extract next() method.
#if PY_MAJOR_VERSION >=3
frame->pystuff.impl->generator_next = bp::getattr(retval, "__next__");
#else
frame->pystuff.impl->generator_next = bp::getattr(retval, "next");
#endif
// and call it for the first time.
// Expect execution up to first 'yield INTERP_EXECUTE_FINISH'.
frame->pystuff.impl->py_returned_int = bp::extract<int>(frame->pystuff.impl->generator_next());
frame->pystuff.impl->py_return_type = RET_YIELD;
} else if (PyStr_Check(retval.ptr())) {
} else if (PyUnicode_Check(retval.ptr())) {
// returning a string sets the interpreter error message and aborts
char *msg = bp::extract<char *>(retval);
ERM("%s", msg);
frame->pystuff.impl->py_return_type = RET_ERRORMSG;
status = INTERP_ERROR;
} else if (PyInt_Check(retval.ptr())) {
} else if (PyLong_Check(retval.ptr())) {
frame->pystuff.impl->py_returned_int = bp::extract<int>(retval);
frame->pystuff.impl->py_return_type = RET_INT;
logPy("Python call %s.%s returned int: %d", module, funcname, frame->pystuff.impl->py_returned_int);
Expand All @@ -224,7 +219,7 @@ int Interp::pycall(setup_pointer settings,
Py_XDECREF(res_str);
ERM("Python call %s.%s returned '%s' - expected generator, int, or float value, got %s",
module, funcname,
PyStr_AsString(res_str),
PyUnicode_AsUTF8(res_str),
Py_TYPE(retval.ptr())->tp_name);
status = INTERP_ERROR;
}
Expand All @@ -239,7 +234,7 @@ int Interp::pycall(setup_pointer settings,
// a plain int (INTERP_OK, INTERP_ERROR, INTERP_EXECUTE_FINISH...) is expected
// must have returned an int
if ((retval.ptr() != Py_None) &&
(PyInt_Check(retval.ptr()))) {
(PyLong_Check(retval.ptr()))) {

// FIXME check new return value convention
status = frame->pystuff.impl->py_returned_int = bp::extract<int>(retval);
Expand All @@ -250,7 +245,7 @@ int Interp::pycall(setup_pointer settings,
res_str = PyObject_Str(retval.ptr());
ERM("Python internal function '%s' expected tuple or int return value, got '%s' (%s)",
funcname,
PyStr_AsString(res_str),
PyUnicode_AsUTF8(res_str),
Py_TYPE(retval.ptr())->tp_name);
Py_XDECREF(res_str);
status = INTERP_ERROR;
Expand Down
6 changes: 3 additions & 3 deletions src/emc/rs274ngc/pyparamclass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Interpreter internals - Python bindings
// Michael Haberler 7/2011
//
#include "py3c/py3c.h"

#define BOOST_PYTHON_MAX_ARITY 4
#include <boost/python/extract.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
Expand All @@ -40,8 +40,8 @@ extern int _task; // zero in gcodemodule, 1 in milltask
#include <interp_parameter_def.hh>
using namespace interp_param_global;

#define IS_STRING(x) (PyStr_Check(x.ptr()))
#define IS_INT(x) (PyInt_Check(x.ptr()))
#define IS_STRING(x) (PyUnicode_Check(x.ptr()))
#define IS_INT(x) (PyLong_Check(x.ptr()))

// access to named and numbered parameters via a pseudo-dictionary
// either params["paramname"] or params[5400] is valid
Expand Down
32 changes: 0 additions & 32 deletions src/emc/task/taskclass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#include "emc_nml.hh"
#include "emcglb.h" // EMC_INIFILE


#include "py3c/py3c.h"
#include "python_plugin.hh"
#include "taskclass.hh"
#include <rtapi_string.h>
Expand Down Expand Up @@ -397,21 +395,12 @@ int return_int(const char *funcname, PyObject *retval)
return -1;
}
if ((retval != Py_None) &&
#if PY_MAJOR_VERSION >=3
(PyLong_Check(retval))) {
return PyLong_AsLong(retval);
#else
(PyInt_Check(retval))) {
return PyInt_AS_LONG(retval);
#endif
} else {
emcOperatorError(0, "return_int(%s): expected int return value, got '%s' (%s)",
funcname,
#if PY_MAJOR_VERSION >=3
PyBytes_AsString(retval),
#else
PyString_AsString(retval),
#endif
Py_TYPE(retval)->tp_name);
Py_XDECREF(retval);
return -1;
Expand All @@ -434,14 +423,6 @@ int emcPluginCall(EMC_EXEC_PLUGIN_CALL *call_msg)
}
}

// int emcAbortCleanup(int reason, const char *message)
// {
// int status = interp.on_abort(reason,message);
// if (status > INTERP_MIN_ERROR)
// print_interp_error(status);
// return status;
// }
#if PY_MAJOR_VERSION >=3
extern "C" PyObject* PyInit_emctask(void);
extern "C" PyObject* PyInit_interpreter(void);
extern "C" PyObject* PyInit_emccanon(void);
Expand All @@ -452,19 +433,6 @@ struct _inittab builtin_modules[] = {
// any others...
{ NULL, NULL }
};
#else
extern "C" void initemctask();
extern "C" void initinterpreter();
extern "C" void initemccanon();
struct _inittab builtin_modules[] = {
{ (char *) "interpreter", initinterpreter },
{ (char *) "emccanon", initemccanon },
{ (char *) "emctask", initemctask },
// any others...
{ NULL, NULL }
};
#endif


Task::Task() : use_iocontrol(0), random_toolchanger(0) {

Expand Down
4 changes: 2 additions & 2 deletions src/emc/usr_intf/axis/extensions/_toglmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <Python.h>
#include "py3c/py3c.h"
#include <emc/usr_intf/axis/extensions/togl.c>
static int first_time = 1;

Expand Down Expand Up @@ -77,7 +76,8 @@ static struct PyModuleDef togl_moduledef = {
togl_methods
};

MODULE_INIT_FUNC(_togl)
PyMODINIT_FUNC PyInit__togl(void);
PyMODINIT_FUNC PyInit__togl(void)
{
PyObject *m = PyModule_Create(&togl_moduledef);
return m;
Expand Down
Loading

0 comments on commit ec6ed34

Please sign in to comment.