Skip to content

Commit

Permalink
py3: Base: files I-R ported to python3
Browse files Browse the repository at this point in the history
PyObjectBase.cpp and PyObjectBase.h not included
issue 0000995
  • Loading branch information
yorikvanhavre authored and wwmayer committed May 18, 2017
1 parent ef22d52 commit 2becb8a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Base/MatrixPyImp.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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*/)
{
Expand Down Expand Up @@ -779,29 +785,34 @@ 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*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
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*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

#if PY_MAJOR_VERSION < 3
PyObject * MatrixPy::number_oct_handler (PyObject * /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
Expand All @@ -813,3 +824,4 @@ PyObject * MatrixPy::number_hex_handler (PyObject * /*self*/)
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}
#endif
29 changes: 27 additions & 2 deletions src/Base/PyTools.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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';
}

Expand All @@ -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
Expand All @@ -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) )
{
Expand Down Expand Up @@ -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 */
}

Expand Down
37 changes: 35 additions & 2 deletions src/Base/QuantityPyImp.cpp
Expand Up @@ -221,9 +221,14 @@ PyObject * QuantityPy::number_int_handler (PyObject *self)
}

QuantityPy* q = static_cast<QuantityPy*>(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))) {
Expand All @@ -234,6 +239,7 @@ PyObject * QuantityPy::number_long_handler (PyObject *self)
QuantityPy* q = static_cast<QuantityPy*>(self);
return PyInt_FromLong((long)q->getValue());
}
#endif

PyObject * QuantityPy::number_negative_handler (PyObject *self)
{
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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<QuantityPy*>(self) ->getQuantityPtr();
double b = (double)PyInt_AsLong(other);
#else
else if (PyLong_Check(other)) {
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
double b = (double)PyLong_AsLong(other);
#endif
return new QuantityPy(new Quantity(*a*b) );
}
}
Expand All @@ -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<QuantityPy*>(other) ->getQuantityPtr();
double b = (double)PyInt_AsLong(self);
#else
else if (PyLong_Check(self)) {
Base::Quantity *a = static_cast<QuantityPy*>(other) ->getQuantityPtr();
double b = (double)PyLong_AsLong(self);
#endif
return new QuantityPy(new Quantity(*a*b) );
}
}
Expand All @@ -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))) {
Expand Down Expand Up @@ -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)
{
Expand All @@ -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");
Expand Down Expand Up @@ -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<QuantityPy*>(self) ->getQuantityPtr();
double b = (double)PyInt_AsLong(other);
#else
else if (PyLong_Check(other)) {
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
double b = (double)PyLong_AsLong(other);
#endif
return new QuantityPy(new Quantity(a->pow(b)));
}
else {
Expand Down Expand Up @@ -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;
Expand All @@ -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

0 comments on commit 2becb8a

Please sign in to comment.