File tree 3 files changed +48
-1
lines changed 3 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,19 @@ export const createFunction = (self, name) => {
45
45
const SetFunction = typedSet ( { typeof : "function" } ) ;
46
46
const SetString = typedSet ( { typeof : "string" } ) ;
47
47
48
+ const inputFailure = `
49
+ import builtins
50
+ def input(prompt=""):
51
+ raise Exception("\\n ".join([
52
+ "input() doesn't work when PyScript runs in the main thread.",
53
+ "Consider using the worker attribute: https://docs.pyscript.net/2023.11.1/user-guide/workers/"
54
+ ]))
55
+
56
+ builtins.input = input
57
+ del builtins
58
+ del input
59
+ ` ;
60
+
48
61
export const hooks = {
49
62
main : {
50
63
/** @type {Set<function> } */
@@ -60,7 +73,7 @@ export const hooks = {
60
73
/** @type {Set<function> } */
61
74
onAfterRunAsync : new SetFunction ( ) ,
62
75
/** @type {Set<string> } */
63
- codeBeforeRun : new SetString ( ) ,
76
+ codeBeforeRun : new SetString ( [ inputFailure ] ) ,
64
77
/** @type {Set<string> } */
65
78
codeBeforeRunAsync : new SetString ( ) ,
66
79
/** @type {Set<string> } */
Original file line number Diff line number Diff line change
1
+ <!doctype html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
6
+ < title > PyScript Next</ title >
7
+ < script >
8
+ addEventListener ( "py:ready" , console . log ) ;
9
+ </ script >
10
+ < link rel ="stylesheet " href ="../dist/core.css ">
11
+ < script type ="module " src ="../dist/core.js "> </ script >
12
+ </ head >
13
+ < body >
14
+ < py-script >
15
+ input("what's your name?")
16
+ </ py-script >
17
+ < mpy-script >
18
+ input("what's your name?")
19
+ </ mpy-script >
20
+ </ body >
21
+ </ html >
Original file line number Diff line number Diff line change @@ -93,6 +93,19 @@ def test_print(self):
93
93
)
94
94
assert self .console .log .lines [- 1 ] == "hello pyscript"
95
95
96
+ @only_main
97
+ def test_input_exception (self ):
98
+ self .pyscript_run (
99
+ """
100
+ <script type="py">
101
+ input("what's your name?")
102
+ </script>
103
+ """
104
+ )
105
+ self .check_py_errors (
106
+ "Exception: input() doesn't work when PyScript runs in the main thread."
107
+ )
108
+
96
109
@skip_worker ("NEXT: exceptions should be displayed in the DOM" )
97
110
def test_python_exception (self ):
98
111
self .pyscript_run (
You can’t perform that action at this time.
0 commit comments