Commit graph

701 commits

Author SHA1 Message Date
Thomas Maslach
de6b433cb1 Fix Python crash when using -threads iterating containers
Also fixes li_std_vector_enum testcase when run with -threads.

Patch supplied on swig-devel mailing list on 12 Sep with details...

==============================================
I just wanted to mention that I found a crash issue in bug..

I am using SWIG 2.0.11 with python and have –threads enabled.  I have a C++ std::vector that I instantiate in SWIG with %template.  I also have a method in a class that returns this vector.  I also include std_vector.i, btw..

When I iterate like so:

children = Action.getActionList()
for child in children:
  pass

Everything is fine..

When I iterate like this:

for child in Action.getActionList()
  pass

Product crashes.

The problem is the following.  This code gets called first:

SWIGINTERN PyObject *_wrap_delete_SwigPyIterator(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  swig::SwigPyIterator *arg1 = (swig::SwigPyIterator *) 0 ;
  void *argp1 = 0 ;
  int res1 = 0 ;
  PyObject * obj0 = 0 ;

  if(!PyArg_UnpackTuple(args,(char *)"delete_SwigPyIterator",1,1,&obj0)) SWIG_fail;
  res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_swig__SwigPyIterator, SWIG_POINTER_DISOWN |  0 );
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_SwigPyIterator" "', argument " "1"" of type '" "swig::SwigPyIterator *""'");
  }
  arg1 = reinterpret_cast< swig::SwigPyIterator * >(argp1);
  {
    SWIG_PYTHON_THREAD_BEGIN_ALLOW;
    delete arg1;
    SWIG_PYTHON_THREAD_END_ALLOW;
  }
  resultobj = SWIG_Py_Void();
  return resultobj;
fail:
  return NULL;
}

Note the SWIG_PYTHON_THREAD_BEGIN_ALLOW/END_ALLOW. In between those two statements, we delete arg1.  That in turn will eventually end up in this code:

namespace swig {
  class SwigPtr_PyObject {
  protected:
    PyObject *_obj;

  public:
    … snip! …
    ~SwigPtr_PyObject()
    {
      Py_XDECREF(_obj);
    }

Uh-oh!  We call Py_XDECREF when we aren’t supposed to because we are in a SWIG_PYTHON_THREAD_BEGIN_ALLOW/END_ALLOW section!

This takes care of the issue:

namespace swig {
  class SwigPtr_PyObject {
  protected:
    PyObject *_obj;

  public:
    … snip! …
    ~SwigPtr_PyObject()
    {
      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
      Py_XDECREF(_obj);
      SWIG_PYTHON_THREAD_END_BLOCK;
    }

There are several other methods in this class that use the Python API, but don’t have the BEGIN/END block defined.  I’m not sure if they are required for all of them, but I believe they are..

I have attached a modified pyclasses.swg with what I believe are the correct changes.  This code is from 2.0.11, but as far as I can tell, it’s the same as what is in 3.0.2…

Apologies for not doing more here (making/running tests, getting it in the code repository, etc..), but I’m under some pressure to get some unrelated things done…
2014-09-23 22:33:25 +01:00
Julien Schueller
6fe71da9fa Fixed remaining pep8 errors 2014-06-07 13:09:15 +02:00
Julien Schueller
f4fffbf668 Fixed another E701 2014-06-06 15:13:23 +02:00
Julien Schueller
0d589349a1 Fixed pep8 issues E701, E203, E231, E261 2014-06-06 11:03:46 +02:00
Harvey Falcic
9846f3f1ea Python 3 byte string output: use errors="surrogateescape"
... if available on the version of Python that's in use. This allows
obtaining the original byte string (and potentially trying a fallback
encoding) if the bytes can't be decoded as UTF-8.

Previously, a UnicodeDecodeError would be raised with no way to treat
the data as bytes or try another codec.
2014-05-23 15:40:01 -04:00
Karl Wette
d5b765d388 Whitespace cleanup of all Makefiles*
- some of the %.clean rules in the test-suite Makefiles were using a single tab
  as an empty rule, dangerous! I've replaced these with the safer '@exit 0'.
2014-05-02 20:06:11 +02:00
William S Fulton
504c2030bb Change in default behaviour wrapping C++ bool for Python.
Only a Python True or False will now work for C++ bool parameters.
This fixes overloading bool with other types.
2014-03-08 12:04:19 +00:00
William S Fulton
7a96fba836 Correct exception thrown attempting to access a non-existent C/C++ global variable on the 'cvar' object.
The exception thrown used to be a NameError. However, as this access is
via a primary, an AttributeError is more correct and so the exception
thrown now is an AttributeError. Reference:
http://docs.python.org/2/reference/expressions.html#attribute-references

SF Patch #346.
2014-03-02 01:31:36 +00:00
Jens Krüger
da44064d6c 'swig_varlink_getattr' throws a wrong exception
If the attribute of a python object could not found a AttributeException
should be thrown instead of a NameException.
2014-03-02 00:24:23 +00:00
William S Fulton
ae7b34ce03 Remove duplicate header includes in director.swg 2014-02-21 19:07:35 +00:00
William S Fulton
0d9a8721f4 Move some header file includes into fragments for UTL languages 2014-02-21 19:02:14 +00: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
Olly Betts
b761131fec "if (strlen(msg))" -> "if (msg[0])" 2014-02-17 16:26:48 +13:00
William S Fulton
fb7eb2bedf Merge branch 'master' of github.com:hfalcic/swig into hfalcic-python3-seg-fault
* 'master' of github.com:hfalcic/swig:
  Fix shadow instance creation failure in Python 3
2014-02-16 17:54:28 +00:00
William S Fulton
bd5c340062 Add <string> fragment
Removes <string> include specifically for clang
Cuts down on duplicate #include <string> in general
2014-02-15 23:30:14 +00: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
William S Fulton
cc650e692e Director exceptions now derive from std::exception 2014-01-20 19:40:52 +00:00
William S Fulton
1a19451c1b Error out attempting to use directors without -c++
Remove redundant #ifdef __cplusplus markers in director.swg
2013-12-23 20:23:54 +00:00
William S Fulton
135a7cc558 Beautify director.swg files
Also some comment corrections for Perl
2013-12-23 19:50:41 +00:00
William S Fulton
279ebdc0cf Beautify director output 2013-12-23 18:21:52 +00:00
Olly Betts
c6e4dea572 Fix a few typos in comments and docs 2013-12-12 11:45:30 +13:00
Vadim Zeitlin
ed28725a15 Add std_auto_ptr.i defining typemaps for returning std::auto_ptr<>.
These typemaps are currently defined for C#, Java and Python only and the
tests are provided only for these languages.

Also add a brief description of the new header to the documentation.
2013-12-03 23:45:20 +01:00
William S Fulton
0e54a51c10 Add missing #include <stddef.h> for offsetof when using -builtin.
Fixes SF #1345
2013-10-17 19:56:18 +01:00
William S Fulton
bcb7aee022 Merge branch 'master' into gsoc2009-matevz
Conflicts:
	Examples/Makefile.in
	Examples/guile/Makefile.in
	Lib/php/php.swg
	Makefile.in
	Source/CParse/parser.y
	configure.ac
2013-10-10 07:26:09 +01:00
William S Fulton
d0cb2b73db Remove X11 detection during configure
X11 is not used anywhere.
2013-09-18 00:40:24 +01:00
William S Fulton
1cc735df5e %implicitconv will now accept None where the implicit conversion takes a C/C++ pointer.
Problem highlighted by Bo Peng on the swig-user mailing list. SF patch #230.
2013-08-28 20:28:15 +01:00
Kris Thielemans
a495b5a985 Remove SwigPyObject_print and SwigPyObject_str, and make the generated wrapper use the default python implementations, which will fall back to repr (for -builtin option).
Advantages:
- it avoids the swig user having to jump through hoops to get print to
  work as expected when redefining repr/str slots.
- typing the name of a variable on the python prompt now prints the
  result of a (possibly redefined) repr, without the swig user having to
do any extra work.
- when redefining repr, the swig user doesn't necessarily have to
  redefine str as it will call the redefined repr
- the behaviour is exactly the same as without the -builtin option while
  requiring no extra work by the user (aside from adding the
%feature("python:slot...) statements of course)
- the patch simplifies pyrun.swg a tiny bit.

Disadvantage:
- default str() will give different (but clearer?) output on swigged
  classes compared to unpatched swig

SF Bug #326
2013-08-07 20:22:03 +01:00
William S Fulton
80f8d1d922 Fix for missing C++ code in std::multimap wrappers.
%template for a std::multimap generated uncompilable code unless a
%template for a std::map of the same template types was also coded up.

This patch is needed in conjunction with previous commit - 5f1fff1849

Closes #64
Closes #65
2013-08-06 07:02:49 +01:00
William S Fulton
d0af4f50d3 Add %pythonbegin directive.
For adding code at the beginning of the generated .py file.
2013-07-05 06:30:16 +01:00
William S Fulton
ace8fcd972 SWIG_AsWCharPtrAndSize improper operation if cptr NULL
SF bug #1327

This doesn't have any noticeable effect with the usage of
SWIG_AsWCharPtrAndSize as shipped by SWIG, but could be a problem if a
user is using this function with cptr equal to zero and psize is non-zero
 - the length would be incorrectly set due to the call to PyUnicode_GetSize
failing.
2013-07-02 18:58:56 +01:00
William S Fulton
7491be12e5 Uninitialized variable fix in SWIG_Python_NonDynamicSetAttr when using -builtin.
SF patch #340
2013-07-01 20:09:31 +01:00
William S Fulton
0058eea3ad Add SF patch #342 fix some director classes crashing on object deletion when using -builtin.
Fixes SF bug #1301 and python test cases director_basic and
director_classic seg faulting when used with -builtin.

Patch from Christian Delbaere.
2013-07-01 19:25:34 +01:00
William S Fulton
7f95c7bb3e Use a less confusing macro name, SWIG_PYTHON_NO_DEBUG => SWIG_PYTHON_INTERPRETER_NO_DEBUG 2013-06-11 19:15:57 +01:00
William S Fulton
5cdfc503e1 Add SWIG_PYTHON_NO_DEBUG macro for building Debug wrappers against the Python Debug dll 2013-06-11 00:22:21 -07:00
William S Fulton
5481270c2a Fix Python 3 inconsistency handling -ve numbers for unsigned C types.
An OverFlow error is now consistently thrown instead of a TypeError.

Fixes primitive_types testcase for Python 3
2013-05-25 10:36:14 +01:00
William S Fulton
38b2b95c30 Fix some invalid iterator usage in Python when deleting/inserting slices from/into containers 2013-03-26 21:38:45 +00:00
William S Fulton
7eda619741 Fix invalid iterators used with -ve ranges - Python
Fixes li_std_containers_int testcase.
Valgrind reports no more problems for this testcase.
2013-03-26 21:38:44 +00:00
Olly Betts
b72aca1d07 Fix typo in comment (swift->swig) 2013-03-20 15:39:03 +13:00
Olly Betts
7c80f007c4 Fix typo in Python docstring for acquire method 2013-03-19 18:38:45 +13:00
William S Fulton
e805d5f925 Merge branch 'master' into gsoc2009-matevz
parser.y still to be fixed up

Conflicts:
	Doc/Devel/engineering.html
	Examples/Makefile.in
	Lib/allegrocl/allegrocl.swg
	Lib/csharp/csharp.swg
	Lib/csharp/enums.swg
	Lib/csharp/enumsimple.swg
	Lib/csharp/enumtypesafe.swg
	Lib/java/java.swg
	Lib/python/pydocs.swg
	Lib/r/rtype.swg
	Source/Include/swigwarn.h
	Source/Modules/octave.cxx
	Source/Modules/python.cxx
	Source/Modules/ruby.cxx
	Source/Swig/scanner.c
	Source/Swig/stype.c
	Source/Swig/swig.h
	configure.ac
2013-01-28 07:01:37 +00:00
William S Fulton
ac74c90fb0 Add rvalue reference typemaps 2013-01-24 20:27:29 +00:00
William S Fulton
a6d456a15e Replace references to Subversion with Git 2013-01-12 01:24:22 +00:00
William S Fulton
16481c999e Bug #3563647 - PyInt_FromSize_t unavailable prior to Python 2.5 for unsigned int types
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13953 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-12-09 20:49:52 +00:00
Vadim Zeitlin
0ca11c8b6f Fix overflow with parameters > LONG_MAX with Python 3.
The typemap incorrectly called PyInt_AsLong() if PyInt_Check() passed, but
this check is the same as PyLong_Check() for Python 3 and so the correct
PyLong_AsUnsignedLong() function was never called. As a consequence, passing
any value greater than LONG_MAX (e.g. 0x87654321 on 32 bit architectures) to a
function taking unsigned int, unsigned long or size_t parameter failed with an
overflow error being generated.

Fix this by simply disabling the part of the code dealing with PyInts for
Python 3 as there is no distinction between PyInt and PyLong there any more.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13877 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-11-09 17:57:42 +00:00
William S Fulton
2c74c90430 Fix for gcc warning -Wunused-value without triggering -Wunused-parameter
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13735 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-09-07 06:06:00 +00:00
William S Fulton
049035ff3e Add discard and add methods to std::set and std::multiset wrappers so that pyabc.i can be used ensuring MutableSet is a valid abstract base class
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13619 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-15 22:36:15 +00:00
William S Fulton
81b755dadc Better error handling in SWIG_Python_NewShadowInstance and SWIG_Python_NewPointerObj to fix seg fault as mentioned on swig-devel mailing list email thread - 'Fix python3 abc set'
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13618 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-15 22:35:24 +00:00
William S Fulton
68862691e6 Fix #3541744 - Missing PyInt_FromSize_t calls for Python 3
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13608 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-15 07:48:57 +00:00
William S Fulton
3b8bc08be7 Suppress -Werror=unused-but-set-variable gcc warning in Python wrappers
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13571 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-08 22:24:46 +00:00
Stefan Zager
00328ccdb3 python: disambiguate SWIG_From_unsigned_SS_int and SWIG_From_unsigned_SS_long.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13106 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-05-23 04:05:11 +00:00