2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-26 06:42:27 +00:00

Added type checking when converting some Python types from python as return values.

[SVN r14478]
This commit is contained in:
Dave Abrahams
2002-07-16 11:45:10 +00:00
parent fa779034b5
commit 2bfeb20550
10 changed files with 165 additions and 28 deletions

View File

@@ -38,6 +38,11 @@ object apply_object_list(object f, list x)
return f(x);
}
list apply_list_list(object f, list x)
{
return call<list>(f.ptr(), x);
}
void append_object(list& x, object y)
{
x.append(y);
@@ -126,6 +131,7 @@ BOOST_PYTHON_MODULE_INIT(list_ext)
.def("listify", listify)
.def("listify_string", listify_string)
.def("apply_object_list", apply_object_list)
.def("apply_list_list", apply_list_list)
.def("append_object", append_object)
.def("append_list", append_list)

View File

@@ -20,9 +20,17 @@ X(22)
5 is not convertible to a list
>>> try: apply_object_list(identity, 5)
>>> try: result = apply_object_list(identity, 5)
... except TypeError: pass
... else: print 'expected an exception'
... else: print 'expected an exception, got', result, 'instead'
>>> assert apply_list_list(identity, letters) is letters
5 is not convertible to a list as a return value
>>> try: result = apply_list_list(len, letters)
... except TypeError: pass
... else: print 'expected an exception, got', result, 'instead'
>>> append_object(letters, '.')
>>> letters
@@ -38,6 +46,24 @@ X(22)
>>> letters
['h', 'e', 'l', 'l', 'o', '.', [1, 2]]
Check that subclass functions are properly called
>>> class mylist(list):
... def append(self, o):
... list.append(self, o)
... if not hasattr(self, 'nappends'):
... self.nappends = 1
... else:
... self.nappends += 1
...
>>> l2 = mylist()
>>> append_object(l2, 'hello')
>>> append_object(l2, 'world')
>>> l2
['hello', 'world']
>>> l2.nappends
2
>>> def printer(*args):
... for x in args: print x,
... print