implicitly_convertible and char*
Hello, The implicitly_convertible sample at http://boost.org/libs/python/doc/v2/implicit.html#implicitly_convertible-spe... works fine for me. However, changing "int" to "char*" makes it stop working. Compiles fine, won't invoke at runtime.
From the traceback:
Boost.Python.ArgumentError: Python argument types in __testmodule__.test_string(X) did not match C++ signature: test_string(char *) I am using MSVC7.1 Any info?
Dusty Leary <dleary@gmail.com> writes:
The implicitly_convertible sample at http://boost.org/libs/python/doc/v2/implicit.html#implicitly_convertible-spe... works fine for me.
However, changing "int" to "char*" makes it stop working. Compiles fine, won't invoke at runtime.
From the traceback:
Boost.Python.ArgumentError: Python argument types in __testmodule__.test_string(X) did not match C++ signature: test_string(char *)
I am using MSVC7.1
Any info?
Full source code of your example? Perhaps the problem is that X isn't actually convertible to char*? -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
full source code: ================================================ #define BOOST_PYTHON_STATIC_LIB #include <boost/python.hpp> using namespace boost::python; struct X { std::string v; X(const std::string& s) : v(s) {} operator char*() const { return (char*)v.c_str(); } }; void test_x(X& x) { char* x_string = x; printf("test_x: %s\n", x_string); } void test_string(char* s) { printf("test_string: %s\n", s); } BOOST_PYTHON_MODULE(test_string) { class_<X>("X", init<const std::string&>()); def("test_x", test_x); def("test_string", test_string); implicitly_convertible<X, char*>(); } ================================================ interpreter:
import test_string x = test_string.X("foo") test_string.test_x(x) test_x: foo test_string.test_string("blah") test_string: blah test_string.test_string(x) Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in test_string.test_string(X) did not match C++ signature: test_string(char *)
On Sun, 27 Jun 2004 20:39:56 -0400, David Abrahams <dave@boost-consulting.com> wrote:
Dusty Leary <dleary@gmail.com> writes:
The implicitly_convertible sample at http://boost.org/libs/python/doc/v2/implicit.html#implicitly_convertible-spe... works fine for me.
However, changing "int" to "char*" makes it stop working. Compiles fine, won't invoke at runtime.
From the traceback:
Boost.Python.ArgumentError: Python argument types in __testmodule__.test_string(X) did not match C++ signature: test_string(char *)
I am using MSVC7.1
Any info?
Full source code of your example? Perhaps the problem is that X isn't actually convertible to char*?
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
Dusty Leary <dleary@gmail.com> writes:
full source code: ================================================ #define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp> using namespace boost::python;
struct X { std::string v; X(const std::string& s) : v(s) {} operator char*() const { return (char*)v.c_str(); } };
void test_x(X& x) { char* x_string = x; printf("test_x: %s\n", x_string); }
void test_string(char* s) { printf("test_string: %s\n", s); }
Oh! char* is not a C++ string, and there's no built-in converter for that. I note that you're casting away the const-ness of v.c_str() above; that's very bad juju. If you make char* into char const* everywhere, your example should work. Alternatively, you can also register a custom from_python converter for char*... but I recommend against it.
BOOST_PYTHON_MODULE(test_string) { class_<X>("X", init<const std::string&>()); def("test_x", test_x); def("test_string", test_string); implicitly_convertible<X, char*>(); } ================================================
interpreter:
import test_string x = test_string.X("foo") test_string.test_x(x) test_x: foo test_string.test_string("blah") test_string: blah test_string.test_string(x) Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in test_string.test_string(X) did not match C++ signature: test_string(char *)
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
Oh!
char* is not a C++ string, and there's no built-in converter for that. I note that you're casting away the const-ness of v.c_str() above; that's very bad juju. If you make char* into char const* everywhere, your example should work. Alternatively, you can also register a custom from_python converter for char*... but I recommend against it.
yeah, I just wrote up the simple test like that because I was lazy and didn't think it would make a difference. the same problem occurs with const char* new source code: =================================================== #define BOOST_PYTHON_STATIC_LIB #include <boost/python.hpp> using namespace boost::python; struct X { std::string v; X(const std::string& s) : v(s) {} operator char const*() const { return v.c_str(); } }; void test_x(X& x) { char const* x_string = x; printf("test_x: %s\n", x_string); } void test_string(char const* s) { printf("test_string: %s\n", s); } BOOST_PYTHON_MODULE(test_string) { class_<X>("X", init<const std::string&>()); def("test_x", test_x); def("test_string", test_string); implicitly_convertible<X, char const*>(); } ===================================================
import test_string x = test_string.X("foo") test_string.test_string(x) Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in test_string.test_string(X) did not match C++ signature: test_string(char const *)
I don't really feel competent in this matter, but I believe char const* and const char* are two different things. Did you try it with "const char*"? --- Dusty Leary <dleary@gmail.com> wrote:
Oh!
char* is not a C++ string, and there's no built-in converter for that. I note that you're casting away the const-ness of v.c_str() above; that's very bad juju. If you make char* into char const* everywhere, your example should work. Alternatively, you can also register a custom from_python converter for char*... but I recommend against it.
yeah, I just wrote up the simple test like that because I was lazy and didn't think it would make a difference. the same problem occurs with const char*
new source code: =================================================== #define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp> using namespace boost::python;
struct X { std::string v; X(const std::string& s) : v(s) {} operator char const*() const { return v.c_str(); } };
void test_x(X& x) { char const* x_string = x; printf("test_x: %s\n", x_string); }
void test_string(char const* s) { printf("test_string: %s\n", s); }
BOOST_PYTHON_MODULE(test_string) { class_<X>("X", init<const std::string&>()); def("test_x", test_x); def("test_string", test_string); implicitly_convertible<X, char const*>(); } ===================================================
import test_string x = test_string.X("foo") test_string.test_string(x) Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in test_string.test_string(X) did not match C++ signature: test_string(char const *)
__________________________________ Do you Yahoo!? New and Improved Yahoo! Mail - 100MB free storage! http://promotions.yahoo.com/new_mail
Ralf W. Grosse-Kunstleve wrote:
I don't really feel competent in this matter, but I believe char const* and const char* are two different things. Did you try it with "const char*"?
Nope. That's the same (read from right to left). Different things is ...* const (constant pointer itself). Mike PS. only ddd afaik treat them differently ;)
"Ralf W. Grosse-Kunstleve" <rwgk@yahoo.com> writes:
I don't really feel competent in this matter, but I believe char const* and const char* are two different things. Did you try it with "const char*"?
They are the same thing. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
so, any news on this? On Tue, 29 Jun 2004 20:49:39 -0400, David Abrahams <dave@boost-consulting.com> wrote:
"Ralf W. Grosse-Kunstleve" <rwgk@yahoo.com> writes:
I don't really feel competent in this matter, but I believe char const* and const char* are two different things. Did you try it with "const char*"?
They are the same thing.
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
Dusty Leary <dleary@gmail.com> writes:
so, any news on this?
What news grould there be? char const* and const char* are the same thing, so trying it won't make any difference.
On Tue, 29 Jun 2004 20:49:39 -0400, David Abrahams <dave@boost-consulting.com> wrote:
"Ralf W. Grosse-Kunstleve" <rwgk@yahoo.com> writes:
I don't really feel competent in this matter, but I believe char const* and const char* are two different things. Did you try it with "const char*"?
They are the same thing.
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
yeah, that was an unrelated side note that someone else brought up... the issue is that implicity_convertible<X, const char*> is not working On Sat, 03 Jul 2004 14:19:27 -0400, David Abrahams <dave@boost-consulting.com> wrote:
Dusty Leary <dleary@gmail.com> writes:
so, any news on this?
What news grould there be? char const* and const char* are the same thing, so trying it won't make any difference.
On Tue, 29 Jun 2004 20:49:39 -0400, David Abrahams <dave@boost-consulting.com> wrote:
"Ralf W. Grosse-Kunstleve" <rwgk@yahoo.com> writes:
I don't really feel competent in this matter, but I believe char const* and const char* are two different things. Did you try it with "const char*"?
They are the same thing.
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
I guess I'm being a pain, but I still have my same problem. To recap: I write saying that implicitly_convertible<X, char*> doesn't work. Dave responds saying that char* is not a valid C++ string, that char const * should work. I amend my example to show that char const * does not work. Thread gets a little sidetracked with "const char*" vs "char const *" ...Limbo On Sat, 3 Jul 2004 14:23:07 -0400, Dusty Leary <dleary@gmail.com> wrote:
yeah, that was an unrelated side note that someone else brought up... the issue is that implicity_convertible<X, const char*> is not working
On Sat, 03 Jul 2004 14:19:27 -0400, David Abrahams
<dave@boost-consulting.com> wrote:
Dusty Leary <dleary@gmail.com> writes:
so, any news on this?
What news grould there be? char const* and const char* are the same thing, so trying it won't make any difference.
On Tue, 29 Jun 2004 20:49:39 -0400, David Abrahams <dave@boost-consulting.com> wrote:
"Ralf W. Grosse-Kunstleve" <rwgk@yahoo.com> writes:
I don't really feel competent in this matter, but I believe char const* and const char* are two different things. Did you try it with "const char*"?
They are the same thing.
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
Dusty Leary <dleary@gmail.com> writes:
I guess I'm being a pain, but I still have my same problem.
To recap:
I write saying that implicitly_convertible<X, char*> doesn't work. Dave responds saying that char* is not a valid C++ string, that char const * should work. I amend my example to show that char const * does not work. Thread gets a little sidetracked with "const char*" vs "char const *" ...Limbo
Sorry, I just don't think I have time to look into the problem right now :( -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
--- David Abrahams <dave@boost-consulting.com> wrote:
I write saying that implicitly_convertible<X, char*> doesn't work. Dave responds saying that char* is not a valid C++ string, that char const * should work. I amend my example to show that char const * does not work. Thread gets a little sidetracked with "const char*" vs "char const *" ...Limbo
Sorry, I just don't think I have time to look into the problem right now :(
In that case I'd work around the problem in one of these ways: - create "thin wrapper" functions. I.e. make standalone function with X const& or X& as the first parameter, and .def() them as member function. You can find several examples of thin wrappers in this short piece of code: http://cvs.sourceforge.net/viewcvs.py/cctbx/scitbx/include/scitbx/stl/set_wr... - if that is impractical due to the large number of functions that you might have to wrap, write your own "custom from python converter" which you could use to convert any Python object to X. Here is an example: http://cvs.sourceforge.net/viewcvs.py/cctbx/scitbx/include/scitbx/boost_pyth... Look for "slice_from_python". It is probably not easy to grasp although it is not much code, but understanding this approach will give you access to a very ^H^H^H^Hextremely powerful tool. Ralf __________________________________ Do you Yahoo!? Yahoo! Mail - 50x more storage than other providers! http://promotions.yahoo.com/new_mail
participants (4)
-
David Abrahams -
Dusty Leary -
Mike Rovner -
Ralf W. Grosse-Kunstleve