Commit Graph

1170 Commits

Author SHA1 Message Date
Erez Geva cb68446ece Fix Length & string reverse order typemap.
And add it to missing languages: C#, Java, Lua, PHP.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2025-07-28 17:07:42 +02:00
William S Fulton 3438289cad Python weakref support for builtin wrappers and limited API
Adds weakref support to the SwigPyObject class used as a base for all
builtin wrapper types defined as heap types (the default).

However, like __dictoffset__, the __weaklistoffset__ members slot is only
available in the limited API from python-3.9 onwards.

Document the previous commit which adds the bulk of the weakref support
to builtin wrappers.
2025-07-23 22:32:54 +01:00
Jim Easterbrook 0242c35936 Tweak weakref tests for Python 2 2025-07-22 10:20:55 +01:00
Jim Easterbrook 18b3b62d43 More tests of weakref implementation
Ensure the object is accessible via the weak reference, that the
object's weak ref count is incremented and decremented correctly, that
the weak refs are invalidated when the object is deleted, and that any
weak ref callback is called when the object is deleted.
2025-07-22 10:05:22 +01:00
Jim Easterbrook 3a37288e06 Merge branch 'master' into fix_1792 2025-07-19 09:53:34 +01:00
crusaderky 6b556a6a1c Add -nogil opt-in flag to remove need for PYTHON_GIL=0
Closes #3215
2025-07-18 22:41:36 +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
Jim Easterbrook 976205ef0d Python: add weakref support to builtin types
These changes add a weakreflist member to SwigPyObject, and set
tp_weaklistoffset to its offset. This fixes #1792.

The Py_TPFLAGS_MANAGED_WEAKREF introduced in Python 3.12 requires
Py_TPFLAGS_HAVE_GC to be set as well, which it currently isn't for
SwigPyObjectType. (See https://github.com/python/cpython/issues/134786).
2025-07-10 08:11:38 +01:00
William S Fulton af6120329c Replace use of tp_name in builtin wrappers for Py_LIMITED_API support
Add SWIG_PyType_GetFullyQualifiedName which is just a wrapper around
PyType_GetFullyQualifiedName, but is only available in python-3.13.
Code up the equivalent for earlier versions - loosely based on the
python-3.13 implementation.

PyType_GetFullyQualifiedName is recommended in PEP-737 for getting the
fully qualified type name of a type.
2025-07-09 19:19:01 +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
William S Fulton 00b59d73b5 Fix testcase error message text which changed in python-3.11 2025-06-27 08:03:39 +01:00
William S Fulton 31dba786a2 Raise Python AttributeError instead of TypeError for member variable errors
The previous commit which fixes the handling of 'del' on a wrappred member variable now
raises an AttributeError now consistently for builtin werappers and non-builtin wrappers.

Also change some other unexpected/internal error handling for wrapped members
to AttributeError instead of TypeError for complete consistency.
2025-06-26 19:35:16 +01: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 f3672e2d6f Py_LIMITED_API support in SwigPyObject_Check for -builtin
Simplify implementation for -builtin, which does not need a fallback to
use strcmp as PyType_IsSubtype() 'just works' even when using multiple
modules (I think perhaps because SwigPyObject_stype->clientdata->pytype is
common across modules due to the implementation in SwigPyObject_Type()).

In the non-builtin case SwigPyObject is not a base type and usage is
different and when multiple modules are being used, SwigPyObject_Type()
returns two implementations of SwigPyObject which is solved in a hacky
way by comparing the types as strings when the pointer comparison fails.

Add import_callback test - tests %import and %callback to exercise
all of SwigPyPacked_Check(). Python only - the main callback example is
not widely tested and needs work in most of the other languages.
2025-06-09 18:36:13 +01: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
William S Fulton 53f4751ffa Fix undefined behaviour in directorout typemaps for void * and const char *&
if the same wrapped function is called more than once. Note that using returning
pointers in directors is still full of traps and not recommended. As such,
warning SWIGWARN_TYPEMAP_DIRECTOROUT_PTR (473) continues to be issued.

The debug Python interpreter failed 3 director testcases which have been
modified in this patch to work around unrecommended director usage
returning from director methods. These are C strings and std::string_view.

A Pyton reference count leak is chosen over undefined behaviour for
returning std::string_view.
2025-05-14 22:10:39 +01:00
William S Fulton 9156ecc669 Add -Walways when running Python interpreter to find more warnings
I tried with -Werror but observed the warning messages disappearing
and tests not failing - probably because the wrapper code is swallowing
warnings and catching the resulting error. Seen in python_overload_simple_cast_runme.py
on Linux (not Windows!). Also when the warning is turned into an error
just a seg fault can occur without the warning message. All round better
to actually see the warning.
2025-05-13 21:49:37 +01:00
William S Fulton ff7ab351a3 Suppress DeprecatedWarning in python_overload_simple_cast_runme testcase
I think these are due to problems inside the Python interpreter sorted out in 3.10.
From 3.10 release notes:

Builtin and extension functions that take integer arguments no longer accept Decimals,
Fractions and other objects that can be converted to integers only with a loss (e.g.
that have the __int__() method but do not have the __index__() method).
2025-05-13 21:49:37 +01:00
William S Fulton d966fd3ba1 Test swig runtime module contains expected names 2025-05-13 21:49:37 +01:00
Julien Schueller 55237efa72 Python: Amend annotations test 2025-04-25 22:13:46 +02:00
William S Fulton 24de76daeb Update/fix hugemod test case to work again 2025-04-20 15:27:28 +01:00
William S Fulton 1b09f4f111 Refactor python no-gil detection
Also additional python interpreter flags cleanup, use PYFLAGS consistently and remove PYTHONFLAGS.
2025-04-17 17:44:02 +01:00
klaus spanderen a75b7b8b9c Added support for Python free threading (aka no-gil) 2025-04-16 19:00:48 +01: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 d9edb22d1c %apply and typedefs improvement
Perform repeated typedef lookups instead of a single typedef
lookup on the type being applied in %apply when looking for a family
of typemaps to apply.

Closes #3064
2024-11-09 13:30:19 +00:00
Olly Betts 0b4496a735 Fix testsuite SWIG warnings; enable SWIG -Werror
SWIG/mzscheme (aka racket) is excluded for now as it currently has a lot
of testsuite warnings and is slated for removal in 4.4.0 anyway.

Closes #3034
2024-10-22 10:30:52 +13:00
William S Fulton d06382ec2a Fix seg fault passing invalid unicode values to std::string
Problem reported as starting with python-3.13.
Due to error not being cleared before calling Python C APIs.

The alternative could have been to ensure SWIG_AsCharPtrAndSize
never returns with a pending error (not that easy).

Closes #3051
2024-10-20 14:19:24 +01:00
William S Fulton 32afafda33 Add li_std_string runtime test 2024-10-20 12:43:43 +01:00
Olly Betts c1e0a68f86 Fix regression wrapping default const bool argument
We need to strip qualifiers before checking the type is `bool`.

This mainly affects Python.  In Ruby there's equivalent code, but
it is only use to generate documentation comments.

Fixes #3052
2024-10-19 15:48:04 +13:00
William S Fulton df87bbe437 inout_typemaps test rework
Use more distinct numbers, now consistent across all languages
2024-10-06 08:57:41 +01:00
William S Fulton d7c90c8f8f Rename inout testcase to inout_typemaps
inout is a D keyword
2024-10-06 00:11:40 +01:00
William S Fulton 60a476982a Run inout testcase for all languages except D 2024-10-05 23:29:34 +01:00
William S Fulton f0f486c762 Single line commment test corrections for last commit 2024-09-28 17:37:18 +01:00
William S Fulton 9018a9a90b Python docstring whitespace strip for single lines
Python does not consistently handle whitespace stripping of
contents in __doc__ across different versions and interfaces.
For example python-3.13 heap types don't strip whitespace
but but static types do. Python-3.12 and earlier didn't strip
any whitespace either. inspect.getdoc() does consistently remove
docstrings though. Given the whitespace is pointless, SWIG now
strips this off for single line docstrings.
2024-09-28 15:30:41 +01:00
Olly Betts 6567c5d186 Add support for C99 _Bool
SWIG now treats _Bool as an alias for the bool keyword when in C mode.
2024-09-25 06:23:48 +12:00
William S Fulton baa725e5ff Python shebang modernisation for python3
PEP-394 recommends specifying python2 or python3 in shebangs unless the
script is known to only run in an activated virtual environment.

PEP-394 also says distributors may not provide the python command.
Newer versions of Ubuntu, at least, no longer provide python unless the
python-is-python3 package is explicitly installed.

Removed some shebangs from testcases (the test-suite does not need them).
2024-09-22 22:11:33 +01:00
William S Fulton 9bf4842002 C++ reference errors when passing in a 'NULL' change of behaviour.
Most languages now use "NullReferenceError" in the error message
where they previously used "ValueError". Also exception changes:

Guile:    "swig-null-reference-error" instead of "swig-value-error"
MzScheme: "swig-null-reference-error" instead of "swig-value-error"
PHP:      zend_ce_type_error instead of zend_ce_value_error
Python:   Consistently raises TypeError instead of a mix of ValueError
          and TypeError.
Ruby:     Consistently raises NullReferenceError instead of a mix of
          ArgumentError and NullReferenceErrorError.

The consistent raising of a TypeError instead of ValueError for Python
ensures that incorrectly passing 'None' into a C++ reference argument
will correctly convert the error into a NotImplemented error for
the rich comparisons implementations per PEP 207. Fixes #2987

Note that the li_constraints checking implementation for the NONNULL
typemap for pointers also makes the same error change from
SWIG_ValueError to SWIG_NullReferenceError.

The D typemaps use SWIG_DNullReferenceException instead of
SWIG_DIllegalArgumentException, although this ultimately has no change
as the same D Exception is still thrown.
2024-09-14 13:03:36 +01:00
Vadim Zeitlin 60a695381e Fix Python docstring for overloads with some Doxygen comments
The existing code didn't work correctly if the last element of the
overload set didn't have a Doxygen comment: in this case, no docstring
was generated at all.

Fix this by trying to find any overload with a Doxygen comment, as
Python docstring is common for all of them: add a helper function to do
it and use it for both all kinds of ordinary functions (global, member
and static) and __init__ functions generated for C++ ctors, as docstring
was generated in the same wrong way for all of them in different places.

Note that currently the overloads without Doxygen comments are not
documented at all at Python level, as saying "there exist other
overloads but there is no documentation for them" doesn't seem very
useful and there doesn't seem anything else that we could do.

Add a new unit test for testing all the different combinations of
overloaded functions with and without Doxygen comments.
2024-07-18 15:35:04 +02:00
Erez Geva 3dfd65e1cd Add test to director guard using multiple threads.
Signed-off-by: Erez Geva <ErezGeva2@gmail.com>

Issue #2949
2024-07-12 18:54:10 +01:00
Vadim Zeitlin cd39cf132c Fix returning null from functions with output parameters in Python
Null pointers returned from functions also using output parameters or C
strings typemaps were lost because the generated code didn't distinguish
between Py_None corresponding to them and Py_None indicating the return
value of a "void" function.

This doesn't seem to be easy to solve properly because the function
return type and output parameters typemaps are independent and there is
no way to customize the behaviour of the latter depending on the former.

So we do it using a hack involving a global variable which is always 0
and is passed to SWIG_Python_AppendOutput() from non-void functions, but
is shadowed by a local variable with the same name set to 1 in the void
ones. This allows us to only discard Py_None if it corresponds to a void
function return value.

This is obviously ugly, but requires only minimal changes, doesn't
impose much extra run-time overhead and is MT-safe (unlike any solution
involving changing global variables).

Also add a unit test checking for this for Python and for JS, where the
same function already worked correctly (unlike in some other languages,
including at least Ruby and PHP).

Closes #2905.
2024-06-15 23:11:46 +01:00
Julien Schueller ffa856c8dc Python 3.13 strips docstring indent 2024-06-13 15:31:26 +02:00
William S Fulton ba9b0a35ab Merge branch 'char_binary_java_fix-tidyup'
* char_binary_java_fix-tidyup:
  Move SWIGStringWithLengthHelper to csharphead.swg
  cdata whitespace/cosmetic fixups
  cdata doc updates
  Rename `typemaps/cdata_struct.swg` to `typemaps/cdata_begin.swg`. And `typemaps/cdata.swg` to `typemaps/cdata.swg`. Move `cdata_apply.swg` content to `typemaps/cdata.swg`.
  Group the C# marshalling of STRING-LENGTH typemap  into C# class named SWIGStringWithLengthHelper.
  Leave Length & string reverse order typemap in typemaps/strings.swg
  Support old C# as "LPUTF8Str" was add in 2017.
  Improve documentation. Follow @wsfulton reviews.
  Use a dummy for MzScheme and untested OCaml cdate. To prevent compilation error.
  Further fixing follow reviews.
  Reorganise raw data typemap,  so typemaps folder contain only common part. Improve document.
  Inline SWIG_string_to_utf8_bytes SWIG_utf8_bytes_to_string code
  Fixes of STRING/BYTES LENGTH typemaps.

 Conflicts:
	CHANGES.current
2024-06-13 10:53:19 +01:00
leake d38bd3298c Adding test case for #2872. 2024-04-27 09:19:09 -07:00
William S Fulton 6226d58065 Fix for using more than one std::string_view type in a method
Fixes #2844
2024-03-27 18:58:58 +00:00
William S Fulton 86e5e8d5e2 Don't run abi3audit on builtin test 2024-03-25 07:52:50 +00:00
William S Fulton 24a66e6125 Add const std::unique_ptr & input typemaps 2024-03-06 21:46:58 +00:00
William S Fulton 846b40793e Add non-const std::unique_ptr & input typemaps 2024-03-06 21:46:58 +00:00
William S Fulton 5712ce6464 std::unique_ptr return by reference typemaps added 2024-03-06 21:46:58 +00:00
William S Fulton 3f1e40d2f4 Add std::unique_ptr && output typemaps
Move semantics are not supported by default.
They behave as if a lvalue reference was returned.
2024-03-06 21:46:58 +00:00
William S Fulton ae22a97f1b Movable std::unique_ptr - add std::unique_ptr && typemaps
Closes #2650
2024-03-06 21:46:58 +00:00