Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2011-02-22 14:32:29 +0300
committerCampbell Barton <ideasman42@gmail.com>2011-02-22 14:32:29 +0300
commitd60a5cfc552e0f5b6e9143a1d85fcc0208021e1a (patch)
treed8be151f4961f7ba65a2da228bed60d07203fe44 /source
parentef6cbc3da0af5a05f59969025d75ae6567c0b299 (diff)
feature back from 2.4x where a python error moves the cursor to the error line, added moving to exact column for syntax errors too.
Diffstat (limited to 'source')
-rw-r--r--source/blender/editors/space_text/text_ops.c15
-rw-r--r--source/blender/python/intern/CMakeLists.txt2
-rw-r--r--source/blender/python/intern/bpy_interface.c17
-rw-r--r--source/blender/python/intern/bpy_traceback.c151
-rw-r--r--source/blender/python/intern/bpy_traceback.h28
5 files changed, 210 insertions, 3 deletions
diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c
index d8cbe93012d..67d931d0932 100644
--- a/source/blender/editors/space_text/text_ops.c
+++ b/source/blender/editors/space_text/text_ops.c
@@ -577,13 +577,22 @@ static int run_script_exec(bContext *C, wmOperator *op)
Text *text= CTX_data_edit_text(C);
SpaceText *st= CTX_wm_space_text(C);
+ /* only for comparison */
+ void *curl_prev= text->curl;
+ int curc_prev= text->curc;
+
if (BPY_text_exec(C, text, op->reports))
return OPERATOR_FINISHED;
-
+
/* Dont report error messages while live editing */
- if(!(st && st->live_edit))
+ if(!(st && st->live_edit)) {
+ if(text->curl != curl_prev || curc_prev != text->curc) {
+ text_update_cursor_moved(C);
+ WM_event_add_notifier(C, NC_TEXT|NA_EDITED, text);
+ }
+
BKE_report(op->reports, RPT_ERROR, "Python script fail, look in the console for now...");
-
+ }
return OPERATOR_CANCELLED;
#endif
}
diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt
index 57c42ef4672..123c8e5b3e0 100644
--- a/source/blender/python/intern/CMakeLists.txt
+++ b/source/blender/python/intern/CMakeLists.txt
@@ -48,6 +48,7 @@ set(SRC
bpy_rna.c
bpy_rna_array.c
bpy_rna_callback.c
+ bpy_traceback.c
bpy_util.c
stubs.c
@@ -59,6 +60,7 @@ set(SRC
bpy_props.h
bpy_rna.h
bpy_rna_callback.h
+ bpy_traceback.h
bpy_util.h
../BPY_extern.h
)
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index b6f0182a267..9ed1865d70d 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -35,6 +35,7 @@
#include "bpy.h"
#include "bpy_rna.h"
#include "bpy_util.h"
+#include "bpy_traceback.h"
#include "DNA_space_types.h"
#include "DNA_text_types.h"
@@ -327,6 +328,18 @@ void BPY_python_end(void)
}
+static void python_script_error_jump_text(struct Text *text)
+{
+ int lineno;
+ int offset;
+ python_script_error_jump(text->id.name+2, &lineno, &offset);
+ if(lineno != -1) {
+ /* select the line with the error */
+ txt_move_to(text, lineno - 1, INT_MAX, FALSE);
+ txt_move_to(text, lineno - 1, offset, TRUE);
+ }
+}
+
/* super annoying, undo _PyModule_Clear(), bug [#23871] */
#define PYMODULE_CLEAR_WORKAROUND
@@ -369,6 +382,7 @@ static int python_script_exec(bContext *C, const char *fn, struct Text *text, st
MEM_freeN( buf );
if(PyErr_Occurred()) {
+ python_script_error_jump_text(text);
BPY_text_free_code(text);
}
}
@@ -414,6 +428,9 @@ static int python_script_exec(bContext *C, const char *fn, struct Text *text, st
}
if (!py_result) {
+ if(text) {
+ python_script_error_jump_text(text);
+ }
BPy_errors_to_report(reports);
} else {
Py_DECREF( py_result );
diff --git a/source/blender/python/intern/bpy_traceback.c b/source/blender/python/intern/bpy_traceback.c
new file mode 100644
index 00000000000..9abf0767b79
--- /dev/null
+++ b/source/blender/python/intern/bpy_traceback.c
@@ -0,0 +1,151 @@
+/**
+ * $Id: bpy_interface.c 35032 2011-02-21 13:13:08Z campbellbarton $
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <Python.h>
+#include <frameobject.h>
+
+#include "bpy_traceback.h"
+
+static const char *traceback_filepath(PyTracebackObject *tb)
+{
+ return _PyUnicode_AsString(tb->tb_frame->f_code->co_filename);
+}
+
+/* copied from pythonrun.c, 3.2.0 */
+static int
+parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
+ int *lineno, int *offset, const char **text)
+{
+ long hold;
+ PyObject *v;
+
+ /* old style errors */
+ if (PyTuple_Check(err))
+ return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
+ lineno, offset, text);
+
+ /* new style errors. `err' is an instance */
+
+ if (! (v = PyObject_GetAttrString(err, "msg")))
+ goto finally;
+ *message = v;
+
+ if (!(v = PyObject_GetAttrString(err, "filename")))
+ goto finally;
+ if (v == Py_None)
+ *filename = NULL;
+ else if (! (*filename = _PyUnicode_AsString(v)))
+ goto finally;
+
+ Py_DECREF(v);
+ if (!(v = PyObject_GetAttrString(err, "lineno")))
+ goto finally;
+ hold = PyLong_AsLong(v);
+ Py_DECREF(v);
+ v = NULL;
+ if (hold < 0 && PyErr_Occurred())
+ goto finally;
+ *lineno = (int)hold;
+
+ if (!(v = PyObject_GetAttrString(err, "offset")))
+ goto finally;
+ if (v == Py_None) {
+ *offset = -1;
+ Py_DECREF(v);
+ v = NULL;
+ } else {
+ hold = PyLong_AsLong(v);
+ Py_DECREF(v);
+ v = NULL;
+ if (hold < 0 && PyErr_Occurred())
+ goto finally;
+ *offset = (int)hold;
+ }
+
+ if (!(v = PyObject_GetAttrString(err, "text")))
+ goto finally;
+ if (v == Py_None)
+ *text = NULL;
+ else if (!PyUnicode_Check(v) ||
+ !(*text = _PyUnicode_AsString(v)))
+ goto finally;
+ Py_DECREF(v);
+ return 1;
+
+finally:
+ Py_XDECREF(v);
+ return 0;
+}
+/* end copied function! */
+
+
+void python_script_error_jump(const char *filepath, int *lineno, int *offset)
+{
+ PyObject *exception, *value;
+ PyTracebackObject *tb;
+
+ *lineno= -1;
+ *offset= 0;
+
+ PyErr_Fetch(&exception, &value, (PyObject **)&tb);
+
+ if(exception && PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
+ /* no traceback available when SyntaxError.
+ * python has no api's to this. reference parse_syntax_error() from pythonrun.c */
+ PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
+ PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */
+
+ if(value) { /* should always be true */
+ PyObject *message;
+ const char *filename, *text;
+
+ if(parse_syntax_error(value, &message, &filename, lineno, offset, &text)) {
+ /* python adds a '/', prefix, so check for both */
+ if( (strcmp(filename, filepath) == 0) ||
+ ((filename[0] == '\\' || filename[0] == '/') && strcmp(filename + 1, filepath) == 0)
+ ) {
+ /* good */
+ }
+ else {
+ *lineno= -1;
+ }
+ }
+ else {
+ *lineno= -1;
+ }
+ }
+
+ /* this avoids an abort in Python 2.3's garbage collecting */
+ }
+ else {
+ PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
+ PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */
+ PyErr_Print();
+
+ for(tb= (PyTracebackObject *)PySys_GetObject("last_traceback"); tb && (PyObject *)tb != Py_None; tb= tb->tb_next) {
+ if(strcmp(traceback_filepath(tb), filepath) != 0) {
+ *lineno= tb->tb_lineno;
+ break;
+ }
+ }
+ }
+}
diff --git a/source/blender/python/intern/bpy_traceback.h b/source/blender/python/intern/bpy_traceback.h
new file mode 100644
index 00000000000..882eab39e23
--- /dev/null
+++ b/source/blender/python/intern/bpy_traceback.h
@@ -0,0 +1,28 @@
+/**
+ * $Id: bpy_interface.c 35032 2011-02-21 13:13:08Z campbellbarton $
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef BPY_TRACEBACK_H
+#define BPY_TRACEBACK_H
+
+void python_script_error_jump(const char *filepath, int *lineno, int *offset);
+
+#endif // BPY_TRACEBACK_H