Commit graph

603 commits

Author SHA1 Message Date
Olly Betts
3b03f920e7 Suppress warnings about PyCFunction casts
These remaining warnings are due to the design of Python's C API,
so suppress them by casting via void(*)(void) (which GCC documents
as the way to suppress this warning).

Closes #1259.
2019-01-20 12:27:36 +13:00
William S Fulton
2e9b270cbb Merge branch 'vadz-better-param-names'
* vadz-better-param-names:
  Enable keyword arguments for keyword_rename unit test
  Update error messages test suite
  Add more tests for Python parameter renaming
  Improve handling parameters clashing with language keywords
2019-01-17 18:19:36 +00:00
William S Fulton
cf1624ebc4 Python static method wrapper changes
- Static method wrappers were using the 'fastproxy' approach by default.
  This is inconsistent with instance method wrappers. The fastproxy approach
  is now turned off by default to be consistent with instance methods.
  Static method wrappers can now also be controlled using the -fastproxy and
  -olddefs options.

  Example:

    struct Klass {
      static int statmethod(int a = 2);
    };

  generates:

  class Klass(object):
      ...
      @staticmethod
      def statmethod(a=2):
          return _example.Klass_statmethod(a)

  instead of:

    class Klass(object):
      ...
      statmethod = staticmethod(_example.Klass_statmethod)

- Modernise wrappers for static methods to use decorator syntax - @staticmethod.

- Add missing runtime test for static class methods and using the actual
  class method.
2019-01-16 08:21:00 +00:00
Vadim Zeitlin
3f5c17824c Improve handling parameters clashing with language keywords
Previously, only Python tried to preserve the original parameter name
(by prepending or appending an underscore to it, but otherwise keeping
the original name) if it conflicted with one of the language keywords,
while all the other languages replaced the parameter name with a
meaningless "argN" in this case.

Now do this for all languages as this results in more readable generated
code and there doesn't seem to be any reason to restrict this to Python
only.
2019-01-16 04:16:59 +01:00
William S Fulton
07884f10ee Python - remove duplicate proxy method definitions for global function wrappers.
Global functions previously generated two definitions, eg:

  def foo():
      return _example.foo()
  foo = _example.foo

The first definition is replaced by the second definition and so the second definition
is the one used when the method is actually called. Now just the first definition is
generated by default and if the -fastproxy command line option is used, just the second
definition is generated. The second definition is faster as it avoids the proxy Python
method as it calls the low-level C wrapper directly. Using both -fastproxy and -olddefs
command line options will restore the previously generated code as it will generate both
method definitions.

With this change, the wrappers for global C/C++ functions and C++ class methods now work
in the same way wrt to generating just a proxy method by default and control via
-fastproxy/-olddefs options.

Closes #639.
2019-01-01 12:12:56 +00:00
William S Fulton
d314b53820 Python -newvwm command line option remains undocumented
Remove option from -help output text
It is hard to explain exactly what it does and is unclear as to how useful it is!
2018-12-18 19:51:38 +00:00
William S Fulton
5d6786598c Python command line options tidyup 2018-12-18 19:51:38 +00:00
William S Fulton
398ac5f01c Remove redundant Python options: -nocastmode -nodirvtable -noextranative -nofastproxy
Also remove redundant %module options: nocastmode, noextranative
Issue #1340
2018-12-18 19:51:32 +00:00
William S Fulton
152b66deaf Tidy up Python command line options help text
Also remove non-existent -noexcept option
2018-12-18 18:32:55 +00:00
William S Fulton
355395200f More Python module loading simplification
Slightly faster checking to see if a module is in a package.
Issue #848
2018-12-18 07:50:48 +00:00
William S Fulton
2a00d0f784 Simpler Python module loading
Simplification possible given Python 2.7 is now the minimum supported.
Issue #848
2018-12-18 07:30:50 +00:00
William S Fulton
03323f5c8b The Python module import logic has changed to stop obfuscating real ImportError problems.
Only one import of the low-level C/C++ module from the pure Python module is
attempted now. Previously a second import of the low-level C/C++ module was attempted
after an ImportError occurred and was done to support 'split modules'. A 'split module' is
a configuration where the pure Python module is a module within a Python package and the
low-level C/C++ module is a global Python module. Now a 'split module' configuration is
no longer supported by default. This configuration can be supported with a simple
customization, such as:

  %module(package="mypackage", moduleimport="import $module") foo

or if using -builtin:

  %module(package="mypackage", moduleimport="from $module import *") foo

instead of

  %module(package="mypackage") foo

See the updated Python chapter titled "Location of modules" in the documentation.

Closes #848 #1343
2018-12-16 16:41:39 +00:00
William S Fulton
90221812c6 Revert removal of tp_print slot for Python -builtin
Some users might still want to customize this slot.
2018-12-11 08:00:44 +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
William S Fulton
67f5ade7ad Remove -noolddefs command line option
This was a pointless option as it is off by default.
2018-12-10 07:07:49 +00:00
William S Fulton
9fe2b05faa Merge branch 'adr26-master'
* adr26-master:
  Cleanup accessing/decref of globals, to avoid code bloat in init function.
  Fix ISOC build errors.
  Fix unused variable warning.
  #1360: Leak of SWIG var link object

Conflicts:
	CHANGES.current
2018-12-04 20:12:42 +00:00
William S Fulton
9a7e2ed9ed Restore Python docstring location to immediately after the SWIG banner 2018-12-04 20:10:49 +00:00
William S Fulton
604ae7186b Fix for running 'python -m' when using swig -builtin
Same as e05b5ea for -builtin.

Also added runtime tests to check 'python -m'.
2018-12-04 19:12:13 +00:00
Andrew Rogers
402d55da52 Merge github.com:swig/swig
# Conflicts:
#	CHANGES.current
2018-11-29 07:02:49 +00:00
William S Fulton
75638e3371 Update comments and docs regarding recent addition to use __package__ for Python module imports 2018-11-28 23:48:58 +00:00
William S Fulton
6b5da094b2 Simpler Python -builtin import
When using -builtin, the two step C-extension module import is now
one step and the wrapped API is only available once and not in an underlying
module attribute like it is without -builtin. To understand this, consider a
module named 'example' (using: %module example). The C-extension is compiled into
a Python module called '_example' and a pure Python module provides the actual
API from the module called 'example'. It was previously possible to additionally
access the API from the module attribute 'example._example'. The latter was an
implementation detail and is no longer available. It shouldn't have been used, but
if necessary it can be resurrected using the moduleimport attribute described in the
Python chapter of the documentation. If both modules are provided in a Python
package, try:

  %module(moduleimport="from . import _example\nfrom ._example import *") example
or more generically:
  %module(moduleimport="from . import $module\nfrom .$module import *") example

and if both are provided as global modules, try:

  %module(moduleimport="import _example\nfrom _example import *") example
or more generically:
  %module(moduleimport="import $module\nfrom $module import *") example

The module import code shown will appear in the example.py file.
2018-11-28 23:36:13 +00:00
William S Fulton
b380de031e Python -builtin %rename constructor refactor
The implementation for a renamed constructor for -builtin no longer
uses the same implementation for non-builtin mode which makes a call
into the compiled C module, eg _constructor_rename for a module called
constructor_rename. The output in the constructor_rename.py file is now:

  RenamedConstructor = new_RenamedConstructor

instead of

  def RenamedConstructor():
      val = _constructor_rename.new_RenamedConstructor()
      return val

when wrapping:

  struct Foo {
      %rename(RenamedConstructor) Foo();
      Foo() {}
  };

See the constructor_rename.i testcase.

This change has been made to make the wrapper more efficient and is a
step towards the next commit which will remove duplicating the symbols
in both the pure Python module and implementation C module
(constructor_rename and _constructor_rename respectively in this case).
2018-11-28 08:20:02 +00:00
Andrew Rogers
a9e29d2bc7 Merge github.com:swig/swig 2018-11-27 23:31:27 +00:00
Andrew Rogers
0fecd1538f Cleanup accessing/decref of globals, to avoid code bloat in init function. 2018-11-27 23:30:51 +00:00
William S Fulton
865dc1e71c Remove Python 2.6 and earlier import code 2018-11-25 22:18:43 +00:00
William S Fulton
e729a868b9 Minimum Python version check correction
Fixes Recent breakage when using -builtin and %import
2018-11-25 21:53:57 +00:00
Andrew Rogers
6814f67cdb Merge branch 'master' of github.com:swig/swig
# Conflicts:
#	CHANGES.current
2018-11-24 20:06:02 +00:00
William S Fulton
0f61c5a847 Python minimum version checking fixes
When  using the moduleimport option, such as:

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

the minimum Python version check disappeared from the generated Python
file. The code has been refactored and _swig_python_version_info is
no longer deleted after initial use as it can be used in a few places,
in particular, when -builtin is used.
2018-11-23 18:08:33 +00:00
Andrew Rogers
65edf2258f #1360: Leak of SWIG var link object 2018-11-23 16:02:46 +00:00
William S Fulton
c8bb566822 Merge branch 'rupertnash-master'
* rupertnash-master:
  Make python shadow sub-modules runable
2018-11-22 08:18:57 +00:00
William S Fulton
abb2a9259c Remove -noproxyimport command line option (Python)
This option turned off the insertion of Python import statements
derived from a %import directive. For example given:

  %module module_b
  %import "module_a.i"

then module_b.py will contain:

  import module_a

This option was originally added in 658add5, apparently to fix some sort
of circular import problem of which there are no details. It is
undocumented and is now removed as part of the simplification of the
SWIG Python command line options for SWIG 4.

Issue #1340.
2018-11-13 21:55:58 +00: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
027a38c71c Remove -cppcast and -nocppcast command line options
The -cppcast option is still turned on by default. The -nocppcast option
to turn off the use of c++ casts (const_cast, static_cast etc) has been
removed. However, defining SWIG_NO_CPLUSPLUS_CAST will still generate C casts
instead of C++ casts for C++ wrappers.

This a revert of commit fc79264a48:
"Revert "Remove -cppcast and -nocppcast command line options""

The Scilab and Javascript casting problems are now fixed, so -cppcast
is now switched on as default.
2018-11-13 07:36:09 +00:00
William S Fulton
fc79264a48 Revert "Remove -cppcast and -nocppcast command line options"
This reverts commit c06f2b4497.

More work to be done as it breaks Scilab and Javascript tests.
2018-11-06 17:22:05 +00:00
William S Fulton
c06f2b4497 Remove -cppcast and -nocppcast command line options
The -cppcast option is still turned on by default. The -nocppcast option
to turn off the use of c++ casts (const_cast, static_cast etc) has been
removed. However, defining SWIG_NO_CPLUSPLUS_CAST will still generate C casts
instead of C++ casts for C++ wrappers.
2018-11-06 10:26:06 +00:00
William S Fulton
23ce449ea5 Remove Python -new_vwm
Use the -newvwm alias name instead.
2018-10-24 20:04:31 +01:00
William S Fulton
39b48b8689 Remove Python -oldrepr option
This option controlled the __repr__ proxy class method.

Also removes:
  -old_repr
  -oldrepr
  -new_repr
  -newrepr

Note: -newrepr was and remains on by default. When -oldrepr/-old_repr
option resulted in code that did not run.
2018-10-24 20:04:31 +01:00
William S Fulton
1274c1abfc Remove Python -classptr option
This option was for backwards compatibility with very old versions of
SWIG that generated a shadow 'XPtr' class derived from proxy class X.

If anyone is still using these, they are best off using the actual proxy
class, or otherwise add them in manually using %pythoncode.

Issue #1340.
2018-10-24 20:04:31 +01:00
William S Fulton
cd8fc0a025 Remove Python -aliasobj0 option
This option was originally added to help users switch to using
-fastunpack so that typemaps using obj0 did not need changing by
providing an alias for swig_obj[0] to obj0. The correct solution was
always to replace obj0 with $self in the typemap. This is now mandatory.
However, the -nofastunpack option can still be used to circumvent using
the mandatory typemap change.
2018-10-22 08:19:50 +01:00
William S Fulton
04c5061698 Hardwire Python -fastunpack be on by default
The -nofastunpack option has been left in as there isn't much code
simplification in removing the nofastunpack code. This is because the
nofastunpack code is still sometimes needed (eg varags and overloaded
functions).
2018-10-22 08:19:50 +01: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
de26ebbad1 Remove Python -safecstrings option.
This option, if used, has not had any effect on Python 3 code since commit a863d3 9 years ago.
I think we can assume that it is not needed for Python 3.
Running the examples and test-suite (Python 2) doesn't change the code
paths with and without -safecstrings because only SWIG_OLDOBJ and SWIG_NEWOBJ
are used in the typemaps and the following code is thus unaltered by -safecstrings
(which sets SWIG_PYTHON_SAFE_CSTRINGS):

%#if defined(SWIG_PYTHON_SAFE_CSTRINGS)
  if (*alloc != SWIG_OLDOBJ)
%#else
  if (*alloc == SWIG_NEWOBJ)
%#endif
  {
    *cptr = %new_copy_array(cstr, len + 1, char);
    *alloc = SWIG_NEWOBJ;
printf("safe strings: %s\n", *cptr ? *cptr : "NULLSTRING");
  } else {
    *cptr = cstr;
    *alloc = SWIG_OLDOBJ;
  }

Note: nosafecstrings was also the default and -O didn't actually change this.
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
d84d26832f Hardwire Python -fastquery option to be always on
The Python dictionary cache used for SWIG types lookup is now always on.
2018-10-22 08:19:50 +01:00
William S Fulton
350396fcae Hardwire Python -fastinit option to be always on
This option runs the equivalent init code in Python C code instead of pure Python code.
The init code adds the C++ pointer into the 'this' variable.
If the variable already exists, it appends it. This
seems to only happen in directors and multiple inheritance, see
PyMulti in director_basic_runme.py.
2018-10-22 08:19:50 +01:00
Olly Betts
4a48e59dc6 Hard-wire -noproxydel on
We only still default to generate a no-op __del__ method for theoretical
compatibility with old code, and 4.0.0 is a good time to make a cut-off
should such code really still exist.

On the flip-side, the presence of a __del__ method seems to be able to
prevent garbage collection from kicking in (see #1215), so it's
definitely desirable to get rid of it when there's nothing for it to do.

 Conflicts:
	Source/Modules/python.cxx

This is a cherry-pick and merge from the patch in #1261
2018-10-12 07:10:47 +01:00
Olly Betts
e4fceee12f Hard-wire -modern and -modernargs on
What SWIG calls "modern" classes are supported by Python 2.3 and up
which means they're supported by all the Python versions we aim to
support in 4.0.0.

 Conflicts:
	Source/Modules/python.cxx

This is a cherry-pick and merge from the patch in #1261
2018-10-12 07:10:47 +01:00
Olly Betts
728b8955bd Drop support for Python classic classes
There were only needed to support Python < 2.2, and we now require at
least Python 2.6.

 Conflicts:
	.travis.yml
	Examples/test-suite/python/autodoc_runme.py
	Source/Modules/python.cxx

This is a cherry-pick and merge from patch in #1261
2018-10-12 07:10:47 +01:00
William S Fulton
18383340e9 Fix C default arguments with -builtin and -fastunpack and -modernargs.
Problem occurred when there is just one (defaulted) parameter in the parameter list.

Closes #1126
2018-10-04 08:06:01 +01:00
William S Fulton
4715a4e72c Python -builtin __contains__ fix for map and set like containers.
Fix when using -builtin and wrapping std::map, std::set, std::unordered_map or
std::unordered_set to ensure __contains__ is called. This is a wrapper for the STL
container's find method. Without it, Python will do its own slower sequence search.
2018-09-21 08:51:22 +01:00