msg161414 - (view) |
Author: Larry Hastings (larry) *  |
Date: 2012-05-23 14:39 |
If you pass a valid PyUnicodeObject into PyUnicode_AsObject(), it incref's the original object and returns it. If you pass a valid PyBytesObject into PyBytes_AsObject()... it fails. I assert that in the PyBytes_AsObject() should behave like PyUnicode_AsObject() when faced with an identity transformation. (Brett: I tagged you because I saw your name in the comments, so I figured you were a good candidate.) |
|
|
msg161418 - (view) |
Author: Larry Hastings (larry) *  |
Date: 2012-05-23 15:26 |
The appropriate four line patch. (Six with whitespace.) |
|
|
msg161420 - (view) |
Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) *  |
Date: 2012-05-23 15:29 |
I suppose you are referring to PyUnicode_FromObject() and PyBytes_FromObject()... And by "it fails" did you simply mean "it fails to return the same object"? |
|
|
msg161422 - (view) |
Author: Larry Hastings (larry) *  |
Date: 2012-05-23 15:30 |
1) Yes, whoopsies. It's late. 2) It fails, as in, it returns NULL. |
|
|
msg161423 - (view) |
Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) *  |
Date: 2012-05-23 15:32 |
> 2) It fails, as in, it returns NULL. It's not my experience. Do you have an example? |
|
|
msg161424 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2012-05-23 15:34 |
Well, the patch is nice but the PyObject_CheckBuffer(...) part should have succeeded, so it's a bit mysterious why it doesn't. |
|
|
msg161483 - (view) |
Author: Stefan Krah (skrah) *  |
Date: 2012-05-24 07:26 |
I also wonder how the buffer interface section can fail. PyBuffer_ToContiguous() should translate to a simple memcpy() for a bytes object. What is going on? |
|
|
msg161485 - (view) |
Author: Larry Hastings (larry) *  |
Date: 2012-05-24 08:06 |
I can't reproduce this, and it was pretty late in my day when I saw it, so let's assume I was mistaken and PyBytes_CheckBuffer() works fine. Nevertheless I think the patch is a good idea--why create a new object when you don't have to? Unless I hear otherwise I'll check it in in the next day or so. As for what I saw, I suspect it was a deliberate TypeError from the regression test suite, passing in an integer 0 as a filename to os.rename(). Naturally PyBytes_FromObject() fails to convert that to a bytes object. |
|
|
msg161551 - (view) |
Author: Larry Hastings (larry) *  |
Date: 2012-05-25 05:41 |
Changing the name of the report to accurately reflect reality. If you passed in a bytes object, PyBytes_FromObject would create a new object, when all it really needed to do was incref and return. My checkin in a minute will add that shortcut. |
|
|
msg161552 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2012-05-25 05:58 |
New changeset f8cd75e8a1d6 by Larry Hastings in branch 'default': Issue #14889: PyBytes_FromObject(bytes) now just increfs and returns. http://hg.python.org/cpython/rev/f8cd75e8a1d6 |
|
|