Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
dynamic-graph-python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stack Of Tasks
dynamic-graph-python
Commits
77fafcbc
Commit
77fafcbc
authored
14 years ago
by
Duong Dang
Browse files
Options
Downloads
Patches
Plain Diff
Add method intepreter::python with stdout and stderr returned along with result.
parent
9e48c902
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/dynamic-graph/python/interpreter.hh
+5
-0
5 additions, 0 deletions
include/dynamic-graph/python/interpreter.hh
src/dg-python.cc
+10
-2
10 additions, 2 deletions
src/dg-python.cc
src/interpreter.cc
+66
-5
66 additions, 5 deletions
src/interpreter.cc
with
81 additions
and
7 deletions
include/dynamic-graph/python/interpreter.hh
+
5
−
0
View file @
77fafcbc
...
...
@@ -41,6 +41,11 @@ namespace dynamicgraph {
/// \param command string to execute
std
::
string
python
(
const
std
::
string
&
command
);
/// \brief Method to start python interperter.
/// \param command string to execute, result, stdout, stderr strings
void
python
(
const
std
::
string
&
command
,
std
::
string
&
result
,
std
::
string
&
out
,
std
::
string
&
err
);
/// \brief Method to exectue a python script.
/// \param filename the filename
void
runPythonFile
(
std
::
string
filename
);
...
...
This diff is collapsed.
Click to expand it.
src/dg-python.cc
+
10
−
2
View file @
77fafcbc
...
...
@@ -71,7 +71,7 @@ int main (int argc, char** argv)
po
::
positional_options_description
p
;
p
.
add
(
"input"
,
-
1
);
po
::
variables_map
vm
;
try
...
...
@@ -121,7 +121,15 @@ int main (int argc, char** argv)
while
(
1
)
{
command
=
interpreter
.
processStream
(
std
::
cin
,
std
::
cout
);
if
(
command
!=
"
\n
"
)
{
std
::
string
result
=
interpreter
.
python
(
command
);
std
::
string
result
,
out
,
err
;
interpreter
.
python
(
command
,
result
,
out
,
err
);
if
(
out
!=
""
)
{
std
::
cout
<<
out
<<
std
::
endl
;
}
if
(
err
!=
""
)
{
std
::
cout
<<
err
<<
std
::
endl
;
}
if
(
result
!=
"None"
)
{
std
::
cout
<<
result
<<
std
::
endl
;
}
...
...
This diff is collapsed.
Click to expand it.
src/interpreter.cc
+
66
−
5
View file @
77fafcbc
...
...
@@ -23,7 +23,19 @@ namespace dynamicgraph {
namespace
python
{
static
const
std
::
string
pythonPrefix
[
5
]
=
{
"import traceback
\n
"
,
"def display(s): return str(s) if not s is None else None"
"def display(s): return str(s) if not s is None else None"
,
"class StdoutCatcher:
\n
"
" def __init__(self):
\n
"
" self.data = ''
\n
"
" def write(self, stuff):
\n
"
" self.data = self.data + stuff
\n
"
" def fetch(self):
\n
"
" s = self.data[:]
\n
"
" self.data = ''
\n
"
" return s
\n
"
"stdout_catcher = StdoutCatcher()
\n
"
"import sys
\n
"
"sys.stdout = stdout_catcher"
};
}
}
...
...
@@ -64,18 +76,18 @@ Interpreter::~Interpreter()
std
::
string
Interpreter
::
python
(
const
std
::
string
&
command
)
{
PyObject
*
result
=
PyRun_String
(
command
.
c_str
(),
Py_eval_input
,
globals_
,
globals_
);
globals_
);
if
(
!
result
)
{
PyErr_Clear
();
result
=
PyRun_String
(
command
.
c_str
(),
Py_single_input
,
globals_
,
globals_
);
globals_
);
}
if
(
result
==
NULL
)
{
if
(
PyErr_Occurred
())
{
PyObject
*
ptype
,
*
pvalue
,
*
ptraceback
;
PyErr_Fetch
(
&
ptype
,
&
pvalue
,
&
ptraceback
);
if
(
ptraceback
==
NULL
)
{
ptraceback
=
Py_None
;
ptraceback
=
Py_None
;
}
PyObject
*
args
=
PyTuple_New
(
3
);
PyTuple_SET_ITEM
(
args
,
0
,
ptype
);
...
...
@@ -86,7 +98,7 @@ std::string Interpreter::python( const std::string& command )
unsigned
int
size
=
PyList_GET_SIZE
(
result
);
std
::
string
stringRes
;
for
(
unsigned
int
i
=
0
;
i
<
size
;
i
++
)
{
stringRes
+=
std
::
string
(
PyString_AsString
(
PyList_GET_ITEM
(
result
,
i
)));
stringRes
+=
std
::
string
(
PyString_AsString
(
PyList_GET_ITEM
(
result
,
i
)));
}
result
=
PyString_FromString
(
stringRes
.
c_str
());
PyErr_Clear
();
...
...
@@ -100,6 +112,55 @@ std::string Interpreter::python( const std::string& command )
return
value
;
}
void
Interpreter
::
python
(
const
std
::
string
&
command
,
std
::
string
&
res
,
std
::
string
&
out
,
std
::
string
&
err
)
{
res
=
""
;
out
=
""
;
err
=
""
;
PyObject
*
result
=
PyRun_String
(
command
.
c_str
(),
Py_eval_input
,
globals_
,
globals_
);
if
(
!
result
)
{
PyErr_Clear
();
result
=
PyRun_String
(
command
.
c_str
(),
Py_single_input
,
globals_
,
globals_
);
}
if
(
result
==
NULL
)
{
if
(
PyErr_Occurred
())
{
PyObject
*
ptype
,
*
pvalue
,
*
ptraceback
,
*
pyerr
;
PyErr_Fetch
(
&
ptype
,
&
pvalue
,
&
ptraceback
);
if
(
ptraceback
==
NULL
)
{
ptraceback
=
Py_None
;
}
PyObject
*
args
=
PyTuple_New
(
3
);
PyTuple_SET_ITEM
(
args
,
0
,
ptype
);
PyTuple_SET_ITEM
(
args
,
1
,
pvalue
);
PyTuple_SET_ITEM
(
args
,
2
,
ptraceback
);
pyerr
=
PyObject_CallObject
(
traceback_format_exception_
,
args
);
assert
(
PyList_Check
(
pyerr
));
unsigned
int
size
=
PyList_GET_SIZE
(
pyerr
);
std
::
string
stringRes
;
for
(
unsigned
int
i
=
0
;
i
<
size
;
i
++
)
{
stringRes
+=
std
::
string
(
PyString_AsString
(
PyList_GET_ITEM
(
pyerr
,
i
)));
}
pyerr
=
PyString_FromString
(
stringRes
.
c_str
());
err
=
PyString_AsString
(
pyerr
);
PyErr_Clear
();
}
else
{
std
::
cout
<<
"Result is NULL but no error occurred."
<<
std
::
endl
;
}
}
PyObject
*
stdout_obj
=
PyRun_String
(
"stdout_catcher.fetch()"
,
Py_eval_input
,
globals_
,
globals_
);
out
=
PyString_AsString
(
stdout_obj
);
std
::
cout
<<
out
;
result
=
PyObject_Repr
(
result
);
res
=
PyString_AsString
(
result
);
return
;
}
PyObject
*
Interpreter
::
globals
()
{
return
globals_
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment