Commit Graph

760 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 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 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 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 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 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 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
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 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 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 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
William S Fulton da03674a77 Code style fixes 2024-09-25 22:13:11 +01:00
Clinton Stimpson f121300b6b Use more portable PyType_FromSpec with Python 3.4+
This makes progress towards building with Py_LIMITED_API.
If using -builtin, PyType_FromSpec is enabled with Python 3.9+
2024-09-25 19:18:26 +01:00
Clinton Stimpson dd97c20f40 Make functions to inialize python types. 2024-09-25 19:00:27 +01:00
Olly Betts 9cba248bec Straighten out handling of integer constants
This provides a generic framework to aid converting C/C++ integer and
boolean literals to target language literals, replacing custom code in
several target language backends (and fixing some bugs in that code).
2024-09-16 16:10:02 +12:00
Olly Betts e02efe25db Merge branch 'fbo/indus_doc_csharp' 2024-09-15 09:59:05 +12:00
Olly Betts 7a8c9fdfa8 Straighten out handling of char and string constants
Fixes #904
Fixes #1907
Fixes #2579
Fixes #2990
2024-08-17 16:12:45 +12:00
William S Fulton 74a8aafac0 Pydoc improvements for overloaded functions added to changes file
Also cosmetic code style improvements to the implementation.
2024-08-02 19:07:47 +01:00
William S Fulton 041e966b12 Merge commit '62b603768164e50af4c236fa2e8929cbb46b2ae9'
* commit '62b603768164e50af4c236fa2e8929cbb46b2ae9':
  Also fix docstrings when using Python with -builtin option
  Pass overloaded function itself to add_method() for clarity
  Fix Python docstring for overloads with some Doxygen comments
2024-08-02 19:06:43 +01:00
William S Fulton e0b83d61b9 Single line C# documentation indentation formatting fix
Indent single line comments to same level as function definition below
the comment.

Also correct String * -> char * type in Python and C# apis
2024-07-30 00:12:01 +01:00
William S Fulton 9ef15fd93f Add support for python-3.13 builtin wrappers
New slot, see 04492cbc9a

Start testing python-3.13 in GHA, but leave as 'can fail' as this version
isi still in beta for a few more days.
2024-07-21 10:30:51 +01:00
William S Fulton 71817abc5b Correct type for tp_watched slot 2024-07-21 09:10:01 +01:00
Alexander Shadchin c3e30c5734 Fix missing-field-initializers warning with Py3.13 (#2968)
Introduced https://github.com/python/cpython/pull/114900
2024-07-21 09:03:09 +01:00
Vadim Zeitlin 62b6037681 Also fix docstrings when using Python with -builtin option
Use the recently added find_overload_with_docstring() in builtin case
too.

Note that this relies on the parent commit as add_method() must be
called with the last function in the overload set, and not the last but
one, for find_overload_with_docstring() to work correctly.

Alternatively, we could change find_overload_with_docstring() to look at
all elements of the overload set, not just the preceding, but also the
following ones. However it seems better to keep things simpler and only
call it for the last one.
2024-07-18 17:03:07 +02:00
Vadim Zeitlin 9454c78fa7 Pass overloaded function itself to add_method() for clarity
It's unclear why did we pass the previous element of the overload set
when calling add_method() from dispatchFunction() but it seems that it
was done just to avoid passing anything at all if this function was not
overloaded, presumably because whatever add_method() does if the "node"
argument is not null was already done from elsewhere in this case.

Unfortunately, add_method() doesn't document when should this argument
be null and when it shouldn't be, so it's just guesswork, but at least
the test suite still passes after this change which makes the code a bit
more clear.

And now we use the properties of this node itself in add_method() when
the function is overloaded instead of, completely inexplicably, using
the properties of another overloaded function (which was probably mostly
fine in practice until now because most overloaded functions have the
same properties anyhow).
2024-07-18 16:59:06 +02: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