diff --git a/src/Base/MatrixPyImp.cpp b/src/Base/MatrixPyImp.cpp index d7f4bf2d92..a781f9d812 100644 --- a/src/Base/MatrixPyImp.cpp +++ b/src/Base/MatrixPyImp.cpp @@ -481,7 +481,11 @@ PyObject* MatrixPy::analyze(PyObject * args) PY_TRY { std::string type = getMatrixPtr()->analyse(); +#if PY_MAJOR_VERSION < 3 return PyString_FromString(type.c_str()); +#else + return PyUniCode_FromString(type.c_str()); +#endif } PY_CATCH; } @@ -696,11 +700,13 @@ int MatrixPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * MatrixPy::number_divide_handler (PyObject* /*self*/, PyObject* /*other*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } +#endif PyObject * MatrixPy::number_remainder_handler (PyObject* /*self*/, PyObject* /*other*/) { @@ -779,10 +785,12 @@ PyObject * MatrixPy::number_or_handler (PyObject* /*self*/, PyObject* /*other*/) return 0; } +#if PY_MAJOR_VERSION < 3 int MatrixPy::number_coerce_handler (PyObject ** /*self*/, PyObject ** /*other*/) { return 1; } +#endif PyObject * MatrixPy::number_int_handler (PyObject * /*self*/) { @@ -790,11 +798,13 @@ PyObject * MatrixPy::number_int_handler (PyObject * /*self*/) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * MatrixPy::number_long_handler (PyObject * /*self*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } +#endif PyObject * MatrixPy::number_float_handler (PyObject * /*self*/) { @@ -802,6 +812,7 @@ PyObject * MatrixPy::number_float_handler (PyObject * /*self*/) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * MatrixPy::number_oct_handler (PyObject * /*self*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); @@ -813,3 +824,4 @@ PyObject * MatrixPy::number_hex_handler (PyObject * /*self*/) PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } +#endif diff --git a/src/Base/PyTools.c b/src/Base/PyTools.c index 3e77359a13..65293202a8 100644 --- a/src/Base/PyTools.c +++ b/src/Base/PyTools.c @@ -149,7 +149,7 @@ PP_Debug_Function(PyObject *func, PyObject *args) /* expand tuple at front */ // it seems that some versions of python want just 2 arguments; in that // case, remove trailing 1 -#if (PY_MAJOR_VERSION==2)&&(PY_MINOR_VERSION>=2) +#if (PY_MAJOR_VERSION>=2)&&(PY_MINOR_VERSION>=2) oops = _PyTuple_Resize(&args, (1 + PyTuple_Size(args))); #else oops = _PyTuple_Resize(&args, (1 + PyTuple_Size(args)),1); @@ -241,9 +241,15 @@ void PP_Fetch_Error_Text() pystring = NULL; if (errobj != NULL && (pystring = PyObject_Str(errobj)) != NULL && /* str(errobj) */ +#if PY_MAJOR_VERSION >= 3 + (PyUnicode_Check(pystring)) ) /* str() increfs */ + { + strncpy(PP_last_error_type, PyUnicode_AsUTF8(pystring), MAX); /*Py->C*/ +#else (PyString_Check(pystring)) ) /* str() increfs */ { strncpy(PP_last_error_type, PyString_AsString(pystring), MAX); /*Py->C*/ +#endif PP_last_error_type[MAX-1] = '\0'; } else @@ -264,7 +270,11 @@ void PP_Fetch_Error_Text() PyObject* value = PyDict_GetItemString(errdata,"swhat"); if (value!=NULL) { - strncpy(PP_last_error_info, PyString_AsString(value), MAX); +#if PY_MAJOR_VERSION < 3 + strncpy(PP_last_error_info, PyString_AsString(pystring), MAX); +#else + strncpy(PP_last_error_info, PyUnicode_AsUTF8(pystring), MAX); +#endif PP_last_error_info[MAX-1] = '\0'; } @@ -273,9 +283,15 @@ void PP_Fetch_Error_Text() } else if (errdata != NULL && (pystring = PyObject_Str(errdata)) != NULL && /* str(): increfs */ +#if PY_MAJOR_VERSION >= 3 + (PyUnicode_Check(pystring)) ) + { + strncpy(PP_last_error_info, PyUnicode_AsUTF8(pystring), MAX); /*Py->C*/ +#else (PyString_Check(pystring)) ) { strncpy(PP_last_error_info, PyString_AsString(pystring), MAX); /*Py->C*/ +#endif PP_last_error_info[MAX-1] = '\0'; } else @@ -289,7 +305,12 @@ void PP_Fetch_Error_Text() pystring = NULL; if (errtraceback != NULL && +#if PY_MAJOR_VERSION < 3 (PP_Run_Function("StringIO", "StringIO", "O", &pystring, "()") == 0) && +#else + (PP_Run_Function("io", "StringIO", "O", &pystring, "()") == 0) && +#endif + (PyTraceBack_Print(errtraceback, pystring) == 0) && (PP_Run_Method(pystring, "getvalue", "s", &tempstr, "()") == 0) ) { @@ -581,7 +602,11 @@ PP_Run_Bytecode(PyObject *codeobj, /* run compiled bytecode object */ if (PP_DEBUG) presult = PP_Debug_Bytecode(codeobj, dict); /* run in pdb */ else +#if PY_MAJOR_VERSION >= 3 + presult = PyEval_EvalCode((PyObject*)codeobj, dict, dict); +#else presult = PyEval_EvalCode((PyCodeObject *)codeobj, dict, dict); +#endif return PP_Convert_Result(presult, resfmt, restarget); /* expr val to C */ } diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index 1eb95ea5ee..310ac007d9 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -221,9 +221,14 @@ PyObject * QuantityPy::number_int_handler (PyObject *self) } QuantityPy* q = static_cast(self); +#if PY_MAJOR_VERSION < 3 return PyInt_FromLong((long)q->getValue()); +#else + return PyLong_FromLong((long)q->getValue()); +#endif } +#if PY_MAJOR_VERSION < 3 PyObject * QuantityPy::number_long_handler (PyObject *self) { if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { @@ -234,6 +239,7 @@ PyObject * QuantityPy::number_long_handler (PyObject *self) QuantityPy* q = static_cast(self); return PyInt_FromLong((long)q->getValue()); } +#endif PyObject * QuantityPy::number_negative_handler (PyObject *self) { @@ -274,7 +280,7 @@ PyObject* QuantityPy::number_add_handler(PyObject *self, PyObject *other) if (!PyObject_TypeCheck(self, &(QuantityPy::Type)) || !PyObject_TypeCheck(other, &(QuantityPy::Type))) { std::stringstream ret; - ret << self->ob_type->tp_name << " and " << other->ob_type->tp_name + ret << Py_TYPE(self)->tp_name << " and " << Py_TYPE(other)->tp_name << " cannot be mixed in Quantity.__add__.\n" << "Make sure to use matching types."; PyErr_SetString(PyExc_TypeError, ret.str().c_str()); @@ -297,7 +303,7 @@ PyObject* QuantityPy::number_subtract_handler(PyObject *self, PyObject *other) if (!PyObject_TypeCheck(self, &(QuantityPy::Type)) || !PyObject_TypeCheck(other, &(QuantityPy::Type))) { std::stringstream ret; - ret << self->ob_type->tp_name << " and " << other->ob_type->tp_name + ret << Py_TYPE(self)->tp_name << " and " << Py_TYPE(other)->tp_name << " cannot be mixed in Quantity.__sub__.\n" << "Make sure to use matching types."; PyErr_SetString(PyExc_TypeError, ret.str().c_str()); @@ -328,9 +334,15 @@ PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other) double b = PyFloat_AsDouble(other); return new QuantityPy(new Quantity(*a*b) ); } +#if PY_MAJOR_VERSION < 3 else if (PyInt_Check(other)) { Base::Quantity *a = static_cast(self) ->getQuantityPtr(); double b = (double)PyInt_AsLong(other); +#else + else if (PyLong_Check(other)) { + Base::Quantity *a = static_cast(self) ->getQuantityPtr(); + double b = (double)PyLong_AsLong(other); +#endif return new QuantityPy(new Quantity(*a*b) ); } } @@ -340,9 +352,15 @@ PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other) double b = PyFloat_AsDouble(self); return new QuantityPy(new Quantity(*a*b) ); } +#if PY_MAJOR_VERSION < 3 else if (PyInt_Check(self)) { Base::Quantity *a = static_cast(other) ->getQuantityPtr(); double b = (double)PyInt_AsLong(self); +#else + else if (PyLong_Check(self)) { + Base::Quantity *a = static_cast(other) ->getQuantityPtr(); + double b = (double)PyLong_AsLong(self); +#endif return new QuantityPy(new Quantity(*a*b) ); } } @@ -351,6 +369,7 @@ PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * QuantityPy::number_divide_handler (PyObject *self, PyObject *other) { if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { @@ -379,6 +398,7 @@ PyObject * QuantityPy::number_divide_handler (PyObject *self, PyObject *other) return 0; } } +#endif PyObject * QuantityPy::number_remainder_handler (PyObject *self, PyObject *other) { @@ -398,8 +418,13 @@ PyObject * QuantityPy::number_remainder_handler (PyObject *self, PyObject *other else if (PyFloat_Check(other)) { d2 = PyFloat_AsDouble(other); } +#if PY_MAJOR_VERSION < 3 else if (PyInt_Check(other)) { d2 = (double)PyInt_AsLong(other); +#else + else if (PyLong_Check(other)) { + d2 = (double)PyLong_AsLong(other); +#endif } else { PyErr_SetString(PyExc_TypeError, "Expected quantity or number"); @@ -443,9 +468,15 @@ PyObject * QuantityPy::number_power_handler (PyObject *self, PyObject *other, Py double b = PyFloat_AsDouble(other); return new QuantityPy(new Quantity(a->pow(b)) ); } +#if PY_MAJOR_VERSION < 3 else if (PyInt_Check(other)) { Base::Quantity *a = static_cast(self) ->getQuantityPtr(); double b = (double)PyInt_AsLong(other); +#else + else if (PyLong_Check(other)) { + Base::Quantity *a = static_cast(self) ->getQuantityPtr(); + double b = (double)PyLong_AsLong(other); +#endif return new QuantityPy(new Quantity(a->pow(b))); } else { @@ -621,6 +652,7 @@ PyObject * QuantityPy::number_or_handler (PyObject* /*self*/, PyObject* /*other* return 0; } +#if PY_MAJOR_VERSION < 3 int QuantityPy::number_coerce_handler (PyObject** /*self*/, PyObject** /*other*/) { return 1; @@ -637,3 +669,4 @@ PyObject * QuantityPy::number_hex_handler (PyObject* /*self*/) PyErr_SetString(PyExc_TypeError, "hex() argument can't be converted to hex"); return 0; } +#endif