Skip to content

Commit 65e74cc

Browse files
author
Ralf W. Grosse-Kunstleve
committed
boost/python/object_core.hpp: new .is_none() member function
[SVN r60625]
1 parent 9398a63 commit 65e74cc

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

doc/v2/object.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ <h4><a name="object-spec-synopsis"></a>Class <code>object</code>
835835
object&amp; operator=(object const&amp;);
836836

837837
PyObject* ptr() const;
838+
839+
bool is_none() const;
838840
};
839841
}}}
840842
</pre>
@@ -895,6 +897,14 @@ <h4><a name="object-spec-observers"></a>Class <code>object</code>
895897
<dt><b>Returns:</b> a pointer to the internally-held Python
896898
object.</dt>
897899
</dl>
900+
901+
<pre>
902+
bool is_none() const;
903+
</pre>
904+
905+
<dl class="function-semantics">
906+
<dt><b>Returns:</b> result of (ptr() == Py_None)</dt>
907+
</dl>
898908
<!-- -->
899909

900910
<h3><a name="proxy-spec"></a>Class template <code>proxy</code></h3>
@@ -1101,7 +1111,7 @@ <h2><a name="examples"></a>Example</h2>
11011111
</pre>
11021112
<p>Revised
11031113
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
1104-
27 May, 2008
1114+
15 March, 2010
11051115
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
11061116
</p>
11071117

include/boost/python/object_core.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef OBJECT_CORE_DWA2002615_HPP
66
# define OBJECT_CORE_DWA2002615_HPP
77

8+
# define BOOST_PYTHON_OBJECT_HAS_IS_NONE // added 2010-03-15 by rwgk
9+
810
# include <boost/python/detail/prefix.hpp>
911

1012
# include <boost/type.hpp>
@@ -239,7 +241,9 @@ namespace api
239241

240242
// Underlying object access -- returns a borrowed reference
241243
inline PyObject* ptr() const;
242-
244+
245+
inline bool is_none() const;
246+
243247
private:
244248
PyObject* m_ptr;
245249
};
@@ -539,6 +543,11 @@ inline PyObject* api::object_base::ptr() const
539543
return m_ptr;
540544
}
541545

546+
inline bool api::object_base::is_none() const
547+
{
548+
return (m_ptr == Py_None);
549+
}
550+
542551
//
543552
// Converter specialization implementations
544553
//

src/exec.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ namespace python
1717
object BOOST_PYTHON_DECL eval(str string, object global, object local)
1818
{
1919
// Set suitable default values for global and local dicts.
20-
object none;
21-
if (global.ptr() == none.ptr())
20+
if (global.is_none())
2221
{
2322
if (PyObject *g = PyEval_GetGlobals())
2423
global = object(detail::borrowed_reference(g));
2524
else
2625
global = dict();
2726
}
28-
if (local.ptr() == none.ptr()) local = global;
27+
if (local.is_none()) local = global;
2928
// should be 'char const *' but older python versions don't use 'const' yet.
3029
char *s = python::extract<char *>(string);
3130
PyObject* result = PyRun_String(s, Py_eval_input, global.ptr(), local.ptr());
@@ -36,15 +35,14 @@ object BOOST_PYTHON_DECL eval(str string, object global, object local)
3635
object BOOST_PYTHON_DECL exec(str string, object global, object local)
3736
{
3837
// Set suitable default values for global and local dicts.
39-
object none;
40-
if (global.ptr() == none.ptr())
38+
if (global.is_none())
4139
{
4240
if (PyObject *g = PyEval_GetGlobals())
4341
global = object(detail::borrowed_reference(g));
4442
else
4543
global = dict();
4644
}
47-
if (local.ptr() == none.ptr()) local = global;
45+
if (local.is_none()) local = global;
4846
// should be 'char const *' but older python versions don't use 'const' yet.
4947
char *s = python::extract<char *>(string);
5048
PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr());
@@ -55,15 +53,14 @@ object BOOST_PYTHON_DECL exec(str string, object global, object local)
5553
object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
5654
{
5755
// Set suitable default values for global and local dicts.
58-
object none;
59-
if (global.ptr() == none.ptr())
56+
if (global.is_none())
6057
{
6158
if (PyObject *g = PyEval_GetGlobals())
6259
global = object(detail::borrowed_reference(g));
6360
else
6461
global = dict();
6562
}
66-
if (local.ptr() == none.ptr()) local = global;
63+
if (local.is_none()) local = global;
6764
// should be 'char const *' but older python versions don't use 'const' yet.
6865
char *s = python::extract<char *>(string);
6966
PyObject* result = PyRun_String(s, Py_single_input, global.ptr(), local.ptr());
@@ -77,15 +74,14 @@ object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
7774
object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
7875
{
7976
// Set suitable default values for global and local dicts.
80-
object none;
81-
if (global.ptr() == none.ptr())
77+
if (global.is_none())
8278
{
8379
if (PyObject *g = PyEval_GetGlobals())
8480
global = object(detail::borrowed_reference(g));
8581
else
8682
global = dict();
8783
}
88-
if (local.ptr() == none.ptr()) local = global;
84+
if (local.is_none()) local = global;
8985
// should be 'char const *' but older python versions don't use 'const' yet.
9086
char *f = python::extract<char *>(filename);
9187
#if PY_VERSION_HEX >= 0x03000000

src/object/function.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ PyObject* function::call(PyObject* args, PyObject* keywords) const
144144
if (n_keyword_actual > 0 // Keyword arguments were supplied
145145
|| n_actual < min_arity) // or default keyword values are needed
146146
{
147-
if (f->m_arg_names.ptr() == Py_None)
147+
if (f->m_arg_names.is_none())
148148
{
149149
// this overload doesn't accept keywords
150150
inner_args = handle<>();
@@ -487,7 +487,7 @@ void function::add_to_namespace(
487487
}
488488

489489
// A function is named the first time it is added to a namespace.
490-
if (new_func->name().ptr() == Py_None)
490+
if (new_func->name().is_none())
491491
new_func->m_name = name;
492492

493493
handle<> name_space_name(
@@ -653,7 +653,7 @@ extern "C"
653653
static PyObject* function_get_name(PyObject* op, void*)
654654
{
655655
function* f = downcast<function>(op);
656-
if (f->name().ptr() == Py_None)
656+
if (f->name().is_none())
657657
#if PY_VERSION_HEX >= 0x03000000
658658
return PyUnicode_InternFromString("<unnamed Boost.Python function>");
659659
#else

src/object/pickle_support.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ namespace {
3838
}
3939
object getinitargs = getattr(instance_obj, "__getinitargs__", none);
4040
tuple initargs;
41-
if (getinitargs.ptr() != none.ptr()) {
41+
if (!getinitargs.is_none()) {
4242
initargs = tuple(getinitargs());
4343
}
4444
result.append(initargs);
4545
object getstate = getattr(instance_obj, "__getstate__", none);
4646
object instance_dict = getattr(instance_obj, "__dict__", none);
4747
long len_instance_dict = 0;
48-
if (instance_dict.ptr() != none.ptr()) {
48+
if (!instance_dict.is_none()) {
4949
len_instance_dict = len(instance_dict);
5050
}
51-
if (getstate.ptr() != none.ptr()) {
51+
if (!getstate.is_none()) {
5252
if (len_instance_dict > 0) {
5353
object getstate_manages_dict = getattr(
5454
instance_obj, "__getstate_manages_dict__", none);
55-
if (getstate_manages_dict.ptr() == none.ptr()) {
55+
if (getstate_manages_dict.is_none()) {
5656
PyErr_SetString(PyExc_RuntimeError,
5757
"Incomplete pickle support"
5858
" (__getstate_manages_dict__ not set)");

0 commit comments

Comments
 (0)