[C++-sig] [] subscript notation with boost::python
I'm trying to support this notation in python:
something = record["somefieldname"]
I had that working with the Python C API, by implementing the len and
getitem functions, setting them in the PyTypeObject::tp_as_mapping
struct field.
I'm now trying to do this with boost::python, like so, because googling
has suggested that this should work:
boost::python::class_("Record")
.def("__getitem__", &Glom::PyGlomRecord::getitem)
.def("__len__", &Glom::PyGlomRecord::len)
But when I try to use that in Python, I get this error:
"Boost.Python.class' object is unsubscriptable"
Is there something else that I need to do?
--
Murray Cumming
[email protected]
www.murrayc.com
www.openismus.com
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] [] subscript notation with boost::python
Murray Cumming schrieb am Montag 23 Februar 2009 um 12:39:
> I'm trying to support this notation in python:
> something = record["somefieldname"]
[snip]
> I'm now trying to do this with boost::python, like so, because googling
> has suggested that this should work:
>
> boost::python::class_("Record")
> .def("__getitem__", &Glom::PyGlomRecord::getitem)
> .def("__len__", &Glom::PyGlomRecord::len)
>
> But when I try to use that in Python, I get this error:
> "Boost.Python.class' object is unsubscriptable"
>
> Is there something else that I need to do?
I assume Glom::PyGlomRecord::getitem hasn't the right signature, which isn't
checked at compile time
{{{
struct PyGlomRecord
{
whatever_type getitem(boost::python::object subscriptionObj);
};
}}}
Here is an working example
{{{
#include
namespace bp = boost::python;
struct Subscriptable
{
double val;
double getitem(bp::object subscriptionObj)
{
return this->val;
}
double setitem(bp::object subscriptionObj, double val)
{
this->val = val;
}
};
BOOST_PYTHON_MODULE(mypy)
{
bp::class_("Subscriptable")
.def("__getitem__", &Subscriptable::getitem)
.def("__setitem__", &Subscriptable::setitem);
}
}}}
HTH,
-- Maik
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] [] subscript notation with boost::python
Maik Beckmann wrote:
Murray Cumming schrieb am Montag 23 Februar 2009 um 12:39:
I'm trying to support this notation in python:
something = record["somefieldname"]
[snip]
I'm now trying to do this with boost::python, like so, because googling
has suggested that this should work:
boost::python::class_("Record")
.def("__getitem__", &Glom::PyGlomRecord::getitem)
.def("__len__", &Glom::PyGlomRecord::len)
But when I try to use that in Python, I get this error:
"Boost.Python.class' object is unsubscriptable"
You're trying to subscript a class, not an instance of a class. I bet
you've got something like this:
r = Record
feh = r['thing']
which is wrong... you want:
r = Record()
feh = r['thing']
-t
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] [] subscript notation with boost::python
troy d. straszheim schrieb am Montag 23 Februar 2009 um 19:15: > > Murray Cumming schrieb am Montag 23 Februar 2009 um 12:39: > >> I'm trying to support this notation in python: > >> something = record["somefieldname"] [snip] > You're trying to subscript a class, not an instance of a class. I bet > you've got something like this: > >r = Record >feh = r['thing'] > > which is wrong... you want: > >r = Record() >feh = r['thing'] > Considering what Murray done so far, I really doubt he makes this kind of a mistake. :P -- Maik ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] [] subscript notation with boost::python
Maik Beckmann wrote: Considering what Murray done so far, I really doubt he makes this kind of a mistake. :P Experts can make this mistake too. I wasn't being condescending. The error message speaks for itself: "Boost.Python.class' object is unsubscriptable" If the problem was what you think it is, I believe the error message would be about c++-to-python conversions, bad function signatures or somesuch... -t ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] [] subscript notation with boost::python
troy d. straszheim schrieb am Montag 23 Februar 2009 um 19:51:
> Maik Beckmann wrote:
> > Considering what Murray done so far, I really doubt he makes this kind of
> > a mistake. :P
>
> Experts can make this mistake too. I wasn't being condescending. The
> error message speaks for itself:
>
>"Boost.Python.class' object is unsubscriptable"
>
> If the problem was what you think it is, I believe the error message
> would be about c++-to-python conversions, bad function signatures or
> somesuch...
>
You are right, the error message looks like this
{{{
Traceback (most recent call last):
File "", line 1, in
Boost.Python.ArgumentError: Python argument types in
Subscriptable.__getitem__(Subscriptable, int)
did not match C++ signature:
__getitem__(Subscriptable {lvalue})
}}}
and I am wrong :(
Best Regards anyway :)
-- Maik
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
