Pythonscript show console on error
one of the features of the console I really like is the ability to click on a link
and the source file opens with the cursor at the position of the possible error.
Without changing any code from my most recent (above), if I create a single-line Pythonscript called t.py and put in it:
z=zzzzz, then I run it with the new exception handler in place, I get this in the console:
(Single-level) Traceback: File "C:\Utilities\npp.bin\plugins\Config\PythonScript\scripts\t.py", line 1, in <module> z=zzzzz <type 'exceptions.NameError'>: name 'zzzzz' is not defined >>>
where the filename in double-quotes is a clickable link which takes me to the file and line number indicated. Thus, while you have a great point, @Claudia-Frank , I really don’t have to do my own formatting like you describe to make it happen. :)
but only as long as the exception happens within the main script.
If it happens in an imported module or script gets executed via another script,
than it won’t work because of the restriction of your levels
but the info is still kept in the value parameter.
I know how to get upvotes – ask people to upvote if tired of this thread! :)
Yea, okay, so I was trying to limit the amount of lines in the traceback, because usually it isn’t all that helpful. However, given what you said, I went in search of different scenarios, and I rapidly found 3, which all produce very different tracebacks, from which a pattern did not emerge on how to reduce the amount of levels. The 3 scenarios I found were as follows:
z=zzzz m = re.search('(', 'abc') x=3
Comment out any two of these script lines at a time, run it and observe a full traceback and note how different they are (if you were to consider coding something to only let certain traceback levels be shown – pretty complicated).
So I’ve decided the best thing is to let all of the levels be shown. My latest (and hopefully last) version of this little custom exception hook, hopefully of value to some that DON’T decide to upvote :-) is:
import sys, traceback def custom_exception_handler_func(exctype, value, trace_back): notepad.runPluginCommand('Python Script', 'Show Console') # can't/don't use console.show() in PS 220.127.116.11 sys.stderr.write('Traceback:' + '\n') for tb in traceback.format_tb(trace_back): sys.stderr.write(tb) sys.stderr.write(str(exctype) + ': ' + str(value) + '\n') sys.excepthook = custom_exception_handler_func
Claudia Frank last edited by Claudia Frank
Hi Alan, I totally forgot about this one and I have found another way to do it.
A more python script plugin way.
There is a class ConsoleError defined in machine startup.py which we can enhance.
class ConsoleError: def __init__(self): global console self._console = console; def write(self, text): notepad.runPluginCommand('Python Script', 'Show Console') self._console.writeError(text); sys.stderr = ConsoleError()
and in addition, it we want to get every print statement behave the same,
we can introduce a similar class for stdout
class ConsoleStdOut: def __init__(self): global console self._console = console; def write(self, text): notepad.runPluginCommand('Python Script', 'Show Console') self._console.write(text); sys.stdout = ConsoleStdOut()
I tried this out and now my print statements in scripts will show the PS console if it is not currently showing. This is helpful. Thank you.
However, I noticed that, regardless of whether or not the PS console is showing when the print statement is encountered, after the script finishes the PS console has the input focus. This situation is OK if an exception in the script occurs, but for simple print statements I’d rather keep the focus where I’m editing.
Perhaps to solve this one would have to use a variation of the “grandpa” technique shown here, but changing it to simply put input focus back on the current editor tab? Sadly, maybe I see the solution but do not have the power to implement it. :-)
We should stress grandpa too much :-D
after the write call.
Of course I wanted to say We should not stress grandpa too much
Hello Claudia. I have implemented the functionality to show Pythonscript console upon every stdout write per your new information. For a long time now I have had the show-PS-console-upon-exception thing in place (per earlier in this thread) with no apparent issues, but it seems that with the addition of stdout that I tend to get N++ hangs often but not everytime that a “print” would happen. I know this is vague, but I wanted to report it. Note that I have made no recent plugin changes, which is often to blame when this kind of thing happens.
ok, I’m using it now for approximately 2 month but didn’t notice it but my laptop is slow anyway ;-)
I will try to run some timed test scripts to see if I can see the issue as well and of course,
if it makes trouble we should avoid using it.
Just to clarify the “hang” is a really hard hang where I have to kill N++ and restart it, not just a long delay…