def _encode_bytes(name, value, dummy0, dummy1): """Encode a python str (python 2.x).""" try: _utf_8_decode(value, None, True) except UnicodeError: raise InvalidStringData("strings in documents must be valid " "UTF-8: %r" % (value, )) return b"\x02" + name + _PACK_INT(len(value) + 1) + value + b"\x00"
def _make_c_string(string: Union[str, bytes]) -> bytes: """Make a 'C' string.""" if isinstance(string, bytes): try: _utf_8_decode(string, None, True) return string + b"\x00" except UnicodeError: raise InvalidStringData("strings in documents must be valid UTF-8: %r" % string) else: return cast(bytes, _utf_8_encode(string)[0]) + b"\x00"
def _make_c_string(string, check_null=False): if check_null and "\x00" in string: raise InvalidDocument("BSON keys / regex patterns must not " "contain a NULL character") if isinstance(string, unicode): return string.encode("utf-8") + "\x00" else: try: string.decode("utf-8") return string + "\x00" except UnicodeError: raise InvalidStringData("strings in documents must be valid " "UTF-8: %r" % string)
def _make_c_string_check(string: Union[str, bytes]) -> bytes: """Make a 'C' string, checking for embedded NUL characters.""" if isinstance(string, bytes): if b"\x00" in string: raise InvalidDocument("BSON keys / regex patterns must not contain a NUL character") try: _utf_8_decode(string, None, True) return string + b"\x00" except UnicodeError: raise InvalidStringData("strings in documents must be valid UTF-8: %r" % string) else: if "\x00" in string: raise InvalidDocument("BSON keys / regex patterns must not contain a NUL character") return cast(bytes, _utf_8_encode(string)[0]) + b"\x00"
def test_insert_or_update_exploits_by_id(self, db_instance): new_exploit = [{'id': 6, 'file': 'file6.py'}] db_instance.collection.update_one = mock.Mock( side_effect=Exception('insert exception')) db_instance.insert_or_update_exploits_by_id(new_exploit) exploits = list(db_instance.collection.find({'id': 6})) assert len(exploits) == 0 assert db_instance.logger.error.call_count == 1 assert db_instance.logger.error.call_args == \ mock.call("Unexpected exception insert exception for id 6.", exc_info=True) db_instance.collection.update_one = mock.Mock( side_effect=InvalidStringData()) db_instance.insert_or_update_exploits_by_id(new_exploit) exploits = list(db_instance.collection.find({'id': 6})) assert len(exploits) == 0 assert db_instance.logger.warn.call_count == 1 assert db_instance.logger.warn.call_args == \ mock.call("InvalidStringData for exploit id 6.")