• Login
Community
  • Login

Pythonscript show console on error

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
38 Posts 3 Posters 27.5k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A
    Alan Kilborn @Claudia Frank
    last edited by Feb 12, 2017, 12:17 AM

    @Claudia-Frank

    Hello Claudia and thank you for your reply. I think using a main function with a try block could work, although it may get tiresome to always use it, plus I would forget to do it, but it is a good idea.

    I was looking for a suggestion along the lines of your exception hook one. I remember researching this a while ago, and I could get it working under standard Python, but not as part of Pythonscript. I could register the hook but it wouldn’t get called. I could not figure out why. Unfortunately, as it was some time ago, I can’t remember any more details, if there were any to recall.

    I would like to revisit it if anyone has any suggestions of a working example under Pythonscript…

    C 3 Replies Last reply Feb 12, 2017, 12:31 AM Reply Quote 0
    • C
      Claudia Frank @Alan Kilborn
      last edited by Feb 12, 2017, 12:31 AM

      @Alan-Kilborn

      to be honest, I didn’t try it with python script yet.
      Just thought it works. I should have known better ;-)
      So, let’s start diggin’ - will follow up.

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 1
      • C
        Claudia Frank @Alan Kilborn
        last edited by Feb 12, 2017, 12:45 AM

        @Alan-Kilborn

        ok - python script is using code.py to execute the scripts.
        …\notepad++\plugins\PythonScript\lib\code.py

        sys.excepthook can be overwritten to catch uncaught exceptions
        unfortunately code.py does the following

            try:
                exec code in self.locals
            except SystemExit:
                raise
            except:
                self.showtraceback()
            else:
                if softspace(sys.stdout, 0):
                    print
        

        So there never will be an uncaught excpetion means we need to
        comment the general except block then a script like

        import sys
        
        def my_logging_func(exctype, value, traceback):
            print 'got it'
        
        sys.excepthook = my_logging_func
        
        mport os  # missing the i on purpose to raise an exception
        

        which gets executed returns got it

        What I didn’t try is to import code and overwrite run_code function.
        Leave it to you if you want to test this. ;-)

        Cheers
        Claudia

        1 Reply Last reply Reply Quote 1
        • C
          Claudia Frank @Alan Kilborn
          last edited by Feb 12, 2017, 12:50 AM

          @Alan-Kilborn

          Btw. a npp restart after modifying code.py is needed.

          Cheers
          Claudia

          A 1 Reply Last reply Feb 12, 2017, 1:00 AM Reply Quote 1
          • A
            Alan Kilborn @Claudia Frank
            last edited by Feb 12, 2017, 1:00 AM

            @Claudia-Frank

            I think you are a little bit advanced for me! :-)

            So in code.py you mean do this:

            #except:
                #self.showtraceback()
            

            You totally lost me with: “What I didn’t try is to import code and overwrite run_code function.”

            …but I will go have a look at code.py and try to figure out what you mean. Thank you again for your good (but a bit over my head at the moment) help.

            C 1 Reply Last reply Feb 12, 2017, 1:09 AM Reply Quote 0
            • C
              Claudia Frank @Alan Kilborn
              last edited by Feb 12, 2017, 1:09 AM

              @Alan-Kilborn

              So in code.py you mean do this:

              yes

              You totally lost me with: “What I didn’t try is to import code and overwrite run_code function.”

              In general python allows to overwrite what ever function if you use the same
              function signature meaning same name and same number of arguments.
              Namespaces (like what globals and locals do) could somehow try to protect function but if you try hard enough you can get it overwritten.

              Depending how python script is loading code.py it could simply mean that if you
              define your own runcode function you might overwrite the one from code.py .

              Cheers
              Claudia

              A 1 Reply Last reply Feb 12, 2017, 1:36 AM Reply Quote 1
              • A
                Alan Kilborn @Claudia Frank
                last edited by Feb 12, 2017, 1:36 AM

                @Claudia-Frank

                I tried to duplicate your “mport os” example, but I didn’t see “got it” – all I saw was the standard “SyntaxError: invalid syntax” traceback. I restarted N++ after commenting out the 2 lines in code.py .

                I see what you mean now about overwriting the run_code function, but it seems like I could avoid that complexity and just copy+paste+modify the functionality of your “sys.excepthook = xxxxx_func” in startup.py and have that xxxxx_func function do what I want (e.g. console.show()) upon unhandled exception. Of course, currently since I don’t see the “got it” example working that is going to be problematic. :-)

                C 2 Replies Last reply Feb 12, 2017, 1:44 AM Reply Quote 0
                • C
                  Claudia Frank @Alan Kilborn
                  last edited by Feb 12, 2017, 1:44 AM

                  @Alan-Kilborn

                  strange.

                  You do use python script 1.0.8?
                  On Windows or like me on linux?
                  Current npp version?

                  Cheers
                  Claudia

                  1 Reply Last reply Reply Quote 1
                  • C
                    Claudia Frank @Alan Kilborn
                    last edited by Feb 12, 2017, 1:47 AM

                    @Alan-Kilborn

                    wait - I guess I know why - just need to investigate why it didn’t happen to me.

                    Cheers
                    Claudia

                    1 Reply Last reply Reply Quote 1
                    • C
                      Claudia Frank
                      last edited by Feb 12, 2017, 2:04 AM

                      Needs more investigation - will follow up on this tomorrow.

                      Good night
                      Cheers
                      Claudia

                      A 1 Reply Last reply Feb 12, 2017, 1:24 PM Reply Quote 1
                      • A
                        Alan Kilborn @Claudia Frank
                        last edited by Feb 12, 2017, 1:24 PM

                        @Claudia-Frank

                        Yes, using PS 1.0.8.0 on Win7. I renamed code.py (and deleted code.pyc) and restarted N++. I thought this would have really bad effects when trying to run PS’s in N++, but it had no effect at all (scripts ran just the same as always). This seems to indicate that code.py has no influence…but this is odd since you seem to see the effects of changing it. I’m confused…

                        1 Reply Last reply Reply Quote 0
                        • A
                          Alan Kilborn
                          last edited by Alan Kilborn Feb 12, 2017, 3:40 PM Feb 12, 2017, 3:40 PM

                          I’m hesitant to post this, because I really prefer the “hook” solution if it can be worked out, but I tried wrapping a “main” function:

                          def main():
                              x=y  # cause exception as y is undefined
                          
                          try:
                              main()
                          except:
                              console.show()
                          

                          Running this results in a hard hang of Notepad++!

                          C 2 Replies Last reply Feb 12, 2017, 11:04 PM Reply Quote 0
                          • C
                            Claudia Frank @Alan Kilborn
                            last edited by Feb 12, 2017, 11:04 PM

                            @Alan-Kilborn

                            ok - tried to understand the python script source code and this is what I assume is
                            what happens.

                            When executing python files, the one we create with Plugins->PythonScript-New Script
                            code.py is NOT used, instead the C++ implementation of the python interface,
                            namely PyRun_Simplefile. Makes a lot of sense.

                            When executing code in the console then code.py is used. (Not interesting for this issue)

                            Because of this, there is no need to change code.py but to make a global execption hook
                            working we have to put the following code into one of the startup.py files.
                            I prefer user startup.py but machine startup.py will work too.

                            import sys
                            
                            def my_logging_func(exctype, value, traceback):
                                console.show()
                                console.write('{}\n{}\n{}\n'.format(exctype, value, traceback))
                            
                            sys.excepthook = my_logging_func
                            

                            Of course the my_logging_func code could look different for each.
                            But the parameters need to be 3!

                            When does it fail?
                            Python interpreter tries to compile the source before it gets executed and that means
                            if an exception is raised while compiling the source which includes the exception hook,
                            the hook cannot be installed.

                            Concerning the console.show() freeze, I’m using the console.show() since I started
                            with python script. I also tried your example it is working for me.

                            When your npp hangs than it means that python script created a deadlock.
                            But what could be the cause when running console.show()?
                            Can you run console.hide() when you open the console manually (via menu)?
                            Is there something special in your startup.pys?

                            Cheers
                            Claudia

                            1 Reply Last reply Reply Quote 1
                            • C
                              Claudia Frank @Alan Kilborn
                              last edited by Feb 12, 2017, 11:36 PM

                              @Alan-Kilborn

                              came just into my mind - could it be that you are using callbacks in your
                              startup.py files which could jump in?

                              Cheers
                              Claudia

                              A 1 Reply Last reply Feb 13, 2017, 1:20 AM Reply Quote 1
                              • A
                                Alan Kilborn @Claudia Frank
                                last edited by Feb 13, 2017, 1:20 AM

                                @Claudia-Frank

                                Okay, I disabled code initiated from startup.py that had a callback associated with it, and…everything (the “my_logging_func” stuff, and the “try/main()” stuff) discussed above now works. So the question becomes, what do the callbacks have to do with anything, as long as the callbacks don’t contain any code with unhandled exceptions?

                                And then the next question is, how do I get it all…my code with callbacks, and a custom exception handler (which does the console.show() )?

                                And again, Claudia, thank you for your diligence!

                                C 1 Reply Last reply Feb 13, 2017, 8:52 AM Reply Quote 0
                                • C
                                  Claudia Frank @Alan Kilborn
                                  last edited by Feb 13, 2017, 8:52 AM

                                  @Alan-Kilborn

                                  Just a quick update - the exception hook should work together with your callbacks,
                                  opening the console is the problem. If you want to know more about this there must
                                  be an old thread at sourceforge forum.
                                  To overcome this, use notepad.runPluginCommand(‘Python Script’, ‘Show Console’)
                                  instead of console.show() (Please double check syntax)

                                  Cheers
                                  Claudia

                                  A 1 Reply Last reply Feb 13, 2017, 1:04 PM Reply Quote 1
                                  • A
                                    Alan Kilborn @Claudia Frank
                                    last edited by Feb 13, 2017, 1:04 PM

                                    @Claudia-Frank

                                    I found this at sourceforge. It doesn’t really detail anything, but it reminds me of our current discussion!
                                    https://sourceforge.net/p/npppythonscript/discussion/1188885/thread/a1ec71f7/?limit=25#c261

                                    C 1 Reply Last reply Feb 13, 2017, 11:32 PM Reply Quote 0
                                    • A
                                      Alan Kilborn
                                      last edited by Feb 13, 2017, 3:52 PM

                                      So here’s what I ended up embedding in startup.py …seems to do the job and meet the original requirement:

                                      import traceback
                                      def custom_exception_handler_func(exctype, value, trace_back):
                                          notepad.runPluginCommand('Python Script', 'Show Console')  # can't/don't use console.show()
                                          sys.stderr.write('(Single-level) Traceback:' + '\n')
                                          sys.stderr.write(traceback.format_tb(trace_back)[-1])  # only write out ONE level
                                      sys.excepthook = custom_exception_handler_func
                                      
                                      1 Reply Last reply Reply Quote 1
                                      • C
                                        Claudia Frank @Alan Kilborn
                                        last edited by Feb 13, 2017, 11:32 PM

                                        @Alan-Kilborn

                                        I found this at sourceforge. It doesn’t really detail anything, but it reminds me of our current discussion!
                                        https://sourceforge.net/p/npppythonscript/discussion/1188885/thread/a1ec71f7/?limit=25#c261

                                        No, I have something in my mind related to the problems using console object
                                        together with editor callbacks. Did a quick search but wasn’t able to find it.

                                        Anyway, good to see that you have a working solution and I hope you don’t open
                                        new python script related threads in the near future. Don’t get me wrong,
                                        but it looks like have a knack to find all those nasty issues ;-)

                                        Cheers
                                        Claudia

                                        A 1 Reply Last reply Feb 14, 2017, 12:23 AM Reply Quote 0
                                        • A
                                          Alan Kilborn @Claudia Frank
                                          last edited by Feb 14, 2017, 12:23 AM

                                          @Claudia-Frank

                                          Again, many thanks for your support. And I have no more “toughies” in the queue for you…but…when I do I know where (and who) to go to! :-)

                                          Also, my most recent example lacks an “import sys” to be complete…

                                          1 Reply Last reply Reply Quote 0
                                          12 out of 38
                                          • First post
                                            12/38
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors