py3: ported Path to python3

This commit is contained in:
wmayer 2016-01-23 21:59:22 +01:00 committed by looooo
parent 71f6a289c6
commit 32bacd0b63
6 changed files with 70 additions and 8 deletions

View File

@ -27,6 +27,7 @@
#endif
#include <Base/Console.h>
#include <Base/PyObjectBase.h>
#include <Base/Interpreter.h>
#include "Command.h"
@ -47,7 +48,7 @@ extern PyObject* initModule();
}
/* Python entry */
PyMODINIT_FUNC initPath()
PyMOD_INIT_FUNC(Path)
{
PyObject* pathModule = Path::initModule();
Base::Console().Log("Loading Path module... done\n");
@ -74,4 +75,6 @@ PyMODINIT_FUNC initPath()
Path::FeatureCompoundPython ::init();
Path::FeatureShape ::init();
Path::FeatureShapePython ::init();
PyMOD_Return(pathModule);
}

View File

@ -74,15 +74,28 @@ int CommandPy::PyInit(PyObject* args, PyObject* kwd)
PyObject *key, *value;
Py_ssize_t pos = 0;
while (parameters && PyDict_Next(parameters, &pos, &key, &value)) {
#if PY_MAJOR_VERSION >= 3
if ( !PyObject_TypeCheck(key,&(PyBytes_Type)) || (!PyObject_TypeCheck(value,&(PyFloat_Type)) && !PyObject_TypeCheck(value,&(PyLong_Type))) ) {
#else
if ( !PyObject_TypeCheck(key,&(PyString_Type)) || (!PyObject_TypeCheck(value,&(PyFloat_Type)) && !PyObject_TypeCheck(value,&(PyInt_Type))) ) {
#endif
PyErr_SetString(PyExc_TypeError, "The dictionary can only contain string:number pairs");
return -1;
}
#if PY_MAJOR_VERSION >= 3
std::string ckey = PyBytes_AsString(key);
#else
std::string ckey = PyString_AsString(key);
#endif
boost::to_upper(ckey);
double cvalue;
#if PY_MAJOR_VERSION >= 3
if (PyObject_TypeCheck(value,&(PyLong_Type))) {
cvalue = (double)PyLong_AsLong(value);
#else
if (PyObject_TypeCheck(value,&(PyInt_Type))) {
cvalue = (double)PyInt_AsLong(value);
#endif
} else {
cvalue = PyFloat_AsDouble(value);
}
@ -124,7 +137,11 @@ Py::Dict CommandPy::getParameters(void) const
{
PyObject *dict = PyDict_New();
for(std::map<std::string,double>::iterator i = getCommandPtr()->Parameters.begin(); i != getCommandPtr()->Parameters.end(); ++i) {
#if PY_MAJOR_VERSION >= 3
PyDict_SetItem(dict,PyBytes_FromString(i->first.c_str()),PyFloat_FromDouble(i->second));
#else
PyDict_SetItem(dict,PyString_FromString(i->first.c_str()),PyFloat_FromDouble(i->second));
#endif
}
return Py::Dict(dict);
}
@ -135,12 +152,22 @@ void CommandPy::setParameters(Py::Dict arg)
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(dict_copy, &pos, &key, &value)) {
#if PY_MAJOR_VERSION >= 3
if ( PyObject_TypeCheck(key,&(PyBytes_Type)) && (PyObject_TypeCheck(value,&(PyFloat_Type)) || PyObject_TypeCheck(value,&(PyLong_Type)) ) ) {
std::string ckey = PyBytes_AsString(key);
#else
if ( PyObject_TypeCheck(key,&(PyString_Type)) && (PyObject_TypeCheck(value,&(PyFloat_Type)) || PyObject_TypeCheck(value,&(PyInt_Type)) ) ) {
std::string ckey = PyString_AsString(key);
#endif
boost::to_upper(ckey);
double cvalue;
#if PY_MAJOR_VERSION >= 3
if (PyObject_TypeCheck(value,&(PyLong_Type))) {
cvalue = (double)PyLong_AsLong(value);
#else
if (PyObject_TypeCheck(value,&(PyInt_Type))) {
cvalue = (double)PyInt_AsLong(value);
#endif
} else {
cvalue = PyFloat_AsDouble(value);
}
@ -156,7 +183,11 @@ void CommandPy::setParameters(Py::Dict arg)
PyObject* CommandPy::toGCode(PyObject *args)
{
if (PyArg_ParseTuple(args, "")) {
#if PY_MAJOR_VERSION >= 3
return PyBytes_FromString(getCommandPtr()->toGCode().c_str());
#else
return PyString_FromString(getCommandPtr()->toGCode().c_str());
#endif
}
throw Py::Exception("This method accepts no argument");
}
@ -226,8 +257,13 @@ int CommandPy::setCustomAttributes(const char* attr, PyObject* obj)
if (isalpha(satt[0])) {
boost::to_upper(satt);
double cvalue;
#if PY_MAJOR_VERSION >= 3
if (PyObject_TypeCheck(obj,&(PyLong_Type))) {
cvalue = (double)PyLong_AsLong(obj);
#else
if (PyObject_TypeCheck(obj,&(PyInt_Type))) {
cvalue = (double)PyInt_AsLong(obj);
#endif
} else if (PyObject_TypeCheck(obj,&(PyFloat_Type))) {
cvalue = PyFloat_AsDouble(obj);
} else {

View File

@ -26,7 +26,7 @@ commands (optional) is a list of Path commands</UserDocu>
<Documentation>
<UserDocu>the number of commands in this path</UserDocu>
</Documentation>
<Parameter Name="Size" Type="Int"/>
<Parameter Name="Size" Type="Long"/>
</Attribute>
<Attribute Name="Commands" ReadOnly="false">
<Documentation>

View File

@ -112,9 +112,9 @@ Py::Float PathPy::getLength(void) const
return Py::Float(getToolpathPtr()->getLength());
}
Py::Int PathPy::getSize(void) const
Py::Long PathPy::getSize(void) const
{
return Py::Int((int)getToolpathPtr()->getSize());
return Py::Long((long)getToolpathPtr()->getSize());
}
// specific methods
@ -178,7 +178,11 @@ PyObject* PathPy::toGCode(PyObject * args)
{
if (PyArg_ParseTuple(args, "")) {
std::string result = getToolpathPtr()->toGCode();
#if PY_MAJOR_VERSION >= 3
return PyBytes_FromString(result.c_str());
#else
return PyString_FromString(result.c_str());
#endif
}
throw Py::Exception("This method accepts no argument");
}

View File

@ -363,11 +363,19 @@ int TooltablePy::PyInit(PyObject* args, PyObject* /*kwd*/)
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(pcObj, &pos, &key, &value)) {
#if PY_MAJOR_VERSION >= 3
if ( !PyObject_TypeCheck(key,&(PyLong_Type)) || !PyObject_TypeCheck(value,&(Path::ToolPy::Type)) ) {
#else
if ( !PyObject_TypeCheck(key,&(PyInt_Type)) || !PyObject_TypeCheck(value,&(Path::ToolPy::Type)) ) {
#endif
PyErr_SetString(PyExc_TypeError, "The dictionary can only contain int:tool pairs");
return -1;
}
#if PY_MAJOR_VERSION >= 3
int ckey = (int)PyLong_AsLong(key);
#else
int ckey = (int)PyInt_AsLong(key);
#endif
Path::Tool &tool = *static_cast<Path::ToolPy*>(value)->getToolPtr();
getTooltablePtr()->setTool(tool,ckey);
}
@ -397,7 +405,11 @@ Py::Dict TooltablePy::getTools(void) const
PyObject *dict = PyDict_New();
for(std::map<int,Path::Tool*>::iterator i = getTooltablePtr()->Tools.begin(); i != getTooltablePtr()->Tools.end(); ++i) {
PyObject *tool = new Path::ToolPy(i->second);
#if PY_MAJOR_VERSION >= 3
PyDict_SetItem(dict,PyLong_FromLong(i->first),tool);
#else
PyDict_SetItem(dict,PyInt_FromLong(i->first),tool);
#endif
}
return Py::Dict(dict);
}
@ -409,8 +421,13 @@ void TooltablePy::setTools(Py::Dict arg)
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(dict_copy, &pos, &key, &value)) {
#if PY_MAJOR_VERSION >= 3
if ( PyObject_TypeCheck(key,&(PyLong_Type)) && (PyObject_TypeCheck(value,&(Path::ToolPy::Type))) ) {
int ckey = (int)PyLong_AsLong(key);
#else
if ( PyObject_TypeCheck(key,&(PyInt_Type)) && (PyObject_TypeCheck(value,&(Path::ToolPy::Type))) ) {
int ckey = (int)PyInt_AsLong(key);
#endif
Path::Tool &tool = *static_cast<Path::ToolPy*>(value)->getToolPtr();
getTooltablePtr()->setTool(tool,ckey);
} else {

View File

@ -51,11 +51,11 @@ extern PyObject* initModule();
}
/* Python entry */
PyMODINIT_FUNC initPathGui()
PyMOD_INIT_FUNC(PathGui)
{
if (!Gui::Application::Instance) {
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
return;
PyMOD_Return(0);
}
try {
Base::Interpreter().runString("import PartGui");
@ -63,9 +63,9 @@ PyMODINIT_FUNC initPathGui()
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
PyMOD_Return(0);
}
(void)PathGui::initModule();
PyObject* mod = PathGui::initModule();
Base::Console().Log("Loading GUI of Path module... done\n");
// instantiating the commands
@ -83,4 +83,6 @@ PyMODINIT_FUNC initPathGui()
// register preferences pages
new Gui::PrefPageProducer<PathGui::DlgSettingsPathColor> ("Path");
PyMOD_Return(mod);
}