[C++-sig] object.attr(object& attrname) proposa

hohe2 hohe hohehohe2 at gmail.com
Tue May 27 07:12:59 CEST 2008


Hi Ralf,

> When you post your patches, I'll try them out locally in our
> multi-platform build; if/once all is fine I'll check them into
> the boost trunk.

I think I have done them. There are five files I've modified:

boost/python/object_attributes.hpp
boost/python/object_core.hpp
    same as before.

libs/python/doc/v2/object.html
    Documentation. It may not be a natural English since I'm not a native.

libs/python/test/object.cpp
libs/python/test/object.py
    Basically I made new tests for the .attr(object&) method that is
    equivalent to the existing tests for attr(char const*) but I added
    a test to see if it surely fails when an object is not a string. I
    also added del() tests.

If something is wrong or not natural, please feel free to fix it.

-Koichi


------------------------------------------------------------------
diff -urN boost_1_35_0/boost/python/object_attributes.hpp
boost_1_35_0_p/boost/python/object_attributes.hpp
--- boost_1_35_0/boost/python/object_attributes.hpp    2008-03-23
06:45:55.000000000 +0900
+++ boost_1_35_0_p/boost/python/object_attributes.hpp    2008-05-27
13:44:00.000000000 +0900
@@ -17,6 +17,7 @@
 {
     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 @@
     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 @@
     return const_object_attribute(x, name);
 }

+template <class U>
+inline object_objattribute object_operators<U>::attr(object const& name)
+{
+    object_cref2 x = *static_cast<U*>(this);
+    return object_objattribute(x, name);
+}
+
+template <class U>
+inline const_object_objattribute object_operators<U>::attr(object const&
name) const
+{
+    object_cref2 x = *static_cast<U const*>(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 @@
     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 @@
     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 -urN boost_1_35_0/boost/python/object_core.hpp
boost_1_35_0_p/boost/python/object_core.hpp
--- boost_1_35_0/boost/python/object_core.hpp    2008-03-23
06:45:55.000000000 +0900
+++ boost_1_35_0_p/boost/python/object_core.hpp    2008-05-27
13:44:00.000000000 +0900
@@ -59,6 +59,8 @@

   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 @@

   typedef proxy<const_attribute_policies> const_object_attribute;
   typedef proxy<attribute_policies> object_attribute;
+  typedef proxy<const_objattribute_policies> const_object_objattribute;
+  typedef proxy<objattribute_policies> object_objattribute;
   typedef proxy<const_item_policies> const_object_item;
   typedef proxy<item_policies> object_item;
   typedef proxy<const_slice_policies> const_object_slice;
@@ -108,6 +112,8 @@
       //
       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 -urN boost_1_35_0/libs/python/doc/v2/object.html
boost_1_35_0_p/libs/python/doc/v2/object.html
--- boost_1_35_0/libs/python/doc/v2/object.html    2008-02-11
01:39:38.000000000 +0900
+++ boost_1_35_0_p/libs/python/doc/v2/object.html    2008-05-27
13:47:23.000000000 +0900
@@ -74,6 +74,32 @@
             </dl>
           </dd>

+          <dt><a href="#const_objattribute_policies-spec">Class
+          <code>const_objattribute_policies</code></a></dt>
+
+          <dd>
+            <dl class="page-index">
+              <dt><a
href="#const_objattribute_policies-spec-synopsis">Class
+              <code>const_objattribute_policies</code> synopsis</a></dt>
+
+              <dt><a href="#const_objattribute_policies-spec-statics">Class
+              <code>const_objattribute_policies</code> static
functions</a></dt>
+            </dl>
+          </dd>
+
+          <dt><a href="#objattribute_policies-spec">Class
+          <code>objattribute_policies</code></a></dt>
+
+          <dd>
+            <dl class="page-index">
+              <dt><a href="#objattribute_policies-spec-synopsis">Class
+              <code>objattribute_policies</code> synopsis</a></dt>
+
+              <dt><a href="#objattribute_policies-spec-statics">Class
+              <code>objattribute_policies</code> static functions</a></dt>
+            </dl>
+          </dd>
+
           <dt><a href="#const_item_policies-spec">Class
           <code>const_item_policies</code></a></dt>

@@ -328,6 +354,102 @@
     <!-- end -->
     <!-- begin -->

+    <h3><a name="const_objattribute_policies-spec"></a>Class
+    <code>const_objattribute_policies</code></h3>
+
+    <p>The policies which are used for proxies representing an attribute
+    access to a <code>const&nbsp;object</code> when the attribute name is
+    given as a <code>const&nbsp;object</code>.</p>
+
+    <h4><a name="const_objattribute_policies-spec-synopsis"></a>Class
+    <code>const_objattribute_policies</code> synopsis</h4>
+<pre>
+namespace boost { namespace python { namespace api
+{
+  struct const_objattribute_policies
+  {
+      typedef object const& key_type;
+      static object get(object const&amp; target, object const& key);
+  };
+}}}
+</pre>
+
+    <h4><a name="const_objattribute_policies-spec-statics"></a>Class
+    <code>const_objattribute_policies</code> static functions</h4>
+<pre>
+static object get(object const&amp; target, object const& key);
+</pre>
+
+    <dl class="function-semantics">
+      <dt><b>Requires:</b> <code>key</code> is an <code>object</code>
+      holding a string.</dt>
+
+      <dt><b>Effects:</b> accesses the attribute of <code>target</code>
named
+      by <code>key</code>.</dt>
+
+      <dt><b>Returns:</b> An <code>object</code> managing the result of the
+      attribute access.</dt>
+
+      <dt><b>Throws:</b> <code><a href=
+      "errors.html#error_already_set-spec">error_already_set</a></code> if
a
+      Python exception is raised.</dt>
+    </dl>
+
+    <h3><a name="objattribute_policies-spec"></a>Class
+    <code>objattribute_policies</code></h3>
+
+    <p>The policies which are used for proxies representing an attribute
+    access to a mutable <code>object</code> when the attribute name is
+    given as a <code>const&nbsp;object</code>.</p>
+
+    <h4><a name="objattribute_policies-spec-synopsis"></a>Class
+    <code>objattribute_policies</code> synopsis</h4>
+<pre>
+namespace boost { namespace python { namespace api
+{
+  struct objattribute_policies : const_objattribute_policies
+  {
+      static object const&amp; set(object const&amp; target, object const&
key, object const&amp; value);
+      static void del(object const&amp;target, object const& key);
+  };
+}}}
+</pre>
+
+    <h4><a name="objattribute_policies-spec-statics"></a>Class
+    <code>objattribute_policies</code> static functions</h4>
+<pre>
+static object const&amp; set(object const&amp; target, object const& key,
object const&amp; value);
+</pre>
+
+    <dl class="function-semantics">
+      <dt><b>Requires:</b> <code>key</code> is an <code>object</code>
+      holding a string.</dt>
+
+      <dt><b>Effects:</b> sets the attribute of <code>target</code> named
by
+      <code>key</code> to <code>value</code>.</dt>
+
+      <dt><b>Throws:</b> <code><a href=
+      "errors.html#error_already_set-spec">error_already_set</a></code> if
a
+      Python exception is raised.</dt>
+    </dl>
+<pre>
+static void del(object const&amp;target, object const& key);
+</pre>
+
+    <dl class="function-semantics">
+      <dt><b>Requires:</b> <code>key</code> is an <code>object</code>
+      holding a string.</dt>
+
+      <dt><b>Effects:</b> deletes the attribute of <code>target</code>
named
+      by <code>key</code>.</dt>
+
+      <dt><b>Throws:</b> <code><a href=
+      "errors.html#error_already_set-spec">error_already_set</a></code> if
a
+      Python exception is raised.</dt>
+    </dl>
+    <!-- end -->
+    <!-- begin -->
+
     <h3><a name="const_item_policies-spec"></a>Class
     <code>const_item_policies</code></h3>

@@ -542,6 +664,8 @@
       //
       proxy&lt;const_object_attribute&gt; attr(char const*) const;
       proxy&lt;object_attribute&gt; attr(char const*);
+      proxy&lt;const_object_objattribute&gt; attr(object const&) const;
+      proxy&lt;object_objattribute&gt; attr(object const&);

       // item access
       //
@@ -608,6 +732,21 @@
       <code>name</code> as its key.</dt>
     </dl>
 <pre>
+proxy&lt;const_object_objattribute&gt; attr(const object& name) const;
+proxy&lt;object_objattribute&gt; attr(const object& name);
+</pre>
+
+    <dl class="function-semantics">
+      <dt><b>Requires:</b> name is a <code>object</code> holding a
string.</dt>
+
+      <dt><b>Effects:</b> accesses the named attribute of
+      <code>*this</code>.</dt>
+
+      <dt><b>Returns:</b> a proxy object which binds
+      <code>object(*static_cast&lt;U*&gt;(this))</code> as its target, and
+      <code>name</code> as its key.</dt>
+    </dl>
+<pre>
 template &lt;class T&gt;
 proxy&lt;const_object_item&gt; operator[](T const&amp; key) const;
 template &lt;class T&gt;
diff -urN boost_1_35_0/libs/python/test/object.cpp
boost_1_35_0_p/libs/python/test/object.cpp
--- boost_1_35_0/libs/python/test/object.cpp    2008-03-23
06:45:55.000000000 +0900
+++ boost_1_35_0_p/libs/python/test/object.cpp    2008-05-27
13:46:35.000000000 +0900
@@ -38,26 +38,61 @@
     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 @@
     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 @@
     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 @@
     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 -urN boost_1_35_0/libs/python/test/object.py
boost_1_35_0_p/libs/python/test/object.py
--- boost_1_35_0/libs/python/test/object.py    2004-08-11 01:14:51.000000000
+0900
+++ boost_1_35_0_p/libs/python/test/object.py    2008-05-27
13:46:40.000000000 +0900
@@ -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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080527/4e3b847e/attachment.htm>


More information about the Cplusplus-sig mailing list