Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(reload module) doesn't reload Hy modules #712

Closed
ghost opened this issue Dec 11, 2014 · 5 comments · Fixed by #1787
Closed

(reload module) doesn't reload Hy modules #712

ghost opened this issue Dec 11, 2014 · 5 comments · Fixed by #1787
Labels

Comments

@ghost
Copy link

ghost commented Dec 11, 2014

No description provided.

@ghost ghost changed the title reload(module) doesn't reload/re-execute .hy files reload doesn't reload/re-execute .hy files Dec 11, 2014
@ghost ghost changed the title reload doesn't reload/re-execute .hy files reload doesn't reload/re-execute .hy files Dec 11, 2014
@ghost ghost changed the title reload doesn't reload/re-execute .hy files (reload module) doesn't reload/re-execute .hy files Dec 11, 2014
@Kodiologist
Copy link
Member

Kodiologist commented May 29, 2017

A simple way to reproduce this (on Python 3) is:

$ echo '(print "LOADED")' > test.hy
$ hy
hy 0.12.1+101.g5bf9ecf using CPython(default) 3.6.1 on Linux
=> (import test)
LOADED
=> (import importlib)
=> (importlib.reload test)
<module 'test' (<hy.importer.MetaLoader object at 0x7f1b0e9badd8>)>

"LOADED" should appear twice, but only appears once.

@Kodiologist Kodiologist changed the title (reload module) doesn't reload/re-execute .hy files (reload module) doesn't reload Hy modules May 29, 2017
@asimjalis
Copy link

asimjalis commented Jun 10, 2017

Here is a solution in Hy for reloading a file using its path. Useful for reloading from the REPL.

(defn slurp [path] (with [f (open path)] (.read  f)))
(defn hy-wrap-with-do [s] (+ "(do\n" s "\n)\n"))
(defmacro hy-load [path] `(->> ~path (slurp) (hy-wrap-with-do) (read-str) (eval)))

To reload the file "dir/script.hy" use (hy-load "dir/script.hy") on the REPL.

The reason for wrapping with (do ...) is that read-str only reads a single expr.

The reason for using defmacro instead of defn is that a defn limits the scope of the eval to its function—the definitions loaded from the script file are lost when we exit this function. Using defmacro loads the definitions into the global namespace.

@minghu6
Copy link

minghu6 commented Jan 5, 2018

I find It's hard to limit the scope, inner function can limit it, but can't works with eval.

we may need let or let with limit its scope.

@Kodiologist
Copy link
Member

Shouldn't this issue stay open? The bug remains in Hy master.

@Kodiologist Kodiologist reopened this May 25, 2018
@vodik
Copy link
Contributor

vodik commented May 27, 2018

The right solution to this, if we want to put a reload in, is to remove the module from sys.modules and then import the module again.

This is what IPython's reloader does - but the reason it doesn't work with Hy is that the reloaded explicitly checks if __file__ ends with ".py". Patching that, it works

Now there are a lot of issues with how Hy manipulates sys.modules, and they're addressed with #1518. I really need to get that done, but the patch works. Its a matter of unit test coverage and documentation that's holding it back.

Soon! I promise. My free time is at a premium at the moment for personal reasons, but I want to make it a priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants