Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53936 - trunk/libs/python/src
From: seefeld_at_[hidden]
Date: 2009-06-15 10:53:48


Author: stefan
Date: 2009-06-15 10:53:48 EDT (Mon, 15 Jun 2009)
New Revision: 53936
URL: http://svn.boost.org/trac/boost/changeset/53936

Log:
Use appropriate default values for global and local dicts.
Text files modified:
   trunk/libs/python/src/exec.cpp | 46 ++++++++++++++++++++++++++++++++++++++++
   1 files changed, 46 insertions(+), 0 deletions(-)

Modified: trunk/libs/python/src/exec.cpp
==============================================================================
--- trunk/libs/python/src/exec.cpp (original)
+++ trunk/libs/python/src/exec.cpp 2009-06-15 10:53:48 EDT (Mon, 15 Jun 2009)
@@ -5,6 +5,7 @@
 
 #include <boost/python/exec.hpp>
 #include <boost/python/borrowed.hpp>
+#include <boost/python/dict.hpp>
 #include <boost/python/extract.hpp>
 #include <boost/python/handle.hpp>
 
@@ -15,6 +16,15 @@
 
 object BOOST_PYTHON_DECL eval(str string, object global, object local)
 {
+ // Set suitable default values for global and local dicts.
+ if (!global)
+ {
+ if (PyObject *g = PyEval_GetGlobals())
+ global = object(detail::borrowed_reference(g));
+ else
+ global = dict();
+ }
+ if (!local) local = global;
   // should be 'char const *' but older python versions don't use 'const' yet.
   char *s = python::extract<char *>(string);
   PyObject* result = PyRun_String(s, Py_eval_input, global.ptr(), local.ptr());
@@ -24,6 +34,15 @@
 
 object BOOST_PYTHON_DECL exec(str string, object global, object local)
 {
+ // Set suitable default values for global and local dicts.
+ if (!global)
+ {
+ if (PyObject *g = PyEval_GetGlobals())
+ global = object(detail::borrowed_reference(g));
+ else
+ global = dict();
+ }
+ if (!local) local = global;
   // should be 'char const *' but older python versions don't use 'const' yet.
   char *s = python::extract<char *>(string);
   PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr());
@@ -31,11 +50,38 @@
   return object(detail::new_reference(result));
 }
 
+object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
+{
+ // Set suitable default values for global and local dicts.
+ if (!global)
+ {
+ if (PyObject *g = PyEval_GetGlobals())
+ global = object(detail::borrowed_reference(g));
+ else
+ global = dict();
+ }
+ if (!local) local = global;
+ // should be 'char const *' but older python versions don't use 'const' yet.
+ char *s = python::extract<char *>(string);
+ PyObject* result = PyRun_String(s, Py_single_input, global.ptr(), local.ptr());
+ if (!result) throw_error_already_set();
+ return object(detail::new_reference(result));
+}
+
 // Execute python source code from file filename.
 // global and local are the global and local scopes respectively,
 // used during execution.
 object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
 {
+ // Set suitable default values for global and local dicts.
+ if (!global)
+ {
+ if (PyObject *g = PyEval_GetGlobals())
+ global = object(detail::borrowed_reference(g));
+ else
+ global = dict();
+ }
+ if (!local) local = global;
   // should be 'char const *' but older python versions don't use 'const' yet.
   char *f = python::extract<char *>(filename);
   // Let python open the file to avoid potential binary incompatibilities.


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk