Repository navigation
Conversation
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
Author
|
recheck |
With swig<4.4, swig's runtime gives `DeprecationWarning: builtin type SwigPyPacked has no __module__ attribute` (also for SwigPyObject and swigvarlink). If warnings are errors, swig ignores the resulting exception and then crashes. This happens when importing our SWIG modules, and also at exit when swig creates a globals object for `extra`, for example after an unclosed file. See pymupdf#4392. src/__init__.py: ignore these warnings while importing `extra` and `mupdf`. src/extra.i: create swig's globals object in module init, so that swig does not create it at exit. tests/test_general.py:test_4392(): expect success with all swig versions, also check exit after an unclosed file, and check that `python -W default -c "import pymupdf"` shows no swig warnings.
ysheikh2
force-pushed
the
swig-import-warnings
branch
from
October 14, 2026 13:28
1574ac1 to
27b0c59
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On macOS,
import pymupdfcrashes with SIGSEGV when warnings are errors, for examplepython -W errororpytest -W error. Without-W errorit prints three warnings.Reproduced with the 1.28.2 wheel from PyPI (
cp310-abi3-macosx_11_0_arm64, CPython 3.12.10), and with a local build ofmain(30d72fc, MuPDF 1.28.5).This is #4392. On Linux and Windows the 1.28.2 wheels are built with swig 4.4.1 and don't crash. The macOS wheels (arm64 and x86_64) are built with swig 4.3.1, because
setup.pypinsswig==4.3.1on macOS with Python < 3.13 to avoid the director problem in swig/swig#3279. I believe this is why @cedral still saw the crash in #4392 with recent releases.Root cause
With swig < 4.4, SWIG's runtime creates
SwigPyPacked,SwigPyObjectandswigvarlinkwithPyType_FromSpec()and names without a module, so CPython 3.12 warns. With-W errorthe warning becomes an exception andPyType_FromSpec()returns NULL. SWIG ignores the NULL and then callsPyObject_New(swig_varlinkobject, NULL). This is swig/swig#3061. It happens in 2 places._mupdf(and_extra, which is imported first).SWIG_Python_DestroyModule()in_extracallsSWIG_globals().extrahas no global variables, so this creates the globals object and a newswigvarlinktype for the first time, duringPy_FinalizeEx(). This is thesys:1:warning above. Whether it crashes depends on the warning filters left at exit. For example,python -W error -c "import pymupdf; open('some.file')"exits 139, as doespytest -W erroron a test that leaves a file open.swig 4.4.0 fixed the warning, which removes both crashes, but on macOS the swig version can't be raised until swig/swig#3279 is fixed.
Fix
src/__init__.pyimportsextraandmupdfinsidewarnings.catch_warnings(), ignoring onlyDeprecationWarningwith the messagebuiltin type ... has no __module__ attribute. Other warnings, and these warnings anywhere else, are unchanged.src/extra.icallsSWIG_globals()in%init, so the globals object (and its type) is created during import, where the warning is ignored, instead of at exit.With swig >= 4.4 the types are named
swig_runtime_data5.SwigPyObjectetc. and the warning is never raised. There, the filter has no effect andSWIG_globals()only creates one small unused object.tests/test_general.py:test_4392()now expectspytest -Werrorandpython -Werror -c "import pymupdf"to succeed with all swig versions, instead of expecting a SEGV with swig < 4.4. It also checkspython -Werror -c "import pymupdf; open(path)", and thatpython -W default -c "import pymupdf"prints none of these warnings.Testing
Built locally with
pip wheel ./PyMuPDFon macOS 27.0.1 arm64, CPython 3.12.10.setup.pychose swig 4.3.1 as it does for the release wheels.mainpython -W default -c "import pymupdf"python -W error -c "import pymupdf"python -W error -c "import pymupdf; open(path)"pytest -Werroron a file withimport pymupdftest_4392e2=-11 e3=-11 e4=-11)test_4392failedMy local build is
pymupdf-litewithout layout, so I rantests/test_tables.pyandtests/test_configurations.pywithPYMUPDF_TEST_USE_LAYOUT=0. Without it, 11 of their tests fail on bothmainand this branch.With only the
src/__init__.pychange,test_4392still fails withe4=-11, which is whysrc/extra.iis changed too.I haven't tested a build with swig >= 4.4 on macOS, or on Linux or Windows.