Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regresson(?) in Python 3.11: ctypes.addressof(...) no longer works on ctypes function type #100397

Closed
ell1e opened this issue Dec 21, 2022 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@ell1e
Copy link

ell1e commented Dec 21, 2022

Bug report

This used to work:

        print("THE TYPE: " + str(
            (sdlttf_funcs["TTF_SizeUTF8"],
            type(sdlttf_funcs["TTF_SizeUTF8"]))))
        _sdl_TextSize_utf8_addrint = (
            ctypes.addressof(  # this is line 84 in the backtrace
            sdlttf_funcs["TTF_SizeUTF8"]))

Now it doesn't, output:

THE TYPE: (<function TTF_SizeUTF8 at 0x7fc0e01d09a0>, <class 'function'>)
Traceback (most recent call last):
  File "/home/user/Develop/mypen/./main.py", line 20, in <module>
    main()
  File "/home/user/.local/lib/python3.11/site-packages/mypen/main.py", line 144, in main
    show_document_list(window)
  File "/home/user/.local/lib/python3.11/site-packages/mypen/uiwindowdocumentlist.py", line 542, in show_document_list
    set_current_ui(DocumentOpenUi(window))
  File "/home/user/.local/lib/python3.11/site-packages/mypen/ui.py", line 238, in set_current_ui
    set_cleared_ui(window)
  File "/home/user/.local/lib/python3.11/site-packages/mypen/ui.py", line 52, in set_cleared_ui
    top_label = Label()
                ^^^^^^^
  File "src/myui/label.pyx", line 84, in myui.label.Label.__init__
  File "src/myui/font/manager.pyx", line 100, in myui.font.manager.Font.render_size
  File "src/myui/font/sdlfont.pyx", line 216, in myui.font.sdlfont.get_thread_safe_render_size
  File "src/myui/font/sdlfont.pyx", line 84, in myui.font.sdlfont._get_font_size_fast_unthreaded
TypeError: invalid type

This is important to get a function from ctypes into a Cython function pointer: https://stackoverflow.com/questions/49635105/ctypes-get-the-actual-address-of-a-c-function

(Which is needed in some situations for performance reasons)

Your environment

  • CPython versions tested on: Python 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux
  • Operating system and architecture: Fedora 37
  • Cython used: dev code from commit b2cad768499cf5763251fe757491e9fc24a1f583 (the latest stable doesn't support Python 3.11)

I'm not 100% sure this isn't a regression in Cython instead, I can't easily test that since older Cython versions don't work with Python 3.11. It looks to me like the ctypes function ctypes.addressof is emitting the error and the source of the problem, but who knows I guess.

@ell1e ell1e added the type-bug An unexpected behavior, bug, or error label Dec 21, 2022
@hugovk
Copy link
Member

hugovk commented Dec 21, 2022

What is sdlttf_funcs?

$ python 1.py
Traceback (most recent call last):
  File "/private/tmp/1.py", line 2, in <module>
    (sdlttf_funcs["TTF_SizeUTF8"],
     ^^^^^^^^^^^^
NameError: name 'sdlttf_funcs' is not defined

Please can you share a minimal, reproducible example?

Tips: https://stackoverflow.com/help/minimal-reproducible-example

@ell1e
Copy link
Author

ell1e commented Dec 21, 2022

(needs pip3 install --user -U PySDL2 as a dependency)

#!/usr/bin/python3

import ctypes

def bla():
    import sdl2.sdlttf as sdlttf
    sdlttf_funcs = {}
    try:
        from sdl2.sdlttf import _funcs as sdlttf_funcs_module
        sdlttf_funcs["TTF_SizeUTF8"] = \
            sdlttf_funcs_module["TTF_SizeUTF8"]
    except ImportError:
        sdlttf_funcs["TTF_SizeUTF8"] = \
            sdlttf.TTF_SizeUTF8
    print("TEST TYPE: " + str(
        (sdlttf_funcs["TTF_SizeUTF8"],
        type(sdlttf_funcs["TTF_SizeUTF8"]))))
    _sdl_TextSize_utf8_addrint = (
        ctypes.addressof(
        sdlttf_funcs["TTF_SizeUTF8"]))

bla()

@ell1e
Copy link
Author

ell1e commented Dec 21, 2022

Nevermind, I'm silly. This is PySDL2 breaking things again without a deprecation warning, I think.

@ell1e ell1e closed this as completed Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants