def test_unicode_nfc_and_nfd_decomposition_equality(self): import unicodedata text = 'Hyv\xe4' assert_equal(safe_str(unicodedata.normalize('NFC', text)), text) # In Mac filesystem umlaut characters are presented in NFD-format. # This is to check that unic normalizes all strings to NFC assert_equal(safe_str(unicodedata.normalize('NFD', text)), text)
def __init__(self, result): if not (is_dict_like(result) and 'status' in result): raise RuntimeError('Invalid remote result dictionary: %s' % result) self.status = result['status'] self.output = safe_str(self._get(result, 'output')) self.return_ = self._get(result, 'return') self.error = safe_str(self._get(result, 'error')) self.traceback = safe_str(self._get(result, 'traceback')) self.fatal = bool(self._get(result, 'fatal', False)) self.continuable = bool(self._get(result, 'continuable', False))
def _get_message(self, record): try: return self.format(record), None except: message = 'Failed to log following message properly: %s' \ % safe_str(record.msg) error = '\n'.join(get_error_details()) return message, error
def _normalize_message(self, msg): if callable(msg): return msg if not is_string(msg): msg = safe_str(msg) if '\r\n' in msg: msg = msg.replace('\r\n', '\n') return msg
def _handle_error(self, name, value, error=None, strict=True): if not strict: return value value_type = '' if isinstance(value, str) else ' (%s)' % type_name(value) ending = ': %s' % error if (error and error.args) else '.' raise ValueError( "Argument '%s' got value '%s'%s that cannot be converted to %s%s" % (name, safe_str(value), value_type, self.type_name, ending))
def replace_string(self, item, custom_unescaper=None, ignore_errors=False): """Replaces variables from a string. Result is always a string. Input can also be an already found VariableMatch. """ unescaper = custom_unescaper or unescape match = self._search_variable(item, ignore_errors=ignore_errors) if not match: return safe_str(unescaper(match.string)) return self._replace_string(match, unescaper, ignore_errors)
def _replace_string(self, match, unescaper, ignore_errors): parts = [] while match: parts.extend([ unescaper(match.before), safe_str(self._get_variable_value(match, ignore_errors)) ]) match = search_variable(match.after, ignore_errors=ignore_errors) parts.append(unescaper(match.string)) return ''.join(parts)
def start_keyword(self, kw): attrs = {'name': kw.kwname, 'library': kw.libname} if kw.type != 'KEYWORD': attrs['type'] = kw.type if kw.sourcename: attrs['sourcename'] = kw.sourcename self._writer.start('kw', attrs) self._write_list('var', kw.assign) self._write_list('arg', [safe_str(a) for a in kw.args]) self._write_list('tag', kw.tags) # Must be after tags to allow adding message when using --flattenkeywords. self._writer.element('doc', kw.doc)
def _list_dir(self, dir_path, incl_suites): try: names = os.listdir(dir_path) except: raise DataError("Reading directory '%s' failed: %s" % (dir_path, get_error_message())) for name in sorted(names, key=lambda item: item.lower()): name = safe_str(name) # Handles NFC normalization on OSX path = os.path.join(dir_path, name) base, ext = os.path.splitext(name) ext = ext[1:].lower() if self._is_init_file(path, base, ext): yield path, True elif self._is_included(path, base, ext, incl_suites): yield path, False else: LOGGER.info("Ignoring file or directory '%s'." % path)
def string(self, string, escape=True, attr=False): if escape and string: if not is_string(string): string = safe_str(string) string = (html_escape if not attr else attribute_escape)(string) return self._strings.add(string)
def _get_extra_attributes(self, kw): args = [a if is_string(a) else safe_str(a) for a in kw.args] return {'kwname': kw.kwname or '', 'libname': kw.libname or '', 'args': args}
def default_repr(self): if self.default is self.NOTSET: return None if isinstance(self.default, Enum): return self.default.name return safe_str(self.default)
def _fail(self, message, default_template, *items): if not message: message = default_template % tuple( safe_str(item) for item in items) raise AssertionError(message)
def test_list_with_objects_containing_unicode_repr(self): objects = [UnicodeRepr(), UnicodeRepr()] result = safe_str(objects) assert_equal(result, '[Hyv\xe4, Hyv\xe4]')
def test_object_containing_unicode_repr(self): assert_equal(safe_str(UnicodeRepr()), 'Hyv\xe4')
def test_bytes_below_128(self): assert_equal(safe_str('\x00-\x01-\x02-\x7f'), '\x00-\x01-\x02-\x7f')
def _to_string(self, item): item = safe_str(item) if item is not None else '' return self._handle_string(item)
def _trace_log_args(self, positional, named): args = [prepr(arg) for arg in positional] args += ['%s=%s' % (safe_str(n), prepr(v)) for n, v in named] return 'Arguments: [ %s ]' % ' | '.join(args)
def test_bytes_above_128(self): assert_equal(safe_str(b'hyv\xe4'), 'hyv\\xe4') assert_equal(safe_str(b'\x00-\x01-\x02-\xe4'), '\x00-\x01-\x02-\\xe4')
def test_bytes_with_newlines_tabs_etc(self): assert_equal(safe_str(b"\x00\xe4\n\t\r\\'"), "\x00\\xe4\n\t\r\\'")
def test_bytearray(self): assert_equal(safe_str(bytearray(b'hyv\xe4')), 'hyv\\xe4') assert_equal(safe_str(bytearray(b'\x00-\x01-\x02-\xe4')), '\x00-\x01-\x02-\\xe4') assert_equal(safe_str(bytearray(b"\x00\xe4\n\t\r\\'")), "\x00\\xe4\n\t\r\\'")
def test_failure_in_str(self): failing = StrFails() assert_equal(safe_str(failing), failing.unrepr)