Skip to content

Commit c79ce92

Browse files
committed
code maintenance
* disable exception chaining in some cases * `debug=False` in `path_deploy_http()` * refine man task clean up logic in thread session
1 parent a92a26e commit c79ce92

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

pywebio/io_ctrl.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,10 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onc
349349
try:
350350
onblur_name, error_msg = v_res
351351
except Exception:
352+
# Use `raise Exception from None` to disable exception chaining
353+
# see: https://docs.python.org/3/tutorial/errors.html#exception-chaining
352354
raise ValueError("The `validate` function for input group must "
353-
"return `(name, error_msg)` when validation failed.")
355+
"return `(name, error_msg)` when validation failed.") from None
354356

355357
send_msg('update_input', dict(target_name=onblur_name, attributes={
356358
'valid_status': False,

pywebio/platform/fastapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def webio_routes(applications, cdn=True, allowed_origins=None, check_origin=None
121121
Missing dependency package `websockets` for websocket support.
122122
You can install it with the following command:
123123
pip install websockets
124-
""".strip(), n=8))
124+
""".strip(), n=8)) from None
125125

126126
applications = make_applications(applications)
127127
for target in applications.values():
@@ -200,7 +200,7 @@ def asgi_app(applications, cdn=True, static_dir=None, debug=False, allowed_origi
200200
Missing dependency package `aiofiles` for static file serving.
201201
You can install it with the following command:
202202
pip install aiofiles
203-
""".strip(), n=8))
203+
""".strip(), n=8)) from None
204204
cdn = cdn_validation(cdn, 'warn')
205205
if cdn is False:
206206
cdn = 'pywebio_static'

pywebio/platform/path_deploy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _path_deploy(base, port=0, host='', static_dir=None, cdn=True, max_payload_s
219219
def path_deploy(base, port=0, host='',
220220
index=True, static_dir=None,
221221
reconnect_timeout=0,
222-
cdn=True, debug=True,
222+
cdn=True, debug=False,
223223
allowed_origins=None, check_origin=None,
224224
max_payload_size='200M',
225225
**tornado_app_settings):
@@ -285,7 +285,7 @@ def get_app(self):
285285

286286
def path_deploy_http(base, port=0, host='',
287287
index=True, static_dir=None,
288-
cdn=True, debug=True,
288+
cdn=True, debug=False,
289289
allowed_origins=None, check_origin=None,
290290
session_expire_seconds=None,
291291
session_cleanup_interval=None,

pywebio/session/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def pop_scope(self):
8686
try:
8787
return self.scope_stack[task_id].pop()
8888
except IndexError:
89-
raise ValueError("ROOT Scope can't pop")
89+
raise ValueError("ROOT Scope can't pop") from None
9090

9191
def push_scope(self, name):
9292
"""进入新scope"""

pywebio/session/threadbased.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ def main_task(target):
9292
if t.is_alive() and t is not threading.current_thread():
9393
t.join()
9494

95-
if self.need_keep_alive():
96-
from ..session import hold
97-
hold()
98-
else:
99-
try:
95+
try:
96+
if self.need_keep_alive():
97+
from ..session import hold
98+
hold()
99+
else:
100100
self.send_task_command(dict(command='close_session'))
101-
except SessionClosedException:
102-
pass
103-
self._trigger_close_event()
104-
self.close()
101+
except SessionException: # ignore SessionException error
102+
pass
103+
finally:
104+
self._trigger_close_event()
105+
self.close()
105106

106107
thread = threading.Thread(target=main_task, kwargs=dict(target=target),
107108
daemon=True, name='main_task')

test/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def run_test(server_func, test_func, address='http://localhost:8080?_pywebio_deb
3939
print(USAGE.format(name=sys.argv[0]))
4040
return
4141

42-
if len(sys.argv) != 2:
42+
if len(sys.argv) != 2: # when execute test script with no argument, only start server
4343
try:
4444
server_func()
4545
except KeyboardInterrupt:
@@ -54,6 +54,7 @@ def run_test(server_func, test_func, address='http://localhost:8080?_pywebio_deb
5454
proc = subprocess.Popen(['coverage', 'run', '--source', 'pywebio', '--append',
5555
sys.argv[0]], stdout=sys.stdout, stderr=subprocess.STDOUT, text=True)
5656
elif sys.argv[-1] == 'debug':
57+
# start server as sub process
5758
proc = subprocess.Popen(['python3', sys.argv[0]], stdout=sys.stdout, stderr=subprocess.STDOUT, text=True)
5859

5960
browser = None

0 commit comments

Comments
 (0)