This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: _ctypes_simple_instance returns inverted logic
Type: enhancement Stage: patch review
Components: ctypes Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: hydroflask
Priority: normal Keywords: patch

Created on 2022-02-09 22:05 by hydroflask, last changed 2022-04-11 14:59 by admin.

File name Uploaded Description Edit
_ctypes_simple_instance_inverted.patch hydroflask, 2022-02-09 22:05
Messages (2)
msg412947 - (view) Author: (hydroflask) Date: 2022-02-09 22:05
`_ctypes_simple_instance` in _ctypes.c returns the opposite logic of what its documentation claims. It is supposed to return true when the argument (a type object) is a direct subclass of `Simple_Type` (`_SimpleCData` in Python code). However it returns false instead.

No bugs have manifested from this because all of the call sites ( `callproc.c::GetResult`, `callbacks.c::_CallPythonObject`,_`ctypes.c::PyCData_get`, `_ctypes.c::Simple_from_outparm`) invert the return value of this function. The last example, `ctypes.c::Simple_from_outparm` only calls `Simple_get_value()` when `_ctypes_simple_instance` returns false, which makes sense because otherwise the invocation of `_ctypes.c::Simple_from_outparm()` could trigger an assertion error.

This is not just simply an issue of inverted logic because the logic isn't inverted in all cases. In `_ctypes_simple_instance` in the case when `PyCSimpleTypeObject_Check(type)` returns false, if this were supposed to be perfect inverted logic then the whole routine would return 1 (True) not 0. Fortunately, due to the way the code is structured, I don't think there is a case when `PyCSimpleTypeObject_Check(type)` returns false so the incorrect case where it returns a constant 0 is effectively dead code.

I have compiled a version of Python with the attached patch and run "make test" with no issues.
msg412948 - (view) Author: (hydroflask) Date: 2022-02-09 22:13
I place that patch into the public domain, I claim no ownership over that patch. The patch is attached purely for demonstration purposes.
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90855
2022-02-09 22:15:04eryksunsetstage: patch review
versions: - Python 3.7, Python 3.8
2022-02-09 22:13:20hydroflasksetmessages: + msg412948
2022-02-09 22:05:40hydroflaskcreate