summaryrefslogtreecommitdiff
path: root/py/modules/IcePy/Util.h
diff options
context:
space:
mode:
Diffstat (limited to 'py/modules/IcePy/Util.h')
-rw-r--r--py/modules/IcePy/Util.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/py/modules/IcePy/Util.h b/py/modules/IcePy/Util.h
index 5520c1f07d9..5d390ccf88c 100644
--- a/py/modules/IcePy/Util.h
+++ b/py/modules/IcePy/Util.h
@@ -36,8 +36,13 @@ namespace IcePy
//
inline PyObject* getFalse()
{
+#if PY_VERSION_HEX >= 0x03000000
+ PyLongObject* i = &_Py_FalseStruct;
+ return reinterpret_cast<PyObject*>(i);
+#else
PyIntObject* i = &_Py_ZeroStruct;
return reinterpret_cast<PyObject*>(i);
+#endif
}
//
@@ -45,13 +50,28 @@ inline PyObject* getFalse()
//
inline PyObject* getTrue()
{
+#if PY_VERSION_HEX >= 0x03000000
+ PyLongObject* i = &_Py_TrueStruct;
+ return reinterpret_cast<PyObject*>(i);
+#else
PyIntObject* i = &_Py_TrueStruct;
return reinterpret_cast<PyObject*>(i);
+#endif
}
+//
+// Create a string object.
+//
inline PyObject* createString(const std::string& str)
{
+#if PY_VERSION_HEX >= 0x03000000
+ //
+ // PyUnicode_FromStringAndSize interprets the argument as UTF-8.
+ //
+ return PyUnicode_FromStringAndSize(str.c_str(), static_cast<Py_ssize_t>(str.size()));
+#else
return PyString_FromStringAndSize(str.c_str(), static_cast<Py_ssize_t>(str.size()));
+#endif
}
//
@@ -60,6 +80,18 @@ inline PyObject* createString(const std::string& str)
std::string getString(PyObject*);
//
+// Verify that the object is a string; None is NOT legal.
+//
+inline bool checkString(PyObject* p)
+{
+#if PY_VERSION_HEX >= 0x03000000
+ return PyUnicode_Check(p) ? true : false;
+#else
+ return PyString_Check(p) ? true : false;
+#endif
+}
+
+//
// Validate and retrieve a string argument; None is also legal.
//
bool getStringArg(PyObject*, const std::string&, std::string&);