Skip to content

Comments

gh-143304: Fix ctypes.CDLL to honor handle parameter on POSIX systems#143318

Merged
encukou merged 7 commits intopython:mainfrom
Koolvansh07:fix-ctypes-handle-argument
Feb 24, 2026
Merged

gh-143304: Fix ctypes.CDLL to honor handle parameter on POSIX systems#143318
encukou merged 7 commits intopython:mainfrom
Koolvansh07:fix-ctypes-handle-argument

Conversation

@Koolvansh07
Copy link
Contributor

The handle parameter was being ignored in the POSIX implementation of CDLL._load_library(), causing it to always call _dlopen() even when a valid handle was provided. This was a regression introduced in recent refactoring.

This PR adds the missing handle check to match the Windows implementation behavior, and includes a regression test.

Fixes gh-143304

The handle parameter was being ignored in the POSIX implementation
of CDLL._load_library(), causing it to always call _dlopen() even
when a valid handle was provided. This was a regression introduced
in recent refactoring.

This commit adds the missing handle check to match the Windows
implementation behavior, and includes a regression test.

Fixes pythongh-143304
@bedevere-app
Copy link

bedevere-app bot commented Dec 31, 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.

@python-cla-bot
Copy link

python-cla-bot bot commented Dec 31, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

picnixz
picnixz previously requested changes Jan 1, 2026
@bedevere-app
Copy link

bedevere-app bot commented Jan 1, 2026

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.

- Remove AI-generated comments from test
- Use skipIf decorator instead of runtime check
- Simplify NEWS entry (don't mention private _dlopen)
lib1 = CDLL(libc_name)
handle = lib1._handle
lib2 = CDLL(name=None, handle=handle)
self.assertIs(handle, lib2._handle)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.assertIs(handle, lib2._handle)
self.assertIs(lib2._handle, handle)

Can you check whether there were tests for Python 3.12 where we had this handle? or if the tests were rewritten as well (see the PRs mentioned on the issue).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked the 3.12 branch source code:

  1. Lib/ctypes/__init__.py in 3.12 DID support the handle parameter (it checked if handle is None:).
    1. Lib/test/test_ctypes/test_loading.py in 3.12 DID NOT have any test using CDLL(..., handle=...) on POSIX.

Copy link
Member

Choose a reason for hiding this comment

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

Ok thx!

The name parameter may be modified by .fwork and AIX processing,
so we need to process it before checking the handle.
Copy link
Contributor

@sharktide sharktide left a comment

Choose a reason for hiding this comment

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

LGTM
Thanks for your contribution @Koolvansh07

@Koolvansh07
Copy link
Contributor Author

LGTM Thanks for your contribution @Koolvansh07

your welcome

@serhiy-storchaka serhiy-storchaka requested a review from zooba January 2, 2026 14:47
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jan 2, 2026
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

I thought it was Windows-only parameter, but it is documented on Posix too.

LGTM. 👍

@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 23, 2026
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @encukou for commit cdb8f6e 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F143318%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 23, 2026
@encukou encukou merged commit 27ded24 into python:main Feb 24, 2026
133 of 135 checks passed
@miss-islington-app
Copy link

Thanks @Koolvansh07 for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 24, 2026
…ystems (pythonGH-143318)

The handle parameter was being ignored in the POSIX implementation
of CDLL._load_library(), causing it to always call _dlopen() even
when a valid handle was provided. This was a regression introduced
in recent refactoring.
(cherry picked from commit 27ded243485670fa836c9bb421e37a6ef16eca8e)

Co-authored-by: Arjit Singh Grover <143692910+Koolvansh07@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 24, 2026
…ystems (pythonGH-143318)

The handle parameter was being ignored in the POSIX implementation
of CDLL._load_library(), causing it to always call _dlopen() even
when a valid handle was provided. This was a regression introduced
in recent refactoring.
(cherry picked from commit 27ded24)

Co-authored-by: Arjit Singh Grover <143692910+Koolvansh07@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Feb 24, 2026

GH-145172 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Feb 24, 2026
@bedevere-app
Copy link

bedevere-app bot commented Feb 24, 2026

GH-145173 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

ctypes.CDLL does no longer honor the handle argument starting Python 3.13.10+ & 3.14.1+?

6 participants