From 8b955cbd323046c671f7f794c19f941c5ab67914 Mon Sep 17 00:00:00 2001 From: nggit <12218311+nggit@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:39:39 +0700 Subject: [PATCH 1/2] add httpout.exceptions --- examples/exc.py | 3 ++- httpout/exceptions.py | 7 +++++++ httpout/httpout.py | 4 +++- tests/test_http.py | 3 ++- 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 httpout/exceptions.py diff --git a/examples/exc.py b/examples/exc.py index c6994aa..980507f 100644 --- a/examples/exc.py +++ b/examples/exc.py @@ -1,12 +1,13 @@ from httpout import run +from httpout.exceptions import WebSocketException print('Hi') async def main(): - raise ValueError + raise WebSocketException('') run(main()) diff --git a/httpout/exceptions.py b/httpout/exceptions.py new file mode 100644 index 0000000..db05d55 --- /dev/null +++ b/httpout/exceptions.py @@ -0,0 +1,7 @@ +# Copyright (c) 2024 nggit + +from tremolo.exceptions import ( # noqa: F401 + WebSocketException, + WebSocketClientClosed, + WebSocketServerClosed +) diff --git a/httpout/httpout.py b/httpout/httpout.py index 4f78d8b..6dd4029 100644 --- a/httpout/httpout.py +++ b/httpout/httpout.py @@ -112,7 +112,9 @@ def ho_import(name, globals=None, locals=None, fromlist=(), level=0): return module - if name == 'httpout' or name.startswith('httpout.'): + parent, _, child = name.partition('.') + + if parent == 'httpout' and (child == '' or child in globals): if '__server__' in globals: module = globals['__main__'].__server__['modules'][ globals['__name__'] diff --git a/tests/test_http.py b/tests/test_http.py index 77a148b..88dffbd 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -139,7 +139,8 @@ def test_exc_after_print(self): self.assertEqual(header[:header.find(b'\r\n')], b'HTTP/1.1 200 OK') self.assertEqual( body, - b'3\r\nHi\n\r\n1F\r\n