diff options
author | Mark Spruiell <mes@zeroc.com> | 2012-04-24 14:16:15 -0700 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2012-04-24 14:16:15 -0700 |
commit | 943a48fc5c0a59b892eb746073c71b8dd88815e7 (patch) | |
tree | 421cfedbc60603d02e0b314d9204e9f85dd781c5 /py/modules/IcePy/Util.h | |
parent | minor fix to IcePHP getLogger (diff) | |
download | ice-943a48fc5c0a59b892eb746073c71b8dd88815e7.tar.bz2 ice-943a48fc5c0a59b892eb746073c71b8dd88815e7.tar.xz ice-943a48fc5c0a59b892eb746073c71b8dd88815e7.zip |
python 3 support
Diffstat (limited to 'py/modules/IcePy/Util.h')
-rw-r--r-- | py/modules/IcePy/Util.h | 32 |
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&); |