interpreter.hh 1.91 KB
Newer Older
1
2
3
4
5
6
7
8
// -*- mode: c++ -*-
// Copyright 2011, Florent Lamiraux, CNRS.

#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#include <Python.h>
#include <string>
#include "dynamic-graph/python/api.hh"
9
#include "dynamic-graph/python/deprecated.hh"
10
11

#ifndef DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
12
#define DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
13

14
15
#include "dynamic-graph/python/api.hh"

16
namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace python {
///
/// This class implements a basis python interpreter.
///
/// String sent to method python are interpreted by an onboard python
/// interpreter.
class DYNAMIC_GRAPH_PYTHON_DLLAPI Interpreter {
 public:
  Interpreter();
  ~Interpreter();
  /// \brief Method to start python interperter.
  /// \param command string to execute
  /// Method deprecated, you *SHOULD* handle error messages.
  DYNAMIC_GRAPH_PYTHON_DEPRECATED std::string python(const std::string& command);
31

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
32
33
34
  /// \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);
35

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
36
37
38
39
40
  /// \brief Method to exectue a python script.
  /// \param filename the filename
  void runPythonFile(std::string filename);
  void runPythonFile(std::string filename, std::string& err);
  void runMain(void);
41

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
42
43
44
  /// \brief Process input stream to send relevant blocks to python
  /// \param stream input stream
  std::string processStream(std::istream& stream, std::ostream& os);
45

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
46
47
  /// \brief Return a pointer to the dictionary of global variables
  PyObject* globals();
48

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
49
50
51
52
53
54
55
56
57
58
59
60
61
 private:
  /// The Pythone thread state
  PyThreadState* _pyState;
  /// Pointer to the dictionary of global variables
  PyObject* globals_;
  /// Pointer to the dictionary of local variables
  PyObject* locals_;
  PyObject* mainmod_;
  PyObject* traceback_format_exception_;
};
}  // namespace python
}  // namespace dynamicgraph
#endif  // DYNAMIC_GRAPH_PYTHON_INTERPRETER_H