Skip to content

Commit 304277b

Browse files
author
Ralf W. Grosse-Kunstleve
committed
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]
1 parent a334649 commit 304277b

File tree

5 files changed

+288
-3
lines changed

5 files changed

+288
-3
lines changed

doc/v2/object.html

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ <h2>Contents</h2>
7474
</dl>
7575
</dd>
7676

77+
<dt><a href="#const_objattribute_policies-spec">Class
78+
<code>const_objattribute_policies</code></a></dt>
79+
80+
<dd>
81+
<dl class="page-index">
82+
<dt><a href="#const_objattribute_policies-spec-synopsis">Class
83+
<code>const_objattribute_policies</code> synopsis</a></dt>
84+
85+
<dt><a href="#const_objattribute_policies-spec-statics">Class
86+
<code>const_objattribute_policies</code> static functions</a></dt>
87+
</dl>
88+
</dd>
89+
90+
<dt><a href="#objattribute_policies-spec">Class
91+
<code>objattribute_policies</code></a></dt>
92+
93+
<dd>
94+
<dl class="page-index">
95+
<dt><a href="#objattribute_policies-spec-synopsis">Class
96+
<code>objattribute_policies</code> synopsis</a></dt>
97+
98+
<dt><a href="#objattribute_policies-spec-statics">Class
99+
<code>objattribute_policies</code> static functions</a></dt>
100+
</dl>
101+
</dd>
102+
77103
<dt><a href="#const_item_policies-spec">Class
78104
<code>const_item_policies</code></a></dt>
79105

@@ -328,6 +354,102 @@ <h4><a name="attribute_policies-spec-statics"></a>Class
328354
<!-- end -->
329355
<!-- begin -->
330356

357+
<h3><a name="const_objattribute_policies-spec"></a>Class
358+
<code>const_objattribute_policies</code></h3>
359+
360+
<p>The policies which are used for proxies representing an attribute
361+
access to a <code>const&nbsp;object</code> when the attribute name is
362+
given as a <code>const&nbsp;object</code>.</p>
363+
364+
<h4><a name="const_objattribute_policies-spec-synopsis"></a>Class
365+
<code>const_objattribute_policies</code> synopsis</h4>
366+
<pre>
367+
namespace boost { namespace python { namespace api
368+
{
369+
struct const_objattribute_policies
370+
{
371+
typedef object const& key_type;
372+
static object get(object const&amp; target, object const& key);
373+
};
374+
}}}
375+
</pre>
376+
377+
<h4><a name="const_objattribute_policies-spec-statics"></a>Class
378+
<code>const_objattribute_policies</code> static functions</h4>
379+
<pre>
380+
static object get(object const&amp; target, object const& key);
381+
</pre>
382+
383+
<dl class="function-semantics">
384+
<dt><b>Requires:</b> <code>key</code> is an <code>object</code>
385+
holding a string.</dt>
386+
387+
<dt><b>Effects:</b> accesses the attribute of <code>target</code> named
388+
by <code>key</code>.</dt>
389+
390+
<dt><b>Returns:</b> An <code>object</code> managing the result of the
391+
attribute access.</dt>
392+
393+
<dt><b>Throws:</b> <code><a href=
394+
"errors.html#error_already_set-spec">error_already_set</a></code> if a
395+
Python exception is raised.</dt>
396+
</dl>
397+
398+
<h3><a name="objattribute_policies-spec"></a>Class
399+
<code>objattribute_policies</code></h3>
400+
401+
<p>The policies which are used for proxies representing an attribute
402+
access to a mutable <code>object</code> when the attribute name is
403+
given as a <code>const&nbsp;object</code>.</p>
404+
405+
<h4><a name="objattribute_policies-spec-synopsis"></a>Class
406+
<code>objattribute_policies</code> synopsis</h4>
407+
<pre>
408+
namespace boost { namespace python { namespace api
409+
{
410+
struct objattribute_policies : const_objattribute_policies
411+
{
412+
static object const&amp; set(object const&amp; target, object const& key, object const&amp; value);
413+
static void del(object const&amp;target, object const& key);
414+
};
415+
}}}
416+
</pre>
417+
418+
<h4><a name="objattribute_policies-spec-statics"></a>Class
419+
<code>objattribute_policies</code> static functions</h4>
420+
<pre>
421+
static object const&amp; set(object const&amp; target, object const& key, object const&amp; value);
422+
</pre>
423+
424+
<dl class="function-semantics">
425+
<dt><b>Requires:</b> <code>key</code> is an <code>object</code>
426+
holding a string.</dt>
427+
428+
<dt><b>Effects:</b> sets the attribute of <code>target</code> named by
429+
<code>key</code> to <code>value</code>.</dt>
430+
431+
<dt><b>Throws:</b> <code><a href=
432+
"errors.html#error_already_set-spec">error_already_set</a></code> if a
433+
Python exception is raised.</dt>
434+
</dl>
435+
<pre>
436+
static void del(object const&amp;target, object const& key);
437+
</pre>
438+
439+
<dl class="function-semantics">
440+
<dt><b>Requires:</b> <code>key</code> is an <code>object</code>
441+
holding a string.</dt>
442+
443+
<dt><b>Effects:</b> deletes the attribute of <code>target</code> named
444+
by <code>key</code>.</dt>
445+
446+
<dt><b>Throws:</b> <code><a href=
447+
"errors.html#error_already_set-spec">error_already_set</a></code> if a
448+
Python exception is raised.</dt>
449+
</dl>
450+
<!-- end -->
451+
<!-- begin -->
452+
331453
<h3><a name="const_item_policies-spec"></a>Class
332454
<code>const_item_policies</code></h3>
333455

@@ -542,6 +664,8 @@ <h4><a name="object_operators-spec-synopsis"></a>Class template
542664
//
543665
proxy&lt;const_object_attribute&gt; attr(char const*) const;
544666
proxy&lt;object_attribute&gt; attr(char const*);
667+
proxy&lt;const_object_objattribute&gt; attr(object const&) const;
668+
proxy&lt;object_objattribute&gt; attr(object const&);
545669

546670
// item access
547671
//
@@ -608,6 +732,21 @@ <h4><a name="object_operators-spec-observers"></a>Class template
608732
<code>name</code> as its key.</dt>
609733
</dl>
610734
<pre>
735+
proxy&lt;const_object_objattribute&gt; attr(const object& name) const;
736+
proxy&lt;object_objattribute&gt; attr(const object& name);
737+
</pre>
738+
739+
<dl class="function-semantics">
740+
<dt><b>Requires:</b> name is a <code>object</code> holding a string.</dt>
741+
742+
<dt><b>Effects:</b> accesses the named attribute of
743+
<code>*this</code>.</dt>
744+
745+
<dt><b>Returns:</b> a proxy object which binds
746+
<code>object(*static_cast&lt;U*&gt;(this))</code> as its target, and
747+
<code>name</code> as its key.</dt>
748+
</dl>
749+
<pre>
611750
template &lt;class T&gt;
612751
proxy&lt;const_object_item&gt; operator[](T const&amp; key) const;
613752
template &lt;class T&gt;
@@ -938,11 +1077,11 @@ <h2><a name="examples"></a>Example</h2>
9381077
</pre>
9391078
<p>Revised
9401079
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
941-
13 January, 2006
1080+
27 May, 2008
9421081
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
9431082
</p>
9441083

9451084
<p><i>&copy; Copyright <a href=
946-
"http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2006.</i></p>
1085+
"http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2008.</i></p>
9471086
</body>
9481087
</html>

include/boost/python/object_attributes.hpp

100755100644
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct const_attribute_policies
1717
{
1818
typedef char const* key_type;
1919
static object get(object const& target, char const* key);
20+
static object get(object const& target, object const& key);
2021
};
2122

2223
struct attribute_policies : const_attribute_policies
@@ -25,6 +26,18 @@ struct attribute_policies : const_attribute_policies
2526
static void del(object const&target, char const* key);
2627
};
2728

29+
struct const_objattribute_policies
30+
{
31+
typedef object const key_type;
32+
static object get(object const& target, object const& key);
33+
};
34+
35+
struct objattribute_policies : const_objattribute_policies
36+
{
37+
static object const& set(object const& target, object const& key, object const& value);
38+
static void del(object const&target, object const& key);
39+
};
40+
2841
//
2942
// implementation
3043
//
@@ -42,11 +55,30 @@ inline const_object_attribute object_operators<U>::attr(char const* name) const
4255
return const_object_attribute(x, name);
4356
}
4457

58+
template <class U>
59+
inline object_objattribute object_operators<U>::attr(object const& name)
60+
{
61+
object_cref2 x = *static_cast<U*>(this);
62+
return object_objattribute(x, name);
63+
}
64+
65+
template <class U>
66+
inline const_object_objattribute object_operators<U>::attr(object const& name) const
67+
{
68+
object_cref2 x = *static_cast<U const*>(this);
69+
return const_object_objattribute(x, name);
70+
}
71+
4572
inline object const_attribute_policies::get(object const& target, char const* key)
4673
{
4774
return python::getattr(target, key);
4875
}
4976

77+
inline object const_objattribute_policies::get(object const& target, object const& key)
78+
{
79+
return python::getattr(target, key);
80+
}
81+
5082
inline object const& attribute_policies::set(
5183
object const& target
5284
, char const* key
@@ -56,13 +88,29 @@ inline object const& attribute_policies::set(
5688
return value;
5789
}
5890

91+
inline object const& objattribute_policies::set(
92+
object const& target
93+
, object const& key
94+
, object const& value)
95+
{
96+
python::setattr(target, key, value);
97+
return value;
98+
}
99+
59100
inline void attribute_policies::del(
60101
object const& target
61102
, char const* key)
62103
{
63104
python::delattr(target, key);
64105
}
65106

107+
inline void objattribute_policies::del(
108+
object const& target
109+
, object const& key)
110+
{
111+
python::delattr(target, key);
112+
}
113+
66114
}}} // namespace boost::python::api
67115

68116
#endif // OBJECT_ATTRIBUTES_DWA2002615_HPP

include/boost/python/object_core.hpp

100755100644
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ namespace api
5959

6060
struct const_attribute_policies;
6161
struct attribute_policies;
62+
struct const_objattribute_policies;
63+
struct objattribute_policies;
6264
struct const_item_policies;
6365
struct item_policies;
6466
struct const_slice_policies;
@@ -67,6 +69,8 @@ namespace api
6769

6870
typedef proxy<const_attribute_policies> const_object_attribute;
6971
typedef proxy<attribute_policies> object_attribute;
72+
typedef proxy<const_objattribute_policies> const_object_objattribute;
73+
typedef proxy<objattribute_policies> object_objattribute;
7074
typedef proxy<const_item_policies> const_object_item;
7175
typedef proxy<item_policies> object_item;
7276
typedef proxy<const_slice_policies> const_object_slice;
@@ -108,6 +112,8 @@ namespace api
108112
//
109113
const_object_attribute attr(char const*) const;
110114
object_attribute attr(char const*);
115+
const_object_objattribute attr(object const&) const;
116+
object_objattribute attr(object const&);
111117

112118
// item access
113119
//

0 commit comments

Comments
 (0)