Commit Graph

85 Commits

Author SHA1 Message Date
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
William S Fulton 9ec4c6f1aa Py_LIMITED_API support in SwigPyBuiltin_SetMetaType for -builtin
Also SWIG_HEAPTYPE redefinition warning fix in test-suite
2025-06-09 18:35:49 +01:00
William S Fulton 170f3fc065 Python type creation functions refactor
Rename to use naming conventions used in the Python C code base and
typically used in extensions when creating types:

SwigPyObject_type -> SwigPyObject_Type
SwigPyPacked_type -> SwigPyPacked_Type
swig_varlink_type -> SwigVarLink_Type

to also match other type creating functions SwigPyObjectType_Type
and SwigPyStaticVar_Type.

Add SwigVarLink singleton creation to init function for thread safety.
2025-06-07 10:50:39 +01:00
William S Fulton 7df18106fc Add SwigPyObjectType and SwigPyStaticVar to the swig runtime module
for default case (they have just been added for -builtin and
SWIG_HEAPTYPES in previous couple of commits).

These are only used by -builtin and were missed when when moving
SwigPyObject, SwigPyPacked and SwigVarLink were added to the swig
runtime module. Should fix potential warnings for these two types:

  DeprecationWarning: builtin type has no __module__ attribute

although this has not been seen in the test-suite.
2025-06-07 10:17:09 +01:00
William S Fulton f4776dcb94 SWIG_HEAPTYPES for SwigPyStaticVar and add it to the runtime module
Renamed from swig_static_var_getset_descriptor, used by -builtin only.
2025-06-07 10:17:03 +01:00
William S Fulton bf297d608c SWIG_HEAPTYPES for SwigPyObjectType and add it to the runtime module
Used by -builtin only.
2025-06-02 19:06:26 +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 da03674a77 Code style fixes 2024-09-25 22:13:11 +01:00
Clinton Stimpson dd97c20f40 Make functions to inialize python types. 2024-09-25 19:00:27 +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
William S Fulton 3ceb009da3 Use PyObject_Free instead of deprecated macros
Python 3.10 comments the macros PyObject_Del and PyObject_DEL as deprecated
2024-03-25 07:52:50 +00:00
William S Fulton d79ab5ac6b Additional casts for Python DecRef change 2024-03-24 18:12:21 +00:00
William S Fulton 67bc1cbb0f Py_XDECREF -> Py_DecRef for stable ABI compliance 2024-03-24 18:12:21 +00:00
William S Fulton 53ee3fe97b Py_DECREF -> Py_DecRef for stable ABI compliance 2024-03-24 18:12:21 +00:00
William S Fulton 5298707057 Py_IncRef casts 2024-03-24 18:12:21 +00:00
William S Fulton b4cbb91101 Py_XINCREF -> Py_IncRef for stable ABI compliance 2024-03-24 18:12:21 +00:00
William S Fulton e3f25aab51 Py_INCREF -> Py_IncRef for stable ABI compliance 2024-03-24 18:12:21 +00:00
Jim Easterbrook c4bdb2ce38 Add NULL test to SwigPyBuiltin_ternaryfunc_closure
This allows the tp_call slot to be used with no keyword parameters.

Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
2024-01-30 08:39:21 +00:00
Jim Easterbrook 0b1abb3dfa Allow passing NULL value to sq_ass_item
The sq_ass_item slot calls PySequence_SetItem, which can be used to
delete an element by passing a NULL value. Calling Py_INCREF on a NULL
value causes a segfault. This commit adds a NULL check similar to that
in other closure functions.

Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
2024-01-23 10:30:26 +00:00
William S Fulton 3c5119c614 Test python-3.12 on GHA, fix missing field initialization for builtin 2023-08-17 00:27:17 +01:00
Alexander Shadchin 8aab1590f2 Fix missing-field-initializers warning with Py3.12 2023-08-16 20:10:34 +03:00
William S Fulton 1d56bc6b85 Add initialisers for additional members in PyHeapTypeObject
(builtin mode) for Python-3.11 - _ht_tpname, _spec_cache.
2022-08-01 08:24:52 +01:00
William S Fulton 72bb68d556 Visual C++ warning fix in Python < 3.2 builtin wrappers
Fixes:
warning C4311: 'type cast': pointer truncation from 'void *' to 'long'
warning C4302: 'type cast': truncation from 'void *' to 'long'
2022-05-07 06:22:25 +01:00
Kris Thielemans 8dc4e3a8cb fix white-space 2022-04-28 08:00:42 +01:00
Kris Thielemans f5934b099b [Python] Replace tp_print with tp_vectorcall_offset slot for Python 3.8
The tp_print slot is no longer supported and replaced with tp_vectorcall_offset.
The (printfunc) cast that we used caused problems on PyPy.

Fixes #2262
2022-04-21 12:28:54 +01:00
Victor Stinner e902ab5160 Add Python 3.11 support: use Py_SET_TYPE()
On Python 3.9 and newer, SwigPyBuiltin_SetMetaType() now calls
Py_SET_TYPE(). Py_TYPE() can no longer be usd as an l-value on
Python 3.11:

* https://docs.python.org/dev/c-api/structures.html#c.Py_SET_TYPE
* https://docs.python.org/dev/whatsnew/3.11.html#c-api-changes
2021-12-06 23:17:33 +01:00
Chris Burr eafe1e2daa
Use Py_TYPE in SwigPyBuiltin_SetMetaType 2020-07-28 08:11:02 +02:00
Chris Burr 917110212e
PyTypeObject::ob_base isn't available in PyPy 2020-07-17 12:15:54 +02:00
Julien Schueller b536702c01 Fix missing-field-initializers warning with Py3.8
Python 3.8 adds tp_vectorcall, tp_print is added for compat just for 3.8
https://github.com/python/cpython/pull/13185/files#diff-b5db2632fa7acaf3b44abb56f18b9615
2019-11-20 15:40:52 +01:00
Christian Kellner db9822788e Use PyObject_GC_UnTrack in lieu of the old variant
The _PyObject_GC_UNTRACK[1] macro got deprecated in 3.6 and finally
removed in 3.8. Therefore use PyObject_GC_UnTrack instead.

[1] https://docs.python.org/3/c-api/gcsupport.html#c._PyObject_GC_UNTRACK
2019-07-17 11:15:17 +02:00
William S Fulton d30effd886 Python builtin closure minor tweaks 2018-09-23 19:20:22 +01:00
William S Fulton 4715a4e72c Python -builtin __contains__ fix for map and set like containers.
Fix when using -builtin and wrapping std::map, std::set, std::unordered_map or
std::unordered_set to ensure __contains__ is called. This is a wrapper for the STL
container's find method. Without it, Python will do its own slower sequence search.
2018-09-21 08:51:22 +01:00
William S Fulton e0b23969d0 Correct PyErr_Fetch/PyErr_Restore variable names 2018-08-10 08:14:19 +01:00
Olly Betts a82c1b943b Drop support for Python 3.0 to 3.3
See #701.
2018-06-08 11:08:31 +12:00
Olly Betts 7c034ead32 Remove special handling for Python < 2.6
See #701.
2018-06-08 09:34:48 +12:00
William S Fulton fbada9959c Fix types when calling PyTuple_New
Replace int with Py_ssize_t to fix warnings on Windows when using -O
and/or -builtin.
2017-08-27 14:18:00 +01:00
William S Fulton 4963a7f88f Fix Python 2 builtin tp_hash hashfunc closure regression when using "python:slot"
Closes https://github.com/swig/swig/issues/843
2016-12-23 23:41:07 +00:00
William S Fulton 87bede9a99 Return to Python builtin tp_new initialization in init function. 2016-10-15 22:43:42 +01:00
William S Fulton 96fae38be2 Fix Python pickling and metaclass for builtin wrappers
The metaclass (SwigPyObjectType) for SWIG objects was not defined in
a way that let importlib successfully import the Python wrappers.
The pickle module failed because it couldn't determine what module the
SWIG wrapped objects are in.

I've changed the definition of SwigPyObjectType using more normal
builtin type definitions. There are still some open questions:
- None of the builtin types, like swig_static_var_getset_descriptor and
  SwigPyObject are added into any module. No call to PyModule_AddObject
  is made, so isinstance cannot be used for any wrapped type, all of
  which are derived from SwigPyObject.

Closes #808
2016-10-14 07:30:44 +01:00
William S Fulton 777c825cd2 Rework Python builtin CLOSURE macros
The new approach uses support functions and minimal code in the macros
to make debugging easier.
2016-09-25 14:15:38 +01:00
William S Fulton 848628ae91 More flexible python builtin slots
The closure names used for builtin slots are mangled with their functype so
 that overloaded C++ method names can be used for multiple slots.
For example:
%feature("python:slot", "mp_subscript", functype="binaryfunc") SimpleArray::__getitem__;
%feature("python:slot", "sq_item", functype="ssizeargfunc") SimpleArray::__getitem__(Py_ssize_t n);
will generate closures:
  SWIGPY_SSIZEARGFUNC_CLOSURE(_wrap_SimpleArray___getitem__) /* defines _wrap_SimpleArray___getitem___ssizeargfunc_closure */
  SWIGPY_BINARYFUNC_CLOSURE(_wrap_SimpleArray___getitem__) /* defines _wrap_SimpleArray___getitem___binaryfunc_closure */
2016-09-25 14:15:38 +01:00
William S Fulton 0769e9b727 Fix iterators for containers of NULL pointers (or Python None) when using -builtin.
Previously iteration would stop at the first element that had a NULL pointer value.
2016-09-17 17:53:48 +01:00
William S Fulton 5b7c08c214 Make Python builtin types hashable by default
Default hash is the underlying C/C++ pointer.
This matches up with testing for equivalence (Py_EQ in SwigPyObject_richcompare)
which compares the pointers.
2016-08-23 19:06:36 +01:00
William S Fulton 253a39fdff Fix hash function type checking for older versions of Python
Python 2.6 was asserting instead of throwing a Python TypeError.
2016-08-23 18:46:28 +01:00
William S Fulton ae32fb4f9a Python builtin minor tweaks
Remove internal Python struct names from generated code
Cosmetic code formatting
2016-08-22 19:28:52 +01:00
William S Fulton f778ee19df Python builtin hashfunc closure fix
If the wrong return type in the hash function was used, an error:
  SystemError: error return without exception set
was raised

Add some tests for testing builtin slots
2016-08-18 07:10:54 +01:00
William S Fulton 7dc5b224cb Fix recent Python -builtin changes for C code 2015-12-16 18:13:31 +00:00
William S Fulton b15ad9349e Merge branch 'coleb-python35_dtor_exception_fix'
* coleb-python35_dtor_exception_fix:
  Add test case for Python 3.5 assertion with a pending StopIteration
  Amend python_destructor_exception runtime test
  Call PyErr_WriteUnraisable if a destructor sets a Python exception (-builtin)
  Extended zjturner's changes to encompass all function dispatch and use PyErr_WriteUnraisable to handle exceptions during __del__.
  Python - Save and restore exception state before calling destroy.
2015-12-16 12:58:01 +00:00
William S Fulton e4264e7ba8 Call PyErr_WriteUnraisable if a destructor sets a Python exception (-builtin)
This fixes the python_destructor_exception testcase for -builtin
2015-12-16 12:57:00 +00:00
William S Fulton 5f93c94e87 Python tp_allocs -> tp_next corrections
Updates for Python 2.5 and later and for -builtin.
2015-12-14 01:56:11 +00:00