Skip to content

Commit 7171a34

Browse files
committed
Daniel Holth's PyBool mods (dholth-at-fastmail.fm)
[SVN r24980]
1 parent f1260e6 commit 7171a34

File tree

8 files changed

+34
-17
lines changed

8 files changed

+34
-17
lines changed

doc/news.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ <h2 align="center">News/Change Log</h2>
2929
<hr>
3030

3131
<dl class="page-index">
32+
<dt>8 Sept 2004</dt>
33+
34+
<dd>
35+
<ul>
36+
Support for Python's Bool type, thanks to <a
37+
mailto="dholth-at-fastmail.fm">Daniel Holth</a>.
38+
</ul>
39+
</dd>
40+
3241
<dt>11 Sept 2003</dt>
3342

3443
<dd>

include/boost/python/converter/builtin_converters.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ namespace detail
9191
: ::PyInt_FromLong(x))
9292

9393
// Bool is not signed.
94+
#if PY_VERSION_HEX >= 0x02030000
95+
BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyBool_FromLong(x))
96+
#else
9497
BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyInt_FromLong(x))
95-
98+
#endif
99+
96100
// note: handles signed char and unsigned char, but not char (see below)
97101
BOOST_PYTHON_TO_INT(char)
98102

test/defaults.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ struct X {
9191
{}
9292

9393
X(std::string s, bool b)
94-
: state("Got exactly two arguments from constructor: string(%s); bool(%s); " % make_tuple(s, b))
94+
: state("Got exactly two arguments from constructor: string(%s); bool(%s); " % make_tuple(s, b*1))
9595
{}
9696

9797
object
@@ -114,19 +114,19 @@ struct X {
114114
object
115115
foo(int a, bool b=false) const
116116
{
117-
return "int(%s); bool(%s); " % make_tuple(a, b);
117+
return "int(%s); bool(%s); " % make_tuple(a, b*1);
118118
}
119119

120120
object
121121
foo(std::string a, bool b=false) const
122122
{
123-
return "string(%s); bool(%s); " % make_tuple(a, b);
123+
return "string(%s); bool(%s); " % make_tuple(a, b*1);
124124
}
125125

126126
object
127127
foo(list a, list b, bool c=false) const
128128
{
129-
return "list(%s); list(%s); bool(%s); " % make_tuple(a, b, c);
129+
return "list(%s); list(%s); bool(%s); " % make_tuple(a, b, c*1);
130130
}
131131

132132
object

test/defaults.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
# Software License, Version 1.0. (See accompanying
33
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
44
"""
5-
>>> False = 0 # Python 2.2 needs these
6-
>>> True = 1
5+
# Use builtin True/False when available:
6+
>>> try:
7+
... assert(True == 1)
8+
... except:
9+
... True = 1
10+
... False = 0
711
812
>>> from defaults_ext import *
913
>>> bar(1)

test/dict.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ void test_templates(object print)
6767
print(tmp.get(44));
6868
print(tmp);
6969
print(tmp.get(2,"default"));
70-
print(tmp.has_key(key));
7170
print(tmp.setdefault(3,"default"));
71+
72+
assert(!tmp.has_key(key));
7273
//print(tmp[3]);
7374
}
7475

test/dict.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
None
2727
{1.5: 13, 1: 'a test string'}
2828
default
29-
0
3029
default
3130
"""
3231

test/str.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ void work_with_string(object print)
2424
print(data.count("t"));
2525
print(data.encode("utf-8"));
2626
print(data.decode("utf-8"));
27-
print(data.endswith("xx"));
28-
print(data.startswith("test"));
27+
28+
assert(!data.endswith("xx"));
29+
assert(!data.startswith("test"));
30+
2931
print(data.splitlines());
3032
print(data.strip());
3133
print(data.swapcase());
@@ -51,8 +53,10 @@ void work_with_string(object print)
5153
print(data.replace("demo",std::string("blabla")));
5254
print(data.rfind("i",5));
5355
print(data.rindex("i",5));
54-
print(data.startswith("asdf"));
55-
print(data.endswith("asdf"));
56+
57+
assert(!data.startswith("asdf"));
58+
assert(!data.endswith("asdf"));
59+
5660
print(data.translate(str('a')*256));
5761

5862

test/str.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
2
1717
this is a demo string
1818
this is a demo string
19-
0
20-
0
2119
['this is a demo string']
2220
this is a demo string
2321
THIS IS A DEMO STRING
@@ -36,8 +34,6 @@
3634
this is a blabla string
3735
18
3836
18
39-
0
40-
0
4137
aaaaaaaaaaaaaaaaaaaaa
4238
"""
4339

0 commit comments

Comments
 (0)