Skip to content
New issue

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

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

Already on GitHub? # to your account

Importing module with capsule attribute fails in sub interpreter #112100

Closed
tonybaloney opened this issue Nov 15, 2023 · 9 comments
Closed

Importing module with capsule attribute fails in sub interpreter #112100

tonybaloney opened this issue Nov 15, 2023 · 9 comments
Labels
extension-modules C modules in the Modules dir pending The issue will be closed if no feedback is provided topic-subinterpreters type-bug An unexpected behavior, bug, or error

Comments

@tonybaloney
Copy link
Contributor

tonybaloney commented Nov 15, 2023

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

@tonybaloney tonybaloney added the type-bug An unexpected behavior, bug, or error label Nov 15, 2023
@tonybaloney
Copy link
Contributor Author

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

@tonybaloney
Copy link
Contributor Author

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.

@Eclips4
Copy link
Member

Eclips4 commented Nov 15, 2023

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

@Eclips4
Copy link
Member

Eclips4 commented Nov 15, 2023

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.

@tonybaloney
Copy link
Contributor Author

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

@ericsnowcurrently
Copy link
Member

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

@ericsnowcurrently ericsnowcurrently added the pending The issue will be closed if no feedback is provided label Jul 8, 2024
@tonybaloney
Copy link
Contributor Author

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

@github-project-automation github-project-automation bot moved this from Todo to Done in Subinterpreters Jul 9, 2024
@terryjreedy terryjreedy closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
@tm-jdelapuente
Copy link

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

@Eclips4
Copy link
Member

Eclips4 commented Nov 18, 2024

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
extension-modules C modules in the Modules dir pending The issue will be closed if no feedback is provided topic-subinterpreters type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

5 participants