Commit graph

965 commits

Author SHA1 Message Date
Eugene Toder
f733efd3c0 Use different capsule names with and without -builtin
Types generated with and without -builtin are not compatible. Mixing
them in a common type list leads to crashes. Avoid this by using
different capsule names: "type_pointer_capsule" without -builtin and
"type_pointer_capsule_builtin" with.

See #1684
2022-03-18 13:44:54 -04:00
William S Fulton
dea0c3f0b2 Add some code comments about Python objects and capsule reference counting
Issue #2208
2022-03-15 08:46:20 +00:00
Olly Betts
b4a92ae34f Fix SWIG_Py*Method_New conditional
This should be a SWIG-time conditional, but was inside %{ %} so was
instead a compile-time conditional.

Fix for bug in the changes in #2191
2022-03-14 12:49:41 +13:00
Olly Betts
e5996be10c [Python] Fix memory leak.
SWIG python objects were being freed after the corresponding SWIG
module information was destroyed in Python 3, causing leaks when as
a result the object destructor could not be invoked. To prevent this
misordering, SWIG python objects now obtain a reference to the
Python capsule wrapping the module information, so that the module
information is correctly destroyed after all SWIG python objects
have been freed (and corresponding destructors invoked).

Fixes #2154
Fixes #2208
2022-03-07 14:29:17 +13:00
Olly Betts
b127e11f1e Fix typos in docs and comments 2022-02-27 18:15:46 +13:00
Seth R Johnson
de78b80de9 Renames performed by %namewarn with rename= are printed in warning message
This is necessary for regex-like renames (where you can't use the #define trick
as is done in many of the %keywordwarn directives). It's now unnecessary to print
the "renaming to '`x`'" code explicitly by the kw.swg files.
2022-02-06 13:51:37 -05:00
Olly Betts
951f946341 [Python] Improve handling of SWIG_Py*Method_New
The SWIG_PyInstanceMethod_New method is no longer added to wrapped
classes except when it's actually needed, which is when
(!builtin && fastproxy) is true (which it isn't by default).

The SWIG_PyStaticMethod_New method is no longer is now similarly
gated - previously only (fastproxy) was checked.

Finally the C/C++ functions that implement these were always compiled
into the module, but now they're only included if
(!builtin && fastproxy) is true.

Issue noted by vadz in #2190.
2022-02-04 12:58:56 +13:00
Olly Betts
bede0b407b [python] Workaround MSVC2022-related bug
Fixes #2090
2022-02-03 17:54:50 +13:00
Olly Betts
7963807308 [python] Remove unused bogus fallback macro
This fallback version of PyString_AS_STRING() for Python 3 makes use of
PyUnicode_AS_STRING, but I can find no evidence that ever existed in
Python - all references I've found are to SWIG or SWIG-generated code.

The only uses of PyString_AS_STRING() in SWIG generated code are for
Python 2 #if-branches, so this fallback is never used by SWIG.

Because it doesn't work it can't be usefully used in user interface
files either, so let's remove it to avoid potential user confusion
(such as #987).
2022-01-27 09:32:25 +13:00
William S Fulton
3aa302c08f %callback and Python class access for C++ static member functions fixes
Fix access to C++ static member functions using Python class
staticmethod syntax, such as Klass.memberfunction instead of
Klass_memberfunction, when using -fastproxy and -builtin in
combination with %callback.

The docstring containing the callback pointers were not being patched
during module initialisation.
2022-01-14 23:00:59 +00: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
John Senneker
160b8c5da3 Added extern "C" block that was removed in previous commit. 2021-12-01 15:15:24 -05:00
John Senneker
ebe14e6e2a Fixed crashes when using embedded Python interpreters.
Fixes #2101. There are 3 related changes made here:
1. Move the SWIG_globals() singleton into pyrun from pyint so it
   is visible to SWIG_Python_DestroyModule(). The static globals
   varlink has been extracted out of the function so that it can
   be set to NULL in SWIG_Python_DestroyModule(), which fixes the
   issue described in #2101. (Now when the second interpreter
   starts up, the Swig_Globals_global pointer will be NULL, so it
   knows it has to create a new one.)
2. Remove a Py_DECREF on the globals varlink. The decrement is now
   performed by DestroyModule(), so there's no need to do it in
   SWIG_init().
3. Fixed similar issue with SWIG_Python_TypeCache().
2021-11-30 16:13:17 -05:00
Andrew Rogers
448e8d57bd Further leak fixes 2021-05-18 13:07:48 +01:00
Andrew Rogers
16123466f4 Update tests for failing Python API calls to all use '!= 0' 2021-05-18 13:05:51 +01:00
Andrew Rogers
4f453e0cde Tidy up handling of OOM exceptions - Py*_New will call PyErr_NoMemory() internally, so there is no need to call it again here, just correctly handle the NULL return value 2021-05-17 22:50:52 +01:00
Andrew Rogers
84ff84f4fb [Python] Fix memory leaks. 2021-05-12 23:42:32 +01:00
Olly Betts
f198ff0a43 Fix more "allows to" and other typos 2021-04-21 15:54:46 +12:00
Julien Schueller
9ed60ac7a0 Drop deprecated PyEval_CallObject method
see https://docs.python.org/3.9/whatsnew/3.9.html
2020-10-15 08:43:21 +02:00
William S Fulton
4b5baf0a60 0.0 float warning fix 2020-10-10 15:02:26 +01:00
William S Fulton
57957787b7 Convert C++ comment to C comment 2020-10-10 14:53:46 +01:00
William S Fulton
4f184500d7 Merge commit '8245277ad3' into c99-complex
* commit '8245277ad3':
  Remove test for unsupported complex or _Complex by itself
  More C99 complex fixes, plus Python tests
  Restore _Complex as standalone type
  Small corrections for handling C99 _Complex
  Properly handle C99 complex types even in C++ mode

Conflicts:
	Examples/test-suite/python/complextest_runme.py
2020-10-10 14:53:33 +01:00
William S Fulton
4d5f4bcd33 Merge branch 'PyBuffer_Release-pybuffer'
* PyBuffer_Release-pybuffer:
  Add tests.
  Fix the error handling for the PyObject_GetBuffer() calls in pybuffer.i
2020-10-07 22:37:17 +01:00
William S Fulton
9c50887daa Python 3.9 support
Remove PyEval_InitThreads() call for Python 3.7 and later as Python calls
it automatically now. This removes a deprecation warning when using Python 3.9.

https://docs.python.org/3/c-api/init.html#c.PyEval_InitThreads
2020-08-27 19:37:26 +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
Leo Singer
511df0e642 More C99 complex fixes, plus Python tests 2020-06-24 20:21:47 -04:00
Leo Singer
07b4b274e5 Restore _Complex as standalone type 2020-06-24 20:21:47 -04:00
Leo Singer
1adc7dac5d Small corrections for handling C99 _Complex 2020-06-24 20:21:47 -04:00
Leo Singer
13260f95b0 Properly handle C99 complex types even in C++ mode
Use the `_Complex` keyword rather than the `complex` macro.

Fixes #1487.
2020-06-24 20:21:47 -04:00
Zackery Spytz
772dfd5dcb Fix the error handling for the PyObject_GetBuffer() calls in pybuffer.i
Closes #1640.
2020-06-07 17:47:11 -06:00
William S Fulton
02e967d05d
Merge pull request #1716 from ZackerySpytz/Python-utf8-cache
Use PyUnicode_AsUTF8() for Python >= 3.3
2020-05-29 23:56:10 +01:00
William S Fulton
67a38589a8 Minor code refactor in Python traits_asptr::asptr 2020-02-13 19:32:13 +00:00
William S Fulton
339d427910 Merge branch 'master-fix-vector-shared_ptr'
* master-fix-vector-shared_ptr:
  fixing memleak of shared_ptr objects in python with creating a [wrapped] vector<shared_ptr<Foo>> from a list of shared_ptr<Bar> where Bar is derived from Foo.
2020-02-13 19:21:12 +00:00
William S Fulton
94b4c7dc21 Better error checking when setting 'this' in Python
If python_append.i is modified to use:
__slots__ = []
instead of
__slots__ = ["this"]
then this additional error checking prevents a crash and shows a stack trace and error:
  AttributeError: 'ForSlots' object has no attribute 'this'

Related to issue #1674
2020-02-06 07:27:08 +00:00
William S Fulton
a01e8474f6 Fixing setting this in Python when using __slots__
Don't attempt to use the class's __dict__ for setting 'this' when
a user has extended a class with:
    __slots__ = ['this'].
Was segfaulting. Now we fall back to a simple PyObject_SetAttr if the
usual approach to setting 'this' in __dict__ does not work.

Closes #1673 Closes #1674
2020-02-06 07:08:16 +00:00
Zackery Spytz
f6d97d2338 Use PyUnicode_AsUTF8() for Python >= 3.3 2020-01-31 23:48:32 -07:00
William S Fulton
10cbc9481f Merge branch 'Python-coverity'
* Python-coverity:
  Fix possible refleaks.
  Check Py{Bytes,String}_AsStringAndSize() for failure
2020-01-31 19:14:47 +00:00
Zackery Spytz
58e409dd2b Fix possible refleaks. 2020-01-29 10:00:22 -07:00
Julien Schueller
96c6bf4cc2 Fix unused-parameter warning in pycontainer.swg 2020-01-17 11:09:57 +01:00
William S Fulton
3759fcf999 Merge branch 'builtin-ctor-kwargs'
* builtin-ctor-kwargs:
  Python -builtin constructors silently ignored keyword arguments.
2020-01-13 19:29:47 +00:00
William S Fulton
67e8334ac8 Python -builtin constructors silently ignored keyword arguments.
Instead of silenty ignoring them, now a "TypeError: f() takes no keyword arguments"
exception is thrown if keyword arguments are used. Hence constructors and normal
methods/functions behave in the same way.

Closes issue #1595
2020-01-13 19:26:22 +00:00
Zackery Spytz
6adf19b52f Check Py{Bytes,String}_AsStringAndSize() for failure
PyBytes_AsStringAndSize() and PyString_AsStringAndSize() were not
being checked for failure.

Closes #1349.
2020-01-09 21:14:12 -07: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
William S Fulton
97a107ed25
Merge pull request #1619 from emminizer/fix-msvc2019-python
Fix error in generated code for Python in MSVC 2019.
2019-10-01 08:12:58 +01:00
William S Fulton
719eea090d Improve error handling calling PyObject_SetAttr
Less obscure error when setting 'this' on the SWIG proxy object
attempting to override __setattr__ in C++ (swig-user mailing list
query 19 Aug 2019).
2019-09-13 07:37:03 +01:00
Daniel Emminizer
9fc57f47bd Fix error in generated code for Python in MSVC 2019.
Visual Studio 2019 release builds:
error C4703: potentially uninitialized local pointer variable 'p' used
2019-08-29 09:10:41 -04:00
William S Fulton
86cb3a9532 Python STL container method overloading fix
Fix method overloading of methods that take STL containers of different types.
Due to some error handling that was not cleared during typehecking.
2019-08-06 19:36:14 +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
b655d3138b Remove the UnknownExceptionHandler director error handling class
Done in order to be C++17 compliant as it uses std::unexpected_handler
which was removed in C++17. This class was intended for director
exception handling but was never used by SWIG and was never documented.

Closes #1538
2019-06-27 07:40:49 +01:00