You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
errors raised by ctypes.Array for invalid _length_ attribute #74029
currently:
>>> from ctypes import*
>>> classT(Array):
... _type_ = c_int
... _length_ =-1
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: array too large
>>> classT(Array):
... _type_ = c_int
... _length_ =-1<<1000
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: The '_length_' attribute is too large
Obviously, here the _length_ attribute is too small, not too large.
Thus, the error messages should be changed to be more accurate (optimally, for
any negative _length_, the error message should be the same).
Moreover, in accordance with bpo-29833 (this is a sub-issue of bpo-29833), ValueError
should be raised for any negative _length_ attribute (instead of
OverflowError).
Also, Note that currently, in case _length_ == 0, no error is raised. ISTM that
a ctypes Array of length 0 is useless, so maybe we should raise a ValueError in
this case too?
A patch draft (which doesn't change anything about the case of _length_ == 0)
is attached.
(I am not opening a PR, because I am not sure the behavior change would be
accepted.)
I ran the test module on my Windows 10, and it seems the patch doesn't break
anything.
LGTM, but I prefer overflow > 0 over overflow == 1.
If use _testcapi the tests should be decorated with cpython_only. But I think that it is better to not use it. Limiting _length_ to C long (rather than size_t) is an implementation detail. The test with _length_ = 1 << 1000 should be enough.
orenmn commented on Mar 18, 2017
orenmnmannequin
MannequinAuthor
More actions
"If use _testcapi the tests should be decorated with cpython_only."
at first, I thought so too, but then I searched for 'cpython_only' in Lib/ctypes/test, and found nothing. then I searched for '_testcapi' there,
and found a top level 'import _testcapi' in Lib/ctypes/test/test_structures.py, and a test using _testcapi.INT_MAX.
so I assumed that test_ctypes itself is cpython_only.
but regardless of this issue, shouldn't test_structures.py be changed
(in a seperate issue)?
ISTM it has a cpython-specific test not decorated in @cpython_only.
I compiled with https://github.com/python/cpython/pull/3006, and got:
classT(ctypes.Array):
_type_ = ctypes.c_int
_length_ =2**1000
Traceback (most recent call last):
File "<stdin>", line 1, in<module>OverflowError: Python int too large to convert to C ssize_t
and also:
classT(ctypes.Array):
_type_ = ctypes.c_int
_length_ =-1
Traceback (most recent call last):
File "<stdin>", line 1, in<module>OverflowError: array too large
i3v commented on Aug 19, 2017
i3vmannequin
Mannequin
More actions
Oren,
I might be completely wrong, but, personally, I think about OverflowError vs ValueError difference like this: if the value couldn't be handled because method's logic cannot handle it - it's a ValueError; if it could not be handled because of a low-level platform-dependent limitation - it's an OverflowError. Before that PR, the _length_ maximum value was hard-coded in the method itself, thus one might say that it was "a part of logic". With this PR, you just need a system with a large enough size_t.
(May be, after a thousand years, it would even handle 2**1000. But negative values would be still logically incorrect. Thus, I'm only talking about "too large" case.)
It would be much more difficult to run into this limitation in a daily practice (e.g. by passing a very long string).
Activity
orenmn commentedon Mar 18, 2017
With regard to ctypes.Array:
Obviously, here the _length_ attribute is too small, not too large.
Thus, the error messages should be changed to be more accurate (optimally, for
any negative _length_, the error message should be the same).
Moreover, in accordance with bpo-29833 (this is a sub-issue of bpo-29833), ValueError
should be raised for any negative _length_ attribute (instead of
OverflowError).
Also, Note that currently, in case _length_ == 0, no error is raised. ISTM that
a ctypes Array of length 0 is useless, so maybe we should raise a ValueError in
this case too?
orenmn commentedon Mar 18, 2017
A patch draft (which doesn't change anything about the case of _length_ == 0)
is attached.
(I am not opening a PR, because I am not sure the behavior change would be
accepted.)
I ran the test module on my Windows 10, and it seems the patch doesn't break
anything.
serhiy-storchaka commentedon Mar 18, 2017
LGTM, but I prefer
overflow > 0overoverflow == 1.If use _testcapi the tests should be decorated with cpython_only. But I think that it is better to not use it. Limiting _length_ to C long (rather than size_t) is an implementation detail. The test with _length_ = 1 << 1000 should be enough.
orenmn commentedon Mar 18, 2017
"If use _testcapi the tests should be decorated with cpython_only."
at first, I thought so too, but then I searched for 'cpython_only' in
Lib/ctypes/test, and found nothing. then I searched for '_testcapi' there,
and found a top level 'import _testcapi' in
Lib/ctypes/test/test_structures.py, and a test using _testcapi.INT_MAX.
so I assumed that test_ctypes itself is cpython_only.
should test_structures.py be changed, then?
serhiy-storchaka commentedon Mar 18, 2017
I suggest just remove the test with LONG_MAX.
orenmn commentedon Mar 18, 2017
yes, you are right, of course.
but regardless of this issue, shouldn't test_structures.py be changed
(in a seperate issue)?
ISTM it has a cpython-specific test not decorated in @cpython_only.
serhiy-storchaka commentedon Mar 18, 2017
I'm working on this. Right now I'm searching other tests that use _testcapi without guards.
serhiy-storchaka commentedon Mar 18, 2017
Opened bpo-29845 for _testcapi issues.
orenmn commentedon Mar 18, 2017
here is the patch updated according to your suggestions, Serhiy.
however, I wonder about the case of a too large _length_.
shouldn't we raise a MemoryError in such a case, in accordance with bpo-29833?
i3v commentedon Aug 17, 2017
Oren,
won't the "too large _length_" case vanish, if #3006 would be accepted ?
( http://bugs.python.org/issue16865 )
orenmn commentedon Aug 19, 2017
I am not sure I understood your question, Igor.
I compiled with https://github.com/python/cpython/pull/3006, and got: class T(ctypes.Array): _type_ = ctypes.c_int _length_ = 2 ** 1000 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: Python int too large to convert to C ssize_t and also: class T(ctypes.Array): _type_ = ctypes.c_int _length_ = -1 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: array too largei3v commentedon Aug 19, 2017
Oren,
I might be completely wrong, but, personally, I think about OverflowError vs ValueError difference like this: if the value couldn't be handled because method's logic cannot handle it - it's a ValueError; if it could not be handled because of a low-level platform-dependent limitation - it's an OverflowError. Before that PR, the _length_ maximum value was hard-coded in the method itself, thus one might say that it was "a part of logic". With this PR, you just need a system with a large enough size_t.
(May be, after a thousand years, it would even handle 2**1000. But negative values would be still logically incorrect. Thus, I'm only talking about "too large" case.)
It would be much more difficult to run into this limitation in a daily practice (e.g. by passing a very long string).
taleinat commentedon Oct 21, 2018
Should this be back-ported to 3.7 and 3.6? 2.7?
vstinner commentedon Oct 22, 2018
IMHO it's a bad idea to introduce a *new* exception in stable version. I suggest to only modify the master branch.
serhiy-storchaka commentedon Oct 22, 2018
This change is big enough now. I think it is better to not backport it.
taleinat commentedon Oct 22, 2018
Thanks for all of your work on this Oren!
vstinner commentedon Oct 22, 2018
( #10029 has been merged, but GitHub webhooks are paused and so no notification has been sent to this bug yet. )
taleinat commentedon Oct 22, 2018
New changeset 2447773 by Tal Einat in branch 'master':
bpo-29843: raise AttributeError if given negative _length_ (GH-10029)
2447773