Skip to content

Conversation

JDevlieghere
Copy link

This fixes the unit tests and should make LLDB functional with Python 3.12. Part 2 fixes the issues in the test suite.

JDevlieghere and others added 4 commits May 24, 2024 08:38
The Python documentation [1] says that `PyImport_AppendInittab` should
be called before `Py_Initialize()`. Starting with Python 3.12, this is
enforced with a fatal error:

  Fatal Python error: PyImport_AppendInittab: PyImport_AppendInittab()
  may not be called after Py_Initialize()

This commit ensures we only modify the table of built-in modules if
Python hasn't been initialized. For Python embedded in LLDB, that means
this happen exactly once, before the first call to `Py_Initialize`,
which becomes a NO-OP after. However, when lldb is imported in an
existing Python interpreter, Python will have already been initialized,
but by definition, the lldb module will already have been loaded, so
it's safe to skip adding it (again).

This fixes llvm#70453.

[1] https://docs.python.org/3.12/c-api/import.html#c.PyImport_AppendInittab

(cherry picked from commit fbce244)
…nit tests (llvm#82096)

The unit tests only test the Python objects and don't actually use
anything from the LLDB module. That means that all the additional
complexity in ScriptInterpreterPythonImpl::Initialize is overkill.

By doing the initialization by hand, we avoid the annoying
ModuleNotFoundError.

  Traceback (most recent call last):
    File "<string>", line 1, in <module>
  ModuleNotFoundError: No module named 'lldb'

The error is the result of us stubbing out the SWIG (specifically
`PyInit__lldb`) because we cannot link against libLLDB from the unit
tests.

The downside of doing the initialization manually is that we do lose a
bit of test coverage. For example, issue llvm#70453 also manifested itself
in the unit tests.

(cherry picked from commit 5c96e71)
…lvm#82098)

Use a Python Bytes and ByteArray object instead of Integers for
TestOwnedReferences and TestBorrowedReferences. These two tests were
failing when building against Python 3.12 because these Integer objects
had a refcount of 4294967296 (-1). I didn't dig into it, but I suspect
the Python runtime has adopted an optimization to decrease refcounting
traffic for these simple objects.

(cherry picked from commit 27f2908)
This is using `FileSystem::Instance()` w/o calling `FileSystem::Initialize()`. Use `SubsystemRAII` to do that.

(cherry picked from commit 6757913)
@JDevlieghere
Copy link
Author

@swift-ci test

@JDevlieghere JDevlieghere requested a review from shahmishal May 24, 2024 15:47
@JDevlieghere JDevlieghere merged commit e98989b into swift/release/5.10 May 24, 2024
@JDevlieghere JDevlieghere deleted the jdevlieghere/5.10/python-3.12-fixes-pt-1 branch May 24, 2024 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants