From 304277b806745a44a3eaf0d629da703b671a3b3d Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 29 May 2008 19:48:55 +0000 Subject: [PATCH] See Python C++-SIG thread: "object.attr(object& attrname) proposal" Started 2008-05-25 by hohehohe2@gmail.com. Excerpts: If char const* is passed to objecjt.attr(), it uses PyObject_GetAttrStrng() or PyObject_SetAttrStrng(). If object is passed to objecjt.attr(), it takes the object as a Python string object and uses PyObject_GetAttr() or PyObject_SetAttr(). If attr() behaves like this, it can be useful when there are lots of objects which you know have the same attribute name. You can save time by first making a boost::python::object and passing it to every object's attr() inside a loop. I just made a bit of modification to boost:python locally and did a quick test, like test 1: for(int i = 0; i < n; ++i) { omain.attr(attrname) = 444; //attrname is a char const* } test 2: for(int i = 0; i < n; ++i) { object o = omain.attr(attrname); //attrname is a char const* } test 3: for(int i = 0; i < n; ++i) { omain.attr(oaaaa) = 444; //oaaaa is boost::python::object that represents a string } test 4: for(int i = 0; i < n; ++i) { object o = omain.attr(oaaaa); //oaaaa is boost::python::object that represents a string } and it reasonably reflected the difference between PyObject_*Attr() and PyObject_*AttrString. test 1 :2783ms test 2 :2357ms test 3 :1882ms test 4 :1267ms test5: PyObject_SetAttrString(po_main, "aaaa", po_num444); test6: Py_DECREF(PyObject_GetAttrString(po_main, "aaaa")); test7: PyObject_SetAttr(po_main, po_aaaa, po_num444); test8: Py_DECREF(PyObject_GetAttr(po_main, po_aaaa)); (po_ prefixed variables are PyObject*), all inside each for loop, and the results were test 5 :2410ms test 6 :2277ms test 7 :1629ms test 8 :1094ms It's boost 1.35.0, Python 2.5 on linux(gcc4.1.2). I also did the same test on windows(vs8) and the tendency was not so different. [SVN r45918] --- doc/v2/object.html | 143 ++++++++++++++++++++- include/boost/python/object_attributes.hpp | 48 +++++++ include/boost/python/object_core.hpp | 6 + test/object.cpp | 55 +++++++- test/object.py | 39 ++++++ 5 files changed, 288 insertions(+), 3 deletions(-) mode change 100755 => 100644 include/boost/python/object_attributes.hpp mode change 100755 => 100644 include/boost/python/object_core.hpp mode change 100755 => 100644 test/object.cpp diff --git a/doc/v2/object.html b/doc/v2/object.html index 5572cae4..f35fa579 100644 --- a/doc/v2/object.html +++ b/doc/v2/object.html @@ -74,6 +74,32 @@ +
Class + const_objattribute_policies
+ +
+
+
Class + const_objattribute_policies synopsis
+ +
Class + const_objattribute_policies static functions
+
+
+ +
Class + objattribute_policies
+ +
+
+
Class + objattribute_policies synopsis
+ +
Class + objattribute_policies static functions
+
+
+
Class const_item_policies
@@ -328,6 +354,102 @@ static void del(object const&target, char const* key); +

Class + const_objattribute_policies

+ +

The policies which are used for proxies representing an attribute + access to a const object when the attribute name is + given as a const object.

+ +

Class + const_objattribute_policies synopsis

+
+namespace boost { namespace python { namespace api
+{
+  struct const_objattribute_policies
+  {
+      typedef object const& key_type;
+      static object get(object const& target, object const& key);
+  };
+}}}
+
+ +

Class + const_objattribute_policies static functions

+
+static object get(object const& target, object const& key);
+
+ +
+
Requires: key is an object + holding a string.
+ +
Effects: accesses the attribute of target named + by key.
+ +
Returns: An object managing the result of the + attribute access.
+ +
Throws: error_already_set if a + Python exception is raised.
+
+ +

Class + objattribute_policies

+ +

The policies which are used for proxies representing an attribute + access to a mutable object when the attribute name is + given as a const object.

+ +

Class + objattribute_policies synopsis

+
+namespace boost { namespace python { namespace api
+{
+  struct objattribute_policies : const_objattribute_policies
+  {
+      static object const& set(object const& target, object const& key, object const& value);
+      static void del(object const&target, object const& key);
+  };
+}}}
+
+ +

Class + objattribute_policies static functions

+
+static object const& set(object const& target, object const& key, object const& value);
+
+ +
+
Requires: key is an object + holding a string.
+ +
Effects: sets the attribute of target named by + key to value.
+ +
Throws: error_already_set if a + Python exception is raised.
+
+
+static void del(object const&target, object const& key);
+
+ +
+
Requires: key is an object + holding a string.
+ +
Effects: deletes the attribute of target named + by key.
+ +
Throws: error_already_set if a + Python exception is raised.
+
+ + +

Class const_item_policies

@@ -542,6 +664,8 @@ namespace boost { namespace python { namespace api // proxy<const_object_attribute> attr(char const*) const; proxy<object_attribute> attr(char const*); + proxy<const_object_objattribute> attr(object const&) const; + proxy<object_objattribute> attr(object const&); // item access // @@ -608,6 +732,21 @@ proxy<object_attribute> attr(char const* name); name as its key.
+proxy<const_object_objattribute> attr(const object& name) const;
+proxy<object_objattribute> attr(const object& name);
+
+ +
+
Requires: name is a object holding a string.
+ +
Effects: accesses the named attribute of + *this.
+ +
Returns: a proxy object which binds + object(*static_cast<U*>(this)) as its target, and + name as its key.
+
+
 template <class T>
 proxy<const_object_item> operator[](T const& key) const;
 template <class T>
@@ -938,11 +1077,11 @@ object sum_items(object seq)
 

Revised - 13 January, 2006 + 27 May, 2008

© Copyright Dave Abrahams 2006.

+ "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams 2008.

diff --git a/include/boost/python/object_attributes.hpp b/include/boost/python/object_attributes.hpp old mode 100755 new mode 100644 index 012240b2..26d59911 --- a/include/boost/python/object_attributes.hpp +++ b/include/boost/python/object_attributes.hpp @@ -17,6 +17,7 @@ struct const_attribute_policies { typedef char const* key_type; static object get(object const& target, char const* key); + static object get(object const& target, object const& key); }; struct attribute_policies : const_attribute_policies @@ -25,6 +26,18 @@ struct attribute_policies : const_attribute_policies static void del(object const&target, char const* key); }; +struct const_objattribute_policies +{ + typedef object const key_type; + static object get(object const& target, object const& key); +}; + +struct objattribute_policies : const_objattribute_policies +{ + static object const& set(object const& target, object const& key, object const& value); + static void del(object const&target, object const& key); +}; + // // implementation // @@ -42,11 +55,30 @@ inline const_object_attribute object_operators::attr(char const* name) const return const_object_attribute(x, name); } +template +inline object_objattribute object_operators::attr(object const& name) +{ + object_cref2 x = *static_cast(this); + return object_objattribute(x, name); +} + +template +inline const_object_objattribute object_operators::attr(object const& name) const +{ + object_cref2 x = *static_cast(this); + return const_object_objattribute(x, name); +} + inline object const_attribute_policies::get(object const& target, char const* key) { return python::getattr(target, key); } +inline object const_objattribute_policies::get(object const& target, object const& key) +{ + return python::getattr(target, key); +} + inline object const& attribute_policies::set( object const& target , char const* key @@ -56,6 +88,15 @@ inline object const& attribute_policies::set( return value; } +inline object const& objattribute_policies::set( + object const& target + , object const& key + , object const& value) +{ + python::setattr(target, key, value); + return value; +} + inline void attribute_policies::del( object const& target , char const* key) @@ -63,6 +104,13 @@ inline void attribute_policies::del( python::delattr(target, key); } +inline void objattribute_policies::del( + object const& target + , object const& key) +{ + python::delattr(target, key); +} + }}} // namespace boost::python::api #endif // OBJECT_ATTRIBUTES_DWA2002615_HPP diff --git a/include/boost/python/object_core.hpp b/include/boost/python/object_core.hpp old mode 100755 new mode 100644 index ecd7257f..b4a0ea82 --- a/include/boost/python/object_core.hpp +++ b/include/boost/python/object_core.hpp @@ -59,6 +59,8 @@ namespace api struct const_attribute_policies; struct attribute_policies; + struct const_objattribute_policies; + struct objattribute_policies; struct const_item_policies; struct item_policies; struct const_slice_policies; @@ -67,6 +69,8 @@ namespace api typedef proxy const_object_attribute; typedef proxy object_attribute; + typedef proxy const_object_objattribute; + typedef proxy object_objattribute; typedef proxy const_object_item; typedef proxy object_item; typedef proxy const_object_slice; @@ -108,6 +112,8 @@ namespace api // const_object_attribute attr(char const*) const; object_attribute attr(char const*); + const_object_objattribute attr(object const&) const; + object_objattribute attr(object const&); // item access // diff --git a/test/object.cpp b/test/object.cpp old mode 100755 new mode 100644 index 1d9f6bcf..04afe841 --- a/test/object.cpp +++ b/test/object.cpp @@ -38,26 +38,61 @@ object obj_getattr(object x, char const* name) return x.attr(name); } +object obj_objgetattr(object x, object const& name) +{ + return x.attr(name); +} + object obj_const_getattr(object const& x, char const* name) { return x.attr(name); } +object obj_const_objgetattr(object const& x, object const& name) +{ + return x.attr(name); +} + void obj_setattr(object x, char const* name, object value) { x.attr(name) = value; } +void obj_objsetattr(object x, object const& name, object value) +{ + x.attr(name) = value; +} + void obj_setattr42(object x, char const* name) { x.attr(name) = 42; } +void obj_objsetattr42(object x, object const& name) +{ + x.attr(name) = 42; +} + void obj_moveattr(object& x, char const* src, char const* dst) { x.attr(dst) = x.attr(src); } +void obj_objmoveattr(object& x, object const& src, object const& dst) +{ + x.attr(dst) = x.attr(src); +} + +void obj_delattr(object x, char const* name) +{ + x.attr(name).del(); +} + +void obj_objdelattr(object x, object const& name) +{ + x.attr(name).del(); +} + object obj_getitem(object x, object key) { return x[key]; @@ -108,11 +143,21 @@ bool test_attr(object y, char* name) return y.attr(name); } +bool test_objattr(object y, object& name) +{ + return y.attr(name); +} + bool test_not_attr(object y, char* name) { return !y.attr(name); } +bool test_not_objattr(object y, object& name) +{ + return !y.attr(name); +} + bool test_item(object y, object key) { return y[key]; @@ -301,11 +346,17 @@ BOOST_PYTHON_MODULE(object_ext) def("number", number); def("obj_getattr", obj_getattr); + def("obj_objgetattr", obj_objgetattr); def("obj_const_getattr", obj_const_getattr); + def("obj_const_objgetattr", obj_const_objgetattr); def("obj_setattr", obj_setattr); + def("obj_objsetattr", obj_objsetattr); def("obj_setattr42", obj_setattr42); + def("obj_objsetattr42", obj_objsetattr42); def("obj_moveattr", obj_moveattr); - + def("obj_objmoveattr", obj_objmoveattr); + def("obj_delattr", obj_delattr); + def("obj_objdelattr", obj_objdelattr); def("obj_getitem", obj_getitem); def("obj_getitem3", obj_getitem); @@ -319,7 +370,9 @@ BOOST_PYTHON_MODULE(object_ext) def("test_not", test_not); def("test_attr", test_attr); + def("test_objattr", test_objattr); def("test_not_attr", test_not_attr); + def("test_not_objattr", test_not_objattr); def("test_item", test_item); def("test_not_item", test_not_item); diff --git a/test/object.py b/test/object.py index b51650a6..82634082 100644 --- a/test/object.py +++ b/test/object.py @@ -34,29 +34,68 @@ >>> try: obj_getattr(x, 'foo') ... except AttributeError: pass ... else: print 'expected an exception' +>>> try: obj_objgetattr(x, 'objfoo') +... except AttributeError: pass +... else: print 'expected an exception' >>> obj_setattr(x, 'foo', 1) >>> x.foo 1 +>>> obj_objsetattr(x, 'objfoo', 1) +>>> try:obj_objsetattr(x, 1) +... except TypeError: pass +... else: print 'expected an exception' +>>> x.objfoo +1 >>> obj_getattr(x, 'foo') 1 +>>> obj_objgetattr(x, 'objfoo') +1 +>>> try:obj_objgetattr(x, 1) +... except TypeError: pass +... else: print 'expected an exception' >>> obj_const_getattr(x, 'foo') 1 +>>> obj_const_objgetattr(x, 'objfoo') +1 >>> obj_setattr42(x, 'foo') >>> x.foo 42 +>>> obj_objsetattr42(x, 'objfoo') +>>> x.objfoo +42 >>> obj_moveattr(x, 'foo', 'bar') >>> x.bar 42 +>>> obj_objmoveattr(x, 'objfoo', 'objbar') +>>> x.objbar +42 >>> test_attr(x, 'foo') 1 +>>> test_objattr(x, 'objfoo') +1 >>> test_not_attr(x, 'foo') 0 +>>> test_not_objattr(x, 'objfoo') +0 >>> x.foo = None >>> test_attr(x, 'foo') 0 +>>> x.objfoo = None +>>> test_objattr(x, 'objfoo') +0 >>> test_not_attr(x, 'foo') 1 +>>> test_not_objattr(x, 'objfoo') +1 +>>> obj_delattr(x, 'foo') +>>> obj_objdelattr(x, 'objfoo') +>>> try:obj_delattr(x, 'foo') +... except AttributeError: pass +... else: print 'expected an exception' +>>> try:obj_objdelattr(x, 'objfoo') +... except AttributeError: pass +... else: print 'expected an exception' Items