Julian Andres Klode <j...@jak-linux.org> added the comment:

The problem I see here is that there is no public way to simply get a C string 
from a unicode object similar to PyBytes_AsString() for bytes. That's bad 
because we don't want to rewrite the whole code to duplicate strings all the 
time and free every string we get from a MyPyUnicode_AsString() like function.

I used the following, but this clearly has a memory leak:


  static const char *MyPyUnicode_AsString(PyObject *op) {
      PyObject *bytes = PyUnicode_AsEncodedString(op,0,0);
      return bytes ? PyBytes_AS_STRING(bytes) : 0;
  }

I now use the following which has no memory leak, but needs an internal 
function (I would use _PyUnicode_AsString, but I need Python 2.X compatibility 
as well):

  static const char *MyPyUnicode_AsString(PyObject *op) {
      PyObject *bytes = _PyUnicode_AsDefaultEncodedString(op, 0);
      return bytes ? PyBytes_AS_STRING(bytes) : 0;
  }

So could something be done about this?

----------
nosy: +jak

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue2799>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to