فهرست منبع

code maintenance

* disable exception chaining in some cases
* `debug=False` in `path_deploy_http()`
* refine man task clean up logic in thread session
wangweimin 3 سال پیش
والد
کامیت
c79ce927e3
6فایلهای تغییر یافته به همراه20 افزوده شده و 16 حذف شده
  1. 3 1
      pywebio/io_ctrl.py
  2. 2 2
      pywebio/platform/fastapi.py
  3. 2 2
      pywebio/platform/path_deploy.py
  4. 1 1
      pywebio/session/base.py
  5. 10 9
      pywebio/session/threadbased.py
  6. 2 1
      test/util.py

+ 3 - 1
pywebio/io_ctrl.py

@@ -349,8 +349,10 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onc
                         try:
                             onblur_name, error_msg = v_res
                         except Exception:
+                            # Use `raise Exception from None` to disable exception chaining
+                            # see: https://docs.python.org/3/tutorial/errors.html#exception-chaining
                             raise ValueError("The `validate` function for input group must "
-                                             "return `(name, error_msg)` when validation failed.")
+                                             "return `(name, error_msg)` when validation failed.") from None
 
                         send_msg('update_input', dict(target_name=onblur_name, attributes={
                             'valid_status': False,

+ 2 - 2
pywebio/platform/fastapi.py

@@ -121,7 +121,7 @@ def webio_routes(applications, cdn=True, allowed_origins=None, check_origin=None
         Missing dependency package `websockets` for websocket support.
         You can install it with the following command:
             pip install websockets
-        """.strip(), n=8))
+        """.strip(), n=8)) from None
 
     applications = make_applications(applications)
     for target in applications.values():
@@ -200,7 +200,7 @@ def asgi_app(applications, cdn=True, static_dir=None, debug=False, allowed_origi
         Missing dependency package `aiofiles` for static file serving.
         You can install it with the following command:
             pip install aiofiles
-        """.strip(), n=8))
+        """.strip(), n=8)) from None
     cdn = cdn_validation(cdn, 'warn')
     if cdn is False:
         cdn = 'pywebio_static'

+ 2 - 2
pywebio/platform/path_deploy.py

@@ -219,7 +219,7 @@ def _path_deploy(base, port=0, host='', static_dir=None, cdn=True, max_payload_s
 def path_deploy(base, port=0, host='',
                 index=True, static_dir=None,
                 reconnect_timeout=0,
-                cdn=True, debug=True,
+                cdn=True, debug=False,
                 allowed_origins=None, check_origin=None,
                 max_payload_size='200M',
                 **tornado_app_settings):
@@ -285,7 +285,7 @@ def path_deploy(base, port=0, host='',
 
 def path_deploy_http(base, port=0, host='',
                      index=True, static_dir=None,
-                     cdn=True, debug=True,
+                     cdn=True, debug=False,
                      allowed_origins=None, check_origin=None,
                      session_expire_seconds=None,
                      session_cleanup_interval=None,

+ 1 - 1
pywebio/session/base.py

@@ -86,7 +86,7 @@ class Session:
         try:
             return self.scope_stack[task_id].pop()
         except IndexError:
-            raise ValueError("ROOT Scope can't pop")
+            raise ValueError("ROOT Scope can't pop") from None
 
     def push_scope(self, name):
         """进入新scope"""

+ 10 - 9
pywebio/session/threadbased.py

@@ -92,16 +92,17 @@ class ThreadBasedSession(Session):
                     if t.is_alive() and t is not threading.current_thread():
                         t.join()
 
-                if self.need_keep_alive():
-                    from ..session import hold
-                    hold()
-                else:
-                    try:
+                try:
+                    if self.need_keep_alive():
+                        from ..session import hold
+                        hold()
+                    else:
                         self.send_task_command(dict(command='close_session'))
-                    except SessionClosedException:
-                        pass
-                self._trigger_close_event()
-                self.close()
+                except SessionException:  # ignore SessionException error
+                    pass
+                finally:
+                    self._trigger_close_event()
+                    self.close()
 
         thread = threading.Thread(target=main_task, kwargs=dict(target=target),
                                   daemon=True, name='main_task')

+ 2 - 1
test/util.py

@@ -39,7 +39,7 @@ def run_test(server_func, test_func, address='http://localhost:8080?_pywebio_deb
         print(USAGE.format(name=sys.argv[0]))
         return
 
-    if len(sys.argv) != 2:
+    if len(sys.argv) != 2:  # when execute test script with no argument, only start server
         try:
             server_func()
         except KeyboardInterrupt:
@@ -54,6 +54,7 @@ def run_test(server_func, test_func, address='http://localhost:8080?_pywebio_deb
         proc = subprocess.Popen(['coverage', 'run', '--source', 'pywebio', '--append',
                                  sys.argv[0]], stdout=sys.stdout, stderr=subprocess.STDOUT, text=True)
     elif sys.argv[-1] == 'debug':
+        # start server as sub process
         proc = subprocess.Popen(['python3', sys.argv[0]], stdout=sys.stdout, stderr=subprocess.STDOUT, text=True)
 
     browser = None