Commit graph

254 commits

Author SHA1 Message Date
Olly Betts
c864546b4a [Python] Fix implicit_conv tuple handling regression
Introduced in SWIG 4.0.0.  Fixes #1553, reported by Alexandre
Duret-Lutz.
2019-06-03 10:13:58 +12:00
William S Fulton
32b963ef1c Re-organise Python method creation and docstring functions declarations
Fix ‘PyMethodDef* SWIG_PythonGetProxyDoc(const char*)’ declared ‘static’ but
never defined [-Wunused-function]

Closes #1448 (again)
2019-02-19 22:54:08 +00:00
William S Fulton
43438b66ab Re-organise some generate Python code for method creation and docstring support
Fix ‘PyMethodDef* SWIG_PythonGetProxyDoc(const char*)’ declared ‘static’ but
never defined [-Wunused-function]

Closes #1448
2019-02-10 22:55:16 +00:00
William S Fulton
ef8a92578b Correct Python implicitconv code
Py_None is only possible with implicitconv so move this block of code
into the implicitconv if block.
2018-12-29 12:09:04 +00:00
William S Fulton
3efea1f4ab Fix typecheck typemaps for non-pointers and NULL
The typecheck typemaps succeed for non pointers (SWIGTYPE, SWIGTYPE&,
SWIGTYPE&&) when the equivalent to C NULL is passed from the target
language. This commit implements a fix for Python to not accept a Python
None for non-pointer types.

Issue #1202
2018-12-29 11:45:46 +00:00
William S Fulton
d9ecff1fca Merge branch 'chlandsi-master'
* chlandsi-master:
  Testcase for testing __new__ override Python 3.6 fix
  fix for creating new shadow class in python 3.6
2018-12-20 08:12:41 +00:00
William S Fulton
b8fc71b5f8 Remove functions associated with tp_print that are now redundant
Removes SwigPyPacked_print and swig_varlink_print
2018-12-11 08:08:14 +00:00
Andrew Rogers
f8bf286a6e #1368: AV in tp_print caused by mismatched Python/extension CRT usage 2018-12-11 07:54:57 +00:00
Christian Landsiedel
3208ff2bf5 fix for creating new shadow class in python 3.6 2018-11-22 14:24:49 +01:00
William S Fulton
0a9b36d3be Remove -outputtuple and -nooutputtuple command line options (Python)
Both the command line and %module options of the same name have been
removed. These were undocumented. The -outputtuple option returned a
Python tuple instead of a list, mostly typically in the OUTPUT
typemap implementations.

It unclear why a tuple instead of a list return type is needed and
hence this option has been removed as part of the simplification of
the SWIG Python command line options for SWIG 4.

Issue #1340.
2018-11-13 19:46:31 +00:00
William S Fulton
a1f40568d6 Remove non-const char * usage where the Python API now supports it
Python fixed many APIs to use const char * instead of char * at around
Python 2.4. As we support 2.7 and later, we can now remove the non-const
string usage.
Types changed:
  PyArg_ParseTuple
  PyArg_ParseTupleAndKeywords
  PyArg_UnpackTuple
  PyDict_SetItemString
  PyMethodDef
  PyModuleDef
  SWIG_Python_UnpackTuple
  SWIG_Python_str_FromChar
  SWIG_addvarlink
  swig_const_info
2018-10-22 08:19:50 +01:00
William S Fulton
b2aa01f492 Python options simplification: Remove -buildnone, -nobuildnone
A custom implementation for Py_None was implemented in SWIG_Py_None().
This was used by default on Windows only. It isn't clear why this
was done just for Windows. Now Py_None is the real Py_None on all
operating systems.
2018-10-22 08:19:50 +01:00
William S Fulton
142d4062df Fix invalid free in SWIG_Python_AddErrMesg
Closes #1331
2018-09-28 07:08:12 +01:00
William S Fulton
3af1b74e57 Python implicitconv assert fix using debug build of the Python interpreter
An error was not being set if an implicit conversion was attempted
calling an explicit constructor.

Fixes:
Fatal Python error: a function returned NULL without setting an error
SystemError: <built-in function new_A> returned NULL without setting an error

File "Examples/test-suite/python/implicittest.py", line 106 in __init_
2018-09-04 07:22:20 +01:00
Andreas Gaeer
0165180735 Fix assert in PyTuple_GET_SIZE
Occurs in debug interpreter builds of python-3.7 when calling tp_new in
a few testcases such as Examples/python/extend.

Closes #1321
2018-09-03 19:52:43 +01:00
William S Fulton
e0b23969d0 Correct PyErr_Fetch/PyErr_Restore variable names 2018-08-10 08:14:19 +01:00
William S Fulton
5ce880ee41 Fix SWIG_Py_None regression from last commit 2018-07-18 11:44:27 +01:00
William S Fulton
344e9fefa8 Python singletons tidy up 2018-07-17 20:23:55 +01:00
William S Fulton
8e6799d783 Revert "Merge branch 'petrmitrichev-remove-function-local-statics'"
This reverts commit b8ed7759d5, reversing
changes made to 6f2399e86d.
2018-07-17 08:46:53 +01:00
William S Fulton
f8ffc04215 Use SWIGRUNTIME for SwigPyObject_repr2 2018-06-30 19:30:58 +01:00
William S Fulton
4b4e0180f4 Python initialization code tidy up
I've moved the initialization of statics a little earlier on - a little
safer as it is hard to follow exactly when some of these were being
used, such as SWIG_Py_None which sometimes replaces Py_None.
2018-06-30 19:28:03 +01:00
William S Fulton
b3763c8dc8 Remove SWIG_STATIC_POINTER (Python)
It's use has been removed to discourage thread unsafe static
initialisation.
2018-06-30 17:47:01 +01:00
Petr Mitrichev
40ea0a9606 Add comments that explain the need for globals. 2018-06-28 17:33:31 +02:00
Petr Mitrichev
d956081fd3 Avoid function-local statics that call Python code to avoid deadlocks with GIL 2018-06-28 14:27:52 +02:00
William S Fulton
ecf61f8e39 Restore Python 3.2/3.3 support 2018-06-12 08:02:23 +01:00
Olly Betts
7f98830110 [Python] Fix new GCC8 warnings in generated code
Avoid casts between incompatible function types where possible (when
keyword args are in use, it is not possible to avoid such warnings as
they are inherent in the design of Python's C API in that particular
case).  Fixes #1259.
2018-06-11 15:51:53 +12:00
Olly Betts
2ea976f1d8 Always use PyCapsule
It is supported by all the Python versions we now support.
2018-06-08 11:08:31 +12: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
1198155083 Drop Python 2.6 support too
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
Olly Betts
c9d2275a0c Remove lingering trace of support for Python 1.x 2018-06-08 08:47:24 +12:00
William S Fulton
b0e29fbdf3 Add missing checks for failures in calls to PyUnicode_AsUTF8String.
Previously a seg fault could occur when passing invalid UTF8 strings (low
surrogates), eg passing u"\udcff" to the C layer (Python 3).
2017-12-04 20:14:04 +00:00
Olly Betts
90f9117e10 Fix various comment and documentation typos 2017-08-13 18:04:33 +12:00
Mark Dufour
21f532975f [Coverity] fix issue reported for SWIG_Python_ConvertFunctionPtr
Fix Coverity issue reported for SWIG_Python_ConvertFunctionPtr:

"Execution cannot reach this statement: *ptr = vptr;"

Because if 'ty' is null, then desc becomes null and we return with
SWIG_ERROR. So 'ty' cannot be null at 'if (ty)'.
2017-02-19 09:47:34 +13: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
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
Brian Cole
a863e98874 Extended zjturner's changes to encompass all function dispatch and use PyErr_WriteUnraisable to handle exceptions during __del__.
Also added test cases for the unnamed temporary destruction that is throwing assertions in Python 3.5.
2015-12-15 08:39:55 -07:00
William S Fulton
3e27d45b2a Merge branch 'ahnolds-python34'
* ahnolds-python34:
  Python tp_allocs -> tp_next corrections
  Cosmetic correction for Python tp_version -> tp_version_tag
  Add -Wmissing-field-initializers to python Travis testing
  Python 3.3 builtin missing field initializers added
  Adding tp_finalize field to PyTypeObject for Python 3.4 and -builtin
  Adding nb_matrix_multiply and nb_inplace_matrix_multiply fields to PyNumberMethods for Python version 3.5 and up
  Adding tp_finalize field to PyTypeObject for Python version 3.4 and up
2015-12-14 01:59:39 +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
William S Fulton
24b4a0fb94 Cosmetic correction for Python tp_version -> tp_version_tag 2015-12-14 01:29:43 +00:00
William S Fulton
c5322a9ecb Python use Py_ssize_t instead of int for better portability 2015-12-05 09:00:04 +00:00
William S Fulton
4e8ea4e853 Python SystemError fix with -builtin
Fix error when append on a SWIG Object with -builtin:

  x.append(10)
  SystemError: error return without exception set

Having append and next methods on a SWIG object by default doesn't seem
right to me though.
2015-11-14 22:14:32 +00:00
Zachary Turner
2fa9454c9f Python - Save and restore exception state before calling destroy.
PyObject_CallFunction has the potential to silently drop the active
exception.  In cases where the user just finished iterating a
generator, StopIteration will be active.  Most of the time this
is fine because destroy() won't raise an exception.  On Python 3
however, and with a debug interpreter, you will get an assertion
failure inside of Python.  And in the worst case scenario, if
destroy() does throw an exception, the intepreter probably won't
be able to correctly detect the end of the iteration.
2015-11-13 21:07:44 -08:00
Alec Cooper
b7a3516801 Adding nb_matrix_multiply and nb_inplace_matrix_multiply fields to PyNumberMethods for Python version 3.5 and up 2015-10-28 16:42:53 -04:00
Alec Cooper
e79349d886 Adding tp_finalize field to PyTypeObject for Python version 3.4 and up 2015-10-27 20:12:03 -04:00
Olly Betts
b06ec2c847 Fix typo in method description 2015-05-10 01:23:09 +12:00
Yoann Vandoorselaere
327d7c968d Attribute of SWIG wrapped classes instances were overwritten on __init__()
When a SWIG classes instances is initialized, its internal dictionary was
reset to NULL, which result in the loss of any attribute that might have
been set for the instance.

Only initialize the internal dictionary on actual PyObject creation.

class Test(MySwigWrappedClass):
        def __init__(self):
                self.val = "Random Value"
                MySwigWrappedClass.__init__(self)

p = Test()
print hasattr(p, "val") # Should return True, but used to return False
2015-04-11 02:05:11 +07:00
Yoann Vandoorselaere
92b88db7ab Make __dict__ accessible for Python builtin classes
Attribute set within instance of a SWIG Python wrapped class are
stored in SwigPyObject->dict, which tp_dictoffset slot is pointing to.

However, SWIG wrapped classes did not have a __dict__ attribute.
Inheriting subclasses did not get the attribute either because the
SWIG wrapped classes initialize the tp_dictoffset slot:

From http://bugs.python.org/issue16272:

"If a type defines a nonzero tp_dictoffset, that type is responsible for
defining a `__dict__` slot as part of the tp_getset structures. Failure to
do so will result in the dict being inaccessible from Python via
`obj.__dict__` from instances of the type or subtypes."

Provide a SwigPyObject_get___dict__() function to retrieve the dict
attribute or create it when it does not exist yet (it is normally
created when setting attribute set), and a PyGetSetDef entry pointing
to this function.
2015-04-11 02:05:06 +07:00
Curtis Dunham
fe91d6449f Remove register storage class declarations
They're unnecessary, anacronistic, deprecated in modern
standards, generally ignored, useless, and (most importantly)
clang complains about them.
2014-02-19 11:58:27 -06:00
Harvey Falcic
c063bb8384 Fix shadow instance creation failure in Python 3
I managed to trace a very nasty Python interpreter segfault to an
allocation failure here. Adding this after the tp_new call:
if (PyErr_Occurred()) {
    PyErr_Print();
}

results in output of 'TypeError: object() takes no parameters', followed
by a segfault that takes down the Python interpeter.

The 'object' constructor doesn't seem to be suitable for instantiating
SWIG shadow instances in this way, so simply use the constructor
function in the PyTypeObject 'tp_new' slot of data->newargs.

The 'if (inst)' check after this doesn't hurt in as much as it prevented
a segfault immediately after this failed allocation, but it doesn't help
much since the null pointer dereference will probably happen sooner or
later anyway.
2014-02-14 19:19:09 -05:00