Skip to content

Commit 7f30db9

Browse files
authored
Merge pull request #47 from cs50/develop
prevent shadowing of package files
2 parents 486715c + f70d6e0 commit 7f30db9

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
package_dir={"": "src"},
1717
packages=["cs50"],
1818
url="https://github.com/cs50/python-cs50",
19-
version="2.3.2"
19+
version="2.3.3"
2020
)

src/cs50/__init__.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1+
import os
12
import sys
23

3-
from .cs50 import eprint, get_char, get_float, get_int, get_string
4-
try:
5-
from .cs50 import get_long
6-
except:
7-
pass
8-
9-
from . import flask
10-
114

125
class CustomImporter(object):
136
"""
@@ -31,4 +24,28 @@ def load_module(self, name):
3124
return SQL
3225

3326

34-
sys.meta_path.append(CustomImporter())
27+
try:
28+
29+
# Save student's sys.path
30+
path = sys.path[:]
31+
32+
# In case student has files that shadow packages
33+
sys.path = [p for p in sys.path if p not in ("", os.getcwd())]
34+
35+
# Import cs50_*
36+
from .cs50 import eprint, get_char, get_float, get_int, get_string
37+
try:
38+
from .cs50 import get_long
39+
except Exception:
40+
pass
41+
42+
# Replace Flask's logger
43+
from . import flask
44+
45+
# Lazily load CS50.SQL
46+
sys.meta_path.append(CustomImporter())
47+
48+
finally:
49+
50+
# Restore student's sys.path (just in case library raised an exception that caller caught)
51+
sys.path = path

0 commit comments

Comments
 (0)