Commit Graph

570 Commits

Author SHA1 Message Date
Vadim Zeitlin db65ae5aea Merge branch 'master' into doxygen
Fix the usual conflicts in autodoc unit test due to fixing the
divergences in autodoc generation between builtin and default cases in
this branch.
2017-09-19 14:02:53 +02:00
William S Fulton 2fee7c96cc Remove C casts in PyTypeObject for Python builtin type declarations
Choose compile time failures over runtime errors.
This change highlighted a problem in the constructor wrappers for tp_init
where the kwargs parameter was missing in the generated wrapper function.
Leave casts in for 0 initialization.
2017-09-12 18:10:57 +01: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 7be6c10d4a Fix display of documented template types when using the autodoc feature for Python. 2017-07-07 19:35:44 +01:00
William S Fulton a2267a8152 Memory leak fixes in Python default argument handling 2017-06-23 14:19:12 +01:00
Olly Betts 2425c8d6d8 Don't handle cases like -1U as Python constants 2017-06-23 14:54:50 +12:00
Michael Thon 80ffb169c1 [Python] fix and improve default argument handling
1. Fix negative octals. Currently not handled correctly by `-py3`
   (unusual case, but incorrect).
2. Fix arguments of type "octal + something" (e.g. `0640 | 04`).
   Currently drops everything after the first octal. Nasty!
3. Fix bool arguments "0 + something" (e.g. `0 | 1`) are always
   "False" (unusual case, but incorrect).
4. Remove special handling of "TRUE" and "FALSE" from
   `convertValue` since there's no reason these have to match
   "true" and "false".
5. Remove the Python 2 vs. Python 3 distinction based on the
   `-py3` flag. Now the same python code is produced for default
   arguments for Python 2 and Python 3. For this, octal default
   arguments, e.g. 0644, are now wrapped as `int('644', 8)`. This
   is required, as Python 2 and Python 3 have incompatible syntax
   for octal literals.

Fixes #707
2017-06-23 13:38:49 +12:00
William S Fulton 687cf9c9c1 Add missing %python:maybecall to operator overloads.
This ensures NotImplemented is returned on error so that the Python
interpreter will handle the operators correctly instead of throwing an
exception. NotImplemented was not being returned for non-builtin wrappers
when the operator overload did not have a function overload.

See PEP 207 and https://docs.python.org/3/library/constants.html#NotImplemented

Mentioned in SF patch #303 and SF bug #1208.
2017-06-19 19:25:27 +01:00
William S Fulton 81ba06e59e Fix Python negative unsigned default values
Closes #993
2017-05-21 00:02:08 +01:00
Julien Schueller ed54cc904c Fix E731: do not assign a lambda expression
https://www.python.org/dev/peps/pep-0008/#programming-recommendations
2017-05-19 22:49:23 +02:00
William S Fulton ebd37155a8 Fix potential use of uninitialized variables in directors 2017-05-13 22:40:59 +01:00
Christophe Duvernois 4777a0ad3c #526 : propagate c++11 noexcept to director classes 2017-04-27 23:37:15 +02:00
Mark Dufour 13eeebd2fb [Coverity] fix issue reported for wrapper argument checking
Fix Coverity issue reported for wrapper argument checking:

"Null-checking args suggests that it may be null, but it has already
been dereferenced on all paths leading to the check."

So 'args' is null checked, but after dereferencing it with
PyTuple_Check(args).
2017-03-06 14:07:47 +01:00
Vadim Zeitlin e668c47b70 Merge branch 'master' into doxygen
The way Python docstrings are indented has changed on master, so use the
standard inspect module in Python autodoc unit test to ignore the differences
in their indentation level between -builtin and non-builtin cases to make the
test still pass with the branch version, which avoids the use of different
(but almost identical) values in the test itself.
2016-12-12 01:24:17 +01:00
William S Fulton 2d8ce2a5d7 Slight tweak to empty lines generated in python proxy file 2016-12-03 15:56:09 +00:00
William S Fulton bed5e5c105 Fix previous commit adding moduleimports option 2016-12-03 15:28:18 +00:00
William S Fulton 2a42031b08 Custom Python module importing code
Add optional moduleimport attribute to %module so that the
default module import code can be overridden. See the
"Searching for the wrapper module" documentation in Python.html.
Example:

  %module(moduleimport="import _foo") foo

$module also expands to the low-level C/C++ module name, so
the following is the same as above

  %module(moduleimport="import $module") foo

Issue https://github.com/swig/swig/issues/769
2016-12-01 18:59:33 +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 d29fc38d1c Fix Python builtin binaryfunc slots when using -O
Code did not compile - recent regression due to 848628
2016-10-14 07:30:44 +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
Olly Betts 849f157efa [Python] Fix import in frozen app for Python 2.6 (#784)
Fix import handling for Python 2.6 to work in a frozen application.
Fixes #145, reported by Thomas Kluyver.
2016-09-12 08:11:47 +12: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 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 ad452edf3e Cosmetic code formatting corrections 2016-08-22 08:48:04 +01:00
William S Fulton bf4174d121 Further additional Python builtin slots as features for user customization
Added:
- tp_flags
- was_sq_slice
- was_sq_ass_slice
2016-08-22 08:39:24 +01:00
William S Fulton e5a09c4141 Add additional Python builtin slots as features for user customization
Added:
- tp_is_gc
- tp_bases
- tp_mro
- tp_cache
- tp_subclasses
- tp_weaklist
- tp_del
- tp_allocs
- tp_frees
- tp_maxalloc
- tp_prev
- tp_next
2016-08-22 07:51:42 +01:00
William S Fulton 3b6f4af15c Set tp_new statically rather than during initialisation 2016-08-21 14:29:27 +01:00
William S Fulton 3744c45082 Customize slots tp_basicsize tp_methods tp_getset 2016-08-19 23:46:50 +01:00
William S Fulton 239785ce7d Additional Python builtin slot overrides and slightly better formatted Python generated code
The following slots can now be overidden by users if they know what they
are doing:
- tp_dictoffset
- tp_init
- tp_new
- tp_as_number
- tp_as_sequence
- tp_as_mapping
- tp_as_buffer
2016-08-19 23:16:32 +01:00
William S Fulton 67ece1d2bf Python getSlot() refactor
getSlot() now takes a default that can override "0" default - simpler
code and for future commits to override some currently non-overridable
slots.
2016-08-19 08:43:40 +01:00
William S Fulton a3fc743f32 Add Python builtin closure method in comment
For less mystifying code and easier code searching
2016-08-18 08:41:07 +01:00
William S Fulton 9b73cd036f Avoid polluting global python namespace with version_info symbol 2016-06-07 08:12:48 +01:00
William S Fulton 81adedd7dd Python: Do not import all of sys when using -relativeimport 2016-06-06 22:20:07 +01:00
Mike Romberg d55151ac70 > to >= 2016-06-06 13:26:55 -06:00
Mike Romberg 9ce4b58df4 Make the check for python3 -relative does python runtime check. 2016-06-06 01:28:40 -06:00
Mike Romberg 1495313e53 fix for new split module test cases. 2016-06-02 23:56:15 -06:00
William S Fulton bee6964b48 Compile warning fix 2016-05-28 15:13:23 +01:00
William S Fulton d01efd82e1 Merge branch 'mromberg-implpkg'
* mromberg-implpkg:
  Minor edits to Python implicit namespace package docs
  use %inline for test
  use relative import for -builtin and python2
  Python3 removes support for relative imports
  Document implicit namespace packages for python
  disable namespace package build
  Attempt to calm the testing gods...
  use whatever name winders uses for .so files.
  Examples (and tests) for python namespace packages
  disable namespace package build
  spelling
  Attempt to calm the testing gods...
  use whatever name winders uses for .so files.
  Don't run example for old pythons
  Examples (and tests) for python namespace packages
  use importlib to load C extension modules for python 2.7 and newer
2016-05-24 22:48:37 +01:00
Mike Romberg 10d71ca01a use relative import for -builtin and python2 2016-05-24 22:46:35 +01:00
William S Fulton 5e6ab1d61d Merge branch 'ejulien-python_operator_overload_test_suite'
* ejulien-python_operator_overload_test_suite:
  Add __str__ to operator_overload testcase for python builtin
  Python operator_overload runtime testcase cleanup
  Work around a limitation of the Python binding generator related to the += family of operators.
  Fix Python 3 division member operator when -builtin is not used.
  Fix class member division operator.
  Remove the PY3BUILTIN switch as its behavior can be achieved with the existing SWIG_FEATURES=-builtin switch.
  Implement the operator overload test suite for Python.

Conflicts:
	Examples/test-suite/operator_overload.i
2016-05-05 23:01:35 +01:00
Mike Romberg ca63491d7a Python3 removes support for relative imports 2016-04-05 11:10:04 -06:00
Mike Romberg 23d2eb555f use importlib to load C extension modules for python 2.7 and newer 2016-04-05 10:58:50 -06:00
William S Fulton a97441613e Merge branch 'ahnolds-py_gettar'
* ahnolds-py_gettar:
  update changes file for attribute fix
  Add python AttributeError test for non-existent attribute
  Fixing python attribute lookup

Conflicts:
	CHANGES.current
2016-04-02 18:06:03 +01:00
Ling Li 79bb343ffe Fix single arguments using python -builtin -O with %feature("compactdefaultargs")
Setattr argcount for non-default-args and non-varargs functions.
Fixes #607
Closes #610
2016-04-02 11:29:43 +01:00
Paweł Tomulik 174523aa80 fix handling of function pointers by %constant directive 2016-03-24 11:22:51 +01:00
Paweł Tomulik 0c819d1ba9 prevent _swigconstant boilerplate for non-class types 2016-03-15 10:27:13 +01:00
Emmanuel Julien 8df7c5fb7e Fix Python 3 division member operator when -builtin is not used. 2016-02-22 16:30:24 +01:00
Emmanuel Julien cf370fb504 Fix class member division operator.
Define the nb_divide/nb_inplace_divide slots in the interface and use it them as nb_divide/nb_inplace_divide for Python 2.x and as nb_true_divide/nb_inplace_trus_divide for Python 3.x.

The Python 3.x nb_floor_divide/nb_inplace_floor_divide slots (operator // in Python) are not populated by the interface.
2016-02-22 14:54:19 +01:00
Alec Cooper 506a5bc8e6 Don't claim to new-style support in classic mode 2016-02-04 15:27:54 -05:00
Alec Cooper 475d03f195 Clean up setting _object
Don't bother setting _object in modern mode since it isn't used
Catch __builtin__.Exception rather than AttributeError since the check for
object hasn't been through something that would raise an AttributeError since
3d8ddfc4 in 2008
2016-02-04 15:27:54 -05:00
Alec Cooper 866429d0f7 Removing __swig_getmethods__ for static methods
Static methods will always be found by normal attribute lookup, so there's no
need to check for them during __getattr__ (aka _swig_getattr_nondynamic)
2016-02-04 15:27:54 -05:00
Alec Cooper b45164e098 Support python(pre|ap)pend on static methods in classic mode
Adding a test that covers this case to the python_append test-suite
Note: staticmethod function has been available since Python 2.2
2016-02-04 15:27:54 -05:00
Alec Cooper d91064aa6a Add support for static methods in classic mode 2016-02-04 15:27:54 -05:00
Alec Cooper de4b3a4a59 Fixing python attribute lookup
Cleans up patch #232
Fixes misleading error messages from #588

If __getattr__ is called, this means that normal attribute lookup has failed.
  If checking thisown and __swig_getmethods__ fails, then give up and raise
  AttributeError instead of calling the non-existent object.__getattr__.
  Note that in practice the result is essentially the same, since trying to
  access object.__getattr__ fails and raises AttributeError, just with a
  misleading message.
In addition, there's no point in having nondynamic attribute lookup.
2016-01-24 15:40:34 -05:00
William S Fulton 55f303d286 Merge pull request #584 from benmwebb/master
(Python) Qualify use of "__builtin__.Exception" class.
2016-01-23 23:24:34 +00:00
Ben Webb 2a5bbb6018 Qualify use of "__builtin__.Exception" class.
It is possible that the module we're wrapping defines an Exception
class. This will confuse code that uses an unqualified "Exception"
class (e.g. "try: ... except Exception") since it now won't match
the Python builtin Exception. Fix this by explicitly using
the class from the __builtin__ module ("builtins" in Python 3).
2016-01-11 21:43:10 -08:00
Olly Betts 584b328239 Fix minor memory leak in Python module docstring handling
Noted in #582 by aurelj.
2016-01-12 13:49:55 +13:00
Olly Betts f910607e26 Fix typo: "neccessary" -> "necessary" 2016-01-12 13:37:39 +13:00
Petre Eftime 0a07cd4c30 Prevent redefinition warnings when compiling with SWIG<module> defined
Signed-off-by: Petre Eftime <petre.p.eftime@intel.com>
2015-12-22 14:33:21 +02:00
William S Fulton f77839dfbf Initialize missing PyNumberMethods for Python 3.5 and -builtin
Add nb_matrix_multiply nb_inplace_matrix_multiply
2015-12-16 18:32:21 +00: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 f7b9466dff Python 3.3 builtin missing field initializers added
Add in ht_qualname and ht_cached_keys for Python 3.3 and later
2015-12-14 01:22:41 +00:00
William S Fulton 5724195b8b Adding tp_finalize field to PyTypeObject for Python 3.4 and -builtin 2015-12-14 01:22:41 +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 ed044e4b8c Warning fix for Visual Studio 2015-11-28 18:48:52 +00:00
William S Fulton ef001de524 Support Python 3.5 and -builtin.
PyAsyncMethods is a new member in PyHeapTypeObject.
Closes#539
2015-10-10 01:25:52 +01:00
William S Fulton faeaacf112 smartptr feature support - factor out common code 2015-09-25 22:57:59 +01:00
Olly Betts 8a6874e633 Fix docstrings for callback functions with -builtin 2015-09-04 15:29:06 +12:00
Olly Betts 8ab622c6d0 [Python] Fix docstrings for %callback functions
Reinstates autodoc for callback function testcase from #467, actually
tests the resulting docstring in the _runme.py and fixes SWIG/Python
so the expected result is obtained.
2015-09-04 13:04:37 +12:00
Sjoerd Job Postmus 5be177e5c3 Do not use bare exception in generated Python code.
By using the 'except:', you can catch all kinds of exceptions, including
the KeyboardInterrupt and SystemExit exceptions. From the generated
code, it is quite obvious that it is not these cases that should be
caught, but more specific ones like AttributeError and TypeError. To be
on the safe side, I decided to keep using 'Exception' for now.
2015-08-28 17:43:28 +02:00
xantares 92328a2016 pep257 & numpydoc conforming docstrings 2015-08-07 22:15:13 +01:00
William S Fulton e69cc0c0f5 Improve python code indentation warning / error messages 2015-07-30 08:26:17 +01:00
William S Fulton a779f9bbc1 Function comment header formatting corrections 2015-07-30 08:26:17 +01:00
William S Fulton fa282b3540 Improve Python docstring indentation handling
SWIG-3.0.5 and earlier sometimes truncated text provided in the docstring
feature.
SWIG-3.0.6 gave a 'Line indented less than expected' error instead of
truncating the docstring text.
Now the indentation for the 'docstring' feature is smarter and is
adjusted so that no truncation occurs.

Closes #475
2015-07-30 08:26:08 +01:00
xantares 812f789db6 Avoid gcc uninitialized variable warnings in Python wrappers.
Just initialize the local array with zeroes.

Closes #453.
2015-07-25 19:10:06 +02:00
Vadim Zeitlin 302955a152 Another merge with master.
Updated Doxygen error numbers yet again, as Python errors got added in the
meanwhile, pushing the Doxygen ones further off.

And re-merged PEP8/whitespace-related conflicts in autodoc_runme.py once again
(if anybody is looking for a motivating example about why significant
whitespace is bad, here is a great use case).
2015-07-20 00:44:26 +02:00
Michael Thon 7ed63b11d7 Set class docstring in tp_doc slot for python -builtin 2015-07-09 02:05:33 +02:00
William S Fulton edcdaaec16 Warning fixes for 64bit visual c++ on Windows 2015-07-03 20:59:24 +01:00
Olly Betts 822b2355c0 Improve handling of whitespace in %pythoncode
Previously SWIG looked at the indentation of the first line and removed
that many characters from each subsequent line, regardless of what those
characters were.  This was made worse because SWIG's preprocessor removes
any whitespace before a '#'.  Fixes github issue #379, reported by Joe
Orton.
2015-06-29 22:12:38 +12:00
William S Fulton 53b7659ebf Fix Python pep8 warning when using -fastinit (or -O)
Fixes: E231 missing whitespace after ','
2015-06-16 08:11:29 +01:00
William S Fulton 180e21269d Fix python -builtin -O and overloaded functions
More specifically fixes compile errors using -builtin -fastunpack -modernargs.
Recent regression (416277b). Closes #436.
2015-06-16 07:59:22 +01:00
William S Fulton b8e1a66a38 Add new feature "python:cdefaultargs"
Controls default argument code generation to obtain the default
arguments from the C++ layer instead of the Python layer.
2015-05-28 20:11:57 +01:00
William S Fulton 986a13f1a0 Fix Python typedef bool default arguments that are not booleans.
Includes code optimisation in PYTHON::convertValue().

Closes #327
2015-05-27 20:55:56 +01:00
William S Fulton 0a70498591 Refactor PYTHON::convertValue
Remove multiple return statements for upcoming commits.
2015-05-27 08:06:23 +01:00
Olly Betts c2972b8bf0 [Python] Deal with an integer as the default value of a bool
parameter in the C++ prototype.  Fixes github #327, reported by
Greg Allen.
2015-05-07 16:25:32 +12:00
Vadim Zeitlin 6988b00aba Fix handling of default arguments after ignored ones in Python.
Don't skip checking subsequent arguments just because one of them has "in"
typemap with numinputs=0 attribute.

Add a unit test showing the problem which is relatively rare as it doesn't
happen for the class methods and is hidden unless autodoc feature is used for
the global functions.

Closes #377.
2015-04-27 23:17:14 +02:00
William S Fulton d64c241e1c gcc-5.1 warning fixes 2015-04-26 01:18:26 +01:00
William S Fulton 416277b3a5 Python code generated with '-builtin -modernargs' segfaults for any method taking zero arguments.
Also fixes: "SystemError: error return without exception set" during error checking
when using just -builtin and the incorrect number of arguments is passed to a class
method expecting zero arguments.

Closes #256
Closes #382
2015-04-24 21:08:17 +01:00
William S Fulton e4d02d20ad Warning fix for VC++ 2015-04-23 19:21:59 +01:00
Vadim Zeitlin 5569d91bd0 Fix handling of "default" typemap in Python.
Use "compact" arguments form for the function if "default" typemap is defined
for any of its arguments to allow omitting this argument when calling it from
Python.

Closes #377.
2015-04-23 15:11:05 +02:00
Vadim Zeitlin 2369e2c500 No changes, just added an explanatory comment to Python module.
Explain a bit better when and why do we decide to use "*args" in the generated
Python code.
2015-04-23 15:11:02 +02:00
Vadim Zeitlin 0eae8a8efa Fix handling of NULL default argument values for pointer types.
Accept not only manifest pointer types (such as e.g. "void *") but also types
that are typedefs for pointer types when checking whether C++ value of 0 must
be represented as 0 or None in Python.

Closes #365, #376.
2015-04-23 15:11:02 +02:00
William S Fulton b886a54fce Merge branch 'amaeldoe-master'
* amaeldoe-master:
  Add python runtime test for dynamically added attributes
  Attribute of SWIG wrapped classes instances were overwritten on __init__()
  Fix SwigPyObject->dict memory leak
  Make __dict__ accessible for Python builtin classes
2015-04-11 12:37:11 +01: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
Olly Betts 7ba0652677 Create director_common.swg for language-indep code
Move -DSWIG_DIRECTOR_STATIC handling there, so this is now supported for
all languages with director support, not just Python and PHP.
2015-03-12 19:51:11 +13:00
Vadim Zeitlin d92bc54662 Remove unused <sstream> header.
std::stringstream is not used in this file, so it is unneeded.
2015-02-16 23:55:01 +01:00
Vadim Zeitlin 300ccce46c Another merge with master.
Change Doxygen error codes to start at 740 instead of at 720 as the latter was
taken by Scilab in the meanwhile.

Resolve conflicts in autodoc_runme.py once again.
2015-02-16 23:46:39 +01:00
William S Fulton 39a75442a1 Fix Python -classic and property setting
Setting properties on classic classes was broken in swig-3.0.3 by attempting to use __setattr__. This regression is fixed now by using __dict__ again when using -classic.

Fixes patch #232.
2015-01-30 23:27:22 +00:00