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

Commit4e63ed3

Browse files
committed
More cleanup of worker.py, some renaming
1 parentffd8846 commit4e63ed3

File tree

6 files changed

+39
-30
lines changed

6 files changed

+39
-30
lines changed

‎core/generate_static_files.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
fromcore.textimportpages,get_pages,chapters
3131
fromcore.utilsimportsite_packages
32-
fromcore.workers.workerimportrun_code
32+
fromcore.workers.workerimportcheck_entry
3333

3434
str("import sentry_sdk after core.utils for stubs")
3535
importsentry_sdk# noqa imported lazily
@@ -55,7 +55,7 @@ def run_steps():
5555
step_name=step_name,
5656
)
5757

58-
run_code(entry,input_callback=None,result_callback=lambda_:0)
58+
check_entry(entry,input_callback=None,result_callback=lambda_:0)
5959

6060

6161
defget_roots():

‎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_internal_errors
8+
fromcore.workers.workerimportcheck_entry_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_internal_errors(entry,input_queue.get,result_queue.put)
93+
check_entry_catch_internal_errors(entry,input_queue.get,result_queue.put)

‎core/workers/worker.py‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def execute(code_obj):
2626
returnTracebackSerializer().format_exception(e)
2727

2828

29-
defrunner(code_source,code):
29+
defrun_code(code_source,code):
3030
ifcode_source=="shell":
3131
mode="single"
3232
code+="\n"# Allow compiling single-line compound statements
@@ -52,14 +52,15 @@ def runner(code_source, code):
5252
print_friendly_syntax_error(e)
5353
return {}
5454

55-
birdseye_objects=None
55+
result={}
5656

5757
ifcode_source=="snoop":
5858
fromcore.workers.snoopimportexec_snoop
5959
traceback_info=exec_snoop(filename,code,code_obj)
6060
elifcode_source=="birdseye":
6161
fromcore.workers.birdseyeimportexec_birdseye
62-
traceback_info,birdseye_objects=exec_birdseye(filename,code)
62+
63+
traceback_info,result["birdseye_objects"]=exec_birdseye(filename,code)
6364
else:
6465
traceback_info=execute(code_obj)
6566

@@ -74,52 +75,46 @@ def runner(code_source, code):
7475
)
7576
output_buffer.parts.append(traceback_info)
7677

77-
returnbirdseye_objects
78+
returnresult
7879

7980

80-
defrun_code_catch_internal_errors(entry,input_callback,result_callback):
81+
defcheck_entry_catch_internal_errors(entry,input_callback,result_callback):
8182
try:
82-
run_code(entry,input_callback,result_callback)
83+
check_entry(entry,input_callback,result_callback)
8384
exceptException:
8485
result_callback(internal_error_result())
8586

8687

87-
defrun_code(entry,input_callback,result_callback):
88+
defcheck_entry(entry,input_callback,result_callback):
8889
ifhasattr(entry,"to_py"):
8990
entry=entry.to_py()
9091

9192
patch_stdin(input_callback,result_callback)
9293

9394
withredirect_stdout(output_buffer.stdout),redirect_stderr(output_buffer.stderr):
94-
birdseye_objects=runner(entry["source"],entry["input"])
95+
run_results=run_code(entry["source"],entry["input"])
9596

9697
output=output_buffer.string()
9798

9899
page=pages[entry["page_slug"]]
99100
step_cls=page.get_step(entry["step_name"])
100-
step_instance=step_cls(entry["input"],output,entry["source"],console)
101101

102102
step_result=False
103103
ifentry["step_name"]!="final_text":
104+
step_instance=step_cls(entry["input"],output,entry["source"],console)
104105
try:
105106
step_result=step_instance.check_with_messages()
106107
exceptSyntaxError:
107108
pass
108109

109-
messages= []
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"])
116-
117-
messages= [highlighted_markdown(message)formessageinmessages]
110+
step_result=normalise_step_result(step_result)
111+
passed=step_result["passed"]
112+
messages= [highlighted_markdown(message)formessageinstep_result["messages"]]
118113

119114
ifpassed:
120115
prediction=dict(
121-
choices=getattr(step_cls,"predicted_output_choices",None),
122-
answer=getattr(step_cls,"correct_output",None),
116+
choices=step_cls.predicted_output_choices,
117+
answer=step_cls.correct_output,
123118
)
124119
else:
125120
prediction=None
@@ -129,12 +124,26 @@ def run_code(entry, input_callback, result_callback):
129124
passed=passed,
130125
messages=messages,
131126
output=output,
132-
birdseye_objects=birdseye_objects,
133127
prediction=prediction,
128+
**run_results,
134129
)
135130
)
136131

137132

133+
defnormalise_step_result(step_result):
134+
ifnotisinstance(step_result,dict):
135+
assertisinstance(step_result,bool)
136+
step_result=dict(passed=step_result,messages=[])
137+
138+
step_result.setdefault("passed",False)
139+
140+
messages=step_result.setdefault("messages", [])
141+
if"message"instep_result:
142+
messages.append(step_result.pop("message"))
143+
144+
returnstep_result
145+
146+
138147
defpatch_stdin(input_callback,result_callback):
139148
defreadline(*_):
140149
result_callback(make_result(awaiting_input=True))

‎frontend/src/Worker.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function loadPyodideAndPackages() {
3939
pyodide.globals.get("load_package_buffer")(buffer);
4040
console.timeEnd("load_package_buffer(buffer)")
4141

42-
runCodeCatchErrors=pyodide.globals.get("run_code_catch_internal_errors");
42+
runCodeCatchErrors=pyodide.globals.get("check_entry_catch_internal_errors");
4343
console.assert(runCodeCatchErrors);
4444
}
4545

‎frontend/src/load.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99

1010
defload_package_buffer(buffer):
11-
globalrun_code_catch_internal_errors
11+
globalcheck_entry_catch_internal_errors
1212
fd=io.BytesIO(buffer.to_py())
1313
withtarfile.TarFile(fileobj=fd)aszf:
1414
zf.extractall(package_path)
1515

16-
fromcore.workers.workerimportrun_code_catch_internal_errors# noqa trigger imports
16+
fromcore.workers.workerimportcheck_entry_catch_internal_errors# noqa trigger imports
1717
print("Python core ready!")

‎tests/test_steps.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
fromcore.textimportpages
99
fromcore.utilsimporthighlighted_markdown
10-
fromcore.workers.workerimportrun_code
10+
fromcore.workers.workerimportcheck_entry
1111

1212

1313
deftest_steps():
@@ -37,7 +37,7 @@ def result_callback(r):
3737
nonlocalresponse
3838
response=r
3939

40-
run_code(entry,input_callback=None,result_callback=result_callback)
40+
check_entry(entry,input_callback=None,result_callback=result_callback)
4141
normalise_response(response,is_message,substep)
4242

4343
transcript_item=dict(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp