Python six.byte2int() Examples
The following are 30
code examples of six.byte2int().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
six
, or try the search function
.
Example #1
Source File: _app.py From vnpy_crypto with MIT License | 6 votes |
def _get_close_args(self, data): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ import inspect # if the on_close callback is "old", just return empty list if sys.version_info < (3, 0): if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3: return [] else: if not self.on_close or len(inspect.getfullargspec(self.on_close).args) != 3: return [] if data and len(data) >= 2: code = 256 * six.byte2int(data[0:1]) + six.byte2int(data[1:2]) reason = data[2:].decode('utf-8') return [code, reason] return [None, None]
Example #2
Source File: _interface.py From ida-minsc with BSD 3-Clause "New" or "Revised" License | 6 votes |
def new(self, name, bits, idaname=None, **kwargs): '''Add a register to the architecture's cache.''' # older if idaapi.__version__ < 7.0: dtype_by_size = internal.utils.fcompose(idaapi.get_dtyp_by_size, six.byte2int) dt_bitfield = idaapi.dt_bitfield # newer else: dtype_by_size = idaapi.get_dtype_by_size dt_bitfield = idaapi.dt_bitfild #dtyp = kwargs.get('dtyp', idaapi.dt_bitfild if bits == 1 else dtype_by_size(bits//8)) dtype = six.next((kwargs[n] for n in ('dtyp', 'dtype', 'type') if n in kwargs), dt_bitfield if bits == 1 else dtype_by_size(bits // 8)) namespace = dict(register_t.__dict__) namespace.update({'__name__':name, '__parent__':None, '__children__':{}, '__dtype__':dtype, '__position__':0, '__size__':bits}) namespace['realname'] = idaname namespace['alias'] = kwargs.get('alias', set()) namespace['architecture'] = self res = type(name, (register_t,), namespace)() self.__register__.__state__[name] = res self.__cache__[idaname or name, dtype] = name return res
Example #3
Source File: _interface.py From ida-minsc with BSD 3-Clause "New" or "Revised" License | 6 votes |
def child(self, parent, name, position, bits, idaname=None, **kwargs): '''Add a child register to the architecture's cache.''' # older if idaapi.__version__ < 7.0: dtype_by_size = internal.utils.fcompose(idaapi.get_dtyp_by_size, six.byte2int) dt_bitfield = idaapi.dt_bitfield # newer else: dtype_by_size = idaapi.get_dtype_by_size dt_bitfield = idaapi.dt_bitfild dtype = six.next((kwargs[n] for n in ('dtyp', 'dtype', 'type') if n in kwargs), dt_bitfield if bits == 1 else dtype_by_size(bits // 8)) #dtyp = kwargs.get('dtyp', idaapi.dt_bitfild if bits == 1 else dtype_by_size(bits//8)) namespace = dict(register_t.__dict__) namespace.update({'__name__':name, '__parent__':parent, '__children__':{}, '__dtype__':dtype, '__position__':position, '__size__':bits}) namespace['realname'] = idaname namespace['alias'] = kwargs.get('alias', set()) namespace['architecture'] = self res = type(name, (register_t,), namespace)() self.__register__.__state__[name] = res self.__cache__[idaname or name, dtype] = name parent.__children__[position] = res return res
Example #4
Source File: _utils.py From ida-minsc with BSD 3-Clause "New" or "Revised" License | 6 votes |
def repr(cls, item): """Given an item, return the `repr()` of it whilst ensuring that a proper ascii string is returned. All unicode strings are encoded to UTF-8 in order to guarantee the resulting string can be emitted. """ if isinstance(item, basestring): res = cls.escape(item, '\'') if all(six.byte2int(ch) < 0x100 for ch in item): return "'{:s}'".format(res) return u"u'{:s}'".format(res) elif isinstance(item, tuple): res = map(cls.repr, item) return "({:s}{:s})".format(', '.join(res), ',' if len(item) == 1 else '') elif isinstance(item, list): res = map(cls.repr, item) return "[{:s}]".format(', '.join(res)) elif isinstance(item, set): res = map(cls.repr, item) return "set([{:s}])".format(', '.join(res)) elif isinstance(item, dict): res = ("{:s}: {:s}".format(cls.repr(k), cls.repr(v)) for k, v in six.iteritems(item)) return "{{{:s}}}".format(', '.join(res)) return repr(item)
Example #5
Source File: rq.py From python-xlib with GNU Lesser General Public License v2.1 | 6 votes |
def parse_binary_value(self, data, display, length, format): values = [] while 1: if len(data) < 2: break # font change if byte2int(data) == 255: values.append(struct.unpack('>L', bytes(data[1:5]))[0]) data = data[5:] # skip null strings elif byte2int(data) == 0 and indexbytes(data, 1) == 0: data = data[2:] # string with delta else: v, data = self.string_textitem.parse_binary(data, display) values.append(v) return values, ''
Example #6
Source File: rq.py From MIA-Dictionary-Addon with GNU General Public License v3.0 | 6 votes |
def parse_binary_value(self, data, display, length, format): values = [] while 1: if len(data) < 2: break # font change if byte2int(data) == 255: values.append(struct.unpack('>L', bytes(data[1:5]))[0]) data = data[5:] # skip null strings elif byte2int(data) == 0 and indexbytes(data, 1) == 0: data = data[2:] # string with delta else: v, data = self.string_textitem.parse_binary(data, display) values.append(v) return values, ''
Example #7
Source File: _app.py From bazarr with GNU General Public License v3.0 | 6 votes |
def _get_close_args(self, data): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ import inspect # if the on_close callback is "old", just return empty list if sys.version_info < (3, 0): if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3: return [] else: if not self.on_close or len(inspect.getfullargspec(self.on_close).args) != 3: return [] if data and len(data) >= 2: code = 256 * six.byte2int(data[0:1]) + six.byte2int(data[1:2]) reason = data[2:].decode('utf-8') return [code, reason] return [None, None]
Example #8
Source File: _decoders.py From py_zipkin with Apache License 2.0 | 6 votes |
def decode_spans(self, spans): """Decodes an encoded list of spans. :param spans: encoded list of spans :type spans: bytes :return: list of spans :rtype: list of Span """ decoded_spans = [] transport = TMemoryBuffer(spans) if six.byte2int(spans) == TType.STRUCT: _, size = read_list_begin(transport) else: size = 1 for _ in range(size): span = zipkin_core.Span() span.read(TBinaryProtocol(transport)) decoded_spans.append(self._decode_thrift_span(span)) return decoded_spans
Example #9
Source File: _app.py From pyRevit with GNU General Public License v3.0 | 6 votes |
def _get_close_args(self, data): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ import inspect # if the on_close callback is "old", just return empty list if sys.version_info < (3, 0): if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3: return [] else: if not self.on_close or len(inspect.getfullargspec(self.on_close).args) != 3: return [] if data and len(data) >= 2: code = 256 * six.byte2int(data[0:1]) + six.byte2int(data[1:2]) reason = data[2:].decode('utf-8') return [code, reason] return [None, None]
Example #10
Source File: _app.py From launcher with GNU General Public License v3.0 | 6 votes |
def _get_close_args(self, data): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ # if the on_close callback is "old", just return empty list if sys.version_info < (3, 0): if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3: return [] else: if not self.on_close or len(inspect.getfullargspec(self.on_close).args) != 3: return [] if data and len(data) >= 2: code = 256 * six.byte2int(data[0:1]) + six.byte2int(data[1:2]) reason = data[2:].decode('utf-8') return [code, reason] return [None, None]
Example #11
Source File: _app.py From Tautulli with GNU General Public License v3.0 | 6 votes |
def _get_close_args(self, data): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ # if the on_close callback is "old", just return empty list if sys.version_info < (3, 0): if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3: return [] else: if not self.on_close or len(inspect.getfullargspec(self.on_close).args) != 3: return [] if data and len(data) >= 2: code = 256 * six.byte2int(data[0:1]) + six.byte2int(data[1:2]) reason = data[2:].decode('utf-8') return [code, reason] return [None, None]
Example #12
Source File: test_simpleion.py From ion-python with Apache License 2.0 | 6 votes |
def generate_scalars_binary(scalars_map, preceding_symbols=0): for ion_type, values in six.iteritems(scalars_map): for native, expected in values: native_expected = expected has_symbols = False if native is None: # An un-adorned 'None' doesn't contain enough information to determine its Ion type native_expected = b'\x0f' elif ion_type is IonType.CLOB: # All six.binary_type are treated as BLOBs unless wrapped by an _IonNature tid = six.byte2int(expected) + 0x10 # increment upper nibble for clob -> blob; keep lower nibble native_expected = bytearray([tid]) + expected[1:] elif ion_type is IonType.SYMBOL and native is not None: has_symbols = True elif ion_type is IonType.STRING: # Encode all strings as symbols too. symbol_expected = _serialize_symbol( IonEvent(IonEventType.SCALAR, IonType.SYMBOL, SymbolToken(None, 10 + preceding_symbols))) yield _Parameter(IonType.SYMBOL.name + ' ' + native, IonPyText.from_value(IonType.SYMBOL, native), symbol_expected, True) yield _Parameter('%s %s' % (ion_type.name, native), native, native_expected, has_symbols) wrapper = _FROM_ION_TYPE[ion_type].from_value(ion_type, native) yield _Parameter(repr(wrapper), wrapper, expected, has_symbols)
Example #13
Source File: _app.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def _get_close_args(self, data): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ import inspect # if the on_close callback is "old", just return empty list if sys.version_info < (3, 0): if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3: return [] else: if not self.on_close or len(inspect.getfullargspec(self.on_close).args) != 3: return [] if data and len(data) >= 2: code = 256 * six.byte2int(data[0:1]) + six.byte2int(data[1:2]) reason = data[2:].decode('utf-8') return [code, reason] return [None, None]
Example #14
Source File: decoders.py From heroprotocol with MIT License | 6 votes |
def read_bits(self, bits): result = 0 resultbits = 0 while resultbits != bits: if self._nextbits == 0: if self.done(): raise TruncatedError(self) self._next = six.byte2int([self._data[self._used]]) self._used += 1 self._nextbits = 8 copybits = min(bits - resultbits, self._nextbits) copy = self._next & ((1 << copybits) - 1) if self._bigendian: result |= copy << (bits - resultbits - copybits) else: result |= copy << resultbits self._next >>= copybits self._nextbits -= copybits resultbits += copybits return result
Example #15
Source File: _app.py From deepWordBug with Apache License 2.0 | 6 votes |
def _get_close_args(self, data): """ this functions extracts the code, reason from the close body if they exists, and if the self.on_close except three arguments """ # if the on_close callback is "old", just return empty list if sys.version_info < (3, 0): if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3: return [] else: if not self.on_close or len(inspect.getfullargspec(self.on_close).args) != 3: return [] if data and len(data) >= 2: code = 256 * six.byte2int(data[0:1]) + six.byte2int(data[1:2]) reason = data[2:].decode('utf-8') return [code, reason] return [None, None]
Example #16
Source File: writer_text.py From ion-python with Apache License 2.0 | 6 votes |
def _bytes_text(code_point_iter, quote, prefix=b'', suffix=b''): quote_code_point = None if len(quote) == 0 else six.byte2int(quote) buf = BytesIO() buf.write(prefix) buf.write(quote) for code_point in code_point_iter: if code_point == quote_code_point: buf.write(b'\\' + quote) elif code_point == six.byte2int(b'\\'): buf.write(b'\\\\') elif _is_printable_ascii(code_point): buf.write(six.int2byte(code_point)) else: buf.write(_escape(code_point)) buf.write(quote) buf.write(suffix) return buf.getvalue()
Example #17
Source File: test_six.py From c4ddev with MIT License | 5 votes |
def test_byte2int(): assert six.byte2int(six.b("\x03")) == 3 assert six.byte2int(six.b("\x03\x04")) == 3 py.test.raises(IndexError, six.byte2int, six.b(""))
Example #18
Source File: packet.py From python-engineio with MIT License | 5 votes |
def decode(self, encoded_packet): """Decode a transmitted package.""" b64 = False if not isinstance(encoded_packet, binary_types): encoded_packet = encoded_packet.encode('utf-8') elif not isinstance(encoded_packet, bytes): encoded_packet = bytes(encoded_packet) self.packet_type = six.byte2int(encoded_packet[0:1]) if self.packet_type == 98: # 'b' --> binary base64 encoded packet self.binary = True encoded_packet = encoded_packet[1:] self.packet_type = six.byte2int(encoded_packet[0:1]) self.packet_type -= 48 b64 = True elif self.packet_type >= 48: self.packet_type -= 48 self.binary = False else: self.binary = True self.data = None if len(encoded_packet) > 1: if self.binary: if b64: self.data = base64.b64decode(encoded_packet[1:]) else: self.data = encoded_packet[1:] else: try: self.data = self.json.loads( encoded_packet[1:].decode('utf-8')) if isinstance(self.data, int): # do not allow integer payloads, see # github.com/miguelgrinberg/python-engineio/issues/75 # for background on this decision raise ValueError except ValueError: self.data = encoded_packet[1:].decode('utf-8')
Example #19
Source File: _abnf.py From vnpy_crypto with MIT License | 5 votes |
def validate(self, skip_utf8_validation=False): """ validate the ABNF frame. skip_utf8_validation: skip utf8 validation. """ if self.rsv1 or self.rsv2 or self.rsv3: raise WebSocketProtocolException("rsv is not implemented, yet") if self.opcode not in ABNF.OPCODES: raise WebSocketProtocolException("Invalid opcode %r", self.opcode) if self.opcode == ABNF.OPCODE_PING and not self.fin: raise WebSocketProtocolException("Invalid ping frame.") if self.opcode == ABNF.OPCODE_CLOSE: l = len(self.data) if not l: return if l == 1 or l >= 126: raise WebSocketProtocolException("Invalid close frame.") if l > 2 and not skip_utf8_validation and not validate_utf8(self.data[2:]): raise WebSocketProtocolException("Invalid close frame.") code = 256 * \ six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2]) if not self._is_valid_close_status(code): raise WebSocketProtocolException("Invalid close opcode.")
Example #20
Source File: _interface.py From ida-minsc with BSD 3-Clause "New" or "Revised" License | 5 votes |
def by_indexsize(self, index, size): '''Lookup a register according to its `index` and `size`.''' dtype_by_size = internal.utils.fcompose(idaapi.get_dtyp_by_size, six.byte2int) if idaapi.__version__ < 7.0 else idaapi.get_dtype_by_size dtype = dtype_by_size(size) return self.by_indextype(index, dtype)
Example #21
Source File: rq.py From MIA-Dictionary-Addon with GNU General Public License v3.0 | 5 votes |
def parse_binary_value(self, data, display, length, format): from . import event estruct = display.event_classes.get(byte2int(data) & 0x7f, event.AnyEvent) if type(estruct) == dict: # this etype refers to a set of sub-events with individual subcodes estruct = estruct[indexbytes(data, 1)] return estruct(display = display, binarydata = data[:32]), data[32:] # # Objects usable for List and FixedList fields. # Struct is also usable. #
Example #22
Source File: rq.py From MIA-Dictionary-Addon with GNU General Public License v3.0 | 5 votes |
def parse_binary(self, data, display): slen = byte2int(data) + 1 return decode_string(data[1:slen]), data[slen:]
Example #23
Source File: gevent_uwsgi.py From python-engineio with MIT License | 5 votes |
def _decode_received(self, msg): """Returns either bytes or str, depending on message type.""" if not isinstance(msg, six.binary_type): # already decoded - do nothing return msg # only decode from utf-8 if message is not binary data type = six.byte2int(msg[0:1]) if type >= 48: # no binary return msg.decode('utf-8') # binary message, don't try to decode return msg
Example #24
Source File: payload.py From python-engineio with MIT License | 5 votes |
def decode(self, encoded_payload): """Decode a transmitted payload.""" self.packets = [] if len(encoded_payload) == 0: return # JSONP POST payload starts with 'd=' if encoded_payload.startswith(b'd='): encoded_payload = urllib.parse.parse_qs( encoded_payload)[b'd'][0] i = 0 if six.byte2int(encoded_payload[0:1]) <= 1: # binary encoding while i < len(encoded_payload): if len(self.packets) >= self.max_decode_packets: raise ValueError('Too many packets in payload') packet_len = 0 i += 1 while six.byte2int(encoded_payload[i:i + 1]) != 255: packet_len = packet_len * 10 + six.byte2int( encoded_payload[i:i + 1]) i += 1 self.packets.append(packet.Packet( encoded_packet=encoded_payload[i + 1:i + 1 + packet_len])) i += packet_len + 1 else: # assume text encoding encoded_payload = encoded_payload.decode('utf-8') while i < len(encoded_payload): if len(self.packets) >= self.max_decode_packets: raise ValueError('Too many packets in payload') j = encoded_payload.find(':', i) packet_len = int(encoded_payload[i:j]) pkt = encoded_payload[j + 1:j + 1 + packet_len] self.packets.append(packet.Packet(encoded_packet=pkt)) i = j + 1 + packet_len
Example #25
Source File: binary_serializer.py From pymacaroons with MIT License | 5 votes |
def deserialize_raw(self, serialized): from pymacaroons.macaroon import MACAROON_V2 from pymacaroons.exceptions import MacaroonDeserializationException first = six.byte2int(serialized[:1]) if first == MACAROON_V2: return self._deserialize_v2(serialized) if _is_ascii_hex(first): return self._deserialize_v1(serialized) raise MacaroonDeserializationException( 'cannot determine data format of binary-encoded macaroon')
Example #26
Source File: _abnf.py From bazarr with GNU General Public License v3.0 | 5 votes |
def validate(self, skip_utf8_validation=False): """ validate the ABNF frame. skip_utf8_validation: skip utf8 validation. """ if self.rsv1 or self.rsv2 or self.rsv3: raise WebSocketProtocolException("rsv is not implemented, yet") if self.opcode not in ABNF.OPCODES: raise WebSocketProtocolException("Invalid opcode %r", self.opcode) if self.opcode == ABNF.OPCODE_PING and not self.fin: raise WebSocketProtocolException("Invalid ping frame.") if self.opcode == ABNF.OPCODE_CLOSE: l = len(self.data) if not l: return if l == 1 or l >= 126: raise WebSocketProtocolException("Invalid close frame.") if l > 2 and not skip_utf8_validation and not validate_utf8(self.data[2:]): raise WebSocketProtocolException("Invalid close frame.") code = 256 * \ six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2]) if not self._is_valid_close_status(code): raise WebSocketProtocolException("Invalid close opcode.")
Example #27
Source File: test_six.py From data with GNU General Public License v3.0 | 5 votes |
def test_byte2int(): assert six.byte2int(six.b("\x03")) == 3 assert six.byte2int(six.b("\x03\x04")) == 3 py.test.raises(IndexError, six.byte2int, six.b(""))
Example #28
Source File: test_six.py From data with GNU General Public License v3.0 | 5 votes |
def test_byte2int(): assert six.byte2int(six.b("\x03")) == 3 assert six.byte2int(six.b("\x03\x04")) == 3 py.test.raises(IndexError, six.byte2int, six.b(""))
Example #29
Source File: _abnf.py From Tautulli with GNU General Public License v3.0 | 5 votes |
def validate(self, skip_utf8_validation=False): """ validate the ABNF frame. skip_utf8_validation: skip utf8 validation. """ if self.rsv1 or self.rsv2 or self.rsv3: raise WebSocketProtocolException("rsv is not implemented, yet") if self.opcode not in ABNF.OPCODES: raise WebSocketProtocolException("Invalid opcode %r", self.opcode) if self.opcode == ABNF.OPCODE_PING and not self.fin: raise WebSocketProtocolException("Invalid ping frame.") if self.opcode == ABNF.OPCODE_CLOSE: l = len(self.data) if not l: return if l == 1 or l >= 126: raise WebSocketProtocolException("Invalid close frame.") if l > 2 and not skip_utf8_validation and not validate_utf8(self.data[2:]): raise WebSocketProtocolException("Invalid close frame.") code = 256 * \ six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2]) if not self._is_valid_close_status(code): raise WebSocketProtocolException("Invalid close opcode.")
Example #30
Source File: _abnf.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def validate(self, skip_utf8_validation=False): """ validate the ABNF frame. skip_utf8_validation: skip utf8 validation. """ if self.rsv1 or self.rsv2 or self.rsv3: raise WebSocketProtocolException("rsv is not implemented, yet") if self.opcode not in ABNF.OPCODES: raise WebSocketProtocolException("Invalid opcode %r", self.opcode) if self.opcode == ABNF.OPCODE_PING and not self.fin: raise WebSocketProtocolException("Invalid ping frame.") if self.opcode == ABNF.OPCODE_CLOSE: l = len(self.data) if not l: return if l == 1 or l >= 126: raise WebSocketProtocolException("Invalid close frame.") if l > 2 and not skip_utf8_validation and not validate_utf8(self.data[2:]): raise WebSocketProtocolException("Invalid close frame.") code = 256 * \ six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2]) if not self._is_valid_close_status(code): raise WebSocketProtocolException("Invalid close opcode.")