Boost logo

Boost Users :

Subject: Re: [Boost-users] TypeError: No registered converter was able to extract a C++ pointer to type char from this Python object of type str
From: David Abrahams (dave_at_[hidden])
Date: 2010-08-14 17:51:01


At Fri, 13 Aug 2010 09:25:22 -0500,
Brian Ray wrote:
>
> Hello:
>
> I think I am experience a build problem here but not really sure;
> nonetheless, below is a simple example that illustrates my problem.
I don't think it's a build issue.

> Any idea what I might be doing wrong. I am using boost_1_43_0 built
> with gcc 4.2.1 on i686 running darwin. I am linking to the standard
> 2.5 python that comes with the distro.
>
> Please note that the PyRun_SimpleString("import sys"); does not error
> at all. The error comes from when the call to import(). I am not
> sure if this is a clue or not but gdb seems not to know what line it
> is on. I am guessing it is just confused because of all the inlines. I
> suspect this is a linking issues of some sort. However I did check
> that they are all linking to the correct location. Why does it
> convert the const char * to a str in the first place.
              ^^^^^

So that it can call Python's import function.

> Seems to be thrashing around a bit.
>
> -- Brian
>
> /// contents of boost_python_issue.cpp
> #include <boost/python.hpp>
> using namespace boost::python;
>
> int main(int argc, char **argv)
> {
> Py_Initialize();
> try {
> PyRun_SimpleString("import sys");
> // This raises exception
> object sys = import("sys");
> } catch (boost::python::error_already_set) {
> PyErr_Print();
> }
> Py_Finalize();
> }

Hmm, looks simple enough.

> Output from gdb:
>
> (gdb) break main
> Breakpoint 1 at 0x100001319: file boost_python_issue.cpp, line 9.
> (gdb) r
> Starting program: /Project/zephyr/src/Composer/boost_python_issue
> Reading symbols for shared libraries .++++++++..... done
>
> Breakpoint 1, main (argc=1, argv=0x7fff5fbff6d8) at boost_python_issue.cpp:9
> 9 Py_Initialize();
> (gdb) s
> 11 PyRun_SimpleString("import sys");
> (gdb)
> 13 object sys = import("sys");
> (gdb)
> boost::python::str::str (this=0x7fff5fbff690, s=0x10000169d "sys") at
> str.hpp:150
> 150 str(const char* s) : base(s) {} // new str
> (gdb)
> boost::python::detail::str_base::str_base (this=0x7fff5fbff690,
> s=0x10000169d "sys") at libs/python/src/str.cpp:39
> 39 ))
> (gdb)
> object [inlined] () at
> /Users/bray/sources/boost_1_43_0/boost/python/object_core.hpp:534
> Line number 534 out of range; errors.hpp has 55 lines.
> (gdb)
> expect_non_null<PyObject> [inlined] () at
> /Users/bray/sources/boost_1_43_0/boost/python/errors.hpp:43
> 44 if (x == 0)
> (gdb)
> object [inlined] () at
> /Users/bray/sources/boost_1_43_0/boost/python/object_core.hpp:513
> 534 : object_base(expect_non_null((PyObject*)p))
> (gdb)
> object_base [inlined] () at
> /Users/bray/sources/boost_1_43_0/boost/python/object_core.hpp:513
> 513 : m_ptr(p)
> (gdb)
> boost::python::detail::str_base::str_base (this=0x7fff5fbff690,
> s=<value temporarily unavailable, due to optimizations>) at
> libs/python/src/str.cpp:40
> 40 {}
> (gdb)
> 0x000000010000154d in boost::python::str::str (this=0x7fff5fbff690,
> s=0x10000169d "sys") at str.hpp:150
> 150 str(const char* s) : base(s) {} // new str
> (gdb)
> boost::python::import (name=@0x7fff5fbff690) at libs/python/src/import.cpp:19
> 19 char *n = python::extract<char *>(name);

This is where the bug is (in Boost.Python). One should never try to
extract a pointer to non-const char from a str. Could you try the
enclosed patch and see if your problem is fixed?


-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net