-
Notifications
You must be signed in to change notification settings - Fork 272
in case student's files shadow packages #46
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
Conversation
@crossroads1112 @brianyu28 @kzidane per email discussion! |
Sorry about that! Just to be safe, we may want to do the importing in a "try" and restore the path in a "finally", just to make sure that we always restore the students' path even if an exception is thrown (this would only matter if the student tried to handle the exception, so it's unlikely that this would ever come up, but it seems like good practice to make sure we always restore |
@crossroads1112 hm, do we really want to swallow in |
I don't mean we'd catch the exception, just that we'd clean up if there was one; the exception is still raised even when there is a finally block e.g.
if an exception is raised, the cleanup code gets run, but the exception still propagates. |
TIL. How's it look now? |
src/cs50/__init__.py
Outdated
if fullname == "cs50.SQL": | ||
return self | ||
return None | ||
class CustomImporter(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aesthetically we could maybe move this above the try block (so it isn't indented an extra level), but not a big deal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not opposed.
src/cs50/__init__.py
Outdated
from .cs50 import eprint, get_char, get_float, get_int, get_string | ||
try: | ||
from .cs50 import get_long | ||
except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably be except Exception:
unless we want to catch SystemExit
et al.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL
src/cs50/__init__.py
Outdated
|
||
# In case student has files that shadow packages | ||
sys.path = [p for p in sys.path if p not in ("", os.getcwd())] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the docs, the directory in which the script is run (or ""
if in the interpreter) is always sys.path[0]
. With this in mind, I think we could just do del sys.path[0]
for the same effect (and thus avoid having to reconstruct the whole list).
Alternatively we could do e.g.
try:
cwd = sys.path.pop(0)
# ...
finally:
sys.path.insert(0, cwd)
which avoids having to store two copies in the first place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, technically, a student could have prepended a path of their own? Feels more robust not to make any assumptions? And cost of reconstruction is pretty trivial.
|
No description provided.