This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author scoder
Recipients BreamoreBoy, scoder
Date 2010-08-20.16:43:07
SpamBayes Score 5.896054e-10
Marked as misclassified No
Message-id <1282322591.87.0.421715128269.issue7415@psf.upfronthosting.co.za>
In-reply-to
Content
Here's a patch against the latest py3k. The following will call the new code, for example:

  str(memoryview(b'abc'), 'ASCII')

whereas bytes and bytesarray continue to use their own special casing code (which has also changed a bit since I wanted to avoid code duplication).

For testing, I wrote a short Cython module that implements the buffer protocol in an extension type and freshly allocates a new bytes object as buffer on each access:

  from cpython.ref cimport Py_INCREF, Py_DECREF, PyObject

  cdef class Test:
      def __getbuffer__(self, Py_buffer* buffer, int flags):
          s = b'abcdefg' * 10
          buffer.buf = <char*> s
          buffer.obj = self
          buffer.len = len(s)
          Py_INCREF(s)
          buffer.internal = <void*> s

      def __releasebuffer__(self, Py_buffer* buffer):
          Py_DECREF(<object>buffer.internal)

Put it into a file "buftest.pyx", build it, start up Python 3.x and call

    >>> import buftest
    >>> print(len( str(buftest.Test(), "ASCII") ))

Under the unpatched Py3, this raises a decoding exception for me when it tries to decode data from the deallocated bytes object. Other systems may happily crash here. The patched Python runtime prints '70' as expected.
History
Date User Action Args
2010-08-20 16:43:12scodersetrecipients: + scoder, BreamoreBoy
2010-08-20 16:43:11scodersetmessageid: <1282322591.87.0.421715128269.issue7415@psf.upfronthosting.co.za>
2010-08-20 16:43:10scoderlinkissue7415 messages
2010-08-20 16:43:08scodercreate