Skip to content

Commit

Permalink
Merge pull request #118 from thp/python33-unicode-string
Browse files Browse the repository at this point in the history
Python 3.3 unicode strings
  • Loading branch information
thp committed Jun 6, 2021
2 parents 16aa5ad + 3b0c325 commit df80d64
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
6 changes: 6 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,12 @@ Known Problems:
ChangeLog
=========

Version UNRELEASED (YYYY-MM-DD)
-------------------------------

* Use `PyUnicode_AsUTF8` from Python 3.3 when converting strings; strings returned
from the converter are now valid as long as the `PyObject` is alive (previously
they were valid until the next string conversion or until converter was destroyed)

Version 1.5.9 (2020-01-17)
--------------------------
Expand Down
17 changes: 2 additions & 15 deletions src/pyobject_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,13 @@ class PyObjectDictIterator : public DictIterator<PyObject *> {

class PyObjectConverter : public Converter<PyObject *> {
public:
PyObjectConverter() : stringcontainer(NULL) {
PyObjectConverter() {
if (!PyDateTimeAPI) {
PyDateTime_IMPORT;
}
}

virtual ~PyObjectConverter() {
if (stringcontainer != NULL) {
Py_DECREF(stringcontainer);
}
}

virtual enum Type type(PyObject * const & o) {
Expand Down Expand Up @@ -176,14 +173,7 @@ class PyObjectConverter : public Converter<PyObject *> {
virtual long long integer(PyObject *&o) { return PyLong_AsLongLong(o); }
virtual double floating(PyObject *&o) { return PyFloat_AsDouble(o); }
virtual bool boolean(PyObject *&o) { return (o == Py_True); }
virtual const char *string(PyObject *&o) {
// XXX: In Python 3.3, we can use PyUnicode_UTF8()
if (stringcontainer != NULL) {
Py_DECREF(stringcontainer);
}
stringcontainer = PyUnicode_AsUTF8String(o);
return PyBytes_AsString(stringcontainer);
}
virtual const char *string(PyObject *&o) { return PyUnicode_AsUTF8(o); }
virtual QByteArray bytes(PyObject *&o) {
return QByteArray(PyBytes_AsString(o), PyBytes_Size(o));
}
Expand Down Expand Up @@ -238,9 +228,6 @@ class PyObjectConverter : public Converter<PyObject *> {
virtual ListBuilder<PyObject *> *newList() { return new PyObjectListBuilder(); }
virtual DictBuilder<PyObject *> *newDict() { return new PyObjectDictBuilder(); }
virtual PyObject * none() { Py_RETURN_NONE; }

private:
PyObject *stringcontainer;
};

#endif /* PYOTHERSIDE_PYOBJECT_CONVERTER_H */

0 comments on commit df80d64

Please sign in to comment.