New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mmap: read_byte/write_byte and object type #49641
Comments
On Python3000, mmap.read_byte returns str not bytes, and mmap.write_byte >>> import mmap
>>> m = mmap.mmap(-1, 10)
>>> type(m.read_byte())
<class 'str'>
>>> m.write_byte("a")
>>> m.write_byte(b"a") Maybe another possibility. read_byte() returns int which represents |
Indeed, I think it should use the "b" code, instead of the "c" code. It might not be ok to backport this to 3.0, since it may break existing |
Furthermore, all other uses of the "c" code might need to be reconsidered. |
loewis> Furthermore, all other uses of the "c" code might $ grep 'BuildValue.*"c"' */*c
Modules/_cursesmodule.c: return Py_BuildValue("c", rtn);
Modules/mmapmodule.c: return Py_BuildValue("c", value);
$ grep '_Parse[^"]\+"[^":;]*c' */*c
Modules/mmapmodule.c: if (!PyArg_ParseTuple(args, "c:write_byte",
&value))
PC/msvcrtmodule.c: if (!PyArg_ParseTuple(args, "c:putch", &ch))
PC/msvcrtmodule.c: if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) So we have:
Notes: msvcrt.putwch() accepts string of length > 1 and |
I think more *bytes* cleanup is needed for mmap module documentation & >>> import mmap
>>> m = mmap.mmap(-1, 10)
>>> m[:] = b"0123456789"
>>> m.find(b'2')
2
>>> m.find('2') # XXX: accepts unicode
2 |
Patch attached. read_byte and write_byte use integer as byte, and other |
In the python-dev thread, most people agree to use bytes in mmap. Did |
Well, there is no conclusion which of your choices (a or b) is preferred.
|
Le Tuesday 17 March 2009 14:39:59 Hirokazu Yamamoto, vous avez écrit :
Guido just wrote in other mail thread (py3k: accept unicode for 'c' and byte "Yeah, mmap should be defined exclusively in terms of bytes."
Cool, thanks.
Sure, we need a review of the patch. Should the patch be ported to 3.0? |
Ah, no, this should not be backported to 3.0. Martin saids so in |
STINNER Victor wrote:
How does that answer the question? We know what data type to use for |
Hum, what was the question? :-) Quote of my email: « About m.read_byte(), we have two choices: About m.write_byte(x), we have also two choices: (b) choices are close to Python 2.x API. But we can already use Oh, I though that the question was about bytes vs str :-/ Ocean-city |
I also think that ints should be used to represent individual bytes. However, your list of alternatives is incomplete: we *could* also change |
martin> However, your list of alternatives is incomplete: we That's extactly the idea that I proposed in issue bpo-5499 ;-) I prefer |
@Ocean-City: Can you update your patch to leave |
Yes, here is the patch. But I noticed Py_BuildValue('c') still returns |
So <mmap object>.read_byte() gives a byte string of 1 byte, ok. Port The patch looks correct, thanks for updating it. We know have to wait |
Yes, you can do I'll update "with getarg('b') version" to compare. |
bpo-5666 is closed. I finally prefers getarg('c') (byte string of lenght 1). |
Fixed in r71174. |
With this patch, read_byte returns an integer in the range -128 to 127 instead of 0 to 255 if char is signed. Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 is affected by this. I think it is a bug. The test code would fail if the test string contained any bytes outside the ASCII range. (Did this really go unnoticed for a year and a half? I noticed it the moment I first tried to use read_byte (which was just now). I see that read_byte was broken in a different way in 3.0. Does anybody actually use it?) |
Thank for pointing this out. I've looked at bytearray and |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: