Skip to content

[<=3.14] ctypes raises plain Exception instead of more suitable SystemError when the version claimed by _ctypes does not match expectations #146549

@jonathandung

Description

@jonathandung

Bug report

Bug description:

In the ctypes module source prior to python 3.15, which deprecated the __version__ attribute of the ctypes module and set it to be removed in 3.20,
the __version__ attribute from _ctypes was imported and checked against the version the python module thinks it's on ("1.1.0"). If they were unequal, this is treated as an internal inconsistency and an error is raised here:

from _ctypes import __version__ as _ctypes_version
from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
from _ctypes import ArgumentError
from _ctypes import SIZEOF_TIME_T
from _ctypes import CField

from struct import calcsize as _calcsize

if __version__ != _ctypes_version:
    raise Exception("Version number mismatch", __version__, _ctypes_version)

This should obviously be a SystemError instead, since it shouldn't happen, is essentially unrecoverable (python source builds on C source) and a plain Exception could be accidentally caught by wildcard except Exception as e: blocks.

I have opened a PR to change this on the 3.14 branch. I believe it should be backported to 3.13 as well.

Of course, the same issue is present in previous versions too, but I thought it unwise to include since they are past their maintenance periods.

CPython versions tested on:

3.14, 3.13

Operating systems tested on:

Windows, Linux, macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions