Skip to content

Conversation

@CodeMaverick-143
Copy link

@CodeMaverick-143 CodeMaverick-143 commented Dec 2, 2025

Remove misleading comment about tuple support in as_parameter and add regression test

Modules/_ctypes/callproc.c contained a comment (marked XXX) stating that as_parameter allows constructing arbitrary tuples and passing them, describing this convention as "dangerous".

Analysis confirms that ctypes does not support returning tuples from as_parameter for default conversions. Attempting to do so raises a TypeError (wrapped in an ArgumentError), meaning the described security risk does not exist in the current codebase.

This PR includes:

  1. Removal of the misleading comment from Modules/_ctypes/callproc.c to avoid confusion.
  2. Addition of a regression test, test_as_parameter_tuple, in Lib/test/test_ctypes/test_parameters.py to verify that returning a tuple correctly raises a TypeError, ensuring this unsafe behavior remains disabled.

…t conversions in ctypes and add a test for the `TypeError` it raises.
@python-cla-bot
Copy link

python-cla-bot bot commented Dec 2, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Dec 2, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@CodeMaverick-143 CodeMaverick-143 changed the title fix: Explicitly disallow _as_parameter_ returning tuples for default conversions in ctypes and add a test for the TypeError it raises. Gh 142174: Explicitly disallow _as_parameter_ returning tuples for default conversions in ctypes and add a test for the TypeError it raises. Dec 2, 2025
@CodeMaverick-143 CodeMaverick-143 changed the title Gh 142174: Explicitly disallow _as_parameter_ returning tuples for default conversions in ctypes and add a test for the TypeError it raises. Gh-142174: Explicitly disallow _as_parameter_ returning tuples for default conversions in ctypes and add a test for the TypeError it raises. Dec 2, 2025
@CodeMaverick-143 CodeMaverick-143 changed the title Gh-142174: Explicitly disallow _as_parameter_ returning tuples for default conversions in ctypes and add a test for the TypeError it raises. Gh-142174: Explicitly disallow _as_parameter_ returning tuples Dec 2, 2025
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use LLMs to generate pull requests. See our AI policy.

Comment on lines +94 to +96
if (tstate && tstate->interp) {
REFTOTAL(tstate->interp) += n;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unrelated to this change and is incorrect. tstate and tstate->interp are never NULL here.


__attribute__((constructor)) void load_dyld_shared_cache_contains_path(void) {
libsystem_b_handle = dlopen("/usr/lib/libSystem.B.dylib", RTLD_LAZY);
libsystem_b_handle = dlopen("/usr/lib/libSystem.B.dylib", RTLD_LAZY | RTLD_GLOBAL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this looks unrelated as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't put your name in the filename. If you'd like to get credit, you can add something like "Patch by Your Name." at the end of the entry.

@@ -0,0 +1,2 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra newline.

Comment on lines +300 to +313
def test_as_parameter_tuple(self):
class Dangerous(object):
@property
def _as_parameter_(self):
return ('i', 42)

func = CDLL(_ctypes_test.__file__)._testfunc_p_p
func.restype = c_int
# func.argtypes = [c_void_p] # Do not set argtypes to force default conversion

# Should raise ArgumentError because tuples are not supported in default conversion
with self.assertRaisesRegex(ArgumentError, "argument 1: TypeError: Don't know how to convert parameter 1"):
func(Dangerous(), 0)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test already passes on main.

@bedevere-app
Copy link

bedevere-app bot commented Dec 2, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants