MIRA
PythonCheck.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Redistribution and modification of this code is strictly prohibited.
9  *
10  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
11  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
12  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
13  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
20  */
21 
30 #ifndef _MIRA_TOOLBOXES_PYTHON_PYTHONCHECK_H_
31 #define _MIRA_TOOLBOXES_PYTHON_PYTHONCHECK_H_
32 
33 #include <iostream>
34 #include <map>
35 
36 #include <error/Exceptions.h>
37 #include <Python.h>
38 #include <python/PythonException.h>
39 
41 
42 namespace mira { namespace python {
43 
45 
47 {
48 public:
49  PythonError() : lineno(-1), offset(-1) {}
50 
51  PythonError(const boost::python::dict& d)
52  {
53  extract(type, d, "type");
54  extract(description, d, "description");
55  extract(filename, d, "filename");
56  extract(lineno, d, "lineno");
57  extract(offset , d, "offset");
58  extract(badline, d, "badline");
59  extract(offsetstr, d, "offsetstr");
60  extract(msg, d, "errorMsg");
61  }
62 
63 public:
64 
65  bool isError() { return !type.empty(); }
66  operator bool() { return isError(); }
67 
68 protected:
69  template <typename T>
70  void extract(T& var, const boost::python::dict& d, const std::string& key)
71  {
72  if (d.has_key(key))
73  var = boost::python::extract<T>(d[key]);
74  }
75 
76 public:
77  std::string badline;
78  std::string description;
79  std::string filename;
80  int lineno;
81  std::string msg;
82  int offset;
83  std::string offsetstr;
84  std::string type;
85 };
86 
87 std::ostream& operator<<(std::ostream& os, const PythonError& err)
88 {
89  os << "\n";
90  if(!err.type.empty()) os << "Error of type " << err.type;
91  if(!err.filename.empty()) os << " in " << err.filename;
92  if(err.lineno > -1) os << " in line " << err.lineno;
93  if(!err.badline.empty()) os << "\n " << err.badline;
94  if(!err.offsetstr.empty()) os << "\n " << err.offsetstr;
95  if(!err.msg.empty()) os << "\nErrorMsg: " << err.msg;
96  if(!err.description.empty()) os << "\nDescription: " << err.description;
97  os << "\n";
98 
99  return os;
100 }
101 
103 
105 PythonError checkSyntax(const std::string& code);
106 
108 
109 }} // namespace
110 
111 #endif
std::string type
Definition: PythonCheck.h:84
Definition: PythonCheck.h:46
std::string description
Definition: PythonCheck.h:78
std::ostream & operator<<(std::ostream &os, const PythonError &err)
Definition: PythonCheck.h:87
bool isError()
Definition: PythonCheck.h:65
PythonError(const boost::python::dict &d)
Definition: PythonCheck.h:51
int offset
Definition: PythonCheck.h:82
std::string offsetstr
Definition: PythonCheck.h:83
std::string badline
Definition: PythonCheck.h:77
PythonError()
Definition: PythonCheck.h:49
Exception handling for python exceptions.
int lineno
Definition: PythonCheck.h:80
std::string msg
Definition: PythonCheck.h:81
PythonError checkSyntax(const std::string &code)
check syntax of python string
Include this instead of boost/python.hpp to reduce compile time warning spam from Boost internal inco...
void extract(T &var, const boost::python::dict &d, const std::string &key)
Definition: PythonCheck.h:70
std::string filename
Definition: PythonCheck.h:79