Skip to content

Importing module with capsule attribute fails in sub interpreter #112100

Closed as not planned

Description

@tonybaloney

Bug report

Bug description:

When importing certain modules (zoneinfo is an example), attributes are missing when inside a sub interpreter.

Importing these modules works inside the main interpreter.

I've written a test to demonstrate the bug

import unittest
import _xxsubinterpreters as interpreters


class TestCapsule(unittest.TestCase):

    def test_capsule(self):
        import zoneinfo

    def test_capsule_in_sub_interpreter(self):
        interp = interpreters.create()
        interpreters.run_string(interp, "import zoneinfo")
.Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.13/zoneinfo/__init__.py", line 14, in <module>
    from _zoneinfo import ZoneInfo
AttributeError: module 'datetime' has no attribute 'datetime_CAPI'
E
======================================================================
ERROR: test_capsule_in_sub_interpreter (test_capsule.TestCapsule.test_capsule_in_sub_interpreter)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/anthonyshaw/projects/subinterpreter-web/test_capsule.py", line 12, in test_capsule_in_sub_interpreter
    interpreters.run_string(interp, "import zoneinfo")
_xxsubinterpreters.RunFailedError: AttributeError: module 'datetime' has no attribute 'datetime_CAPI'

----------------------------------------------------------------------
Ran 2 tests in 0.457s

FAILED (errors=1)

I haven't seen this behaviour on any other modules, the thing that seemed special with datetime and this attribute is that it is a PyCapsule. So the issue is likely related to that?

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. tonybaloney commented on Nov 15, 2023

    @tonybaloney
    ContributorAuthor

    @ericsnowcurrently this is one of the issues I've found trying to run Django inside a sub interpreter

  2. tonybaloney commented on Nov 15, 2023

    @tonybaloney
    ContributorAuthor

    This is the specific line that's failing. https://github.com/python/cpython/blob/main/Modules/_zoneinfo.c#L2717

    The _zoneinfo module runs PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0) which fails.

    There is no check for whether the datetime module has been imported and is initialised.

  3. Eclips4 commented on Nov 15, 2023

    @Eclips4
    Member

    I'm not sure, but it's probably related to the fact that _datetime isn't isolated for subinterpreters yet: #102995.

  4. Eclips4 commented on Nov 15, 2023

    @Eclips4
    Member

    This is the specific line that's failing. https://github.com/python/cpython/blob/main/Modules/_zoneinfo.c#L2717

    The _zoneinfo module runs PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0) which fails.

    There is no check for whether the datetime module has been imported and is initialised.

    PyCapsule_Import imports the datetime module.

  5. tonybaloney commented on Nov 15, 2023

    @tonybaloney
    ContributorAuthor

    I'm not sure, but it's probably related to the fact that _datetime isn't isolated for subinterpreters yet: #102995.

    Yep, looking at that PR and the related changes it would probably cause the behaviour I'm seeing in this test

  6. ericsnowcurrently commented on Jul 8, 2024

    @ericsnowcurrently
    Member

    @tonybaloney, could you verify if this is still a problem in 3.13?

  7. tonybaloney commented on Jul 9, 2024

    @tonybaloney
    ContributorAuthor

    Just confirmed this test case was fixed since 3.13b1 (tested 3.13b3)

  8. tm-jdelapuente commented on Nov 18, 2024

    @tm-jdelapuente

    Hey! To clarify, was this backported to a patch version in 3.12? I assume not - just checking

  9. Eclips4 commented on Nov 18, 2024

    @Eclips4
    Member

    Hey! To clarify, was this backported to a patch version in 3.12? I assume not - just checking

    This wasn't backported because there was no fix 🙂

    But if you're talking about datetime isolation, it wasn't backported to 3.12.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirpendingThe issue will be closed if no feedback is providedtopic-subinterpreterstype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions