Skip to content

[C API] Private functions used by mypyc #121489

Description

@cdce8p

I've been working on adding support for 3.13 to mypyc. After updating the codebase, there are still some functions left we depend on which are now private after #106320. Would it make sense to export these again? That would make adopting 3.13 a bit easier. /CC @vstinner

  • _PyBytes_Join defined in pycore_bytesobject.h
  • _PyObject_CallMethodIdObjArgs defined in pycore_call.h
  • _PyType_CalculateMetaclass defined in pycore_object.h
  • _PyUnicode_EQ defined in pycore_unicodeobject.h
  • _PyUnicode_FastCopyCharacters defined in pycore_unicodeobject.h (this could be replaced with PyUnicode_CopyCharacters but it's used in a simplified copy of _PyUnicode_JoinArray which also uses _PyUnicode_FastCopyCharacters).

Other functions include these two. However, as they are inlined, it isn't a problem to include them.

  • _PyObject_CallMethodIdNoArgs and _PyObject_CallMethodIdOneArg defined in pycore_call.h

It would be fine to keep all of them in internal/pycore_* and just add PyAPI_FUNC.

/CC @hauntsaninja

Linked PRs

Activity

  1. vstinner commented on Jul 8, 2024

    @vstinner
    Member

    _PyBytes_Join defined in pycore_bytesobject.h

    You can call something like:

    PyObject *empty = PyBytes_FromString("");
    PyObject *res = PyObject_CallMethod(empty, "join", ...);
    Py_DECREF(empty);

    _PyObject_CallMethodIdObjArgs defined in pycore_call.h

    We are trying to get rid of _Py_IDENTIFIER() API. You should use your own mecanism for constant strings.

    You can call PyObject_CallMethodObjArgs().

    _PyType_CalculateMetaclass defined in pycore_object.h

    For this one, I don't know. You might reimplement it, use the internal C API, or we should expose it as a public C API.

    _PyUnicode_EQ defined in pycore_unicodeobject.h

    You can use PyUnicode_Compare(). Maybe we should expose it.

    _PyUnicode_FastCopyCharacters defined in pycore_unicodeobject.h (this could be replaced with PyUnicode_CopyCharacters but it's used in a simplified copy of _PyUnicode_JoinArray which also uses _PyUnicode_FastCopyCharacters).

    Would you mind to elaborate your use case? Can you use the new https://docs.python.org/dev/c-api/unicode.html#pyunicodewriter API instead?

  2. cdce8p commented on Jul 8, 2024

    @cdce8p
    ContributorAuthor

    _PyUnicode_FastCopyCharacters defined in pycore_unicodeobject.h (this could be replaced with PyUnicode_CopyCharacters but it's used in a simplified copy of _PyUnicode_JoinArray which also uses _PyUnicode_FastCopyCharacters).

    Would you mind to elaborate your use case? Can you use the new https://docs.python.org/dev/c-api/unicode.html#pyunicodewriter API instead?

    It's used here https://github.com/python/mypy/blob/4e3346ee1dee83868adc2411c4a0f4050cf2f95a/mypyc/lib-rt/str_ops.c#L120 in a function which basically reimplements _PyUnicode_JoinArray(). Guess it would work if we replaced it with PyUnicode_CopyCharacters.

  3. vstinner commented on Jul 8, 2024

    @vstinner
    Member

    It's used here https://github.com/python/mypy/blob/4e3346ee1dee83868adc2411c4a0f4050cf2f95a/mypyc/lib-rt/str_ops.c#L120 in a function which basically reimplements _PyUnicode_JoinArray().

    IMO PyUnicodeWriter would be a good fit for this function, especially if you can compute the exact output string length in advance.

  4. vstinner commented on Jul 12, 2024

    @vstinner
    Member

    cc @erlend-aasland @serhiy-storchaka

  5. vstinner commented on Jul 12, 2024

    @vstinner
    Member

    _PyBytes_Join defined in pycore_bytesobject.h

    I created issue gh-121645: [C API] Add PyBytes_Join() function.

  6. added 2 commits that reference this issue on Jul 21, 2024
  7. added a commit that references this issue on Jul 25, 2024
  8. added a commit that references this issue on Jul 25, 2024
  9. added a commit that references this issue on Jul 25, 2024
  10. vstinner commented on Aug 2, 2024

    @vstinner
    Member

    @cdce8p: Python 3.13rc1 has been released. What's the mypyc status? Which functions do you still need?

  11. cdce8p commented on Aug 2, 2024

    @cdce8p
    ContributorAuthor

    @cdce8p: Python 3.13rc1 has been released. Which functions do you still need?

    I believe we've now resolved all issues here. There is one open PR on the mypy side which needs to get merged but that should be it https://github.com/python/mypy/pull/17526.

    What's the mypyc status?

    Not strictly related to this issue. There are still two test issues with mypyc before we can support 3.13. Maybe you've an idea.

    1. mypyc still uses some functions from internal/pycore_*.h. I've added #define Py_CORE_BUILD which works. However, there seems to be an issue with linking mimalloc. This is one of the error messages. pythonsupport.h is part of mypyc.
    /usr/include/python3.13/internal/mimalloc/mimalloc.h:433:1: error: template with C linkage
      433 | template<class T> struct _mi_stl_allocator_common {
          | ^~~~~~~~
    In file included from /usr/include/python3.13/internal/pycore_runtime.h:17,
                     from /usr/include/python3.13/internal/pycore_pystate.h:12,
                     from /usr/include/python3.13/internal/pycore_call.h:12,
                     from pythonsupport.h:21,
                     from CPy.h:12,
                     from test_capi.cc:5:
    /usr/include/python3.13/internal/pycore_interp.h:4:1: note: ‘extern "C"’ linkage started here
        4 | extern "C" {
          | ^~~~~~~~~~
    In file included from /usr/include/python3.13/internal/pycore_mimalloc.h:39,
                     from /usr/include/python3.13/internal/pycore_interp.h:31,
                     from /usr/include/python3.13/internal/pycore_runtime.h:17,
                     from /usr/include/python3.13/internal/pycore_pystate.h:12,
                     from /usr/include/python3.13/internal/pycore_call.h:12,
                     from pythonsupport.h:21,
                     from CPy.h:12,
                     from test_capi.cc:5:
    
    1. The other category of errors is extension module '...' is already cached. I've bisected that to https://github.com/python/cpython/pull/118532. It might be something which can be fixed in the mypyc test runner.
      Traceback (most recent call last): (diff)
        File "driver.py", line 1, in <module> (diff)
          from native import f1 (diff)
        File "<frozen importlib._bootstrap>", line 1360, in _find_and_load (diff)
        File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked (diff)
        File "<frozen importlib._bootstrap>", line 921, in _load_unlocked (diff)
        File "<frozen importlib._bootstrap>", line 819, in module_from_spec (diff)
        File "<frozen importlib._bootstrap>", line 782, in _init_module_attrs (diff)
      SystemError: extension module 'native' is already cached (diff)
    
  12. vstinner commented on Aug 5, 2024

    @vstinner
    Member

    error: template with C linkage

    The problem is that mimalloc is included inside extern "C" { ... }.

    Ah, another issue was already created: issue gh-122584.

  13. vstinner commented on Aug 5, 2024

    @vstinner
    Member

    SystemError: extension module 'native' is already cached (diff)

    Please open a separated issue for that.

  14. vstinner commented on Aug 27, 2024

    @vstinner
    Member

    @cdce8p: Can you close the issue? Or are there still issues not tracked by other open issues?

  15. cdce8p commented on Aug 27, 2024

    @cdce8p
    ContributorAuthor

    @cdce8p: Can you close the issue? Or are there still issues not tracked by other open issues?

    Yes. Thanks again for your help!

  16. vstinner commented on Sep 25, 2024

    @vstinner
    Member

    _PyUnicode_EQ defined in pycore_unicodeobject.h

    I created #124502 to add PyUnicode_Equal() function.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions