Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 256358e

Browse files
committed
[Python 2] fix type checks for old types
1 parent 90822c1 commit 256358e

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/dynamic_graph/convert-dg-to-py.cc

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,43 +75,63 @@ command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& val
7575
return Value(bvalue);
7676
break;
7777
case (Value::UNSIGNED):
78-
if (!PyLong_Check(pyObject)) {
78+
if (PyLong_Check(pyObject)) {
79+
uvalue = (unsigned int)PyLong_AsUnsignedLongMask(pyObject);
80+
#if PY_MAJOR_VERSION == 2
81+
} else if (PyInt_Check(pyObject)) {
82+
uvalue = (unsigned int)PyInt_AsUnsignedLongMask(pyObject);
83+
#endif
84+
} else {
7985
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "unsigned int");
8086
}
81-
uvalue = (unsigned int)PyLong_AsUnsignedLongMask(pyObject);
8287
return Value(uvalue);
8388
break;
8489
case (Value::INT):
85-
if (!PyLong_Check(pyObject)) {
90+
if (PyLong_Check(pyObject)) {
91+
ivalue = (int)PyLong_AsLong(pyObject);
92+
#if PY_MAJOR_VERSION == 2
93+
} else if (PyInt_Check(pyObject)) {
94+
ivalue = (int)PyInt_AsLong(pyObject);
95+
#endif
96+
} else {
8697
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "int");
8798
}
88-
ivalue = (int)PyLong_AsLong(pyObject);
8999
return Value(ivalue);
90100
break;
91101
case (Value::FLOAT):
92102
if (PyFloat_Check(pyObject)) {
93103
fvalue = (float)PyFloat_AsDouble(pyObject);
94-
return Value(fvalue);
95104
} else if (PyLong_Check(pyObject)) {
96105
fvalue = (float)PyLong_AsLong(pyObject);
97-
return Value(fvalue);
106+
#if PY_MAJOR_VERSION == 2
107+
} else if (PyInt_Check(pyObject)) {
108+
fvalue = (float)PyInt_AsLong(pyObject);
109+
#endif
98110
} else {
99111
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "float");
100112
}
113+
return Value(fvalue);
101114
break;
102115
case (Value::DOUBLE):
103116
if (PyFloat_Check(pyObject)) {
104117
dvalue = PyFloat_AsDouble(pyObject);
105-
return Value(dvalue);
106118
} else if (PyLong_Check(pyObject)) {
107119
dvalue = (double)PyLong_AsLong(pyObject);
108-
return Value(dvalue);
120+
#if PY_MAJOR_VERSION == 2
121+
} else if (PyInt_Check(pyObject)) {
122+
dvalue = (double)PyInt_AsLong(pyObject);
123+
#endif
109124
} else {
110125
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "double");
111126
}
127+
return Value(dvalue);
112128
break;
113129
case (Value::STRING):
114-
if (!PyUnicode_Check(pyObject)) {
130+
if (!PyUnicode_Check(pyObject)
131+
#if PY_MAJOR_VERSION == 2
132+
&& !PyString_Check(pyObject)
133+
#endif
134+
) {
115135
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "string");
116136
}
117137
svalue = obj_to_str(pyObject);
@@ -130,6 +150,10 @@ command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& val
130150
v(i) = PyFloat_AsDouble(pyDouble);
131151
else if (PyLong_Check(pyDouble))
132152
v(i) = (int)PyLong_AsLong(pyDouble) + 0.0;
153+
#if PY_MAJOR_VERSION == 2
154+
else if (PyInt_Check(pyDouble))
155+
v(i) = (int)PyInt_AsLong(pyDouble) + 0.0;
156+
#endif
133157
else
134158
throw ExceptionPython(ExceptionPython::VECTOR_PARSING,
135159
"element of vector should be a floating "

0 commit comments

Comments
 (0)