FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/src/exec.cpp at develop · boostorg/python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
boostorg
/
python
Public
Notifications
You must be signed in to change notification settings
Fork
223
Star
537
Code
Issues
173
Pull requests
34
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
src
/
exec.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
138 lines (127 loc) · 4.54 KB
Breadcrumbs
python
/
src
/
exec.cpp
Copy path
File metadata and controls
138 lines (127 loc) · 4.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
Copyright Stefan Seefeld 2005.
//
Distributed under the Boost Software License, Version 1.0. (See
//
accompanying file LICENSE_1_0.txt or copy at
//
http://www.boost.org/LICENSE_1_0.txt)
#
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
>
namespace
boost
{
namespace
python
{
object
BOOST_PYTHON_DECL
eval
(str string, object global, object local)
{
return
eval
(python::extract<
char
const
*>(string), global, local);
}
object
BOOST_PYTHON_DECL
eval
(
char
const
*string, object global, object local)
{
//
Set suitable default values for global and local dicts.
if
(global.
is_none
())
{
if
(PyObject *g =
PyEval_GetGlobals
())
global =
object
(
detail::borrowed_reference
(g));
else
global =
dict
();
}
if
(local.
is_none
()) local = global;
//
should be 'char const *' but older python versions don't use 'const' yet.
char
*s =
const_cast
<
char
*>(string);
PyObject* result =
PyRun_String
(s, Py_eval_input, global.
ptr
(), local.
ptr
());
if
(!result)
throw_error_already_set
();
return
object
(
detail::new_reference
(result));
}
object
BOOST_PYTHON_DECL
exec
(str string, object global, object local)
{
return
exec
(python::extract<
char
const
*>(string), global, local);
}
object
BOOST_PYTHON_DECL
exec
(
char
const
*string, object global, object local)
{
//
Set suitable default values for global and local dicts.
if
(global.
is_none
())
{
if
(PyObject *g =
PyEval_GetGlobals
())
global =
object
(
detail::borrowed_reference
(g));
else
global =
dict
();
}
if
(local.
is_none
()) local = global;
//
should be 'char const *' but older python versions don't use 'const' yet.
char
*s =
const_cast
<
char
*>(string);
PyObject* result =
PyRun_String
(s, Py_file_input, global.
ptr
(), local.
ptr
());
if
(!result)
throw_error_already_set
();
return
object
(
detail::new_reference
(result));
}
object
BOOST_PYTHON_DECL
exec_statement
(str string, object global, object local)
{
return
exec_statement
(python::extract<
char
const
*>(string), global, local);
}
object
BOOST_PYTHON_DECL
exec_statement
(
char
const
*string, object global, object local)
{
//
Set suitable default values for global and local dicts.
if
(global.
is_none
())
{
if
(PyObject *g =
PyEval_GetGlobals
())
global =
object
(
detail::borrowed_reference
(g));
else
global =
dict
();
}
if
(local.
is_none
()) local = global;
//
should be 'char const *' but older python versions don't use 'const' yet.
char
*s =
const_cast
<
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)
{
return
exec_file
(python::extract<
char
const
*>(filename), global, local);
}
object
BOOST_PYTHON_DECL
exec_file
(
char
const
*filename, object global, object local)
{
//
Set suitable default values for global and local dicts.
if
(global.
is_none
())
{
if
(PyObject *g =
PyEval_GetGlobals
())
global =
object
(
detail::borrowed_reference
(g));
else
global =
dict
();
}
if
(local.
is_none
()) local = global;
//
should be 'char const *' but older python versions don't use 'const' yet.
char
*f =
const_cast
<
char
*>(filename);
#
if
PY_VERSION_HEX >= 0x03010000
//
Let python manage any UTF bits to avoid potential incompatibilities.
PyObject *fo =
Py_BuildValue
(
"
s
"
, f);
PyObject *fb = Py_None;
PyUnicode_FSConverter
(fo, &fb);
char
*f_as_uft =
PyBytes_AsString
(fb);
FILE
*fs =
fopen
(f_as_uft,
"
r
"
);
Py_DECREF
(fo);
Py_DECREF
(fb);
#
elif
PY_VERSION_HEX >= 0x03000000
//
Let python open the file to avoid potential binary incompatibilities.
PyObject *fo =
Py_BuildValue
(
"
s
"
, f);
FILE
*fs =
fopen
(fo,
"
r
"
);
Py_DECREF
(fo);
#
else
//
Let python open the file to avoid potential binary incompatibilities.
PyObject *pyfile =
PyFile_FromString
(f,
const_cast
<
char
*>(
"
r
"
));
if
(!pyfile)
throw
std::invalid_argument
(
std::string
(f) +
"
: no such file
"
);
python::handle<>
file
(pyfile);
FILE
*fs =
PyFile_AsFile
(file.
get
());
#
endif
PyObject* result =
PyRun_File
(fs,
f,
Py_file_input,
global.
ptr
(), local.
ptr
());
fclose
(fs);
if
(!result)
throw_error_already_set
();
return
object
(
detail::new_reference
(result));
}
}
//
namespace boost::python
}
//
namespace boost
Back
|
FazBrowse Home
|
New Git URL