Skip to content

Commit a8af4a5

Browse files
committed
Import all friendly submodules before settings limits
1 parent 08a8cbf commit a8af4a5

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

backend/main/tests/test_transcript.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3517,7 +3517,7 @@
35173517
]
35183518
}
35193519
],
3520-
"friendly": "<p>An <code>IndexError</code> occurs when you are try to get an item from a list,\na tuple, or a similar object (sequence), by using an index which\ndoes not exists; typically, this is because the index you give\nis greater than the length of the sequence.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
3520+
"friendly": "<p>An <code>IndexError</code> occurs when you are try to get an item from a list,\na tuple, or a similar object (sequence), by using an index which\ndoes not exists; typically, this is because the index you give\nis greater than the length of the sequence.</p>\n<p>You have tried to get the item with index <code>4</code> of <code>words</code>,\na <code>list</code> of length <code>4</code>.\nThe largest valid index of <code>words</code> is <code>3</code>.</p>",
35213521
"tail": ""
35223522
}
35233523
]
@@ -4125,7 +4125,7 @@
41254125
]
41264126
}
41274127
],
4128-
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
4128+
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>The parenthesis <code>()</code> following <code>f</code> are interpreted\nby Python as a function call for <code>f</code>.\nHowever, <code>f</code> is not a function but an object of type <code>str</code>.</p>",
41294129
"tail": ""
41304130
}
41314131
]
@@ -4226,7 +4226,7 @@
42264226
]
42274227
}
42284228
],
4229-
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
4229+
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>No information is known about this exception.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>\n<p>If you are using the Friendly console, use <code>www()</code> to\ndo an Internet search for this particular case.</p>",
42304230
"tail": ""
42314231
}
42324232
]
@@ -4316,7 +4316,7 @@
43164316
]
43174317
}
43184318
],
4319-
"friendly": "<p>An <code>AttributeError</code> occurs when the code contains something like\n <code>object.x</code>\nand <code>x</code> is not a method or attribute (variable) belonging to <code>object</code>.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
4319+
"friendly": "<p>An <code>AttributeError</code> occurs when the code contains something like\n <code>object.x</code>\nand <code>x</code> is not a method or attribute (variable) belonging to <code>object</code>.</p>\n<p>The object <code>word</code> has no attribute named <code>append</code>.\nThe following are some of its known attributes:\n<code>capitalize, casefold, center, count, encode, endswith, expandtabs, find, format, ...</code>.</p>",
43204320
"tail": ""
43214321
}
43224322
]
@@ -5038,7 +5038,7 @@
50385038
"variables": []
50395039
}
50405040
],
5041-
"friendly": "<p>An <code>AttributeError</code> occurs when the code contains something like\n <code>object.x</code>\nand <code>x</code> is not a method or attribute (variable) belonging to <code>object</code>.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
5041+
"friendly": "<p>An <code>AttributeError</code> occurs when the code contains something like\n <code>object.x</code>\nand <code>x</code> is not a method or attribute (variable) belonging to <code>object</code>.</p>\n<p>The object <code>'Python'</code> has no attribute named <code>append</code>.\nThe following are some of its known attributes:\n<code>capitalize, casefold, center, count, encode, endswith, expandtabs, find, format, ...</code>.</p>",
50425042
"tail": ""
50435043
}
50445044
]
@@ -5425,7 +5425,7 @@
54255425
]
54265426
}
54275427
],
5428-
"friendly": "<p>An <code>IndexError</code> occurs when you are try to get an item from a list,\na tuple, or a similar object (sequence), by using an index which\ndoes not exists; typically, this is because the index you give\nis greater than the length of the sequence.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
5428+
"friendly": "<p>An <code>IndexError</code> occurs when you are try to get an item from a list,\na tuple, or a similar object (sequence), by using an index which\ndoes not exists; typically, this is because the index you give\nis greater than the length of the sequence.</p>",
54295429
"tail": ""
54305430
}
54315431
]
@@ -5797,7 +5797,7 @@
57975797
]
57985798
}
57995799
],
5800-
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
5800+
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>You tried to concatenate (add) two different types of objects:\na string (<code>str</code>) and an integer (<code>int</code>).</p>",
58015801
"tail": ""
58025802
}
58035803
]
@@ -6107,7 +6107,7 @@
61076107
"variables": []
61086108
}
61096109
],
6110-
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
6110+
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>You tried to add two incompatible types of objects:\nan integer (<code>int</code>) and a string (<code>str</code>).</p>",
61116111
"tail": ""
61126112
}
61136113
]
@@ -15995,7 +15995,7 @@
1599515995
"variables": []
1599615996
}
1599715997
],
15998-
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>Internal error for Friendly-traceback.\nPlease report this example to\nhttps://github.com/aroberge/friendly-traceback/issues</p>",
15998+
"friendly": "<p>A <code>TypeError</code> is usually caused by trying\nto combine two incompatible types of objects,\nby calling a function with the wrong type of object,\nor by trying to do an operation not allowed on a given type of object.</p>\n<p>You tried to add two incompatible types of objects:\nan integer (<code>int</code>) and a string (<code>str</code>).\nPerhaps you forgot to convert the string <code>'34'</code> into an integer (<code>int</code>).</p>",
1599915999
"tail": ""
1600016000
}
1600116001
]

backend/main/workers/limits.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import sys
55
from functools import lru_cache
66
from importlib import import_module
7-
8-
from main.utils import get_suggestions_for_exception
7+
import friendly.runtime_errors
8+
import friendly.syntax_errors
9+
from main.workers.tracebacks import TracebackSerializer
10+
from main.workers.utils import import_submodules
911

1012

1113
def patch_cwd():
@@ -35,22 +37,14 @@ def set_limits():
3537

3638
# Trigger imports before limiting access to files
3739
from main.workers import birdseye, snoop # noqa
38-
from friendly_traceback.runtime_errors import ( # noqa
39-
type_error,
40-
attribute_error,
41-
stdlib,
42-
)
43-
from friendly_traceback.syntax_errors import ( # noqa
44-
analyze_syntax,
45-
line_analyzer,
46-
message_analyzer,
47-
source_analyzer,
48-
)
40+
import_submodules(friendly.runtime_errors)
41+
import_submodules(friendly.syntax_errors)
4942

50-
try:
51-
sdfsdfsdfsd # noqa
52-
except NameError as e:
53-
list(get_suggestions_for_exception(e, e.__traceback__))
43+
for bad_code in ["nameerror", "syntax error", "1 + '2'", "list.set", "[][0]", "{}[0]"]:
44+
try:
45+
eval(bad_code) # noqa
46+
except Exception as e:
47+
TracebackSerializer().format_exception(e)
5448

5549
# Put all modules in linecache so that tracebacks work
5650
for mod in list(sys.modules.values()):

backend/main/workers/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib
2+
import pkgutil
13
import json
24
import multiprocessing.queues
35
import sys
@@ -127,3 +129,20 @@ def dumps(self, x):
127129

128130

129131
multiprocessing.queues._ForkingPickler = json_pickler = JsonPickler()
132+
133+
134+
def import_submodules(package, recursive=True):
135+
"""
136+
Import all submodules of a module, recursively, including subpackages
137+
138+
https://stackoverflow.com/questions/3365740/how-to-import-all-submodules
139+
"""
140+
if isinstance(package, str):
141+
package = importlib.import_module(package)
142+
results = {}
143+
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
144+
full_name = package.__name__ + '.' + name
145+
results[full_name] = importlib.import_module(full_name)
146+
if recursive and is_pkg:
147+
results.update(import_submodules(full_name))
148+
return results

0 commit comments

Comments
 (0)