Skip to content

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

Merged
merged 3 commits into from
Dec 22, 2017
Merged

in case student's files shadow packages #46

merged 3 commits into from
Dec 22, 2017

Conversation

dmalan
Copy link
Member

@dmalan dmalan commented Dec 20, 2017

No description provided.

@dmalan dmalan self-assigned this Dec 20, 2017
@dmalan
Copy link
Member Author

dmalan commented Dec 20, 2017

@crossroads1112 @brianyu28 @kzidane per email discussion!

@cmlsharp cmlsharp closed this Dec 20, 2017
@cmlsharp cmlsharp reopened this Dec 20, 2017
@cmlsharp
Copy link
Contributor

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 sys.path).

@dmalan
Copy link
Member Author

dmalan commented Dec 20, 2017

@crossroads1112 hm, do we really want to swallow in __init__.py any exceptions that our own code accidentally raises? atm, if any such exception is raised, that should indicate a bug in the library, which we're best off seeing in the wild?

@cmlsharp
Copy link
Contributor

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.

path = sys.path
sys.path = ...
try:
    import foo
    import bar
    # ...
finally:
    sys.path = path

if an exception is raised, the cleanup code gets run, but the exception still propagates.

@dmalan
Copy link
Member Author

dmalan commented Dec 21, 2017

TIL. How's it look now?

if fullname == "cs50.SQL":
return self
return None
class CustomImporter(object):
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not opposed.

from .cs50 import eprint, get_char, get_float, get_int, get_string
try:
from .cs50 import get_long
except:
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL


# In case student has files that shadow packages
sys.path = [p for p in sys.path if p not in ("", os.getcwd())]
Copy link
Contributor

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.

Copy link
Member Author

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.

@dmalan
Copy link
Member Author

dmalan commented Dec 22, 2017

:shipit:, @crossroads1112? 71b92c6

@dmalan dmalan merged commit f70d6e0 into develop Dec 22, 2017
@dmalan dmalan deleted the sys.path branch December 22, 2017 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants