Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit205e108

Browse files
committed
Simplify run_code, especially stream patching and checking step
Create load.py, import with raw-loader
1 parente4691f0 commit205e108

File tree

7 files changed

+102
-78
lines changed

7 files changed

+102
-78
lines changed

‎core/text.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
fromtextwrapimportdedent,indent
1414
fromtokenizeimportUntokenizer,generate_tokens
1515
fromtypesimportFunctionType
16-
fromtypingimportType,Union,List,get_type_hints
16+
fromtypingimportUnion,List,get_type_hints
1717

1818
importpygments
1919
fromastcheckimportis_ast_like
@@ -294,15 +294,7 @@ def step_dicts(self):
294294

295295

296296
classPage(metaclass=PageMeta):
297-
@classmethod
298-
defcheck_step(cls,code_entry,output,console):
299-
step_cls:Type[Step]=cls.get_step(code_entry['step_name'])
300-
step=step_cls(code_entry['input'],output,code_entry['source'],console)
301-
try:
302-
returnstep.check_with_messages()
303-
exceptSyntaxError:
304-
returnFalse
305-
297+
pass
306298

307299
classDisallowed:
308300
def__init__(self,template,*,label="",message="",max_count=0,predicate=lambdan:True,function_only=False):

‎core/workers/master.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
fromthreadingimportThread,RLock
66

77
fromcore.workers.utilsimportinternal_error_result
8-
fromcore.workers.workerimportrun_code_catch_errors
8+
fromcore.workers.workerimportrun_code_catch_internal_errors
99

1010
log=logging.getLogger(__name__)
1111

@@ -90,4 +90,4 @@ def run_code_entry(entry):
9090
defworker_loop(task_queue,input_queue,result_queue):
9191
whileTrue:
9292
entry=task_queue.get()
93-
run_code_catch_errors(entry,input_queue.get,result_queue.put)
93+
run_code_catch_internal_errors(entry,input_queue.get,result_queue.put)

‎core/workers/worker.py‎

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importlogging
44
importsys
55
fromcodeimportInteractiveConsole
6+
fromcontextlibimportredirect_stdout,redirect_stderr
67

78
importstack_data
89

@@ -76,7 +77,7 @@ def runner(code_source, code):
7677
returnbirdseye_objects
7778

7879

79-
defrun_code_catch_errors(entry,input_callback,result_callback):
80+
defrun_code_catch_internal_errors(entry,input_callback,result_callback):
8081
try:
8182
run_code(entry,input_callback,result_callback)
8283
exceptException:
@@ -87,63 +88,66 @@ def run_code(entry, input_callback, result_callback):
8788
ifhasattr(entry,"to_py"):
8889
entry=entry.to_py()
8990

90-
defreadline(*_):
91-
result_callback(make_result(awaiting_input=True))
92-
result=input_callback()
93-
ifnotisinstance(result,str):
94-
whileTrue:
95-
pass# wait for the interrupt
96-
returnresult
91+
patch_stdin(input_callback,result_callback)
9792

98-
sys.stdin.readline=readline
93+
withredirect_stdout(output_buffer.stdout),redirect_stderr(output_buffer.stderr):
94+
birdseye_objects=runner(entry["source"],entry["input"])
9995

100-
defpatched_input(prompt=""):
101-
print(prompt,end="")
102-
returnsys.stdin.readline().rstrip("\n")
96+
output=output_buffer.string()
10397

104-
builtins.input=patched_input
98+
page=pages[entry["page_slug"]]
99+
step_cls=page.get_step(entry["step_name"])
100+
step_instance=step_cls(entry["input"],output,entry["source"],console)
105101

106-
orig_stdout=sys.stdout
107-
orig_stderr=sys.stderr
108-
try:
109-
sys.stdout=output_buffer.stdout
110-
sys.stderr=output_buffer.stderr
111-
birdseye_objects=runner(entry['source'],entry['input'])
112-
finally:
113-
sys.stdout=orig_stdout
114-
sys.stderr=orig_stderr
102+
step_result=False
103+
ifentry["step_name"]!="final_text":
104+
try:
105+
step_result=step_instance.check_with_messages()
106+
exceptSyntaxError:
107+
pass
115108

116109
messages= []
117-
passed=False
118-
output=output_buffer.string()
119-
120-
page=pages[entry['page_slug']]
121-
step=page.get_step(entry["step_name"])
122-
123-
ifentry['step_name']!="final_text":
124-
step_result=page.check_step(entry,output,console)
125-
ifisinstance(step_result,dict):
126-
passed=step_result.get("passed",False)
127-
messages=step_result.get("messages", [])
128-
if"message"instep_result:
129-
messages.append(step_result["message"])
130-
else:
131-
passed=step_result
110+
passed=step_result
111+
ifisinstance(step_result,dict):
112+
passed=step_result.get("passed",False)
113+
messages=step_result.get("messages", [])
114+
if"message"instep_result:
115+
messages.append(step_result["message"])
132116

133117
messages= [highlighted_markdown(message)formessageinmessages]
134118

135119
ifpassed:
136120
prediction=dict(
137-
choices=getattr(step,"predicted_output_choices",None),
138-
answer=getattr(step,"correct_output",None),
121+
choices=getattr(step_cls,"predicted_output_choices",None),
122+
answer=getattr(step_cls,"correct_output",None),
139123
)
140124
else:
141125
prediction=None
142126

143-
result_callback(make_result(
144-
passed=passed,
145-
messages=messages,
146-
output=output,
147-
birdseye_objects=birdseye_objects,
148-
prediction=prediction,
149-
))
127+
result_callback(
128+
make_result(
129+
passed=passed,
130+
messages=messages,
131+
output=output,
132+
birdseye_objects=birdseye_objects,
133+
prediction=prediction,
134+
)
135+
)
136+
137+
138+
defpatch_stdin(input_callback,result_callback):
139+
defreadline(*_):
140+
result_callback(make_result(awaiting_input=True))
141+
result=input_callback()
142+
ifnotisinstance(result,str):
143+
whileTrue:
144+
pass# wait for the interrupt
145+
returnresult
146+
147+
sys.stdin.readline=readline
148+
149+
defpatched_input(prompt=""):
150+
print(prompt,end="")
151+
returnsys.stdin.readline().rstrip("\n")
152+
153+
builtins.input=patched_input

‎frontend/package-lock.json‎

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎frontend/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
]
6161
},
6262
"devDependencies": {
63+
"raw-loader":"^4.0.2",
6364
"worker-loader":"^3.0.8"
6465
}
6566
}

‎frontend/src/Worker.js‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import*asComlinkfrom'comlink';
66
importpythonCoreUrlfrom"./python_core.tar"
7+
importloadPythonStringfrom"!!raw-loader!./load.py"
78

89
asyncfunctiongetPackageBuffer(){
910
constresponse=awaitfetch(pythonCoreUrl);
@@ -13,6 +14,8 @@ async function getPackageBuffer() {
1314
returnawaitresponse.arrayBuffer()
1415
}
1516

17+
letrunCodeCatchErrors;
18+
1619
asyncfunctionloadPyodideOnly(){
1720
console.time("importScripts pyodide")
1821
importScripts('https://cdn.jsdelivr.net/pyodide/v0.17.0/full/pyodide.js');
@@ -22,25 +25,7 @@ async function loadPyodideOnly() {
2225
awaitloadPyodide({indexURL:'https://cdn.jsdelivr.net/pyodide/v0.17.0/full/'});
2326
console.timeEnd("loadPyodide")
2427

25-
pyodide.runPython(`
26-
import io
27-
import tarfile
28-
import sys
29-
30-
package_path = "/tmp/package/"
31-
tarfile.TarFile.chown = lambda *_, **__: None
32-
33-
def load_package_buffer(buffer):
34-
global run_code_catch_errors
35-
fd = io.BytesIO(buffer.to_py())
36-
with tarfile.TarFile(fileobj=fd) as zf:
37-
zf.extractall(package_path)
38-
39-
sys.path.append(package_path)
40-
41-
from core.workers.worker import run_code_catch_errors # noqa trigger imports
42-
print("Python core ready!")
43-
`)
28+
pyodide.runPython(loadPythonString)
4429
}
4530

4631

@@ -53,6 +38,9 @@ async function loadPyodideAndPackages() {
5338
console.time("load_package_buffer(buffer)")
5439
pyodide.globals.get("load_package_buffer")(buffer);
5540
console.timeEnd("load_package_buffer(buffer)")
41+
42+
runCodeCatchErrors=pyodide.globals.get("run_code_catch_internal_errors");
43+
console.assert(runCodeCatchErrors);
5644
}
5745

5846
letpyodideReadyPromise=loadPyodideAndPackages();
@@ -96,7 +84,6 @@ class Runner {
9684
}
9785

9886
pyodide.setInterruptBuffer(interruptBuffer);
99-
construnCodeCatchErrors=pyodide.globals.get("run_code_catch_errors");
10087
constresultCallbackToObject=(result)=>this.resultCallback(toObject(result.toJs()));
10188
runCodeCatchErrors(entry,inputCallback,resultCallbackToObject)
10289
}

‎frontend/src/load.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
importio
2+
importtarfile
3+
importsys
4+
5+
package_path="/tmp/package/"
6+
sys.path.append(package_path)
7+
tarfile.TarFile.chown=lambda*_,**__:None
8+
9+
10+
defload_package_buffer(buffer):
11+
globalrun_code_catch_internal_errors
12+
fd=io.BytesIO(buffer.to_py())
13+
withtarfile.TarFile(fileobj=fd)aszf:
14+
zf.extractall(package_path)
15+
16+
fromcore.workers.workerimportrun_code_catch_internal_errors# noqa trigger imports
17+
print("Python core ready!")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp