Menu

Console problem - causing npp to crash

Help
Davey
2015-02-24
2015-03-02
1 2 > >> (Page 1 of 2)
  • Davey

    Davey - 2015-02-24

    Hi Dave
    Firstly, I would like to thank you for this incredible plugin.
    I got to know it a few months ago and I am already a die-hard user. I gain form it on a daily basis!
    It's a great plugin with tons of functionality and build incredibly well!
    THANKS!

    Regarding the problem mentioned, we discussed this once before (here), but I figure I'd try again in this forum (which I just found about thanks to Yaron)

    In that thread, I mentioned that a simple script like such:

    console.show()
    console.clear()
    

    causes npp to crash. (Although, you mentioned that on your end everything works well....)

    I've done more testing, and seemingly it is a problem with clear function.
    I made a new script with just console.show() and it worked fine
    Another script, with just console.clear() and this causes npp to crash! 1 line script. However, this is only if the console was not open. If it was already open, then it does not crash.
    (I found tonight that a similar point and problem was brought up not too long ago here)

    However, some more showed more issues
    a simple script like this:

    console.show()
    console.hide()
    

    causes npp to crash!
    But this is very interesting:
    without any import commands, it works fine on the first time, but crashes when running a second time.
    But when I have import re or import os at the top of the script, it crashes even on the first time!

    I'm trying to make sense of this but I just can't figure it out
    I hope this is not too long of a reprt, and I hope this testing analysis helps you in finding this / these bugs.

    Running Windows7 / N++ 6.7.2 / Python plugin 1.0.8.0
    Testing was done without any other plugins installed

    Thanks again
    All the best
    Davey

     

    Last edit: Davey 2015-02-26
  • Dave Brotherstone

    Ok, just tested this again with exactly the same setup (N++ 6.7.2, PS 1.0.8), and everything worked for me fine.

    I suspect this is a library that you've got installed that's being loaded instead of one of the defaults. I've just tried enabling "prefer installed libraries", (I've just got a standard 2.7 installed though), and everything still worked, so I expect it's something else extra that's installed, and some import means importing some native extension that isn't compatible.

    On the console, can you do:

    import sys
    sys.path
    

    And then check if there's anything outside of the N++ directory in there (if you've got a python installation there will be).

    Then try setting sys.path to just the N++ directories, and try your script again.

    Assuming that works, it's just a case of adding the directories back slowly until you find the one that causes the issue.

    Hope this helps

    Dave

     
  • Davey

    Davey - 2015-02-24

    Hi Dave,
    Thanks for your response and help - I really appreciate it
    This is the result I got from writing sys.path to console:

    ['Y:\\Notepad++\\plugins\\PythonScript\\lib',
    'Y:\\Notepad++\\plugins\\Config\\PythonScript\\lib',
    'Y:\\Notepad++\\plugins\\PythonScript\\scripts',
    'Y:\\Notepad++\\plugins\\Config\\PythonScript\\scripts',
    'Y:\\Notepad++\\plugins\\PythonScript\\lib\\lib-tk',
    'Y:\\Notepad++\\python27.zip',
    'Y:\\Notepad++\\DLLs',
    'Y:\\Notepad++\\lib',
    'Y:\\Notepad++\\lib\\plat-win',
    'Y:\\Notepad++\\lib\\lib-tk',
    'Y:\\Notepad++']
    


    Seems like everything is from the Notepad++ folder which is on drive Y
    Do you see anything peculiar in here?

    Thanks again
    Davey

     
  • Yaron

    Yaron - 2015-02-25

    Hello Davey and Dave,

    Sometimes, the most simple suggestions might be helpful.
    How about downloading a portable NP++ and installing the Python plugin there?

    I hope you find the cause of the problem.

    Best regards.

     

    Last edit: Yaron 2015-02-25
  • Davey

    Davey - 2015-02-25

    Hi Yaron, thanks for the suggestion
    However, I am not familiar with that - what is a portable version of n++?
    In what way is it different than the standard?
    How do I know if that's what I am using??
    And where do I download it from?

    Thanks
    Davey

     

    Last edit: Davey 2015-02-25
  • Yaron

    Yaron - 2015-02-25

    Hello Davey,

    Please go to this page.
    Instead of downloading the Installer, download either the ZIP package or the 7Z package.
    Unpack the file and you get a copy of NP unrelated to the one you had already.

    The portable version is not supposed to make any changes in the Registry, and all the files are placed under the main folder (to which you unpacked the files).
    If you use the installer, some files would be in one of the folders under AppData.

    Note: if you have some files associated with NP, they would open in your old copy.
    So, for the test we're doing open the new copy and Drag and Drop files into it.

    Now, download the Python Plugin Installer, and install it in the new copy.

    If the crash doesn't happen with the new copy, you can uninstall the old one.
    If it does crash, we'll have to wait for another suggestion from Dave.

    Good luck!
    I'll stay up late, waiting for updates.

    Best regards.

     
  • Davey

    Davey - 2015-02-26

    Hi Dave and Yaron

    @ Yaron, thanks for the instructions and clarification. It comes out that I've been using the portable version for over a year now! :)
    See below for my diagnosis
    (Thanks for trying yo help with this! especially for staying up late... :) )

    @ Dave
    I did what Yaron suggested: I setup a new portable version, installed python script, and ran the snippet - n++ did NOT crash
    Then, I brought in my entire plugin folder and n++ DID crash!
    After all the diagnosing... it comes out that the problem was with the callback script that I mentioned in the thread I mentioned above
    It's a CHARADDED callback that is initiated on startup through the user's startup script
    (Python script set to startup ATSTARTUP, however, doesn't have to be installed in startup - even if you just run the script after n++ starts, we will still get the crash)

    So, final diagnosis shows:

    1. Brand new portable version of n++
    2. Install latest python
    3. Initiate the callback function
    4. run the snippet that shows and clears the console
    5. N++ crashes
    6. Clear the callback (or remove it from startup), run snippet, N++ is great!

    If Dave or anyone can shed some light on this issue I would truly appreciate it
    Also, if anyone can try it on their end to verify this problem I would also appreciate it

    Thanks a lot
    Davey

     

    Last edit: Davey 2015-02-26
  • Yaron

    Yaron - 2015-02-26

    Hello Davey,

    I was glad to reciprocate a bit. :)

    I can reproduce the crash.
    I've tried several "SCINTILLANOTIFICATION callbacks", and if you call the console in them - NP hangs.
    It does not happen with "NOTIFICATION callbacks".

    The "console" may not be recognized in those callbacks (like sending "this" to setTimeout).

    Let's wait for Dave's theory.

    Best regards.

     
  • Davey

    Davey - 2015-02-26

    Wow! Great! Thank you Yaron
    You also clarified the situation even more!

    As you said, now we have to just wait for Dave...

    Thanks,
    Davey

     
  • Yaron

    Yaron - 2015-02-26

    Hello Davey,

    I'm really glad I could help. You are a kind gentleman.
    I hope Dave will find a solution.

    Best wishes.

     
  • Dave Brotherstone

    You guys rock. Thanks for hanging in there and narrowing the bug down.

    You'll be glad to know I've been able to reproduce it now, and also (hopefully) fix it.

    It'd be great if you could try out this version (it's just the PythonScript.dll) to check it fixes the issue. I could reliably reproduce it with your steps before, and now I can't :)

    http://www.brotherstone.co.uk/npp/ps/PythonScript_1.1.7z

    I started playing with ways of causing a "flash" by the way - and I wanted to do something like this:

    def swapColours():
        for i in range(0, 256):
            foreground = editor.styleGetFore(i)
            background = editor.styleGetBack(i)
            editor.styleSetFore(i, background)
            editor.styleSetBack(i, foreground)
    
    def flash():
        swapColours()
        time.sleep(0.1)
        swapColours()
    

    But it was just too slow - a combination of Python and Scintilla painting the window after every colour change. So, I added this to this version:

    editor.flash()
    

    You can pass it milliseconds to wait before switching back, or use the default of 50ms, which (at least for me) seems to be a reasonable value to cause an obvious-but-not-too-intrusive flash.

    Thanks again, and please let me know if this fixes it, so I can get a new version out.

    Cheers,

    Dave

     
    • Davey

      Davey - 2015-02-27

      Hi Dave
      Thanks so much for your help!!
      I just tried again and it seems to work perfectly!
      I tried both:

      console.show()
      console.clear()
      

      and

      console.show()
      console.hide()
      

      and they worked fine without crashing
      Problem seems to be solved! - THANK YOU

      @ Yaron - you are still getting a crash of some sort... that's interesting
      I ran your script and everything worked perfectly
      What kind of behavior are you getting?

      Thanks!
      Davey

       
      • Yaron

        Yaron - 2015-02-27

        Hello gentlemen,

        Please run the script, and add two characters to the file.
        NP crashes after the second char is added.

        Regards.

         
        • Davey

          Davey - 2015-02-27

          Hi Yaron, thank you.

          I just tried what you said, and here is my report:

          • Entering any 2 characters did not cause a crash by me. (Actually, besides the fact that I had to return to the editor after every keystroke :), I was able to type a whole sentence without a problem)
          • HOWEVER, hitting the enter (return) key two times Does cause a crash! The 2 return keys do not have to be consecutive. Hit return, character, return; result = crash

          Dave....

          Thanks,
          Davey

           
  • Yaron

    Yaron - 2015-02-26

    Hello Dave,

    Thank you so much! You are great.
    I'm so glad for Davey. His patience is unique.

    I see a somewhat different behavior, but NP still crashes.
    Davey?

    Please run the following script.

    import time
    
    def getExpression():
        if editor.getSelections() == 1: # Never 0.
            expression = editor.getSelText()
            if expression != "":
                return expression
    
            global oldPos
            sPos = editor.wordStartPosition(oldPos, True)
            ePos = editor.wordEndPosition(oldPos, True)
            oldPos = sPos
        else:
            mainSel = editor.getMainSelection()
            sPos = editor.getSelectionNStart(mainSel)
            ePos = editor.getSelectionNEnd(mainSel)
    
        return editor.getTextRange(sPos, ePos)
    
    def highlightFirst():
        editor.setTargetStart(0)
        editor.setTargetEnd(editor.getLength())
        sSel = editor.searchInTarget(findExpression)
    
        if sSel != -1:  # Would be -1 if not found; - Match whole word etc.
            eSel = sSel + len(findExpression)
            editor.setSel(sSel, eSel)
    
            if oldPos == sSel or oldPos == eSel:
                editor.flash()
    #           flashSel()
    
    def flashSel():
        for flashSel in range(0, 3):
            time.sleep(0.2)
            editor.hideSelection(True)
            time.sleep(0.2)
            editor.hideSelection(False)
    
    def comments(args): # A test for Davey.
        console.show()
    
    editor.callback(comments, [SCINTILLANOTIFICATION.CHARADDED]) # A test for Davey.
    
    oldPos = editor.getCurrentPos()
    findExpression = getExpression()
    
    if findExpression != "":    # A symbol etc.
        highlightFirst()
    

    And now allow me to thank you for editor.flash(). I really appreciate it.
    I don't want to be ungrateful, but I meant flashing the title bar and NP's tab in Windows task bar (like wrapping around in a search).

    We have managed to flash the selection (def flashSel():). What do you think?

    With your permission, one more question:
    Is "editor.searchInTarget" (and setTargetStart/End) the best search method for this script?
    Davey initially suggested editor.find().

    Please refer to my questions after the crash issue has been settled. :)

    I wish you the very best.

     

    Last edit: Yaron 2015-02-26
    • Davey

      Davey - 2015-02-27

      Hi Yaron @ Dave:

      Regarding the flash,
      I agree with Yaron on that. The best thing would be to flash the tilte bar. Which essentially means to make n++ lose focus for a short time and then get it back. (The flashing of the entire background was a little to harsh for me :) )


      Regarding the search method:
      Yaron, I initially proposed find() because it was just easier to do it on one call.
      The Scintilla docs recommend that all searches be done using target - quote:

      Most applications should use SCI_SEARCHINTARGET as the basis for their search implementations
      But I too would like to hear Dave's thought on this.


      By the way, regarding searching with target, there is a helper method which makes it a little shorter by setting the target start and end in one call:

      Editor.setTarget(start, end)
      

      Sets the target start and end in one call. See Editor.setTargetStart() and Editor.setTargetEnd()


      Thanks and all the best
      Davey

       
      • Yaron

        Yaron - 2015-02-27

        Hello Davey,

        Here I am thanking you again. :)

        Regards.

         
  • Davey

    Davey - 2015-02-27

     

    Last edit: Davey 2015-02-27
  • Dave Brotherstone

    I've found one further issue, but I'm not totally convinced that it's your issue. This fix is to do with the console prompt, and the only thing is that when you press enter after having run your script, the console focuses (due to the console.show() in the callback), and then the "enter" key takes effect there, effectively running an empty command on the console, thereby requiring the prompt to be recalculated.

    So, whilst I'm not convinced we've solved everything there is an explanation that sounds plausible that I can blame it on if everything seems to be fixed :)

    http://www.brotherstone.co.uk/npp/ps/PythonScript_1.1.1.7z

    If not, I'll keep looking, but probably won't be till next week.

    Dave.

    PS. Have you thought of using editor.search() for searching? - it's safer than messing with targets yourself.

     
  • Yaron

    Yaron - 2015-02-27

    Hello Dave and Davey,

    Works for me. :)
    Thank you. I appreciate it.

    For further investigation:
    With v1.1 NP crashed on my machine without pressing Enter.

    Type any character.
    Return the focus to the editor by clicking in its area.
    Type another character.

    Result: NP hangs.


    How do I get the start position using editor.search()?

    Best regards.

     
  • Davey

    Davey - 2015-02-27

    Hi Dave and Yaron
    This works for me also!
    Great going dave!


    @ Yaron

    Result: NP hangs.

    I am still not getting that by me... but at this point its only theoretical!


    @ Dave

    PS. Have you thought of using editor.search() for searching? - it's safer than messing with targets yourself.

    1. I suppose you mean searchNext and searchPrev because I didn't see a function called search.
    2. The reason why I didn't use those functions is because Yaron wanted to find the first or last in the document which means we have to set the range where to search. As Yaron mentioned, I didn't see that that is possible with the search functions.
    3. What do you mean by safer and messing? is there anything that can be messed up by using targets?


    Thanks again Dave!
    All the best
    Davey

     
  • Yaron

    Yaron - 2015-02-27

    Hello Davey,

    I'm glad it works for both of us. :)

    I found editor.research(), and assumed that editor.search() takes the same arguments.
    Thank you.

    Have a nice weekend.

     
  • Yaron

    Yaron - 2015-02-27

    Hello again,

    Regarding the different behavior with v1.1.
    I don't know if this is relevant: I use Plugin Manager v1.3.5.

    BR.

     
  • Davey

    Davey - 2015-02-27

    Hi Yaron,
    I just did some investigation and it seems that your assumption is correct
    Using dir(editor) I was able to verify that there is such a function
    And then, using wrong parameters, I was able to get all the parameter possiblities.
    Here is what I got: (after removing some unnecessary text from the beginning of each line)

        search(object search, object handlerFunction, int flags, int startPosition, int endPosition, int maxCount)
        search(object search, object handlerFunction, int flags, int startPosition, int endPosition)
        search(object search, object handlerFunction, int flags, int startPosition)
        search(object search, object handlerFunction, int flags)
        search(object search, object handlerFunction)
    


    So, we could use this function as such:

    s = []
    editor.search(findExpression, lambda m: s.append(m.span(0)[0]))
    if len(s):
        foundPos = s[0]
    else:
        … … … 
    


    And if we want we can add more params:

    flags, start, end, max
    


    And to get the last find, I guess we just run it on the whole doc and use:

    foundPos = s[len(s)-1]
    


    However, regarding the flags, are they the re.* flags? It does work but seems funny...? (Dave... )


    Have a great weekend guys
    Davey

     
    • Dave Brotherstone

      You can see the parameters with help(Editor.search), and also in the docs,
      along with an explanation of each one.

      http://npppythonscript.sourceforge.net/docs/latest/scintilla.html#Editor.research

      research is the same, it just uses a regex instead of a string, the rest of
      the function is the same.

      By "messing with targets", and "safety", I was referring the fact that lots
      of things use the target, including n++ tags highlighting for example, and
      because python is running on a separate thread, there's always the chance
      that they run at the "same" time, and you end up with the start position
      from N++ and the end position from you. This is even more likely if you're
      running it in a callback. editor.replace works around this by finding all
      the replacements, then sending them to the main thread to actually perform
      the updates, then it can't overlap with N++ (or another plugin).

      So, we're good to go with the latest version then? i.e. 1.1.1?

      I'm pretty sure there's an API for "attracting attention" with the window
      focus - I'll see if I can get a notepad.flash() going too (I think
      editor.flash could still be useful, just maybe for different cases)

      Hope that helps.

      Dave

       
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.