@@ -149,7 +149,7 @@ static PyMethodDef OutputRedirector_methods[] = {
149
149
150
150
PyObject *PythonContext::main_dict = NULL ;
151
151
152
- void FetchException (QString &typeStr, QString &valueStr, QList<QString> &frames)
152
+ void FetchException (QString &typeStr, QString &valueStr, int &finalLine, QList<QString> &frames)
153
153
{
154
154
PyObject *exObj = NULL , *valueObj = NULL , *tracebackObj = NULL ;
155
155
@@ -184,14 +184,21 @@ void FetchException(QString &typeStr, QString &valueStr, QList<QString> &frames)
184
184
PyObject *args = Py_BuildValue (" (N)" , tracebackObj);
185
185
PyObject *formattedTB = PyObject_CallObject (func, args);
186
186
187
+ PyTracebackObject *tb = (PyTracebackObject *)tracebackObj;
188
+
189
+ while (tb->tb_next )
190
+ tb = tb->tb_next ;
191
+
192
+ finalLine = tb->tb_lineno ;
193
+
187
194
if (formattedTB)
188
195
{
189
196
Py_ssize_t size = PyList_Size (formattedTB);
190
197
for (Py_ssize_t i = 0 ; i < size; i++)
191
198
{
192
199
PyObject *el = PyList_GetItem (formattedTB, i);
193
200
194
- frames << ToQStr (el);
201
+ frames << ToQStr (el). trimmed () ;
195
202
}
196
203
197
204
Py_DecRef (formattedTB);
@@ -473,7 +480,8 @@ void PythonContext::executeString(const QString &filename, const QString &source
473
480
{
474
481
emit exception (
475
482
lit (" SystemError" ),
476
- tr (" Python integration failed to initialise, see diagnostic log for more information." ), {});
483
+ tr (" Python integration failed to initialise, see diagnostic log for more information." ), -1 ,
484
+ {});
477
485
return ;
478
486
}
479
487
@@ -519,18 +527,19 @@ void PythonContext::executeString(const QString &filename, const QString &source
519
527
520
528
QString typeStr;
521
529
QString valueStr;
530
+ int finalLine = -1 ;
522
531
QList<QString> frames;
523
532
bool caughtException = (ret == NULL );
524
533
525
534
if (caughtException)
526
- FetchException (typeStr, valueStr, frames);
535
+ FetchException (typeStr, valueStr, finalLine, frames);
527
536
528
537
Py_XDECREF (ret);
529
538
530
539
PyGILState_Release (gil);
531
540
532
541
if (caughtException)
533
- emit exception (typeStr, valueStr, frames);
542
+ emit exception (typeStr, valueStr, finalLine, frames);
534
543
}
535
544
536
545
void PythonContext::executeString (const QString &source, bool interactive)
@@ -544,7 +553,8 @@ void PythonContext::executeFile(const QString &filename)
544
553
545
554
if (!f.exists ())
546
555
{
547
- emit exception (lit (" FileNotFoundError" ), tr (" No such file or directory: %1" ).arg (filename), {});
556
+ emit exception (lit (" FileNotFoundError" ), tr (" No such file or directory: %1" ).arg (filename), -1 ,
557
+ {});
548
558
return ;
549
559
}
550
560
@@ -556,7 +566,7 @@ void PythonContext::executeFile(const QString &filename)
556
566
}
557
567
else
558
568
{
559
- emit exception (lit (" IOError" ), QFormatStr (" %1: %2" ).arg (f.errorString ()).arg (filename), {});
569
+ emit exception (lit (" IOError" ), QFormatStr (" %1: %2" ).arg (f.errorString ()).arg (filename), - 1 , {});
560
570
}
561
571
}
562
572
@@ -566,7 +576,8 @@ void PythonContext::setGlobal(const char *varName, const char *typeName, void *o
566
576
{
567
577
emit exception (
568
578
lit (" SystemError" ),
569
- tr (" Python integration failed to initialise, see diagnostic log for more information." ), {});
579
+ tr (" Python integration failed to initialise, see diagnostic log for more information." ), -1 ,
580
+ {});
570
581
return ;
571
582
}
572
583
@@ -587,7 +598,7 @@ void PythonContext::setGlobal(const char *varName, const char *typeName, void *o
587
598
emit exception (lit (" RuntimeError" ), tr (" Failed to set variable '%1' of type '%2'" )
588
599
.arg (QString::fromUtf8 (varName))
589
600
.arg (QString::fromUtf8 (typeName)),
590
- {});
601
+ - 1 , {});
591
602
return ;
592
603
}
593
604
@@ -694,7 +705,8 @@ void PythonContext::setPyGlobal(const char *varName, PyObject *obj)
694
705
{
695
706
emit exception (
696
707
lit (" SystemError" ),
697
- tr (" Python integration failed to initialise, see diagnostic log for more information." ), {});
708
+ tr (" Python integration failed to initialise, see diagnostic log for more information." ), -1 ,
709
+ {});
698
710
return ;
699
711
}
700
712
@@ -711,7 +723,7 @@ void PythonContext::setPyGlobal(const char *varName, PyObject *obj)
711
723
return ;
712
724
713
725
emit exception (lit (" RuntimeError" ),
714
- tr (" Failed to set variable '%1'" ).arg (QString::fromUtf8 (varName)), {});
726
+ tr (" Failed to set variable '%1'" ).arg (QString::fromUtf8 (varName)), - 1 , {});
715
727
}
716
728
717
729
void PythonContext::outstream_del (PyObject *self)
@@ -821,13 +833,14 @@ extern "C" void HandleException(PyObject *global_handle)
821
833
{
822
834
QString typeStr;
823
835
QString valueStr;
836
+ int finalLine = -1 ;
824
837
QList<QString> frames;
825
838
826
- FetchException (typeStr, valueStr, frames);
839
+ FetchException (typeStr, valueStr, finalLine, frames);
827
840
828
841
OutputRedirector *redirector = (OutputRedirector *)global_handle;
829
- if (redirector->context )
830
- emit redirector->context ->exception (typeStr, valueStr, frames);
842
+ if (redirector && redirector ->context )
843
+ emit redirector->context ->exception (typeStr, valueStr, finalLine, frames);
831
844
}
832
845
833
846
extern " C" bool IsThreadBlocking (PyObject *global_handle)
0 commit comments