Skip to content

Commit

Permalink
Figured out the problem (see comment on pull request) and fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
tfpf committed Dec 21, 2024
1 parent 55cf057 commit 11b01b9
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/pysorteddict/pysorteddict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,46 @@
#include <string>

/**
* Convert a Python string into a C string.
* Convert a Python object into a C++ string.
*
* @param unicode Python string.
* @param ob Python object.
*
* @return C string possibly containing null bytes in the middle.
* @return C++ string possibly truncated at the first null byte.
*/
char const* to_string(PyObject* unicode)
std::string to_string(PyObject* ob, PyObject* (*stringifier)(PyObject*))
{
PyObject* unicode = stringifier(ob); // New reference.
if (unicode == nullptr)
{
return nullptr;
}
static thread_local std::string result;
result = PyUnicode_AsUTF8(unicode);
std::string result = PyUnicode_AsUTF8(unicode);
Py_DECREF(unicode);
return result.data();
return result;
}

/**
* Obtain the Python representation of a Python object.
*
* @param ob Python object.
*
* @return C string possibly containing null bytes in the middle.
* @return C++ string possibly truncated at the first null byte.
*/
char const* repr(PyObject* ob)
std::string repr(PyObject* ob)
{
PyObject* ob_repr = PyObject_Repr(ob); // New reference.
return to_string(ob_repr);
return to_string(ob, PyObject_Repr);
}

/**
* Obtain a human-readable string representation of a Python object.
*
* @param ob Python object.
*
* @return C string possibly containing null bytes in the middle.
* @return C++ string possibly truncated at the first null byte.
*/
char const* str(PyObject* ob)
std::string str(PyObject* ob)
{
PyObject* ob_str = PyObject_Str(ob); // New reference.
return to_string(ob_str);
return to_string(ob, PyObject_Str);
}

/**
Expand Down

0 comments on commit 11b01b9

Please sign in to comment.