Edit last few commits for python-3.14 and 3.15 support

GHA - python-3.14 testing changed to "can fail" as 3.14 is still in alpha status.

Restore __package__ fallback check as it was.

Correct docs given the import changes.

Issue #3159
Issue #2967

more
This commit is contained in:
William S Fulton 2025-04-28 22:50:40 +01:00
parent c8bedcc661
commit e1cf7b37ac
4 changed files with 16 additions and 5 deletions

View File

@ -135,6 +135,7 @@ jobs:
- SWIGLANG: python
VER: '3.14'
CSTD: gnu99
continue-on-error: true
- SWIGLANG: python
PY2: 2
SWIG_FEATURES: -builtin

View File

@ -7,6 +7,15 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.4.0 (in progress)
===========================
2025-04-28: jschueller, henryiii
[Python] #2967 Use __spec__.parent as the first approach to obtaining
a module's package name when importing the low-level C/C++ module as the
previous approach looking for __package__ is due to be removed in python-3.15.
__package__ is now used as a fallback if __spec__.parent fails.
2025-04-28: jschueller, henryiii
[Python] #3066 #3159 Add ht_token slot for python-3.14 support.
2025-04-18: akorobka
#103 Thread safety bugfix in the runtime type system.

View File

@ -6675,7 +6675,7 @@ The code is generated into the pure Python module, foo.py, and merely imports th
<div class="targetlang">
<pre>
if getattr(__spec__, "parent", None) or '.' in __name__:
if getattr(globals().get("__spec__"), "parent", None) or __package__ or '.' in __name__:
from . import _foo
else:
import _foo
@ -6760,7 +6760,7 @@ The default import loading code is thus different:
<div class="targetlang">
<pre>
if getattr(__spec__, "parent", None) or '.' in __name__:
if getattr(globals().get("__spec__"), "parent", None) or __package__ or '.' in __name__:
from ._foo import *
else:
from _foo import *

View File

@ -705,20 +705,21 @@ public:
*
* First check for __spec__.parent which is available from 3.4 onwards,
* see https://docs.python.org/3/reference/import.html#spec. If not,
* check for __package__, which was set before 3.14.
* check for __package__, which is available from 2.6 onwards (see PEP366),
* but will no longer be set/used in 3.15.
* Next try determine the shadow wrapper's package based on the __name__ it
* was given by the importer that loaded it.
* If the module is in a package, load the low-level C/C++ module from the
* same package, otherwise load it as a global module.
*/
Printv(default_import_code, "# Import the low-level C/C++ module\n", NULL);
Printv(default_import_code, "if getattr(globals().get(\"__spec__\"), \"parent\", None) or globals().get(\"__package__\") or \".\" in __name__:\n", NULL);
Printv(default_import_code, "if getattr(globals().get(\"__spec__\"), \"parent\", None) or __package__ or \".\" in __name__:\n", NULL);
Printv(default_import_code, tab4, "from . import ", module, "\n", NULL);
Printv(default_import_code, "else:\n", NULL);
Printv(default_import_code, tab4, "import ", module, "\n", NULL);
} else {
Printv(default_import_code, "# Pull in all the attributes from the low-level C/C++ module\n", NULL);
Printv(default_import_code, "if getattr(globals().get(\"__spec__\"), \"parent\", None) or globals().get(\"__package__\") or \".\" in __name__:\n", NULL);
Printv(default_import_code, "if getattr(globals().get(\"__spec__\"), \"parent\", None) or __package__ or \".\" in __name__:\n", NULL);
Printv(default_import_code, tab4, "from .", module, " import *\n", NULL);
Printv(default_import_code, "else:\n", NULL);
Printv(default_import_code, tab4, "from ", module, " import *\n", NULL);