-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Description
Documentation
Background
I am one of the maintainers of comtypes
. comtypes
is based on ctypes
and uses metaclasses to implement IUnknown
.
It was reported to the comtypes
community that an error occurs when attempting to use conventional metaclasses with Python 3.13.
A similar error was encountered in pyglet
, which also uses metaclasses to implement COM interfaces, when running on Python 3.13.
By referring to pyglet/pyglet#1196 and pyglet/pyglet#1199, I made several modifications to the code through trial and error, and now comtypes
works in both Python 3.13 and earlier versions without problems:
- https://github.com/enthought/comtypes/compare/a3a8733..04b766a
- It is necessary to pass arguments directly to the metaclass instead of using
__new__
in places where the metaclass is instantiated (i.e., where the class is dynamically defined). This also works in versions prior to Python 3.13. - After calling
type.__new__
, any remaining initialization would be handled in__init__
instead of in__new__
. Since this results in an error in versions prior to Python 3.13, a bridge usingsys.version_info
is necessary.
- It is necessary to pass arguments directly to the metaclass instead of using
I think these changes are likely related to #114314 and #117142 and were introduced by the PRs linked to those issues.
Since this change to ctypes
breaks compatibility, I think it should be mentioned in the What’s New In Python 3.13 and/or in the ctypes
documentation.
There are likely other projects besides comtypes
and pyglet
that rely on the combination of ctypes
and metaclasses, and I want to prevent confusion for those maintainers when they try to support Python 3.13.
(Additionally, I would like to ask with the ctypes
maintainers to confirm whether the changes for the metaclasses in comtypes
(and pyglet
) are appropriate.)
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
Status
Activity
AA-Turner commentedon Sep 25, 2024
cc @vstinner @ericsnowcurrently
encukou commentedon Sep 25, 2024
Thanks for reporting this! If I knew about this earlier I'd try to make your life easier, but at this point it looks like a docs change is the best way to go.
I'm afraid that by using
type(c_void_p)
to get toctypes.PyCSimpleType
internals, and by importing_pointer_type_cache
, you're getting in the realm of things that can change.Unfortunately,
ctypes
has been... stable? neglected? ... for a long time, and these workarounds are common in the wild.If you can, please do plan to test with 3.14 as the alphas/betas come out, and ping me on any other incompatibilities you might find.
For the diff you linked:
Unfortunately I don't have a Windows box to test on. What error are you getting if you put all of the logic in
__init__
? (Is it__init__() should return None
? Don't returnself
from__init__
.)If the if/else is needed, I recommend putting the logic in common methods like
_new
and_init
to remove the duplication, so the branch becomes something like:or even:
encukou commentedon Sep 25, 2024
Hm, I don't understand this point. Do you have an example?
pythongh-124520: What's New for ctypes metaclass __new__/__init__ change
junkmd commentedon Sep 25, 2024
In
comtypes
, the following part corresponds to this.https://github.com/enthought/comtypes/compare/c631f97..6f036d4
enthought/comtypes#618 (comment)
junkmd commentedon Sep 25, 2024
The following error occurs in Python 3.11 when making the changes mentioned above.
encukou commentedon Sep 25, 2024
Thanks!
I won't be able to debug that by reading the code. I'll follow up next week when I get back to a Windows PC.
junkmd commentedon Sep 25, 2024
Thank you.
Fortunately, libraries like
comtypes
andpyglet
were able to adapt with changes to the codebase.However, if there is a codebase that performs more complex pre-processing before calling
super().__new__
within__new__
, it might need more complex workarounds.If it is still possible to modify things so that performing all initialization in
__new__
does not result in an error, that would be great.Alternatively, documenting a workaround for such cases would be helpful for maintainers of those kinds of packages.
31 remaining items