我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用codecs.escape_decode()。
def files_in_archive(self, force_refresh=False): if self._files_in_archive and not force_refresh: return self._files_in_archive cmd = [ self.cmd_path, '--list', '-C', self.dest ] if self.zipflag: cmd.append(self.zipflag) if self.opts: cmd.extend([ '--show-transformed-names' ] + self.opts) if self.excludes: cmd.extend([ '--exclude=' + quote(f) for f in self.excludes ]) cmd.extend([ '-f', self.src ]) rc, out, err = self.module.run_command(cmd, cwd=self.dest, environ_update=dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')) if rc != 0: raise UnarchiveError('Unable to list files in the archive') for filename in out.splitlines(): # Compensate for locale-related problems in gtar output (octal unicode representation) #11348 # filename = filename.decode('string_escape') filename = codecs.escape_decode(filename)[0] if filename and filename not in self.excludes: self._files_in_archive.append(to_native(filename)) return self._files_in_archive
def decode(self, input, final=False): return codecs.escape_decode(input, self.errors)[0]
def unescape(value): return codecs.escape_decode(value)[0].decode('utf-8')
def raw_bytes(raw): """Make a bytes object out of a raw string. This is analogous to raw_unicode, but processes byte escape characters to produce a bytes object. """ return codecs.escape_decode(raw)[0]
def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False): ''' assemble a file from a directory of fragments ''' tmpfd, temp_path = tempfile.mkstemp() tmp = os.fdopen(tmpfd, 'wb') delimit_me = False add_newline = False for f in sorted(os.listdir(src_path)): if compiled_regexp and not compiled_regexp.search(f): continue fragment = u"%s/%s" % (src_path, f) if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')): continue fragment_content = open(fragment, 'rb').read() # always put a newline between fragments if the previous fragment didn't end with a newline. if add_newline: tmp.write(b('\n')) # delimiters should only appear between fragments if delimit_me: if delimiter: # un-escape anything like newlines delimiter = codecs.escape_decode(delimiter)[0] tmp.write(delimiter) # always make sure there's a newline after the # delimiter, so lines don't run together if delimiter[-1] != b('\n'): tmp.write(b('\n')) tmp.write(fragment_content) delimit_me = True if fragment_content.endswith(b('\n')): add_newline = False else: add_newline = True tmp.close() return temp_path
def _assemble_from_fragments(self, src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False): ''' assemble a file from a directory of fragments ''' tmpfd, temp_path = tempfile.mkstemp() tmp = os.fdopen(tmpfd, 'wb') delimit_me = False add_newline = False for f in (to_text(p, errors='surrogate_or_strict') for p in sorted(os.listdir(src_path))): if compiled_regexp and not compiled_regexp.search(f): continue fragment = u"%s/%s" % (src_path, f) if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')): continue fragment_content = open(self._loader.get_real_file(fragment), 'rb').read() # always put a newline between fragments if the previous fragment didn't end with a newline. if add_newline: tmp.write(b'\n') # delimiters should only appear between fragments if delimit_me: if delimiter: # un-escape anything like newlines delimiter = codecs.escape_decode(delimiter)[0] tmp.write(delimiter) # always make sure there's a newline after the # delimiter, so lines don't run together if delimiter[-1] != b'\n': tmp.write(b'\n') tmp.write(fragment_content) delimit_me = True if fragment_content.endswith(b'\n'): add_newline = False else: add_newline = True tmp.close() return temp_path
def load_string(self): orig = self.readline() rep = orig[:-1] for q in (b'"', b"'"): # double or single quote if rep.startswith(q): if not rep.endswith(q): raise ValueError("insecure string pickle") rep = rep[len(q):-len(q)] break else: raise ValueError("insecure string pickle: %r" % orig) self.append(codecs.escape_decode(rep)[0] .decode(self.encoding, self.errors))
def test_empty(self): self.assertEqual(codecs.escape_decode(""), ("", 0))
def test_raw(self): decode = codecs.escape_decode for b in range(256): b = chr(b) if b != '\\': self.assertEqual(decode(b + '0'), (b + '0', 2))
def test_escape(self): decode = codecs.escape_decode check = coding_checker(self, decode) check(b"[\\\n]", b"[]") check(br'[\"]', b'["]') check(br"[\']", b"[']") check(br"[\\]", br"[\]") check(br"[\a]", b"[\x07]") check(br"[\b]", b"[\x08]") check(br"[\t]", b"[\x09]") check(br"[\n]", b"[\x0a]") check(br"[\v]", b"[\x0b]") check(br"[\f]", b"[\x0c]") check(br"[\r]", b"[\x0d]") check(br"[\7]", b"[\x07]") check(br"[\8]", br"[\8]") check(br"[\78]", b"[\x078]") check(br"[\41]", b"[!]") check(br"[\418]", b"[!8]") check(br"[\101]", b"[A]") check(br"[\1010]", b"[A0]") check(br"[\501]", b"[A]") check(br"[\x41]", b"[A]") check(br"[\X41]", br"[\X41]") check(br"[\x410]", b"[A0]") for b in range(256): b = chr(b) if b not in '\n"\'\\abtnvfr01234567x': check('\\' + b, '\\' + b)
def test_errors(self): decode = codecs.escape_decode self.assertRaises(ValueError, decode, br"\x") self.assertRaises(ValueError, decode, br"[\x]") self.assertEqual(decode(br"[\x]\x", "ignore"), (b"[]", 6)) self.assertEqual(decode(br"[\x]\x", "replace"), (b"[?]?", 6)) self.assertRaises(ValueError, decode, br"\x0") self.assertRaises(ValueError, decode, br"[\x0]") self.assertEqual(decode(br"[\x0]\x0", "ignore"), (b"[]", 8)) self.assertEqual(decode(br"[\x0]\x0", "replace"), (b"[?]?", 8))
def to_byte(val): unicode_type = str if PY3 else unicode if isinstance(val, unicode_type): try: return val.encode(ENCODING) except UnicodeEncodeError: if PY3: return codecs.escape_decode(val)[0] else: return val.encode("unicode-escape").decode("string-escape") return val
def test_empty(self): self.assertEqual(codecs.escape_decode(b""), (b"", 0))
def test_raw(self): decode = codecs.escape_decode for b in range(256): b = bytes([b]) if b != b'\\': self.assertEqual(decode(b + b'0'), (b + b'0', 2))
def test_escape(self): decode = codecs.escape_decode check = coding_checker(self, decode) check(b"[\\\n]", b"[]") check(br'[\"]', b'["]') check(br"[\']", b"[']") check(br"[\\]", br"[\]") check(br"[\a]", b"[\x07]") check(br"[\b]", b"[\x08]") check(br"[\t]", b"[\x09]") check(br"[\n]", b"[\x0a]") check(br"[\v]", b"[\x0b]") check(br"[\f]", b"[\x0c]") check(br"[\r]", b"[\x0d]") check(br"[\7]", b"[\x07]") check(br"[\8]", br"[\8]") check(br"[\78]", b"[\x078]") check(br"[\41]", b"[!]") check(br"[\418]", b"[!8]") check(br"[\101]", b"[A]") check(br"[\1010]", b"[A0]") check(br"[\501]", b"[A]") check(br"[\x41]", b"[A]") check(br"[\X41]", br"[\X41]") check(br"[\x410]", b"[A0]") for b in range(256): if b not in b'\n"\'\\abtnvfr01234567x': b = bytes([b]) check(b'\\' + b, b'\\' + b)