~ubuntu-branches/ubuntu/utopic/mysql-workbench/utopic

« back to all changes in this revision

Viewing changes to library/base/python_utils.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2014-05-31 12:03:58 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20140531120358-cjik5ofkmj0fxsn8
Tags: 6.1.6+dfsg-1
* New upstream release [May 2014].
* Dropped "prtcl.patch".
* "debian/clean": better clean-up.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; version 2 of the
 
7
 * License.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
#if defined(_WIN32)
 
21
# include <Python/Python.h>
 
22
#else
 
23
# include <Python.h>
 
24
#endif
 
25
 
 
26
#include <frameobject.h>
 
27
#include "base/python_utils.h"
 
28
#include "base/string_utilities.h"
 
29
 
 
30
 
 
31
 
 
32
std::string format_python_traceback(PyObject *tb)
 
33
{
 
34
  PyTracebackObject *trace = (PyTracebackObject*)tb;
 
35
  std::string stack;
 
36
 
 
37
  stack = "Traceback:\n";
 
38
  while (trace && trace->tb_frame)
 
39
  {
 
40
    PyFrameObject *frame = (PyFrameObject*)trace->tb_frame;
 
41
    stack += base::strfmt("  File \"%s\", line %i, in %s\n",
 
42
                          PyString_AsString(frame->f_code->co_filename),
 
43
                          trace->tb_lineno,
 
44
                          PyString_AsString(frame->f_code->co_name));
 
45
    PyObject *code = PyErr_ProgramText(PyString_AsString(frame->f_code->co_filename), trace->tb_lineno);
 
46
    if (code)
 
47
    {
 
48
      stack += base::strfmt("    %s", PyString_AsString(code));
 
49
      Py_DECREF(code);
 
50
    }
 
51
    trace = trace->tb_next;
 
52
  }
 
53
  return stack;
 
54
}
 
55
 
 
56
 
 
57
std::string base::format_python_exception(std::string &summary)
 
58
{
 
59
  std::string reason, stack;
 
60
  PyObject *exc, *val, *tb;
 
61
 
 
62
  PyErr_Fetch(&exc, &val, &tb);
 
63
  PyErr_NormalizeException(&exc, &val, &tb);
 
64
 
 
65
  if (val)
 
66
  {
 
67
    PyObject *tmp = PyObject_Str(val);
 
68
    if (tmp)
 
69
    {
 
70
      reason = PyString_AsString(tmp);
 
71
      Py_DECREF(tmp);
 
72
    }
 
73
  }
 
74
 
 
75
  if (tb)
 
76
    stack = format_python_traceback(tb);
 
77
  else
 
78
    stack = "No stack information. ";
 
79
 
 
80
  PyErr_Restore(exc, val, tb);
 
81
 
 
82
  summary = reason;
 
83
 
 
84
  return stack + reason+"\n";
 
85
}