Skip to content

Commit b2f53e1

Browse files
jzwinckstefanseefeld
authored andcommitted
exec/eval(): add overloads for char const*
Many times the caller may have a string created in C++, so there is no need to wrap it in a Python object when the only thing done with the object is extract<char*>.
1 parent 3844c4f commit b2f53e1

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

doc/tutorial.qbk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,8 @@ eval evaluates the given expression and returns the resulting value.
14171417
exec executes the given code (typically a set of statements) returning the result,
14181418
and exec_file executes the code contained in the given file.
14191419

1420+
There are also overloads taking `char const*` instead of str as the first argument.
1421+
14201422
The [^globals] and [^locals] parameters are Python dictionaries
14211423
containing the globals and locals of the context in which to run the code.
14221424
For most intents and purposes you can use the namespace dictionary of the

include/boost/python/exec.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,43 @@ object
2020
BOOST_PYTHON_DECL
2121
eval(str string, object global = object(), object local = object());
2222

23+
object
24+
BOOST_PYTHON_DECL
25+
eval(char const *string, object global = object(), object local = object());
26+
2327
// Execute an individual python statement from str.
2428
// global and local are the global and local scopes respectively,
2529
// used during execution.
2630
object
2731
BOOST_PYTHON_DECL
2832
exec_statement(str string, object global = object(), object local = object());
2933

34+
object
35+
BOOST_PYTHON_DECL
36+
exec_statement(char const *string, object global = object(), object local = object());
37+
3038
// Execute python source code from str.
3139
// global and local are the global and local scopes respectively,
3240
// used during execution.
3341
object
3442
BOOST_PYTHON_DECL
3543
exec(str string, object global = object(), object local = object());
3644

45+
object
46+
BOOST_PYTHON_DECL
47+
exec(char const *string, object global = object(), object local = object());
48+
3749
// Execute python source code from file filename.
3850
// global and local are the global and local scopes respectively,
3951
// used during execution.
4052
object
4153
BOOST_PYTHON_DECL
4254
exec_file(str filename, object global = object(), object local = object());
4355

56+
object
57+
BOOST_PYTHON_DECL
58+
exec_file(char const *filename, object global = object(), object local = object());
59+
4460
}
4561
}
4662

src/exec.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ namespace python
1515
{
1616

1717
object BOOST_PYTHON_DECL eval(str string, object global, object local)
18+
{
19+
return eval(python::extract<char const *>(string));
20+
}
21+
22+
object BOOST_PYTHON_DECL eval(char const *string, object global, object local)
1823
{
1924
// Set suitable default values for global and local dicts.
2025
if (global.is_none())
@@ -26,13 +31,18 @@ object BOOST_PYTHON_DECL eval(str string, object global, object local)
2631
}
2732
if (local.is_none()) local = global;
2833
// should be 'char const *' but older python versions don't use 'const' yet.
29-
char *s = python::extract<char *>(string);
34+
char *s = const_cast<char *>(string);
3035
PyObject* result = PyRun_String(s, Py_eval_input, global.ptr(), local.ptr());
3136
if (!result) throw_error_already_set();
3237
return object(detail::new_reference(result));
3338
}
3439

3540
object BOOST_PYTHON_DECL exec(str string, object global, object local)
41+
{
42+
return exec(python::extract<char const *>(string));
43+
}
44+
45+
object BOOST_PYTHON_DECL exec(char const *string, object global, object local)
3646
{
3747
// Set suitable default values for global and local dicts.
3848
if (global.is_none())
@@ -44,13 +54,18 @@ object BOOST_PYTHON_DECL exec(str string, object global, object local)
4454
}
4555
if (local.is_none()) local = global;
4656
// should be 'char const *' but older python versions don't use 'const' yet.
47-
char *s = python::extract<char *>(string);
57+
char *s = const_cast<char *>(string);
4858
PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr());
4959
if (!result) throw_error_already_set();
5060
return object(detail::new_reference(result));
5161
}
5262

5363
object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
64+
{
65+
return exec_statement(python::extract<char const *>(string), global, local);
66+
}
67+
68+
object BOOST_PYTHON_DECL exec_statement(char const *string, object global, object local)
5469
{
5570
// Set suitable default values for global and local dicts.
5671
if (global.is_none())
@@ -62,7 +77,7 @@ object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
6277
}
6378
if (local.is_none()) local = global;
6479
// should be 'char const *' but older python versions don't use 'const' yet.
65-
char *s = python::extract<char *>(string);
80+
char *s = const_cast<char *>(string);
6681
PyObject* result = PyRun_String(s, Py_single_input, global.ptr(), local.ptr());
6782
if (!result) throw_error_already_set();
6883
return object(detail::new_reference(result));
@@ -72,6 +87,11 @@ object BOOST_PYTHON_DECL exec_statement(str string, object global, object local)
7287
// global and local are the global and local scopes respectively,
7388
// used during execution.
7489
object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
90+
{
91+
return exec_file(python::extract<char const *>(filename), global, local);
92+
}
93+
94+
object BOOST_PYTHON_DECL exec_file(char const *filename, object global, object local)
7595
{
7696
// Set suitable default values for global and local dicts.
7797
if (global.is_none())
@@ -83,7 +103,7 @@ object BOOST_PYTHON_DECL exec_file(str filename, object global, object local)
83103
}
84104
if (local.is_none()) local = global;
85105
// should be 'char const *' but older python versions don't use 'const' yet.
86-
char *f = python::extract<char *>(filename);
106+
char *f = const_cast<char *>(filename);
87107
// Let python open the file to avoid potential binary incompatibilities.
88108
#if PY_VERSION_HEX >= 0x03040000
89109
FILE *fs = _Py_fopen(f, "r");

0 commit comments

Comments
 (0)