From 65e74ccf1e6cede1fba9c6adfd8c8ac16dbf852b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 15 Mar 2010 22:00:30 +0000 Subject: [PATCH] boost/python/object_core.hpp: new .is_none() member function [SVN r60625] --- doc/v2/object.html | 12 +++++++++++- include/boost/python/object_core.hpp | 11 ++++++++++- src/exec.cpp | 20 ++++++++------------ src/object/function.cpp | 6 +++--- src/object/pickle_support.cpp | 8 ++++---- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/doc/v2/object.html b/doc/v2/object.html index 6fa48fa5..8df8ef5f 100644 --- a/doc/v2/object.html +++ b/doc/v2/object.html @@ -835,6 +835,8 @@ namespace boost { namespace python { namespace api object& operator=(object const&); PyObject* ptr() const; + + bool is_none() const; }; }}} @@ -895,6 +897,14 @@ PyObject* ptr() const;
Returns: a pointer to the internally-held Python object.
+ +
+bool is_none() const;
+
+ +
+
Returns: result of (ptr() == Py_None)
+

Class template proxy

@@ -1101,7 +1111,7 @@ object sum_items(object seq)

Revised - 27 May, 2008 + 15 March, 2010

diff --git a/include/boost/python/object_core.hpp b/include/boost/python/object_core.hpp index 5857422f..4d81e2ea 100644 --- a/include/boost/python/object_core.hpp +++ b/include/boost/python/object_core.hpp @@ -5,6 +5,8 @@ #ifndef OBJECT_CORE_DWA2002615_HPP # define OBJECT_CORE_DWA2002615_HPP +# define BOOST_PYTHON_OBJECT_HAS_IS_NONE // added 2010-03-15 by rwgk + # include # include @@ -239,7 +241,9 @@ namespace api // Underlying object access -- returns a borrowed reference inline PyObject* ptr() const; - + + inline bool is_none() const; + private: PyObject* m_ptr; }; @@ -539,6 +543,11 @@ inline PyObject* api::object_base::ptr() const return m_ptr; } +inline bool api::object_base::is_none() const +{ + return (m_ptr == Py_None); +} + // // Converter specialization implementations // diff --git a/src/exec.cpp b/src/exec.cpp index e772dcd7..9fe1b23b 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -17,15 +17,14 @@ namespace python object BOOST_PYTHON_DECL eval(str string, object global, object local) { // Set suitable default values for global and local dicts. - object none; - if (global.ptr() == none.ptr()) + if (global.is_none()) { if (PyObject *g = PyEval_GetGlobals()) global = object(detail::borrowed_reference(g)); else global = dict(); } - if (local.ptr() == none.ptr()) local = global; + if (local.is_none()) local = global; // should be 'char const *' but older python versions don't use 'const' yet. char *s = python::extract(string); 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) object BOOST_PYTHON_DECL exec(str string, object global, object local) { // Set suitable default values for global and local dicts. - object none; - if (global.ptr() == none.ptr()) + if (global.is_none()) { if (PyObject *g = PyEval_GetGlobals()) global = object(detail::borrowed_reference(g)); else global = dict(); } - if (local.ptr() == none.ptr()) local = global; + if (local.is_none()) local = global; // should be 'char const *' but older python versions don't use 'const' yet. char *s = python::extract(string); 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) object BOOST_PYTHON_DECL exec_statement(str string, object global, object local) { // Set suitable default values for global and local dicts. - object none; - if (global.ptr() == none.ptr()) + if (global.is_none()) { if (PyObject *g = PyEval_GetGlobals()) global = object(detail::borrowed_reference(g)); else global = dict(); } - if (local.ptr() == none.ptr()) local = global; + if (local.is_none()) local = global; // should be 'char const *' but older python versions don't use 'const' yet. char *s = python::extract(string); 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) object BOOST_PYTHON_DECL exec_file(str filename, object global, object local) { // Set suitable default values for global and local dicts. - object none; - if (global.ptr() == none.ptr()) + if (global.is_none()) { if (PyObject *g = PyEval_GetGlobals()) global = object(detail::borrowed_reference(g)); else global = dict(); } - if (local.ptr() == none.ptr()) local = global; + if (local.is_none()) local = global; // should be 'char const *' but older python versions don't use 'const' yet. char *f = python::extract(filename); #if PY_VERSION_HEX >= 0x03000000 diff --git a/src/object/function.cpp b/src/object/function.cpp index 0054966e..98c7c966 100644 --- a/src/object/function.cpp +++ b/src/object/function.cpp @@ -144,7 +144,7 @@ PyObject* function::call(PyObject* args, PyObject* keywords) const if (n_keyword_actual > 0 // Keyword arguments were supplied || n_actual < min_arity) // or default keyword values are needed { - if (f->m_arg_names.ptr() == Py_None) + if (f->m_arg_names.is_none()) { // this overload doesn't accept keywords inner_args = handle<>(); @@ -487,7 +487,7 @@ void function::add_to_namespace( } // A function is named the first time it is added to a namespace. - if (new_func->name().ptr() == Py_None) + if (new_func->name().is_none()) new_func->m_name = name; handle<> name_space_name( @@ -653,7 +653,7 @@ extern "C" static PyObject* function_get_name(PyObject* op, void*) { function* f = downcast(op); - if (f->name().ptr() == Py_None) + if (f->name().is_none()) #if PY_VERSION_HEX >= 0x03000000 return PyUnicode_InternFromString(""); #else diff --git a/src/object/pickle_support.cpp b/src/object/pickle_support.cpp index f70eb17f..428c07b6 100644 --- a/src/object/pickle_support.cpp +++ b/src/object/pickle_support.cpp @@ -38,21 +38,21 @@ namespace { } object getinitargs = getattr(instance_obj, "__getinitargs__", none); tuple initargs; - if (getinitargs.ptr() != none.ptr()) { + if (!getinitargs.is_none()) { initargs = tuple(getinitargs()); } result.append(initargs); object getstate = getattr(instance_obj, "__getstate__", none); object instance_dict = getattr(instance_obj, "__dict__", none); long len_instance_dict = 0; - if (instance_dict.ptr() != none.ptr()) { + if (!instance_dict.is_none()) { len_instance_dict = len(instance_dict); } - if (getstate.ptr() != none.ptr()) { + if (!getstate.is_none()) { if (len_instance_dict > 0) { object getstate_manages_dict = getattr( instance_obj, "__getstate_manages_dict__", none); - if (getstate_manages_dict.ptr() == none.ptr()) { + if (getstate_manages_dict.is_none()) { PyErr_SetString(PyExc_RuntimeError, "Incomplete pickle support" " (__getstate_manages_dict__ not set)");