bpo-29843: raise AttributeError if given negative _length_#10029
Conversation
|
Also |
|
The issue number looks wrong. |
Fixed. |
Co-authored-by: Oren Milman <orenmn@gmail.com>
ba7d522 to
e8d54b5
Compare
|
@serhiy-storchaka, I've addressed the exception handling and raised exception types as per your comment. Please take another look! |
Lib/ctypes/test/test_arrays.py
Outdated
| with self.assertRaises(OverflowError): | ||
|
|
||
| def test_bad_length(self): | ||
| import sys |
There was a problem hiding this comment.
It is imported at the top of the file.
| @@ -0,0 +1,4 @@ | |||
| Raise `ValueError` instead of `OverflowError` in case of a negative | |||
| ``_length_`` in a `ctypes.Array` subclass. Also raise `TypeError` | |||
| instead of `OverflowError` for non-integer ``_length_``. | |||
There was a problem hiding this comment.
"instead of AttributeError".
| @@ -0,0 +1,4 @@ | |||
| Raise `ValueError` instead of `OverflowError` in case of a negative | |||
There was a problem hiding this comment.
There are some issues with using the default role. It is better to use :exc:`ValueError` or ``ValueError`` or just remove mark up.
Modules/_ctypes/_ctypes.c
Outdated
| "which must be a positive integer"); | ||
| Py_XDECREF(length_attr); | ||
| if (!length_attr) { | ||
| if (!PyErr_Occurred() || |
There was a problem hiding this comment.
PyErr_Occurred() should always return true here. Remove this redundant check.
Modules/_ctypes/_ctypes.c
Outdated
| Py_XDECREF(length_attr); | ||
| if (!length_attr) { | ||
| if (!PyErr_Occurred() || | ||
| PyErr_ExceptionMatches(PyExc_AttributeError)) |
There was a problem hiding this comment.
It is worth to add similar check for _type_.
There was a problem hiding this comment.
I'll make a new PR for that. Should I also make a new issue?
|
@serhiy-storchaka, I've made the requested changes. |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Thank you. LGTM. Just a tiny nit.
Modules/_ctypes/_ctypes.c
Outdated
| "which must be a positive integer"); | ||
| Py_XDECREF(length_attr); | ||
| if (!length_attr) { | ||
| if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
There was a problem hiding this comment.
For PEP 7 put { on the same line as if.
Modules/_ctypes/_ctypes.c
Outdated
| "which must be a positive integer"); | ||
| Py_XDECREF(length_attr); | ||
| if (!length_attr) { | ||
| if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
There was a problem hiding this comment.
For PEP 7 put { on the same line as if. It should be on a new line only for multiline conditions.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Thank you. LGTM. Just a tiny nit.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Thank you. LGTM. Just a tiny nit.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
LGTM. Just a tiny nit.
Modules/_ctypes/_ctypes.c
Outdated
| "which must be a positive integer"); | ||
| Py_XDECREF(length_attr); | ||
| if (!length_attr) { | ||
| if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
There was a problem hiding this comment.
For PEP 7 put { on the same line as if. It should be on a new line only for multiline conditions.
Modules/_ctypes/_ctypes.c
Outdated
| "which must be a positive integer"); | ||
| Py_XDECREF(length_attr); | ||
| if (!length_attr) { | ||
| if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
There was a problem hiding this comment.
For PEP 7 put { on the same line as if. It should be on a new line only for multiline conditions.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
LGTM. Just a nitpick.
|
Should this be backported, and if so, how far back? |
|
|
Hum, GitHub is sick today. I approved the PR again :-) |
|
All right until GitHub allow to merge the PR twice. 😉 |
This is based on @orenmn's PR #3822.
The tests are the same, but this uses
_PyLong_Sign()to check for negative values rather thanPyLong_AsLongAndOverflow().This raises
AttributeErrorsince the existing code already raises that for other invalid values (non-integers).https://bugs.python.org/issue29843