Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/websocket/_utils.py: 42%
36 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
1"""
2_url.py
3websocket - WebSocket client library for Python
5Copyright 2022 engn33r
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18"""
19__all__ = ["NoLock", "validate_utf8", "extract_err_message", "extract_error_code"]
22class NoLock:
24 def __enter__(self) -> None:
25 pass
27 def __exit__(self, exc_type, exc_value, traceback) -> None:
28 pass
31try:
32 # If wsaccel is available we use compiled routines to validate UTF-8
33 # strings.
34 from wsaccel.utf8validator import Utf8Validator
36 def _validate_utf8(utfbytes: bytes) -> bool:
37 return Utf8Validator().validate(utfbytes)[0]
39except ImportError:
40 # UTF-8 validator
41 # python implementation of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
43 _UTF8_ACCEPT = 0
44 _UTF8_REJECT = 12
46 _UTF8D = [
47 # The first part of the table maps bytes to character classes that
48 # to reduce the size of the transition table and create bitmasks.
49 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
50 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
51 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
53 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
54 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
55 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
56 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
58 # The second part is a transition table that maps a combination
59 # of a state of the automaton and a character class to a state.
60 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
61 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
62 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
63 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
64 12,36,12,12,12,12,12,12,12,12,12,12, ]
66 def _decode(state, codep, ch) -> tuple:
67 tp = _UTF8D[ch]
69 codep = (ch & 0x3f) | (codep << 6) if (
70 state != _UTF8_ACCEPT) else (0xff >> tp) & ch
71 state = _UTF8D[256 + state + tp]
73 return state, codep
75 def _validate_utf8(utfbytes: bytes) -> bool:
76 state = _UTF8_ACCEPT
77 codep = 0
78 for i in utfbytes:
79 state, codep = _decode(state, codep, i)
80 if state == _UTF8_REJECT:
81 return False
83 return True
86def validate_utf8(utfbytes: str) -> bool:
87 """
88 validate utf8 byte string.
89 utfbytes: utf byte string to check.
90 return value: if valid utf8 string, return true. Otherwise, return false.
91 """
92 return _validate_utf8(utfbytes)
95def extract_err_message(exception: Exception) -> str or None:
96 if exception.args:
97 return exception.args[0]
98 else:
99 return None
102def extract_error_code(exception: Exception) -> int or None:
103 if exception.args and len(exception.args) > 1:
104 return exception.args[0] if isinstance(exception.args[0], int) else None