Message254052
I prefer to merge issue24802 and issue24803 and discuss them at one place.
Here is merged and revised patch.
The patch is changed. There is a very rare corner case: when the type of argument is a subclass of bytes or bytearray with overloaded tp_as_buffer->bf_getbuffer, returned view can be not NUL terminated. To avoid ambiguity and for unifying with int constructor, I have added special cases for bytes and bytearray (this is also restores pre-issue22896 behavior for int(bytes, base)).
Tests are moved and added additional tests for memoryview slices.
>>> int(memoryview(b'123')[1:3])
23
>>> int(memoryview(b'123\x00')[1:3])
23
>>> int(memoryview(b'123 ')[1:3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: b'23'
>>> int(memoryview(b'123A')[1:3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: b'23'
>>> int(memoryview(b'1234')[1:3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: b'23'
>>> float(memoryview(b'12.3')[1:4])
2.3
>>> float(memoryview(b'12.3\x00')[1:4])
2.3
>>> float(memoryview(b'12.3 ')[1:4])
2.3
>>> float(memoryview(b'12.3A')[1:4])
2.3
>>> float(memoryview(b'12.34')[1:4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: <memory at 0xb6fee02c>
There is similar dangerously looking code for complex. But it is never executed, because complex accepts only one non-numeric type: str. The patch removes this misleading dead code. |
|
Date |
User |
Action |
Args |
2015-11-04 13:55:12 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, mark.dickinson, eric.smith, JohnLeitch |
2015-11-04 13:55:11 | serhiy.storchaka | set | messageid: <1446645311.99.0.31330481373.issue24802@psf.upfronthosting.co.za> |
2015-11-04 13:55:11 | serhiy.storchaka | link | issue24802 messages |
2015-11-04 13:55:11 | serhiy.storchaka | create | |
|