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.
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.
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
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).
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.
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
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.
* 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
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.
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.
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.
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).
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.
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
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
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
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.
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).
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.
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.
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.
* 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