[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Bug reports
. . -> [Subject]  A problem about python script reloading(Bug or NOT)

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: A problem about python script reloading(Bug or NOT)
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please).
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Pages: 1 2  

Posted by Worstje   Netherlands  (867 posts)  [Biography] bio
Date Thu 20 Nov 2008 07:40 PM (UTC)  quote  ]
Message
Nick, unloading and loading again (not reinstalling) doesn't make a difference if I recall properly, although it's been ages since I tested. :( In essence, something hangs around in memory that says which modules have been loaded, and it won't re-read them. Somehow I suspect that the WSH-bindings (pywin32) are what make the sys.modules trick not work. But I'd need to do some serious testing to be sure.
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Thu 20 Nov 2008 07:37 PM (UTC)  quote  ]
Message
Ah, it's not a plugin, it's a script :/ Is there an easy way to do this? I'm writing my dissertation in MC and I wouldn't want to turn scripting off and on every time I change a few lines of code, while the automatic reload on save would be pretty handy...

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Thu 20 Nov 2008 07:36 PM (UTC)  quote  ]
Message
The "reload script file" menu item does exactly what it advertises - it reloads the script file.

To completely re-initialize the script engine you could turn scripting off, then turn it back on again.

However I found a similar problem with Lua, and with an extra line (similar to what Worstje suggested) it forced the module to be reloaded.

In case anyone is wondering, the Lua code is:


package.loaded.my_module = nil -- force reload

require "my_module"


For Python, if you can't find something similar, doesn't reloading the plugin work? That should give you a fresh copy. Failing that, delete the plugin and reinstall it. That would certainly reinitialize the script engine.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Worstje   Netherlands  (867 posts)  [Biography] bio
Date Thu 20 Nov 2008 07:12 PM (UTC)  quote  ]
Message
Bah, useful. From what I understood, clearing sys.modules would essentially wipe the cache of previously loaded modules, pushing Python to re-read them from disk. :/

In that case, I draw a blank for now. Sadly the issue isn't urgent enough for me to really go chase a fix down, although if I find a moment, I'll have a looker into it again.
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Thu 20 Nov 2008 05:57 PM (UTC)  quote  ]

Amended on Thu 20 Nov 2008 05:58 PM (UTC) by Poromenos

Message
Hmm, that just causes the scripting engine loading to fail:

Quote:

loading scripting engine
World: world
Error -2147467259 occurred when loading scripting engine:

Unspecified error

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Worstje   Netherlands  (867 posts)  [Biography] bio
Date Thu 20 Nov 2008 05:39 PM (UTC)  quote  ]

Amended on Thu 20 Nov 2008 05:41 PM (UTC) by Worstje

Message
Take a look at my plugin MudStatus I posted a few days ago. I ran into the same problem and was too lazy to come up with a proper fix, so I pseudo-coded my way around it with the MUSHclient import directive since the plugin was small anyhow and the functionality not meant to be shared across other non-MUSHclient Pythonscripts anyhow.

However, I have since then figured (although not tested which you will need to do) that you can probably do the following to fix it (at the top of your plugin):

import sys
sys.modules = {}


I should really test to see if that theory holds, but I tend to have the habit of using the MUSHclient <import> tag anyhow because it allows me to split my triggers up also. Let us know if the above holds for you?
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Thu 20 Nov 2008 04:56 PM (UTC)  quote  ]
Message
I have been bitten by this behaviour as well... Is there anything that can be done in MC to avoid this hackery (like restarting the interpreter whenever the script is reloaded)?

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Sun 17 Apr 2005 10:08 AM (UTC)  quote  ]
Message
Quote:
I didn't realise you could have MC triggers call functions in modules...


When you do "from module import *" it just takes every (almost every) key/value pair from the imported module's global dict and places it directly into the global dict of the importing module, so the contents of the imported module becomes the same thing as if it all was declared in the importing one. So technically speaking, you aren't calling a function in an imported module - you are calling one in the "top-level" script. Allowing the dot notation in triggers/aliases would mean allowing methods and properties access on objects, which might be a problem since different languages have different rules for doing that.
[Go to top] top

Posted by Hairui   China  (24 posts)  [Biography] bio
Date Sun 17 Apr 2005 06:06 AM (UTC)  quote  ]

Amended on Sun 17 Apr 2005 06:10 AM (UTC) by Hairui

Message
I have tested the method from Ked in his recent post, it works well, though the codes :

import PassForest
if DEBUGMODE :
    reload(PassForest)
from PassForest import *

seem some strange. :)
I like it just because it works.
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Sun 17 Apr 2005 01:32 AM (UTC)  quote  ]
Message
I didn't realise you could have MC triggers call functions in modules... It should be changed to allow the dot in the function name, I think, since it's valid...

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Sat 16 Apr 2005 06:52 PM (UTC)  quote  ]
Message
Maybe try:


import ModuleA
reload(ModuleA)
from ModuleA import *


Hypothetically, it could work since the first import results in a module object, the reload() call returns the same (or newer) module, so "from ModuleA import *", if we follow the rationale of Python's import mechanism should load the contents of the already existing object - the one that was just reloaded one line prior. No guarantees though.
[Go to top] top

Posted by Hairui   China  (24 posts)  [Biography] bio
Date Fri 15 Apr 2005 12:43 PM (UTC)  quote  ]

Amended on Fri 15 Apr 2005 12:56 PM (UTC) by Hairui

Message
What I mean is if I use
import ModuleA

I can not assign the functions in ModuleA to the triggers of mushclient, because dot is forbidden for a function name of triggers , such as
ModuleA.FunctionA 
.
But if I use
from ModuleA import *
, the reload function would not work .
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Fri 15 Apr 2005 12:29 PM (UTC)  quote  ]
Message
If you do a "from module import *", you have to use just the function name, FunctionA. If you do "import module" you have to use module.FunctionA. I don't know if I understood what you're saying correctly.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Hairui   China  (24 posts)  [Biography] bio
Date Thu 14 Apr 2005 11:48 PM (UTC)  quote  ]

Amended on Fri 15 Apr 2005 01:12 AM (UTC) by Hairui

Message
Maybe I should add a DEBUGMODE flag into the code:

import ModuleA
if DEBUGMODE == 1:
    reload(ModuleA)

It seemed all the problems have beed solved, but the new problem is :
reload(ModuleA)
can not support the import clause like:
from ModuleA import *

And I can not assigned a function like :
ModuleA.FunctionA
to a trigger or an alias in MUSHClient.I think this is really a new problem.
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Thu 14 Apr 2005 08:30 PM (UTC)  quote  ]
Message
This is neither a Python, nor a Mushclient bug. This is perfectly valid and expected behavior of Python's "import" function. When you import the same module more than once per an interpreter session, all subsequent imports result in a copy of an already compiled and loaded module being returned. It's done that way for efficiency reasons, and I personally like it - it lets me organize interaction between plugin and is extremely useful. The official way around this feature is the "reload" function that you've already found.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


6,901 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]