Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Bug reports ➜ A problem about python script reloading(Bug or NOT)

A problem about python script reloading(Bug or NOT)

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  

Posted by Hairui   China  (24 posts)  Bio
Date Mon 05 Jul 2004 05:56 AM (UTC)

Amended on Mon 05 Jul 2004 06:09 AM (UTC) by Hairui

Message
This post is related with

We can test it like this:
writing a main script named "MainTest.pys":
def OnWorldOpen():
	pass
def OnWorldConnect():
	pass
def OnWorldClose():
	pass
def OnWorldDisconnect():
	pass
def OnWorldGetFocus():
	pass
def OnWorldLoseFocus():
	pass
import sys
sys.path.append("C:\fy4")
import AddAliases
AddAliases.NoteTest(world)
world.note(str(AddAliases.AddSkAlias()))


And the script AddAliases.py's content is :
def AddSkAlias():
    return ["skillsAlias", "sk", "skills", 1, ""]

def NoteTest(world):
    world.note("Test")

if __name__=="__main__":
    print "AddAliases Test"
    print AddSkAlias()
else:
    print "AddAliases Imported"
    print AddSkAlias()


When you first run the script, the mushclient met no error and display text "Test" on the screen.

Then we changed the line 5 of AddAliases.py to :
   world.note("TestA")

and save the file.
Now press the button "Reload Script", the mushclient will still display text "Test" but not "TestA" on the screen.

Before rebooting the MushClient, the changes made to the AddAliases.py will not take affect.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 05 Jul 2004 07:34 AM (UTC)
Message
From what you describe, this is not a problem in MUSHclient, but the Python scripting engine looks like it is caching the import file.

Try putting a display into the main script to confirm it is reloaded.

After all, the "reload script file" function only reloads the main script file. It is up to the scripting engine to reload imported files itself. There is no way MUSHclient can know to do that.

One thing you could test is to put the test into a plugin - then re-install the plugin. The plugin's script space should be reloaded when you do that. The problem may or may not go away then.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Hairui   China  (24 posts)  Bio
Date Reply #2 on Thu 14 Apr 2005 02:50 PM (UTC)

Amended on Thu 14 Apr 2005 11:48 PM (UTC) by Hairui

Message
Same problem still can be found under MUSHClient 3.65 and ActivePython 2.4.

I had found a method to solve the problem,
which like this:
# Everytime we want to import a moudle
import ModuleA
reload(ModuleA)


It seems not a perfect method but really works.
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #3 on Thu 14 Apr 2005 08:30 PM (UTC)
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.
Top

Posted by Hairui   China  (24 posts)  Bio
Date Reply #4 on Thu 14 Apr 2005 11:48 PM (UTC)

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.
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #5 on Fri 15 Apr 2005 12:29 PM (UTC)
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!
Top

Posted by Hairui   China  (24 posts)  Bio
Date Reply #6 on Fri 15 Apr 2005 12:43 PM (UTC)

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 .
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #7 on Sat 16 Apr 2005 06:52 PM (UTC)
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.
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #8 on Sun 17 Apr 2005 01:32 AM (UTC)
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!
Top

Posted by Hairui   China  (24 posts)  Bio
Date Reply #9 on Sun 17 Apr 2005 06:06 AM (UTC)

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.
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #10 on Sun 17 Apr 2005 10:08 AM (UTC)
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.
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #11 on Thu 20 Nov 2008 04:56 PM (UTC)
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!
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #12 on Thu 20 Nov 2008 05:39 PM (UTC)

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?
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #13 on Thu 20 Nov 2008 05:57 PM (UTC)

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!
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #14 on Thu 20 Nov 2008 07:12 PM (UTC)
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.
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.


56,372 views.

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

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.