Boost logo

Boost :

From: Norman Shelley (rrdn60) (rrdn60_at_[hidden])
Date: 2001-04-16 17:36:59


Last week I had a program that on occasion would throw an error and it
would show up in the python environment perfectly. This week I'm having
problems. Throwing an error causes seg faults. See two examples using
the same shared library below.

% python2.1
Python 2.1b2 (#2, Apr 5 2001, 11:37:45)
[GCC 2.95.3 20010315 (release)] on hp-uxB
Type "copyright", "credits" or "license" for more information.
>>> import tst
>>> tst.amethod()
amethod 'VOID'
amethod before throw
( 0) 0xc0428cb8 _sigreturn [/usr/lib/libc.1]
( 1) 0xc20c92b8 call__CQ45boost6python6detail8functionP7_objectT1 +
0x578 [/usr/local/lib/libboost_python.sl]
Segmentation fault

% python2.1
Python 2.1b2 (#2, Apr 5 2001, 11:37:45)
[GCC 2.95.3 20010315 (release)] on hp-uxB
Type "copyright", "credits" or "license" for more information.
>>> import tst
>>> tst.world()
World creation 'VOID'
World creation before throw
( 0) 0xc0428cb8 _sigreturn [/usr/lib/libc.1]
( 1) 0xc20c92b8 call__CQ45boost6python6detail8functionP7_objectT1 +
0x578 [/usr/local/lib/libboost_python.sl]
( 2) 0xc20cc590
instance_call__CQ45boost6python6detailt8callable1ZQ45boost6python6detailt11type_
+ 0x70 [/usr/local/lib/libboost_python.sl]
( 3) 0xc20e82dc
call__H2ZP7_objectZP7_object_Q35boost6python26_GLOBAL_$N$types.cppC5Xi8aP7_objec
+ 0x1cc [/usr/local/lib/libboost_python.sl]
( 4) 0xc20e15c0 do_instance_call + 0x50
[/usr/local/lib/libboost_python.sl]
( 5) 0x00079068 call_object + 0xa8 [/usr/local/bin/python2.1]
( 6) 0x00078f68 PyEval_CallObjectWithKeywords + 0xc8
[/usr/local/bin/python2.1]
( 7) 0xc20ca06c
call__CQ45boost6python6detail14bound_functionP7_objectT1 + 0x38c
[/usr/local/lib/libboost_python.sl]
( 8) 0xc20cc240
instance_call__CQ45boost6python6detailt8callable1ZQ45boost6python6detailt11type_
+ 0x70 [/usr/local/lib/libboost_python.sl]
Segmentation fault

#include <string>
#include <boost/python/classes.hpp>
#include <boost/python/class_builder.hpp>
#include <boost/python/module_builder.hpp>
#include <boost/python/errors.hpp>

namespace python = boost::python;

namespace { // Avoid cluttering the global namespace.
 void amethod() {
  fprintf(stderr, "amethod 'VOID'\n");
  PyErr_SetString(PyExc_TypeError, "wrong number of arguments");
  fprintf(stderr, "amethod before throw\n");
  throw boost::python::argument_error();
 }

  // A friendly class.
  class world
  {
    public:
  world() {
   fprintf(stderr, "World creation 'VOID'\n");
   PyErr_SetString(PyExc_TypeError, "wrong number of arguments");
   fprintf(stderr, "World creation before throw\n");
   throw boost::python::argument_error();
  }

  };
}

extern "C" {
 void U_STACK_TRACE(int);
}

// Python requires an exported function called init<module-name> in
every
// extension module. This is where we build the module contents.
BOOST_PYTHON_MODULE_INIT(tst)
{
 signal(SIGSEGV, U_STACK_TRACE);
 try
 {
  // Create an object representing this extension module
  python::module_builder this_module("tst");

  this_module.def(amethod, "amethod");

  // Create the Python type object for our extension class.
  python::class_builder<world> world_class(this_module, "world");

  // Add the __init__ function.
  world_class.def(python::constructor<>());
 }
 catch(...)
 {
  python::handle_exception(); // Deal with the exception for Python
 }
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk