Commit Graph

1061 Commits

Author SHA1 Message Date
William S Fulton d2a071e8b0 Fix for python-3.11+ and stable abi
For printf
2023-12-19 08:54:41 +00:00
William S Fulton dff1afce33 Fix seg fault using stable abi for Python < 3.10 for invalid unicode
Restore Python error instead of seg faulting

Fixes unicode_strings testcase for Py_LIMITED_API < 3.10
2023-12-19 00:02:25 +00:00
William S Fulton 2e956c1607 SWIG_AsCharPtrAndSize simplification and performance improvement
Only make a copy of the returned string if the passed in Python object
does not own the returned char* string. Callers of SWIG_AsCharPtrAndSize
should now ensure that the returned string is only used while the passed
in PyObject * string is valid. Performance improvement is for Python 3
input string handling.

The new SWIG_PyUnicode_AsUTF8AndSize function allows for this simplification.
2023-12-15 22:31:50 +00:00
William S Fulton 17ca54844c Revert mistaken directorout std::string_view typemap change 2023-12-14 07:24:12 +00:00
William S Fulton 84f6031aa5 Fix std::string_view typemaps for Py_LIMITED_API
Except for directorout typemaps which are fundamentally broken as a string_view is returned when viewing a deleted char *
2023-12-13 22:12:17 +00:00
William S Fulton 2c4c404e44 -Werror=declaration-after-statement fix for Python 2 2023-12-13 21:21:39 +00:00
William S Fulton f89dd59d4b Replace SWIG_Python_str_AsChar with SWIG_PyUnicode_AsUTF8AndSize
SWIG_Python_str_AsChar has undefined behaviour when Py_LIMITED_API is defined
as it returns a pointer to a string in a PyBytes object that no longer exists.

SWIG_PyUnicode_AsUTF8AndSize is an efficient replacement, but requires a
different API and the caller to decrement the refcount on the intermediate
PyObject in the Py_LIMITED_API < 0x030A0000 implementation. The alternative
would have required copying the returned char * string as was done in a
previous implementation requiring a call to the defunct SWIG_Python_str_DelForPy3
function.

Any users calling the removed API will have to make changes.
2023-12-13 19:55:34 +00:00
William S Fulton 2808e1d834 Simpler SWIG_AsCharPtrAndSize implementation - remove usage of SWIG_Python_str_AsChar 2023-12-13 08:30:22 +00:00
William S Fulton 360b19102c Python stable abi memory leak fix
Add missing Py_XDECREF after calling PyObject_GetAttrString.

Cleanup:
SWIG_Python_str_AsChar() no longer needs a call to SWIG_Python_str_DelForPy3
to free allocated memory, see 0b9d4eff09
2023-12-04 20:14:32 +00:00
William S Fulton 29626ad18d Python stable abi for std::filesystem 2023-12-04 19:18:36 +00:00
William S Fulton 1451607eda Merge branch 'py3-stable-abi'
* py3-stable-abi:
  Add CI build using -py3-stable-abi option
  Make Python buffer typemaps compatible with limited API
  Don't use PyUnicode_AsUTF8() when python limited API is used
  Make directors implementation for Python work with limited API
  Support using stable Python ABI

Conflicts:
	.github/workflows/ci.yml
	Lib/python/pyhead.swg
	Lib/python/pyrun.swg
	Source/Modules/python.cxx
2023-12-01 18:38:07 +00:00
William S Fulton 4fa04e621f Merge branch 'fix-python-threads-crash'
* fix-python-threads-crash:
  [python] Prevent a potential multithreading crash
2023-12-01 10:11:24 +00:00
William S Fulton faf1e17b45 Merge branch 'std_filesystem_python'
* std_filesystem_python:
  Add <type_traits> fragment for traits usage in std::filesystem
  Correct const std::filesystem & typemaps
  Fix memory leak wrapping const std::filesystem&
  std::filesystem pointer handling correction
  Python std::filesystem cleanup
  Cosmetic whitespace corrections
  Try to use wstring/wchar_t on windows
  Reformat
  Rename the helpers within the fragment to be more uniquely named
  Make CI rerun (and output all failed cases for the specialPath)
  Address review comments
  Need to see which assert is failing on mingw
  Verbose assert to try and see the failure on mingw
  path::string() returns a copy not a const ref.
  Remove cpp17_std_filesystem from common.mk, only defined for Python for now
  Extend roundtrip test
  Address review comment by @degasus on #1999 originally
  Had trouble getting the test to be properly ignored. If I wrap the inline line 35 in #if __cplusplus the wrapper code isn't generated
  Tweak tests setup
  Cherry pick from yasamoka/master

Conflicts:
	CHANGES.current
2023-11-18 10:50:29 +00:00
William S Fulton bd093173af Add <type_traits> fragment for traits usage in std::filesystem 2023-11-18 10:32:31 +00:00
William S Fulton 9bbd176038 Correct const std::filesystem & typemaps
Marshal to pathlib.Path
2023-11-18 10:03:52 +00:00
William S Fulton d2221f1547 Fix memory leak wrapping const std::filesystem& 2023-11-17 23:11:30 +00:00
William S Fulton ac5cd598f8 std::filesystem pointer handling correction
Only passing std:filesystem by value and const ref is marshalled to a pathlib.Path.
This is consistent with all other marhalling in SWIG.
2023-11-17 23:01:28 +00:00
William S Fulton f6d8c6b089 Python std::filesystem cleanup 2023-11-17 22:29:59 +00:00
William S Fulton 50312e21f5 Cosmetic whitespace corrections 2023-11-17 22:06:16 +00:00
William S Fulton d701f0b4b2 Correct the implementation of Python PEP 207.
Don't convert all exceptions into a NotImplemented return
when wrapping operators which are marked with %pythonmaybecall.

Closes #1783
2023-10-21 09:25:10 +01:00
Rose e2e18a7a67 Call swig_msg.c_str() directly rather than virtual method
This is faster and does not call virtual method during construction
2023-10-16 16:49:00 -04:00
William S Fulton ecaa052f3d carrays.i library modified to use size_t instead of int
in the %array_functions and %array_class macros.

Affects C#, D, Go, Guile, Java, Javascript, Lua, Ocaml, R, Racket.
Closes #1680

If the old types are required for backwards compatibility, use %apply to
restore the old types as follows:

  %include "carrays.i"
  %apply int { size_t nelements, size_t index }
  ... %array_functions and %array_class ...
  %clear size_t nelements, size_t index; # To be safe in case used elsewhere
2023-10-11 08:23:16 +01:00
William S Fulton 4b6b711fa3 Merge branch 'issue/2625'
* issue/2625:
  Enhanced std::map for non-default constructible types changes entry
  Add missing exception.i for std::map wrappers for MzScheme and Guile
  std::map wrappers and non-default constructible
  Fixed make file ordering
  Fixed line endings
  Added unit test
  Using #ifdef instead of #if to prevent warnings
  Fix for #2625 Using c++17 insert_or_assign for std::map when available.

Conflicts:
	CHANGES.current
2023-09-18 19:29:56 +01: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
Christophe Calmejane f7d9f5e931 Using #ifdef instead of #if to prevent warnings 2023-07-04 09:18:20 +02:00
Christophe Calmejane 2b583e9d19 Fix for #2625
Using c++17 insert_or_assign for std::map when available.
2023-07-04 09:18:20 +02:00
Julien Marrec e3e92986ad
Try to use wstring/wchar_t on windows 2023-07-03 09:45:58 +02:00
Julien Marrec 156b069253
Reformat 2023-07-03 09:42:39 +02:00
Julien Marrec bdce563e94
Rename the helpers within the fragment to be more uniquely named 2023-06-30 14:11:14 +02:00
Julien Marrec b2639da892
Address review comments 2023-06-30 14:11:14 +02:00
Julien Marrec 32fe204771
path::string() returns a copy not a const ref. 2023-06-30 14:11:14 +02:00
Julien Marrec 7bdbbcb028
Address review comment by @degasus on #1999 originally 2023-06-30 14:11:14 +02:00
Julien Marrec 0798a46e0d
Had trouble getting the test to be properly ignored. If I wrap the inline line 35 in #if __cplusplus the wrapper code isn't generated 2023-06-30 14:11:14 +02:00
Ramzi Sabra 2fcbd937f7
Cherry pick from yasamoka/master
Co-authored-by: Julien Marrec <julien.marrec@gmail.com>
2023-06-30 14:11:14 +02:00
Olly Betts 8ad0b79740 Lib/python/embed.i: Note doesn't work with Python 3 2023-06-15 15:32:38 +12:00
Olly Betts 779fa45601 Remove deprecated BOTH typemap rule
This has been a deprecated alias for INOUT since 2002 (SWIG 1.3.14).
2023-06-15 15:07:04 +12:00
Olly Betts 71eacdf90c Avoid setting python error for bytes input 2023-05-31 11:51:07 +12:00
Olly Betts 4f73ba8653 Remove commented out code left over from development 2023-05-31 10:51:15 +12:00
Olly Betts 51431b4b14 [python] Support bytes in std::string_view typemaps
Handle bytes or unicode where we do for std::string.
2023-05-31 10:49:39 +12:00
Olly Betts 47fcd1fb80 Remove lingering uses of ancient documentation system
The documentation system these were from was removed from SWIG 23 years
ago - the %subsection directive now only emits a warning, and the %text
directive is a functional no-op.

The text in these directives seems useful to keep so just convert them
to comments.

Presumably nobody noticed because these remaining uses were all
protected by `#ifdef AUTODOC` which nothing now defines.
2023-05-29 12:03:12 +12:00
Olly Betts 29d1f95d0d [Python] Add string_view support
Only implemented for Python 3, at least for now.

See #1567
2023-05-26 17:45:24 +12:00
Olly Betts fcf0d7deab Tweak comment formatting 2023-05-23 09:20:31 +12:00
William S Fulton 3a2318cb57 Finish removal of SwigPySequence_Cont 2023-04-26 19:38:39 +01:00
William S Fulton 1835580589 Remove undocumented and non-existent STL std::carray 2023-04-26 18:18:15 +01:00
William S Fulton 08aee96cbd Remove assign method uses by the removed Python Sequence Protocol 2023-04-26 18:18:15 +01:00
William S Fulton d5291a77e1 Remove now redundant use of Python Sequence protocol in STL wrappers
Removes SwigPySequence_Cont and support classes:
SwigPySequence_ArrowProxy, SwigPySequence_Cont, SwigPySequence_Ref

The recently added Iterator Protocol support should cover the Sequence
Protocol usage for converting from Python containers to STL containers.
2023-04-26 18:18:15 +01:00
William S Fulton b2fd91bc41 Add support for all STL containers to be constructible from a Python set 2023-04-26 18:18:15 +01:00
William S Fulton 6098b26f3e Iterator Protocol support for std::array wrappers
Introduce swig::IteratorProtocol class and assign which can be partially
specialized by different container types, such as std::array.
2023-04-26 18:18:11 +01:00
William S Fulton b72703acdf STL support for copying Python objects supporting Iterator protocol
std::array not working though
2023-04-01 21:57:34 +01:00