From 6226d58065005a31f3737530ae93eaa628ae4995 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 27 Mar 2024 18:58:14 +0000 Subject: [PATCH] Fix for using more than one std::string_view type in a method Fixes #2844 --- CHANGES.current | 3 +++ Examples/test-suite/cpp17_string_view.i | 3 +++ .../python/cpp17_string_view_runme.py | 2 ++ Lib/python/std_string_view.i | 20 +++++++++---------- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 594d95777..c2840524d 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.3.0 (in progress) =========================== +2024-03-27: wsfulton + [Python] #2844 Fix for using more than one std::string_view type in a method. + 2024-03-27: wsfulton [R] #2847 Add missing std::vector and std::vector typemaps which were missing depending on whether or not SWIGWORDSIZE64 was defined. diff --git a/Examples/test-suite/cpp17_string_view.i b/Examples/test-suite/cpp17_string_view.i index ae108372a..aa9e0f015 100644 --- a/Examples/test-suite/cpp17_string_view.i +++ b/Examples/test-suite/cpp17_string_view.i @@ -52,6 +52,9 @@ std::string_view test_reference_input(std::string_view &input) { return input; } +void test_multiple(std::string_view aa, std::string_view bb, const std::string_view &cc, const std::string_view &dd) { +} + void test_throw() TESTCASE_THROW1(std::string_view){ static std::string_view x = "test_throw message"; throw x; diff --git a/Examples/test-suite/python/cpp17_string_view_runme.py b/Examples/test-suite/python/cpp17_string_view_runme.py index 0f1be15e4..71b5688d1 100644 --- a/Examples/test-suite/python/cpp17_string_view_runme.py +++ b/Examples/test-suite/python/cpp17_string_view_runme.py @@ -35,6 +35,8 @@ stringPtr = cpp17_string_view.test_reference_out() cpp17_string_view.test_reference(stringPtr) +cpp17_string_view.test_multiple("fee", "fi", "fo", "fum") + # Global variables s = "initial string" if cpp17_string_view.ConstGlobalString != "const global string": diff --git a/Lib/python/std_string_view.i b/Lib/python/std_string_view.i index 1c0d64ade..702dd9b49 100644 --- a/Lib/python/std_string_view.i +++ b/Lib/python/std_string_view.i @@ -28,13 +28,13 @@ namespace std { #endif %} - %typemap(in) string_view (PyObject *bytes = NULL) %{ + %typemap(in) string_view (PyObject *bytes = NULL) { Py_ssize_t len; -#ifdef SWIG_PYTHON_STRICT_BYTE_CHAR +%#ifdef SWIG_PYTHON_STRICT_BYTE_CHAR const char *p = PyBytes_AsString($input); if (!p) SWIG_fail; len = PyBytes_Size($input); -#else +%#else const char *p; if (PyUnicode_Check($input)) { p = SWIG_PyUnicode_AsUTF8AndSize($input, &len, &bytes); @@ -44,21 +44,21 @@ namespace std { if (!p) SWIG_fail; len = PyBytes_Size($input); } -#endif +%#endif $1 = std::string_view(p, len); - %} + } %typemap(freearg) string_view %{ SWIG_Py_XDECREF(bytes$argnum); %} - %typemap(in) const string_view & ($*1_ltype temp, PyObject *bytes = NULL) %{ + %typemap(in) const string_view & ($*1_ltype temp, PyObject *bytes = NULL) { Py_ssize_t len; -#ifdef SWIG_PYTHON_STRICT_BYTE_CHAR +%#ifdef SWIG_PYTHON_STRICT_BYTE_CHAR const char *p = PyBytes_AsString($input); if (!p) SWIG_fail; len = PyBytes_Size($input); -#else +%#else const char *p; if (PyUnicode_Check($input)) { p = SWIG_PyUnicode_AsUTF8AndSize($input, &len, &bytes); @@ -68,10 +68,10 @@ namespace std { if (!p) SWIG_fail; len = PyBytes_Size($input); } -#endif +%#endif temp = std::string_view(p, len); $1 = &temp; - %} + } %typemap(freearg) const string_view & %{ SWIG_Py_XDECREF(bytes$argnum);