Commit Graph

3988 Commits

Author SHA1 Message Date
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 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
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
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 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 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
William S Fulton 936767da8e Few error message improvements and consistency tweaks 2024-11-04 23:21:25 +00:00
Olly Betts 9187aaf22c [guile] Improve constant wrapping
Allow wrapping anything with a `varout` typemap as a constant.

See #3034
2024-10-25 11:16:56 +13:00
Olly Betts c77498d5db [Perl] Destroy C++ local variables on exception
Ensure C++ local variables get destroyed before throwing a Perl
exception.

Fixes https://sourceforge.net/p/swig/bugs/1134/
2024-10-24 16:58:17 +13:00
William S Fulton 46bee2dc83 Potentially uninitialised variable warning in visual c++ suppression 2024-10-19 16:15: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
Olly Betts e528260b83 [ruby] Fix doc comments for bool parameter default value
Documentation comments now use `true` and `false` for bool parameter
default values, instead of `True` and `False` (which are the Python
names and wrong for Ruby!)
2024-10-19 15:46:25 +13:00
William S Fulton 07a7c939e3 Add nested classes to parse tree dumped out by -xml
Previously the parse tree contained the forward class declaration for
nested classes, the <classforward> XML element as nested class
support had not been turned on during parsing for -xml. The nested
class support has now been turned on so that the nested classes appear
in the parse tree in a <class> XML element.

Closes #874
2024-10-17 00:41:44 +01:00
William S Fulton af0e1e035d Error out if more than one target language specified
Multiple target language options were previously quietly ignored and only the last one was used.
2024-10-17 00:05:56 +01:00
William S Fulton 86498e46c6 Remove -xmllang option used with -xml
which had no effect on the output.
2024-10-15 08:59:51 +01:00
William S Fulton 42b9e254f0 Use Py_ prefixed names Py_T_PYSSIZET and Py_READONLY
Prefer these over their globally polluting old names
2024-10-14 08:49:27 +01:00
William S Fulton 9e9e5e0312 Go code refactor to use is_void and returntype
Cosmetic, no change
2024-10-06 16:35:13 +01:00
William S Fulton 09001ee302 Add $isvoid special variable expansion for directors
Complete the removal of Ruby's output_helper macro and replacement
with SWIG_AppendOutput for director typemaps.
Requires $isvoid special variable support in director code.
2024-10-06 15:00:37 +01:00
William S Fulton a95017050e More maintainable warning messages 2024-10-06 12:28:17 +01:00
William S Fulton 82f714d841 Merge branch 'py3-typefromspec'
* py3-typefromspec:
  Only use heaptypes for non-builtin and python>=3.3
  Partial revert "Resolve warnings about __module__ attribute missing for SwigPyPacked"
  Single line commment test corrections for last commit
  Python docstring whitespace strip for single lines
  Prettier generated python C/C++ code
  Use HEAPTYPES for python-3.3 and later (non-builtin)
  Github Actions - also test newer versions of python builtin
  Resolve warnings about __module__ attribute missing for SwigPyPacked
  Allow deriving from SwigPyObject
  Generated code formatting tweaks
  Py_INCREF -> SWIG_Py_INCREF
  Code style fixes
  Use more portable PyType_FromSpec with Python 3.4+
  Make functions to inialize python types.
2024-09-29 11:35:25 +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
William S Fulton ad036e98d7 Prettier generated python C/C++ code 2024-09-28 12:03:42 +01:00
Clinton Stimpson 7911683101 Resolve warnings about __module__ attribute missing for SwigPyPacked 2024-09-27 22:37:27 -06:00
William S Fulton 447cd4dd14 Formalise the deprecation of target languages with a Deprecation status 2024-09-27 19:42:16 +01:00
William S Fulton 3b982b6d1b Replace experimental warning with deprecated warning for MzScheme
Use an 'in your face deprecation' notice to publicise the deprecation
to try help find a new maintainer for Racket.
2024-09-27 08:07:58 +01:00
Olly Betts d8d6b733ed [go] Wrap const static members with getters
This reverts behaviour to what's documented and how it was in 4.2.1.

See #3036
2024-09-27 10:53:31 +12:00
Olly Betts e261f9adda Support --version as an alias for -version
SWIG has supported --help as an alias for -help since 2006 and it
seems inconsistent not to recognise --version as well.
2024-09-26 14:17:41 +12:00
William S Fulton 54d7ebebce Generated code formatting tweaks 2024-09-25 23:53:44 +01:00
William S Fulton 769f20bf23 Py_INCREF -> SWIG_Py_INCREF 2024-09-25 22:32:55 +01:00