Skip to content

Commit 15dd3fc

Browse files
committed
Put imported modules in linecache
1 parent 6d4d60f commit 15dd3fc

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

backend/main/tests/test_transcript.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@
934934
"result": [
935935
{
936936
"color": "red",
937-
"text": " print('---')\n ^\nIndentationError: unindent does not match any outer indentation level\nat line 3\n\nAn `IndentationError` occurs when a given line of code is\nnot indented (aligned vertically with other lines) as expected.\n\n"
937+
"text": " print('---')\n ^\nIndentationError: unindent does not match any outer indentation level\nat line 3\n\nAn `IndentationError` occurs when a given line of code is\nnot indented (aligned vertically with other lines) as expected.\n\n"
938938
},
939939
{
940940
"color": "red",
@@ -2685,7 +2685,7 @@
26852685
"result": [
26862686
{
26872687
"color": "red",
2688-
"text": "SyntaxError: expression cannot contain assignment, perhaps you meant \"==\"?\nat line 1\n\nA `SyntaxError` occurs when Python cannot understand your code.\n\n"
2688+
"text": " print(1 + 2 = 3)\n ^\nSyntaxError: expression cannot contain assignment, perhaps you meant \"==\"?\nat line 1\n\nA `SyntaxError` occurs when Python cannot understand your code.\n\n"
26892689
},
26902690
{
26912691
"color": "red",
@@ -5158,7 +5158,7 @@
51585158
"result": [
51595159
{
51605160
"color": "white",
5161-
"text": "['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']"
5161+
"text": "['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']"
51625162
},
51635163
{
51645164
"color": "white",
@@ -14998,7 +14998,7 @@
1499814998
"result": [
1499914999
{
1500015000
"color": "red",
15001-
"text": " is_friend = name == \"Alice\" or\n ^\nSyntaxError: invalid syntax\nat line 1\n\nA `SyntaxError` occurs when Python cannot understand your code.\n\n"
15001+
"text": " is_friend = name == \"Alice\" or\n ^\nSyntaxError: invalid syntax\nat line 1\n\nA `SyntaxError` occurs when Python cannot understand your code.\n\n"
1500215002
},
1500315003
{
1500415004
"color": "red",
@@ -15515,7 +15515,7 @@
1551515515
"result": [
1551615516
{
1551715517
"color": "red",
15518-
"text": " \"XOX\n ^\nSyntaxError: EOL while scanning string literal\nat line 7\n\nA `SyntaxError` occurs when Python cannot understand your code.\n\n"
15518+
"text": " \"XOX\n ^\nSyntaxError: EOL while scanning string literal\nat line 7\n\nA `SyntaxError` occurs when Python cannot understand your code.\n\n"
1551915519
},
1552015520
{
1552115521
"color": "red",

backend/main/workers/limits.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import inspect
2+
import linecache
23
import os
4+
import sys
35
from functools import lru_cache
46
from importlib import import_module
57

@@ -29,15 +31,6 @@ def set_limits():
2931
import resource
3032
destroy_dangerous_functions()
3133

32-
usage = resource.getrusage(resource.RUSAGE_SELF)
33-
34-
# TODO tests can exceed this time since the process is not restarted, causing failure
35-
max_time = int(usage.ru_utime + usage.ru_stime) + 2
36-
try:
37-
resource.setrlimit(resource.RLIMIT_CPU, (max_time, max_time))
38-
except ValueError:
39-
pass
40-
4134
patch_cwd()
4235

4336
# Trigger imports before limiting access to files
@@ -59,6 +52,22 @@ def set_limits():
5952
except NameError as e:
6053
list(get_suggestions_for_exception(e, e.__traceback__))
6154

55+
# Put all modules in linecache so that tracebacks work
56+
for mod in list(sys.modules.values()):
57+
try:
58+
linecache.getlines(mod.__file__, mod.__dict__)
59+
except Exception:
60+
pass
61+
62+
usage = resource.getrusage(resource.RUSAGE_SELF)
63+
64+
# TODO tests can exceed this time since the process is not restarted, causing failure
65+
max_time = int(usage.ru_utime + usage.ru_stime) + 2
66+
try:
67+
resource.setrlimit(resource.RLIMIT_CPU, (max_time, max_time))
68+
except ValueError:
69+
pass
70+
6271
resource.setrlimit(resource.RLIMIT_NOFILE, (0, 0))
6372

6473

0 commit comments

Comments
 (0)