-
-
Notifications
You must be signed in to change notification settings - Fork 572
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
Make .sage modules importable #27074
Comments
comment:3
Do we need to worry about |
comment:4
Replying to @dimpase:
I don't see why there is a big worry about this. We just have to decide on an order of trying filenames. This is no different from having a So the |
comment:5
Replying to @jdemeyer:
Perhaps so. The idea there was just to make it less likely for it to shadow Python modules on But if the other mitigation strategies for that are employed, then perhaps we don't need to use that convention in the first place. Also, many stdlib libraries would likely be imported already by the time the import hooks necessary for supporting |
comment:6
Replying to @embray:
Again, this is no different from having an |
comment:7
+100 to having this! Thanks Erik for starting this ticket (it has been on my todo list for a while now). Replying to @jdemeyer:
I agree with this. However the ability to import Still, if there's |
comment:8
Replying to @jdemeyer:
That's true about And I agree it's no different. My only point is that currently it does not work that way for |
comment:9
Replying to @defeo:
It would, just as in Jeroen's analogy to |
comment:10
Per discussion with Luca, I found this prototype I worked on in #24681 which might be helpful here: https://gitlab.com/sagemath/dev/trac/compare/8.2.beta4...u%2Fembray%2Fpython3%2Fcython-source-prototype I remember now that the context to that work was to make the source code for Cython modules findable on Python 3, so the idea was to add an import hook that could recognize imports of Cython-modules based on the presence of the actual For importing It turns out I didn't implement this for Python 2 after all. As I wrote on #24681 it used to be easier to make I think for now we might as well ignore Python 2. On Python 3 I think it's now a little easier than it used to be to implement custom module loaders. But adding custom module finders is more of a problem. It's a kind of chicken/egg problem, where in Python 2 it was actually easier to override the default behavior with import hooks, because the default behavior was completely hard-coded in the interpreter. But now the default import system is bootstrapped on top of the same import hook system that is used to extend it, with the result that it's now harder to extend the default behavior while not also breaking/contradicting with the default import system. |
comment:11
[ Sorry for barging in : I saw some interesting light... ] One of the corollaries of this enhancement is that it should be possible in a similar way to monkey-patch some Sage classes, i. e., for example, adding new methods to Sage-defined classes. This would considerably ease the prototyping of new extensions (which currently require forking a branch and re Would you consider this aspect of the issue in future development ? |
comment:13
Replying to @EmmanuelCharpentier:
I'm afraid I don't see at all how that's related. This is just about making Nicolas worked on something like this with https://github.com/nthiery/recursive-monkey-patch so you might want to ask him about it. |
comment:14
Let's see if we can revive this idea now that the py3 transition is complete... I think it's a key to the recurring question "how to distribute user packages" -- for user packages that consist of a bunch of sage files |
This comment has been minimized.
This comment has been minimized.
comment:18
Setting new milestone based on a cursory review of ticket status, priority, and last modification date. |
It's relatively easy using import system hooks in Python to make new file types 'importable' using the standard import statement. E.g. a file like
mymodule.sage
could be imported asmymodule
just as though it weremymodule.py
.Importing a
.sage
module is of course just a thin layer over a normal Python module import, but to pass it through the Sage preparser.There are of course some risks to making
.sage
modules importable. For example, what if a user has both afoo.sage
and anfoo.py
? Which one takes priority uponimport foo
? In particular it could introduce quite some surprises if a user has something likeos.sage
in their current directory.Unfortunately it's not possible to extend the syntax of import statements, which would be nice. But, while the "module name" portion of an import statement must be a valid identifier, we can still otherwise process it however we want. So maybe
.sage
module imports would be initiated only if.sage
is used explicitly in the import statement. So for example, to import a sage module namedfoo.sage
one would literally write:to distinguish it from a Python module named
foo.py
. Of course, if there is a Python package namedfoo
that happens to have asage
sub-module there is still a conflict. But this is a bit unlikely for most cases.So in order to make such a feature available, while mitigating potential problems with it, I might suggest a few additional restrictions:
There would be a function to enable/disable
.sage
import functionality on the fly. When first introduced this would be disabled by default, but we would certainly want to make the feature easily discoverable through the documentation, with all the caveats discussed.In the off chance that there is a conflict between a
.sage
module an a plain Python module, the system should check for that. In that case the.sage
module still takes priority (if.sage
imports are enabled), but a warning about the conflict is shown.This feature should be Python 3 only. That will make it easier to implement, due to the significant differences between the import systems, and it will also give a nice motivation, to anyone who wants the feature, to switch to Python 3.
See also: https://groups.google.com/d/msg/sage-devel/tL8Whabyaoc/RMzlMyUtBwAJ
CC: @dimpase @jdemeyer @EmmanuelCharpentier @nthiery @nbruin @yuan-zhou
Component: interfaces
Issue created by migration from https://trac.sagemath.org/ticket/27074
The text was updated successfully, but these errors were encountered: