Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/websocket/_utils.py: 43%

37 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:34 +0000

1from typing import Union 

2 

3""" 

4_url.py 

5websocket - WebSocket client library for Python 

6 

7Copyright 2023 engn33r 

8 

9Licensed under the Apache License, Version 2.0 (the "License"); 

10you may not use this file except in compliance with the License. 

11You may obtain a copy of the License at 

12 

13 http://www.apache.org/licenses/LICENSE-2.0 

14 

15Unless required by applicable law or agreed to in writing, software 

16distributed under the License is distributed on an "AS IS" BASIS, 

17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

18See the License for the specific language governing permissions and 

19limitations under the License. 

20""" 

21__all__ = ["NoLock", "validate_utf8", "extract_err_message", "extract_error_code"] 

22 

23 

24class NoLock: 

25 

26 def __enter__(self) -> None: 

27 pass 

28 

29 def __exit__(self, exc_type, exc_value, traceback) -> None: 

30 pass 

31 

32 

33try: 

34 # If wsaccel is available we use compiled routines to validate UTF-8 

35 # strings. 

36 from wsaccel.utf8validator import Utf8Validator 

37 

38 def _validate_utf8(utfbytes: bytes) -> bool: 

39 return Utf8Validator().validate(utfbytes)[0] 

40 

41except ImportError: 

42 # UTF-8 validator 

43 # python implementation of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ 

44 

45 _UTF8_ACCEPT = 0 

46 _UTF8_REJECT = 12 

47 

48 _UTF8D = [ 

49 # The first part of the table maps bytes to character classes that 

50 # to reduce the size of the transition table and create bitmasks. 

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 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, 

54 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, 

55 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, 

56 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, 

57 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, 

58 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, 

59 

60 # The second part is a transition table that maps a combination 

61 # of a state of the automaton and a character class to a state. 

62 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12, 

63 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12, 

64 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12, 

65 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12, 

66 12,36,12,12,12,12,12,12,12,12,12,12, ] 

67 

68 def _decode(state: int, codep: int, ch: int) -> tuple: 

69 tp = _UTF8D[ch] 

70 

71 codep = (ch & 0x3f) | (codep << 6) if ( 

72 state != _UTF8_ACCEPT) else (0xff >> tp) & ch 

73 state = _UTF8D[256 + state + tp] 

74 

75 return state, codep 

76 

77 def _validate_utf8(utfbytes: Union[str, bytes]) -> bool: 

78 state = _UTF8_ACCEPT 

79 codep = 0 

80 for i in utfbytes: 

81 state, codep = _decode(state, codep, i) 

82 if state == _UTF8_REJECT: 

83 return False 

84 

85 return True 

86 

87 

88def validate_utf8(utfbytes: Union[str, bytes]) -> bool: 

89 """ 

90 validate utf8 byte string. 

91 utfbytes: utf byte string to check. 

92 return value: if valid utf8 string, return true. Otherwise, return false. 

93 """ 

94 return _validate_utf8(utfbytes) 

95 

96 

97def extract_err_message(exception: Exception) -> Union[str, None]: 

98 if exception.args: 

99 return exception.args[0] 

100 else: 

101 return None 

102 

103 

104def extract_error_code(exception: Exception) -> Union[int, None]: 

105 if exception.args and len(exception.args) > 1: 

106 return exception.args[0] if isinstance(exception.args[0], int) else None