Commit Graph

6246 Commits

Author SHA1 Message Date
crusaderky 6b556a6a1c Add -nogil opt-in flag to remove need for PYTHON_GIL=0
Closes #3215
2025-07-18 22:41:36 +01:00
William S Fulton 7a7aba03b6 Polish off heap types buffer support
Clean up generated code and make it c90 compliant.
Add changes entry for this work in issue #3219.
2025-07-18 18:59:43 +01:00
Jim Easterbrook 20da01780f Enable Python builtin heap types buffer interface (#3219)
For Python < 3.9 the tp_as_buffer member is set explicitly if the
interface has a bf_getbuffer slot defined. This fixes #3211.

Enabled buffer interface for non-builtin test.
This only works with Python >= 3.12, where methods __buffer__ and
__release_buffer__ were added. Unfortunately it's not practical for
these methods to reuse the slot methods (or vice versa).

Disable Py_LIMITED_API if below 3.11. The Py_buffer struct and
associated functions are not defined in earlier stable API versions.

Closes #3211
2025-07-18 07:43:34 +01:00
Tim Felgentreff 5ea4449c3e Correct SwigPyObject_richcompare and SwigPyObject_compare undefined behaviour (#3216)
Correct SwigPyObject_richcompare and SwigPyObject_compare signatures
and avoid potential read beyond object memory.

Squashed commit of #3216 plus changes file entry and whitespace fixes.

Closes #3217
2025-07-09 19:07:44 +01:00
Jim Easterbrook 40378d0405
Remove PyErr_SetString if type init fails (#3210)
PyType_Ready sets an exception which should not be over written.
Closes #3209.
2025-07-02 21:27:05 +01:00
Julien Schueller 6c3bc2d18a Python: Use multi-phase initialization
Implements https://peps.python.org/pep-0489/
Bumps minimal python3 version from 3.4 to 3.5.
The idea is to move the initialization of the module into a new SWIG_mod_exec function.

Closes #3168
2025-06-23 22:04:47 +01:00
William S Fulton 22a8088a98 Fix -Wunused-variable warning in Lua and Octave wrappers
Add bool output parameter to Swig_overload_dispatch to say when it has
generated code that will use the typecheck typemap code to help Lua and
Octave to not emit unused argv[] arrays.

Alas, resorted to warning suppression for deficient cpp11_initializer_list
typecheck typemap in cpp11_std_initializer_list testcase.
2025-06-23 21:39:49 +01:00
William S Fulton 4bf9547982 Fix -Wunused-variable warning for overloaded methods in Octave
Fixes testcases: cpp11_director_using_constructor and cpp11_using_constructor.
2025-06-23 21:39:49 +01:00
William S Fulton b4ee44fad4 Fix -Wunused-variable warnings in R 2025-06-21 23:24:51 +01:00
William S Fulton 91bcdae967 Fix -Wunused-variable warning when using -java assumeoverride feature 2025-06-21 23:24:46 +01:00
William S Fulton 0fddc95cc2 Fix -Wunused-variable Ruby warning when using -globalmodule 2025-06-21 23:24:46 +01:00
William S Fulton 6e83542054 Only generate Python swig_obj[] if required
Removes warning -Wunused-variable
2025-06-21 22:49:17 +01:00
William S Fulton 2f00a7dc07 Use heap types for builtin wrappers
Closes #3196
2025-06-20 23:04:35 +01:00
William S Fulton cde0447240 Heap type slots generation slimdown
Only generate slots if provided by SWIG or if they are provided by users via a %feature.
So stop generating large tables of slots containing "0".

Issue #3196
2025-06-20 23:04:22 +01:00
William S Fulton fa6500251b Add a few missing slots and remove deprecated slots when using heap types
Based on slots in Python's typeslots.h and then removing effectively deprecated
slots mentioned in https://docs.python.org/3/c-api/typeobj.html.

Issue #3196
2025-06-20 23:03:56 +01:00
William S Fulton 3115a91f0b Merge branch 'heaptypes-builtin'
* heaptypes-builtin:
  Restore Stable ABI for < python-3.9
  Python heap types fix for < python-3.9
  Move dictoffset from derived classes to SwigPyObject for builtin wrappers

Conflicts:
	CHANGES.current
2025-06-18 19:31:21 +01:00
William S Fulton e6283e7552 Python heap types fix for < python-3.9
structmember.h inclusion moved for external runtime to work without
requiring users to include structmember.h and the Py_READONLY and
Py_ST_PYSSIZE_T definitions. Including Python.h remains the requirement
for using the external runtime (Examples/python/external_runtime).

Note that stddef.h is not always included by structmember.h

Rename dict to swigdict in PySwigObject for easier identification.

SWIG_HEAPTYPES definition is moved to support both external runtime
and Python Stable ABI.
2025-06-18 19:29:43 +01:00
Petr Viktorin 80292de98f Move dictoffset from derived classes to SwigPyObject for builtin wrappers
Issue #3196

Fixes "TypeError: multiple bases have instance lay-out conflict"
for python-3.11 and earlier for multiple inheritance, see 22d9fda0 and 4908133e.
Fix is achieved by moving __dictoffset__ to the single common base class SwigPyObject.
2025-06-17 08:27:27 +01:00
Olly Betts 61c542162c Fix parsing of a::b.c() and x::y.z
Fixes #3197
2025-06-17 15:23:15 +12:00
Olly Betts 3f4ed82945 Improve parsing of expressions related to functions calls
For example, the following no longer give parse errors:  `g().d()`,
`g().D`, `(f(1) < g(2))`, `sizeof f(a,b)`

Partly addresses #3197
2025-06-17 14:59:54 +12:00
Olly Betts 98b454a476 Simplify grammar for exprmem
Just have one rule for exprmem[in] LPAREN rather than an LPAREN variant
of each rule.  Makes no difference to what can be parsed.
2025-06-17 14:14:14 +12:00
William S Fulton 9c4aa6099d Merge branch 'builtin-heap-types'
* builtin-heap-types:
  Changes entry for python -builtin and heap types
  Revert previous commit as it breaks setting arbitrary attributes on builtin wrapped types
  Fix TypeError: multiple bases have instance lay-out conflict
  Py_LIMITED_API support in SwigPyObject_Check for -builtin
  Py_LIMITED_API support in SwigPyBuiltin_SetMetaType for -builtin
  Remove pyname_compat.i
  Python type creation functions refactor
  Small move towards Py_LIMITED_API for -builtin
  Add SwigPyObjectType and SwigPyStaticVar to the swig runtime module
  SWIG_HEAPTYPES for SwigPyStaticVar and add it to the runtime module
  Cosmetic whitespace formatting around PY_VERSION_HEX
  SWIG_HEAPTYPES for SwigPyObjectType and add it to the runtime module
  Better error handling creating types with python -builtin
  Gracefully handle errors in PyType_FromSpecWithBases for -builtin
  Gracefully handle errors in PyModule_AddObject for -builtin
  More graceful error in failures calling back_reference
  Show ref count before final decrement when using SWIG_REFCNT_DEBUG
2025-06-11 08:16:48 +01:00
William S Fulton 4908133e83 Revert previous commit as it breaks setting arbitrary attributes on builtin wrapped types
When using -builtin and -DSWIG_HEAPTYPES.

Otherwise there are two testcase failures when trying to set arbitrary
attributes onto the wrapped Python types:

Traceback (most recent call last):
  File "/home/william/swig/github/swig/Examples/test-suite/python/./li_std_vector_back_reference_runme.py", line 8, in <module>
    size = first_element().size
           ^^^^^^^^^^^^^^^
  File "/home/william/swig/github/swig/Examples/test-suite/python/./li_std_vector_back_reference_runme.py", line 6, in first_element
    return v[0]
           ~^^^
AttributeError: 'li_std_vector_back_reference.Wheel' object has no attribute '__swig_container'
make[1]: *** [Makefile:143: li_std_vector_back_reference.cpptest] Error 1

Traceback (most recent call last):
  File "/home/william/swig/github/swig/Examples/test-suite/python/./struct_value_runme.py", line 15, in <module>
    b.added = 123
    ^^^^^^^
AttributeError: 'struct_value.Bar' object has no attribute 'added'
make[1]: *** [Makefile:141: struct_value.cpptest] Error 1
2025-06-09 19:13:09 +01:00
William S Fulton 22d9fda0cc Fix TypeError: multiple bases have instance lay-out conflict
Fix when using -builtin and -DSWIG_HEAPTYPES in these tests:

constructor_copy_non_const
contract
default_constructor
director_basic
minherit
using_composition
using_extend
using_member_multiple_inherit
cpp11_variadic_templates

Fix required for python <= 3.11.
Python 3.12 does not have the TypeError due to
https://github.com/python/cpython/pull/96028.
2025-06-09 18:45:05 +01:00
Olly Betts 3be7697a33 Remove long-deprecated $function variable
The $function variable in %exception/feature:except typemaps is
no longer recognised.  It was marked as deprecated in 2008.
Use $action (added in 2001) instead which has exactly the same
value.

SWIG/D has an unrelated $function variable which is substituted in
%pragma(d) wrapperloaderbindcommand - this is still supported.
2025-06-09 13:58:42 +12:00
Olly Betts f0fb79152e [Python] Fix wrapping of bool const&x=true param
Fix wrapping of a bool const& parameter with a default value.
Regression introduced in SWIG 4.3.0.

Fixes #3162
2025-06-09 11:31:19 +12:00
Olly Betts b5deab76bc [Tcl] Fix -external-runtime for Tcl 8.x
Fix -external-runtime to generate a header which works with Tcl 8.x.
Regression introduced in SWIG 4.2.1.  As a bonus  the generated header
no longer requires the user to include tcl.h and various standard C
library headers before including it.

Fixes #2887
2025-06-09 11:01:30 +12:00
William S Fulton fd26718dfc Small move towards Py_LIMITED_API for -builtin 2025-06-07 10:17:09 +01:00
William S Fulton 73e8070a5e Better error handling creating types with python -builtin 2025-05-31 10:51:27 +01:00
William S Fulton dca38962ef Gracefully handle errors in PyType_FromSpecWithBases for -builtin
Add workaround for missing __dictoffset__ support prior to python-3.9 -
fixes struct_value and li_std_vector_back_reference testcases which failed
when attempting to set attributes on a class's dict.

Also surfaces Python error 'TypeError: multiple bases have instance lay-out conflict'
in a few testcases < python-3.12 with SWIG_HEAPTYPES defined with -builtin, for example
constructor_copy_non_const testcase.
2025-05-31 08:58:03 +01:00
William S Fulton c1179c13da Gracefully handle errors in PyModule_AddObject for -builtin 2025-05-31 08:58:03 +01:00
Olly Betts 739997707c Handle C++14 auto return type function declaraction
Fix parse error for C++14 forward declaration of function with auto
return type and no trailing return type.

Fixes #3186
2025-05-28 14:10:03 +12:00
William S Fulton fde28cb9cb Replace use of Python internal ob_type with public API Py_TYPE 2025-05-24 09:54:25 +01:00
William S Fulton 307b474769 Fix regression expanding templates in function parameter values
Only using the parameter value in the generated code, such as when using
kwargs. This is known to be fundamentally flawed as sometimes the
generated value is not accessible outside of the class it is defined in.

Fixes regression due to a037e2f2e2.
The new fix now attempts to treat all value and name attributes as
a SwigType instead of an unparsed type in a String. However, it does
it more consistently instead of just for the "value" attribute in
add_parms.

Also subtle problem fix where cpatchlist was incorrectly being used
instead of patchlist.

Closes #3179
2025-05-17 00:21:51 +01:00
Sergei Trofimovich 01bf5808dd Makefile.am: avoid double `bison` execution on parallel build
Before the change `make -j16` ran `bison` twice on the same input:

    $ make -j16
    ...
    make[1]: Entering directory '/build/source/Source'
    bison -d -Wall -Werror  --output=CParse/parser.c ./CParse/parser.y
    gcc -g -O2 -Wall -W -I.   -c -o mdfour.o mdfour.c
    bison -d -Wall -Werror  --output=CParse/parser.c ./CParse/parser.y
    ...

Note how two parallel `bison` calls both generate the same files. It works
most of the time, but is prone to generate unexpected output.

This happens because Makefile rules like:

    a b: c; COMMAND
    all: a b

will run COMMAND for each target (twice here: once for `a` and once for
`b`),

The fix uses GNU make's Grouped Targets to use single command to produce
multiple targets.
2025-05-01 07:04:07 +12:00
William S Fulton f91a9cb8e4 Use SWIG_NOEXCEPT for deprecated dynamic exception specifications
SWIG_NOEXCEPT is generated instead of throw() which is deprecated in
c++11. If c++11 or later is being used, then this macro expands to
noexcept instead of throw().

Affects director code only.

Also fix up some testcase to not use throw() when using c++11 or later.
Tested with clang and -Wdeprecated-dynamic-exception-spec as gcc
doesn't seem to warn for this deprecation.

Closes #3027
2025-04-29 22:41:09 +01:00
William S Fulton 4bc2a3f514 Warning fix for clang 2025-04-29 20:51:05 +01:00
William S Fulton e1cf7b37ac 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
2025-04-28 22:57:17 +01:00
Julien Schueller 3bfdf13c60 Python: Add ht_token 2025-04-25 22:13:46 +02:00
Julien Schueller 50e1cc8bc0 Python: Handle __package__ removal
Closes #2967
2025-04-25 22:13:46 +02:00
William S Fulton 34b71a3bba Fix javac -Xlint warning [this-escape] - JDK 21 in director constructors
[this-escape] possible 'this' escape before subclass is fully initialized

Suppress false positive warning in director constructors.
2025-04-02 22:56:41 +01:00
Olly Betts f3ea85af0b [MzScheme/Racket] Drop support
Closes #920
Closes #2830
2025-04-01 14:05:28 +13:00
Olly Betts 0db0a3994e Omit empty deprecated/experiemtnal lists
If there are no deprecated target languages then omit the list
entirely, and similarly for experimental target languages.
2025-04-01 14:00:43 +13:00
William S Fulton 837cc1c060 int conversion warnings 2025-03-28 07:54:47 +00:00
Olly Betts 6dbf867d23 Fix handling of corner cases by previous change
Don't collapse runs of whitespace to a single space, instead replace
each whitespace with one space.

Leave the expression as-is if it contains one or more double quotes
as we don't want to change the value of string literals, and any
expression containing double quotes won't currently work in the context
where we need to replace newlines anyway.

Fixes #3127 better
2025-03-03 11:22:37 +13:00
Olly Betts 4a0372aacc Improve parsing multi-line expr via skip_balanced()
Fix bad generated code in some cases when a constant expression is split
over multiple lines and used as part of a type.  This manifested in
cases where SWIG's parser calls skip_balanced('(', ')') then grabs the
skipped expression's program text from scanner_ccode.

Fixes #3127
2025-02-28 17:30:08 +13:00
William S Fulton 26b0911a74 Add support for $n special variable expansion in the names of typemap local variables
For example:

  %typemap(in) int MYINT (int $1_temp) { ... }

$1_temp is typically expanded to arg1_temp, arg2_temp etc depending on
which argument the typemap is applied to.
2025-02-19 20:44:40 +00:00
William S Fulton e8787615a7 Regression fix when using -keyword, kwargs or compactdefaultargs option
Restore generating a non-const cast to the default value when
wrapping const pointer default arguments.

Fixes #3075.
2025-02-05 08:09:44 +00:00
Olly Betts f362bb0993 Explicitly include errno.h for ERANGE
Also zero errno before strtoull() call.
2024-11-21 17:15:17 +12:00
Olly Betts dfe9d8ea2f [Java] Fix regression wrapping enum values
Fix regression wrapping enum values which don't fit in a Java signed
int.

Fixes #3070
2024-11-21 17:05:31 +13:00