我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用os.fsdecode()。
def test_non_ascii(self): # Mac OS X denies the creation of a file with an invalid UTF-8 name. # Windows allows to create a name with an arbitrary bytes name, but # Python cannot a undecodable bytes argument to a subprocess. if (support.TESTFN_UNDECODABLE and sys.platform not in ('win32', 'darwin')): name = os.fsdecode(support.TESTFN_UNDECODABLE) elif support.TESTFN_NONASCII: name = support.TESTFN_NONASCII else: self.skipTest("need support.TESTFN_NONASCII") # Issue #16218 source = 'print(ascii(__file__))\n' script_name = _make_test_script(os.curdir, name, source) self.addCleanup(support.unlink, script_name) rc, stdout, stderr = assert_python_ok(script_name) self.assertEqual( ascii(script_name), stdout.rstrip().decode('ascii'), 'stdout=%r stderr=%r' % (stdout, stderr)) self.assertEqual(0, rc)
def test_undecodable_filename(self): enc = sys.getfilesystemencoding() filename = os.fsdecode(support.TESTFN_UNDECODABLE) + '.txt' with open(os.path.join(self.tempdir, filename), 'wb') as f: f.write(support.TESTFN_UNDECODABLE) response = self.request(self.tempdir_name + '/') if sys.platform == 'darwin': # On Mac OS the HFS+ filesystem replaces bytes that aren't valid # UTF-8 into a percent-encoded value. for name in os.listdir(self.tempdir): if name != 'test': # Ignore a filename created in setUp(). filename = name break body = self.check_status_and_reason(response, 200) quotedname = urllib.parse.quote(filename, errors='surrogatepass') self.assertIn(('href="%s"' % quotedname) .encode(enc, 'surrogateescape'), body) self.assertIn(('>%s<' % html.escape(filename)) .encode(enc, 'surrogateescape'), body) response = self.request(self.tempdir_name + '/' + quotedname) self.check_status_and_reason(response, 200, data=support.TESTFN_UNDECODABLE)
def _get_soname(f): if not f: return None try: proc = subprocess.Popen(("/usr/ccs/bin/dump", "-Lpv", f), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) except OSError: # E.g. command not found return None with proc: data = proc.stdout.read() res = re.search(br'\[.*\]\sSONAME\s+([^\s]+)', data) if not res: return None return os.fsdecode(res.group(1))
def _get_soname(f): # assuming GNU binutils / ELF if not f: return None objdump = shutil.which('objdump') if not objdump: # objdump is not available, give up return None try: proc = subprocess.Popen((objdump, '-p', '-j', '.dynamic', f), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) except OSError: # E.g. bad executable return None with proc: dump = proc.stdout.read() res = re.search(br'\sSONAME\s+([^\s]+)', dump) if not res: return None return os.fsdecode(res.group(1))
def find_library(name): ename = re.escape(name) expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename) expr = os.fsencode(expr) try: proc = subprocess.Popen(('/sbin/ldconfig', '-r'), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) except OSError: # E.g. command not found data = b'' else: with proc: data = proc.stdout.read() res = re.findall(expr, data) if not res: return _get_soname(_findLib_gcc(name)) res.sort(key=_num_version) return os.fsdecode(res[-1])
def _findLib_ld(name): # See issue #9998 for why this is needed expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) cmd = ['ld', '-t'] libpath = os.environ.get('LD_LIBRARY_PATH') if libpath: for d in libpath.split(':'): cmd.extend(['-L', d]) cmd.extend(['-o', os.devnull, '-l%s' % name]) result = None try: p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) out, _ = p.communicate() res = re.search(expr, os.fsdecode(out)) if res: result = res.group(0) except Exception as e: pass # result will be None return result
def fsdecode(filename): if isinstance(filename, text_type): return filename elif isinstance(filename, bytes): return filename.decode(_fsencoding, _fserrors) else: raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
def unistr(text, errors='strict'): if isinstance(text, text_type): return text try: return text.decode('utf-8', errors=errors) except UnicodeDecodeError: if HAS_FSCODEC: return os.fsdecode(text) raise
def unistr(text, errors='strict'): # type: (Union[str, bytes], str) -> str if isinstance(text, text_type): return text try: return text.decode('utf-8', errors=errors) except UnicodeDecodeError: if HAS_FSCODEC: return os.fsdecode(text) raise
def unifilename(filename): # type: (Union[str, bytes]) -> str if isinstance(filename, text_type): return filename try: return filename.decode(sys.getfilesystemencoding()) except UnicodeDecodeError: try: return filename.decode('utf-8') except UnicodeDecodeError: if HAS_FSCODEC: return os.fsdecode(filename) else: raise
def obj_image_load(context_imagepath_map, line, DIR, recursive, relpath): """ Mainly uses comprehensiveImageLoad But we try all space-separated items from current line when file is not found with last one (users keep generating/using image files with spaces in a format that does not support them, sigh...) Also tries to replace '_' with ' ' for Max's exporter replaces spaces with underscores. """ filepath_parts = line.split(b' ') image = None for i in range(-1, -len(filepath_parts), -1): imagepath = os.fsdecode(b" ".join(filepath_parts[i:])) image = context_imagepath_map.get(imagepath, ...) if image is ...: image = load_image(imagepath, DIR, recursive=recursive, relpath=relpath) if image is None and "_" in imagepath: image = load_image(imagepath.replace("_", " "), DIR, recursive=recursive, relpath=relpath) if image is not None: context_imagepath_map[imagepath] = image break; if image is None: imagepath = os.fsdecode(filepath_parts[-1]) image = load_image(imagepath, DIR, recursive=recursive, place_holder=True, relpath=relpath) context_imagepath_map[imagepath] = image return image
def __exit__(self, exc_type, exc_value, traceback): self.close() #: A ``namedtuple`` (wd, mask, cookie, name) for an inotify event. #: ``namedtuple`` objects are very lightweight to instantiate and access, whilst #: being human readable when printed, which is useful for debugging and #: logging. For best performance, note that element access by index is about #: four times faster than by name. Note: in Python 2, name is a bytestring, #: not a unicode string. In Python 3 it is a string decoded with ``os.fsdecode()``.
def setUp(self): if support.TESTFN_UNENCODABLE: self.dir = support.TESTFN_UNENCODABLE else: self.dir = support.TESTFN self.bdir = os.fsencode(self.dir) bytesfn = [] def add_filename(fn): try: fn = os.fsencode(fn) except UnicodeEncodeError: return bytesfn.append(fn) add_filename(support.TESTFN_UNICODE) if support.TESTFN_UNENCODABLE: add_filename(support.TESTFN_UNENCODABLE) if not bytesfn: self.skipTest("couldn't create any non-ascii filename") self.unicodefn = set() os.mkdir(self.dir) try: for fn in bytesfn: f = open(os.path.join(self.bdir, fn), "w") f.close() fn = os.fsdecode(fn) if fn in self.unicodefn: raise ValueError("duplicate filename") self.unicodefn.add(fn) except: shutil.rmtree(self.dir) raise
def test_nop(self): self.assertEqual(os.fsencode(b'abc\xff'), b'abc\xff') self.assertEqual(os.fsdecode('abc\u0141'), 'abc\u0141')
def test_identity(self): # assert fsdecode(fsencode(x)) == x for fn in ('unicode\u0141', 'latin\xe9', 'ascii'): try: bytesfn = os.fsencode(fn) except UnicodeEncodeError: continue self.assertEqual(os.fsdecode(bytesfn), fn)