2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00
Files
python/test/object.cpp
Dave Abrahams a1924a2a72 Merging all trunk changes in Boost.Python to release. The logs for
these changes follow.  r44807 is a merge from another branch; many of
those changes probably don't apply.

  ------------------------------------------------------------------------
  r43423 | danieljames | 2008-02-27 14:22:01 -0500 (Wed, 27 Feb 2008) | 1 line

  Fix incorrect links to copyright of the form 'http:#www.boost.org
  ------------------------------------------------------------------------
  r43546 | rwgk | 2008-03-08 23:30:13 -0500 (Sat, 08 Mar 2008) | 1 line

  map std::invalid_argument -> Python ValueError
  ------------------------------------------------------------------------
  r43799 | grafik | 2008-03-22 18:53:35 -0400 (Sat, 22 Mar 2008) | 1 line

  Remove svn exec property from files that should not be executable to fix permission problems in archives.
  ------------------------------------------------------------------------
  r43845 | dave | 2008-03-24 14:27:22 -0400 (Mon, 24 Mar 2008) | 1 line

  Work around vc9 bugs
  ------------------------------------------------------------------------
  r43901 | bemandawes | 2008-03-27 22:11:13 -0400 (Thu, 27 Mar 2008) | 1 line

  Remove per email from Dave
  ------------------------------------------------------------------------
  r44041 | rwgk | 2008-04-04 14:57:27 -0400 (Fri, 04 Apr 2008) | 1 line

  bogus VC8 warning C4180 disabled
  ------------------------------------------------------------------------
  r44449 | djowel | 2008-04-15 23:07:06 -0400 (Tue, 15 Apr 2008) | 1 line

  Andreas patch
  ------------------------------------------------------------------------
  r44450 | djowel | 2008-04-15 23:07:11 -0400 (Tue, 15 Apr 2008) | 1 line

  Andreas indexing suite patch
  ------------------------------------------------------------------------
  r44807 | danieljames | 2008-04-27 03:39:49 -0400 (Sun, 27 Apr 2008) | 78 lines

  Merge in documentation fixes.  Apart from the change to optional's documenation
  Jamfile, which I included by mistake.

  Fixes #1659, #1661, #1684, #1685, 1687, #1690, #1801

  I wrote about this at:

  http://lists.boost.org/Archives/boost/2008/04/136405.php

  Merged revisions 44585-44806 via svnmerge from 
  https://svn.boost.org/svn/boost/branches/doc

  ........
    r44585 | danieljames | 2008-04-19 16:25:27 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix broken link to vacpp in bjam docs. Refs #1512
  ........
    r44586 | danieljames | 2008-04-19 16:27:36 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix broken link to bcpp in bjam docs. Refs #1513
  ........
    r44587 | danieljames | 2008-04-19 16:33:58 +0100 (Sat, 19 Apr 2008) | 2 lines

    DateTime documentation - Fix a link to the serialization library. Refs #1659
  ........
    r44588 | danieljames | 2008-04-19 16:35:36 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix some links in interprocess & intrusive. Refs #1661
  ........
    r44589 | danieljames | 2008-04-19 16:37:39 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix some links in the python docs. Refs #1684.
  ........
    r44590 | danieljames | 2008-04-19 16:38:29 +0100 (Sat, 19 Apr 2008) | 2 lines

    Work around a quickbook bug which is affecting the python docs. Refs #1684.
  ........
    r44591 | danieljames | 2008-04-19 16:39:34 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix a broken link in the numeric conversion docs. Refs #1685
  ........
    r44592 | danieljames | 2008-04-19 16:40:45 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix some links in the optional docs. Refs #1687
  ........
    r44593 | danieljames | 2008-04-19 16:42:09 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix link to the hash documentation from bimap. Refs #1690
  ........
    r44599 | danieljames | 2008-04-19 18:07:33 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix a typo in the format library. Refs #1801
  ........
    r44600 | danieljames | 2008-04-19 19:20:59 +0100 (Sat, 19 Apr 2008) | 1 line

    Initialise svnmerge.
  ........
    r44641 | danieljames | 2008-04-20 18:59:47 +0100 (Sun, 20 Apr 2008) | 2 lines

    Fix the lincense url in shared container iterator documentation.
  ........
    r44642 | danieljames | 2008-04-20 19:00:00 +0100 (Sun, 20 Apr 2008) | 2 lines

    Fix image link in the mpi documentation.
  ........
    r44643 | danieljames | 2008-04-20 19:00:11 +0100 (Sun, 20 Apr 2008) | 2 lines

    Fix a typo in the spirit docs.
  ........
    r44644 | danieljames | 2008-04-20 19:00:23 +0100 (Sun, 20 Apr 2008) | 2 lines

    Escape the slash so that quickbook doesn't think it the start of an italic section, and mess up the link. Refs #1844
  ........
    r44647 | danieljames | 2008-04-20 19:39:47 +0100 (Sun, 20 Apr 2008) | 2 lines

    Fix another typo in spirit docs.
  ........

  ------------------------------------------------------------------------
  r45283 | danieljames | 2008-05-11 09:49:20 -0400 (Sun, 11 May 2008) | 1 line

  Quote href values - our tools don't support unquoted values.
  ------------------------------------------------------------------------
  r45359 | rwgk | 2008-05-14 15:38:08 -0400 (Wed, 14 May 2008) | 1 line

  braces added to resolve g++ 4.3.0 warning
  ------------------------------------------------------------------------
  r45918 | rwgk | 2008-05-29 15:48:55 -0400 (Thu, 29 May 2008) | 66 lines

  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.

  ------------------------------------------------------------------------
  r45920 | rwgk | 2008-05-29 16:04:19 -0400 (Thu, 29 May 2008) | 1 line

  Projects using Boost.Python: PolyBoRi (text provided by Michael Brickenstein)
  ------------------------------------------------------------------------
  r46115 | schoepflin | 2008-06-04 11:11:05 -0400 (Wed, 04 Jun 2008) | 1 line

  Added missing ostream header file.
  ------------------------------------------------------------------------
  r46445 | dgregor | 2008-06-17 09:54:46 -0400 (Tue, 17 Jun 2008) | 1 line

  Add mime-type and eol-style properties as needed
  ------------------------------------------------------------------------
  r46721 | dave | 2008-06-26 12:41:34 -0400 (Thu, 26 Jun 2008) | 2 lines

  Compatibility with Apache STDCXX library.  Don't assume eh.h comes along with the other headers automatically.

  ------------------------------------------------------------------------
  r46808 | bemandawes | 2008-06-28 09:45:21 -0400 (Sat, 28 Jun 2008) | 1 line

  With his kind permission, change Jaakko "J<LATIN SMALL LETTER A WITH DIARESIS>rvi" to "Jarvi"
  ------------------------------------------------------------------------
  r46817 | djowel | 2008-06-28 14:24:17 -0400 (Sat, 28 Jun 2008) | 1 line

  added note on removing targets
  ------------------------------------------------------------------------
  r46844 | dave | 2008-06-29 08:26:17 -0400 (Sun, 29 Jun 2008) | 2 lines

  Make valid HTML

  ------------------------------------------------------------------------




[SVN r47459]
2008-07-15 21:05:24 +00:00

387 lines
8.7 KiB
C++

// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/object.hpp>
#include <boost/python/class.hpp>
using namespace boost::python;
class NotCopyable
{
} not_copyable;
object ref_to_noncopyable()
{
return object(boost::ref(not_copyable));
}
object call_object_3(object f)
{
return f(3);
}
object message()
{
return object("hello, world!");
}
object number()
{
return object(42);
}
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];
}
object obj_getitem3(object x)
{
return x[3];
}
object obj_const_getitem(object const& x, object key)
{
return x[key];
}
void obj_setitem(object x, object key, object value)
{
x[key] = value;
}
void obj_setitem42(object x, object key)
{
x[key] = 42;
}
void obj_moveitem(object& x, object src, object dst)
{
x[dst] = x[src];
}
void obj_moveitem2(object const& x_src, object k_src, object& x_dst, object k_dst)
{
x_dst[k_dst] = x_src[k_src];
}
bool test(object y)
{
return y;
}
bool test_not(object y)
{
return !y;
}
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];
}
bool test_not_item(object y, object key)
{
return !y[key];
}
bool check_string_slice()
{
object s("hello, world");
if (s.slice(_,-3) != "hello, wo")
return false;
if (s.slice(-3,_) != "rld")
return false;
if (s.slice(_,_) != s)
return false;
if (", " != s.slice(5,7))
return false;
return s.slice(2,-1).slice(1,-1) == "lo, wor";
}
bool check_binary_operators()
{
int y;
object x(3);
#define TEST_BINARY(op) \
for (y = 1; y < 6; ++y) \
{ \
if ((x op y) != (3 op y)) \
return false; \
} \
for (y = 1; y < 6; ++y) \
{ \
if ((y op x) != (y op 3)) \
return false; \
} \
for (y = 1; y < 6; ++y) \
{ \
object oy(y); \
if ((oy op x) != (oy op 3)) \
return false; \
}
TEST_BINARY(>)
TEST_BINARY(>=)
TEST_BINARY(<)
TEST_BINARY(<=)
TEST_BINARY(==)
TEST_BINARY(!=)
TEST_BINARY(+)
TEST_BINARY(-)
TEST_BINARY(*)
TEST_BINARY(/)
TEST_BINARY(%)
TEST_BINARY(<<)
TEST_BINARY(>>)
TEST_BINARY(&)
TEST_BINARY(^)
TEST_BINARY(|)
return true;
}
bool check_inplace(object l, object o)
{
int y;
#define TEST_INPLACE(op) \
for (y = 1; y < 6; ++y) \
{ \
object x(666); \
x op##= y; \
if (x != (666 op y)) \
return false; \
} \
for (y = 1; y < 6; ++y) \
{ \
object x(666); \
x op##= object(y); \
if (!(x == (666 op y))) \
return false; \
}
TEST_INPLACE(+)
TEST_INPLACE(-)
TEST_INPLACE(*)
TEST_INPLACE(/)
TEST_INPLACE(%)
TEST_INPLACE(<<)
TEST_INPLACE(>>)
TEST_INPLACE(&)
TEST_INPLACE(^)
TEST_INPLACE(|)
l += l;
for (y = 0; y < 6; ++y)
{
if (l[y] != y % 3)
return false;
}
#define TEST_ITEM_INPLACE(index, op, n, r1, r2) \
l[index] op##= n; \
if (l[index] != r1) \
return false; \
l[index] op##= object(n); \
if (!(l[index] == r2)) \
return false;
TEST_ITEM_INPLACE(0,+,7,7,14)
TEST_ITEM_INPLACE(1,-,2,-1,-3)
TEST_ITEM_INPLACE(2,*,3,6,18)
TEST_ITEM_INPLACE(2,/,2,9,4)
TEST_ITEM_INPLACE(0,%,4,2,2)
l[0] += 1;
TEST_ITEM_INPLACE(0,<<,2,12,48)
TEST_ITEM_INPLACE(0,>>,1,24,12)
l[4] = 15;
TEST_ITEM_INPLACE(4,&,(16+4+1),5,5)
TEST_ITEM_INPLACE(0,^,1,13,12)
TEST_ITEM_INPLACE(0,|,1,13,13)
o.attr("x0") = 0;
o.attr("x1") = 1;
o.attr("x2") = 2;
o.attr("x3") = 0;
o.attr("x4") = 1;
#define TEST_ATTR_INPLACE(index, op, n, r1, r2) \
o.attr("x" #index) op##= n; \
if (o.attr("x" #index) != r1) \
return false; \
o.attr("x" #index) op##= object(n); \
if (o.attr("x" #index) != r2) \
return false;
TEST_ATTR_INPLACE(0,+,7,7,14)
TEST_ATTR_INPLACE(1,-,2,-1,-3)
TEST_ATTR_INPLACE(2,*,3,6,18)
TEST_ATTR_INPLACE(2,/,2,9,4)
TEST_ATTR_INPLACE(0,%,4,2,2)
o.attr("x0") += 1;
TEST_ATTR_INPLACE(0,<<,2,12,48)
TEST_ATTR_INPLACE(0,>>,1,24,12)
o.attr("x4") = 15;
TEST_ATTR_INPLACE(4,&,(16+4+1),5,5)
TEST_ATTR_INPLACE(0,^,1,13,12)
TEST_ATTR_INPLACE(0,|,1,13,13)
if (l[0] != o.attr("x0"))
return false;
if (l[1] != o.attr("x1"))
return false;
if (l[2] != o.attr("x2"))
return false;
if (l[3] != o.attr("x3"))
return false;
if (l[4] != o.attr("x4"))
return false;
// set item 5 to be a list, by calling l.__class__
l[5] = l.attr("__class__")();
// append an element
l[5].attr("append")(2);
// Check its value
if (l[5][0] != 2)
return false;
return true;
}
BOOST_PYTHON_MODULE(object_ext)
{
class_<NotCopyable, boost::noncopyable>("NotCopyable", no_init);
def("ref_to_noncopyable", ref_to_noncopyable);
def("call_object_3", call_object_3);
def("message", message);
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);
def("obj_const_getitem", obj_const_getitem);
def("obj_setitem", obj_setitem);
def("obj_setitem42", obj_setitem42);
def("obj_moveitem", obj_moveitem);
def("obj_moveitem2", obj_moveitem2);
def("test", test);
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);
def("check_binary_operators", check_binary_operators);
def("check_inplace", check_inplace);
def("check_string_slice", check_string_slice);
;
}
#include "module_tail.cpp"