Commit d6f7466d authored by David Gobbi's avatar David Gobbi
Browse files

Fix typo PYTHON_VERSION_HEX to PY_VERSION_HEX

A typo in vtkPythonArgs.cxx resulted in the use of a "char *" from a
deleted Python object.  I've fixed the typo and have completely
removed the code that can give rise to the invalid pointer.

This commit also changes a BuildBytes to actually build a Bytes object
on Python 3 (it was building a string).

Under Python 3.2, the default locale encoding will be used for 8-bit
strings rather than utf8 (similar to Python 2).  It wasn't until 3.3
that unicode objects kept a cached copy of the utf8 string.
parent 1ab26ad6
......@@ -98,21 +98,14 @@ bool vtkPythonGetStringValue(PyObject *o, T *&a, const char *exctext)
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(o))
{
#if PYTHON_VERSION_HEX >= 0x03030000
#if PY_VERSION_HEX >= 0x03030000
a = PyUnicode_AsUTF8(o);
return true;
#else
#ifdef _PyUnicode_AsDefaultEncodedString
PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
#else
PyObject *s = PyUnicode_AsEncodedString(o, 0, NULL);
#endif
if (s)
{
a = PyBytes_AS_STRING(s);
#ifndef _PyUnicode_AsDefaultEncodedString
Py_DECREF(s);
#endif
return true;
}
......@@ -138,26 +131,19 @@ inline bool vtkPythonGetStdStringValue(PyObject *o, std::string &a, const char *
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(o))
{
#if PYTHON_VERSION_HEX >= 0x03030000
#if PY_VERSION_HEX >= 0x03030000
Py_ssize_t len;
const char* val = PyUnicode_AsUTF8AndSize(o, &len);
a = std::string(val, len);
return true;
#else
#ifdef _PyUnicode_AsDefaultEncodedString
PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
#else
PyObject *s = PyUnicode_AsEncodedString(o, 0, NULL);
#endif
if (s)
{
char* val;
Py_ssize_t len;
PyBytes_AsStringAndSize(s, &val, &len);
a = std::string(val, len);
#ifndef _PyUnicode_AsDefaultEncodedString
Py_DECREF(s);
#endif
return true;
}
......
......@@ -711,7 +711,13 @@ PyObject *vtkPythonArgs::BuildValue(const char *a)
{
if (a)
{
#if PY_VERSION_HEX >= 0x03030000
return PyUnicode_FromString(a);
#elif PY_VERSION_HEX >= 0x03000000
return PyUnicode_Decode(a, static_cast<Py_ssize_t>(strlen(a)), NULL, NULL);
#else
return PyString_FromString(a);
#endif
}
Py_INCREF(Py_None);
return Py_None;
......@@ -720,7 +726,13 @@ PyObject *vtkPythonArgs::BuildValue(const char *a)
inline
PyObject *vtkPythonArgs::BuildValue(const std::string &a)
{
#if PY_VERSION_HEX >= 0x03030000
return PyUnicode_FromStringAndSize(a.c_str(), static_cast<Py_ssize_t>(a.size()));
#elif PY_VERSION_HEX >= 0x03000000
return PyUnicode_Decode(a.c_str(), static_cast<Py_ssize_t>(a.size()), NULL, NULL);
#else
return PyString_FromStringAndSize(a.c_str(), static_cast<Py_ssize_t>(a.size()));
#endif
}
inline
......@@ -823,7 +835,7 @@ PyObject *vtkPythonArgs::BuildValue(unsigned __int64 a)
inline
PyObject *vtkPythonArgs::BuildBytes(const char *a, int n)
{
return PyString_FromStringAndSize(a, n);
return PyBytes_FromStringAndSize(a, n);
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment