swig/Lib
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
..
allegrocl Merge branch 'master' into gsoc2009-matevz 2013-01-28 07:01:37 +00:00
cffi Merge branch 'master' into gsoc2009-matevz 2013-10-10 07:26:09 +01:00
chicken Fix assorted comment and documentation typos 2014-02-23 17:15:22 +13:00
clisp merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
csharp add bool array type to arrays_csharp.i [issue #228] 2014-09-17 22:17:36 +01:00
d Remove register storage class declarations 2014-02-19 11:58:27 -06:00
gcj Fix {python|perl5|ruby|tcl}/java examples 2014-05-02 21:44:42 +02:00
go [Go] Add goargout typemap. 2014-09-09 11:28:04 -07:00
guile Fix compiler warnings in generated code when using -std=c++98 -std=gnu89 -pedantic -Wreturn-type 2014-05-24 14:13:19 +01:00
java Doc/comment improvements in Java various.i 2014-08-04 19:22:02 +01:00
javascript Fix std::vector<bool> compile problems on OSX for Javascript 2014-05-31 19:58:42 +01:00
lua Moving variable declaration to the beginning of the block 2014-05-28 22:02:47 +04:00
modula3 Add rvalue reference typemaps 2013-01-24 20:27:28 +00:00
mzscheme Whitespace cleanup of all Makefiles* 2014-05-02 20:06:11 +02:00
ocaml Make Lib/ocaml/swigp4.ml a non-generated file. 2014-02-19 22:35:45 +13:00
octave Octave: remove Python code from std_carray.i 2014-05-29 23:42:55 +02:00
perl5 Whitespace cleanup of all Makefiles* 2014-05-02 20:06:11 +02:00
php [PHP] Fix throwing a PHP exception through C++ from a subclassed 2014-09-11 13:09:08 -03:00
pike Merge branch 'master' into gsoc2009-matevz 2013-10-10 07:26:09 +01:00
python Fix Python crash when using -threads iterating containers 2014-09-23 22:33:25 +01:00
r Fix Visual Studio compile error in C++ wrappers due to #include <exception> within extern "C" block. 2013-10-17 21:54:58 +01:00
ruby Fix compiler warnings in generated code when using -std=c++98 -std=gnu89 -pedantic -Wreturn-type 2014-05-24 14:13:19 +01:00
std small suggestions for changes in std_ios.i 2014-08-12 23:45:02 +01:00
tcl Remove author names - they are in the COPYRIGHT file 2014-06-24 18:56:52 +01:00
typemaps Merge branch 'master' into devel 2014-03-30 23:07:56 +02:00
uffi merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
xml delete the perl5 typemaps, ignored here 2006-01-05 19:34:51 +00:00
allkw.swg [Lua] Add keyword warnings for Lua keywords and Basic Functions. 2014-02-19 23:04:40 +13:00
attribute.i Fix %arg in Lib/attribute.i 2013-05-25 00:37:57 +01:00
carrays.i Fix constructors in named typedef class declarations 2011-08-25 19:27:38 +00:00
cdata.i Add support for cdata library for Java 2014-02-15 22:13:50 +00:00
cmalloc.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
constraints.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
cpointer.i Remove warning: 'self' is a ruby keyword, renaming to 'C_self' since improving autodoc for Ruby 2011-12-21 22:01:27 +00:00
cstring.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
cwstring.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
exception.i Remove unnecessary block from PHP version of SWIG_exception macro 2014-04-03 11:23:57 +13:00
intrusive_ptr.i Convert to unix fileformat 2013-01-12 16:54:37 +00:00
inttypes.i Remove duplicate declarations of strtoimax and strtoumax in inttypes.i 2014-04-08 23:47:22 +01:00
linkruntime.c fix macro issue reported by Vladimir Menshakov 2006-04-10 19:00:44 +00:00
math.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
pointer.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
runtime.swg fix VC++ warning 2005-09-29 13:17:05 +00:00
shared_ptr.i Add std_shared_ptr.i and document shared_ptr library 2010-06-01 19:03:55 +00:00
std_except.i Added support for the D programming languge. 2010-11-18 00:24:02 +00:00
stdint.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
stl.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
swig.swg Fix assorted comment and documentation typos 2014-02-23 17:15:22 +13:00
swigarch.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
swigerrors.swg Remove trailing spaces in the generated code. 2013-09-13 10:02:39 +02:00
swiginit.swg Fix assorted comment and documentation typos 2014-02-23 17:15:22 +13:00
swiglabels.swg Remove trailing spaces in the generated code. 2013-09-13 10:02:39 +02:00
swigrun.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
swigrun.swg Remove register storage class declarations 2014-02-19 11:58:27 -06:00
swigwarnings.swg Rework std::initializer_list handling to warn about usage in any method, not just constructors. A typemap is used to issue the warning and can be overridden with user defined behaviour. 2013-02-04 20:03:22 +00:00
wchar.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00
windows.i merge revisions 11872:11876 from trunk to gsoc2009-matevz branch - license changes 2010-03-06 00:51:54 +00:00