interpreter.hh 2.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// -*- mode: c++ -*-
// Copyright 2011, Florent Lamiraux, CNRS.
//
// This file is part of dynamic-graph-python.
// dynamic-graph is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// dynamic-graph is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Lesser Public License for more details.  You should have
// received a copy of the GNU Lesser General Public License along with
// dynamic-graph. If not, see <http://www.gnu.org/licenses/>.

#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#include <Python.h>
#include <string>
#include "dynamic-graph/python/api.hh"
22
#include "dynamic-graph/python/deprecated.hh"
23
24

#ifndef DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
25
#define DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
26

27
28
#include "dynamic-graph/python/api.hh"

29
namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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);
44

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
45
46
47
  /// \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);
48

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
49
50
51
52
53
  /// \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);
54

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
55
56
57
  /// \brief Process input stream to send relevant blocks to python
  /// \param stream input stream
  std::string processStream(std::istream& stream, std::ostream& os);
58

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

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
62
63
64
65
66
67
68
69
70
71
72
73
74
 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