1616
1717from __future__ import annotations
1818
19- import sys
2019import codecs
2120from array import array
2221from typing import Any , Generator , TYPE_CHECKING
3029 from .msgs import Message
3130
3231
33- _PY3 = (sys .version_info >= (3 ,))
34-
35-
3632def py3enc_unic_bytes_fix (dat : Any ) -> Any :
37- # python 3 unicode fix
38- if isinstance (dat , str ) and _PY3 :
33+ if isinstance (dat , str ):
3934 dat = dat .encode ('raw_unicode_escape' )
4035 return dat
4136
4237
4338def py3dec_unic_bytes_fix (dat : bytes ) -> str :
44- # python 3 unicode fix
45- if _PY3 :
46- return dat .decode ('raw_unicode_escape' )
47- return dat
39+ return dat .decode ('raw_unicode_escape' )
4840
4941
5042def py3_array_frombytes (msg : array , data : bytes ) -> None :
51- if _PY3 :
52- return msg .frombytes (data )
53- else :
54- return msg .fromstring (data )
43+ return msg .frombytes (data )
5544
5645
5746def py3_array_tobytes (msg : array ) -> bytes :
58- if _PY3 :
59- return msg .tobytes ()
60- else :
61- return msg .tostring ()
47+ return msg .tobytes ()
6248
6349
6450def check_completion_code (cc : int ) -> None :
@@ -112,7 +98,7 @@ def pop_unsigned_int(self, length: int) -> int:
11298 return value
11399
114100 def push_string (self , value : str | bytes ) -> None :
115- if _PY3 and isinstance (value , str ):
101+ if isinstance (value , str ):
116102 # Encode Unicode to UTF-8
117103 value = value .encode ()
118104 py3_array_frombytes (self .array , value )
@@ -121,7 +107,6 @@ def pop_string(self, length: int) -> bytes:
121107 string = self .array [0 :length ]
122108 del self .array [0 :length ]
123109 return py3_array_tobytes (string )
124- # return py3dec_unic_bytes_fix(string.tostring())
125110
126111 def pop_slice (self , length : int ) -> ByteBuffer :
127112 if len (self .array ) < length :
@@ -167,8 +152,6 @@ def bcd_decode(encoded_input: Any) -> tuple[str, int]:
167152 chars = list ()
168153 try :
169154 for data in encoded_input :
170- if not _PY3 :
171- data = ord (data )
172155 chars .append (BCD_MAP [data >> 4 & 0xf ] + BCD_MAP [data & 0xf ])
173156 return ('' .join (chars ), len (encoded_input ) * 2 )
174157 except IndexError :
@@ -183,5 +166,4 @@ def bcd_search(name: str) -> codecs.CodecInfo | None:
183166
184167
185168def is_string (string : Any ) -> bool :
186- if _PY3 :
187- return isinstance (string , str )
169+ return isinstance (string , str )
0 commit comments