Python not working consistently

  • Davros30

    Davros30 - 2013-08-12

    I am running it on Windows 7 with Notepad++ 6.4.3.
    The Python Script version is
    The underlying python runtime is 2.7.1 which was installed with the Python script plugin for notepad++

    I have a very simple set of python instructions but I dont get consistent results when I run it.


    the intention is to turn this


    into this


    If I run the script with the cursor at the beginning of the document then it works OK for the a small list of values eg under 20 lines.

    If I then undo the change and run it again it wont work and gives some random outcome of deleting lines and only partly replacing values.

    If I run the script with the cursor not at the beginning of the document then it wont work and gives some random outcome.

    If I run the script with a large list of values then it gives some random outcome.
    This is very disappointing. I was hoping I could rely on this to save me a lot of fiddling about but it just isnt reliable. Am I doing something wrong. Surely it must be more reliable for others or nobody would be using Python Script with Notepad++

    Any help much appreciated.


    • Nick Odaemus

      Nick Odaemus - 2013-11-04

      I also get incredibly inconsistent results with Editor replace functions. I've devised a set of scripts that rely on a single Python class to do common #rereplace (the worst of the whole lot in my experience) work, and results are correct sometimes, wildly off most times. Crazily enough, there seem to be better hours/days than others.

      It seems that the more work I ask a script to do with Editor (the more functions I call in a single run), the more likely it is to lose its mind. For the most part, it seems like the limit is about two #rereplace calls per script, including the calls from the instantiated class. It's kind of a nightmare.

      I'd really rather use the editor, but I feel like my best bet is to either use NPP's regular expression find-replace, or to start refactoring to take files as input...

  • Dave Brotherstone

    3 things...

    1) Sorry I didn't see this earlier.
    2) This is rubbish. I need to spend some time on this, especially now we've got proper regex in rereplace, hence the use cases for pyreplace are limited to calling a function for the replacement text (I think, if you know of others then shout).
    3) I've just played around, and in N++ 6.2, things "mostly" worked, there was some odd stuff around undo actions (you have to editor.endUndoAction() after a rereplace(), even though you've not started one, and then if you wrap the whole thing in a beginUndoAction, endUndoAction, then you get expected behaviour), but in 6.4.5, it all goes pear shaped. I'll take a look at this, as it's obviously connected with the recent changes in regex replacements in N++ itself.

    Sorry for the rubbish state of affairs. We'll get this fixed as soon as we can - scripting replacements is the main reason I made this plugin.


  • Dave Brotherstone

    Right, I've had a good play around, and run some tests, and I can definitely reproduce it occasionally. There was an issue with the undo actions for editor.replace, but even with that fixed, and using N++ 6.5.1 (which uses the new Scintilla), I still get the odd behaviour now and again. It's an odd issue as the code appears correct, but what actually happens is something different - I'm afraid it might be in the actual replacement code in N++/Scintilla.

    I'll post back here as soon as I have something fixed.


  • Dave Brotherstone

    Just for anyone how didn't see the update on twitter - I've now found the source of this issue, and am working on a fix. The fix should also drastically improve the performance of the .replace()/.rereplace functions. I've not got firm numbers yet, so it's difficult to say, but from other tests it should be much quicker.

    The same fix will work for pyreplace, but I'm not sure if the impact will be as big in terms of performance.

    In the meantime, you can disable smart highlighting, highlight matching XML tags, and clickable links, and that will certainly help. It might not fix it 100%, but I've not been able to reproduce the issue with these options disabled.

    I'll come back when I've got something working :)


  • Dave Brotherstone

    It works! Taking the

    line 1
    line 2
    line 3

    example, with 10000 lines, it takes about 50 seconds (when it works) on my PC with the old editor.replace(), using the new code, it's just under 2 seconds, and works every time.

    There's still work to do - the aim is to get editor.replace to be the main replace method, and support everything that pyreplace currently supports, and a bit more.

    Anyway, I just wanted to say that progress is being made, and it's looking good!

    And you thought this was dead :)


Log in to post a comment.