本文整理匯總了Python中sys.getfilesystemencodeerrors方法的典型用法代碼示例。如果您正苦於以下問題:Python sys.getfilesystemencodeerrors方法的具體用法?Python sys.getfilesystemencodeerrors怎麽用?Python sys.getfilesystemencodeerrors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sys
的用法示例。
在下文中一共展示了sys.getfilesystemencodeerrors方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: write_file
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getfilesystemencodeerrors [as 別名]
def write_file(
self,
path: PATH_TYPES,
contents: Union[str, bytes, IO],
encoding: Optional[str] = None,
errors: Optional[str] = None,
) -> None:
"""Write data to the provided path. If **contents** is a string, the file will
be opened and written in "r" + "utf-8" mode, if bytes are supplied it will be
accessed using "rb" mode (i.e. binary write)."""
if encoding is not None:
if errors is None:
try:
errors = sys.getfilesystemencodeerrors() # type: ignore
except AttributeError:
errors = "surrogateescape"
if isinstance(contents, str):
contents = contents.encode(encoding=encoding, errors=errors)
elif isinstance(contents, bytes):
contents = contents.decode(encoding=encoding, errors=errors)
with self.connection() as conn:
conn.put_object(self.default_container, str(path), contents)
return
示例2: read_file
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getfilesystemencodeerrors [as 別名]
def read_file(
self,
path: PATH_TYPES,
text: bool = True,
encoding: str = "utf-8",
errors: Optional[str] = None,
) -> Union[str, bytes]:
"""Return the contents of the requested file, either a a bytestring or a unicode
string depending on whether **text** is True"""
content: Union[str, bytes]
if not errors:
try:
errors = sys.getfilesystemencodeerrors() # type: ignore
except AttributeError:
errors = "surrogateescape"
kwargs: Dict[str, Any] = {}
if errors:
kwargs["errors"] = errors
content = self.get_object(self.default_container, str(path))
if text and isinstance(content, bytes):
content = content.decode(encoding=encoding, **kwargs)
return content
示例3: _fscodec
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getfilesystemencodeerrors [as 別名]
def _fscodec():
encoding = sys.getfilesystemencoding()
errors = sys.getfilesystemencodeerrors()
def fsencode(filename):
"""Encode filename (an os.PathLike, bytes, or str) to the filesystem
encoding with 'surrogateescape' error handler, return bytes unchanged.
On Windows, use 'strict' error handler if the file system encoding is
'mbcs' (which is the default encoding).
"""
filename = fspath(filename) # Does type-checking of `filename`.
if isinstance(filename, str):
return filename.encode(encoding, errors)
else:
return filename
def fsdecode(filename):
"""Decode filename (an os.PathLike, bytes, or str) from the filesystem
encoding with 'surrogateescape' error handler, return str unchanged. On
Windows, use 'strict' error handler if the file system encoding is
'mbcs' (which is the default encoding).
"""
filename = fspath(filename) # Does type-checking of `filename`.
if isinstance(filename, bytes):
return filename.decode(encoding, errors)
else:
return filename
return fsencode, fsdecode
示例4: collect_sys
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getfilesystemencodeerrors [as 別名]
def collect_sys(info_add):
attributes = (
'_framework',
'abiflags',
'api_version',
'builtin_module_names',
'byteorder',
'dont_write_bytecode',
'executable',
'flags',
'float_info',
'float_repr_style',
'hash_info',
'hexversion',
'implementation',
'int_info',
'maxsize',
'maxunicode',
'path',
'platform',
'prefix',
'thread_info',
'version',
'version_info',
'winver',
)
copy_attributes(info_add, sys, 'sys.%s', attributes)
call_func(info_add, 'sys.androidapilevel', sys, 'getandroidapilevel')
call_func(info_add, 'sys.windowsversion', sys, 'getwindowsversion')
encoding = sys.getfilesystemencoding()
if hasattr(sys, 'getfilesystemencodeerrors'):
encoding = '%s/%s' % (encoding, sys.getfilesystemencodeerrors())
info_add('sys.filesystem_encoding', encoding)
for name in ('stdin', 'stdout', 'stderr'):
stream = getattr(sys, name)
if stream is None:
continue
encoding = getattr(stream, 'encoding', None)
if not encoding:
continue
errors = getattr(stream, 'errors', None)
if errors:
encoding = '%s/%s' % (encoding, errors)
info_add('sys.%s.encoding' % name, encoding)
# Were we compiled --with-pydebug or with #define Py_DEBUG?
Py_DEBUG = hasattr(sys, 'gettotalrefcount')
if Py_DEBUG:
text = 'Yes (sys.gettotalrefcount() present)'
else:
text = 'No (sys.gettotalrefcount() missing)'
info_add('Py_DEBUG', text)
示例5: get_expected_config
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getfilesystemencodeerrors [as 別名]
def get_expected_config(self, expected_core, expected_global, env):
expected_core = dict(self.DEFAULT_CORE_CONFIG, **expected_core)
expected_global = dict(self.DEFAULT_GLOBAL_CONFIG, **expected_global)
code = textwrap.dedent('''
import json
import sys
data = {
'prefix': sys.prefix,
'base_prefix': sys.base_prefix,
'exec_prefix': sys.exec_prefix,
'base_exec_prefix': sys.base_exec_prefix,
'Py_FileSystemDefaultEncoding': sys.getfilesystemencoding(),
'Py_FileSystemDefaultEncodeErrors': sys.getfilesystemencodeerrors(),
}
data = json.dumps(data)
data = data.encode('utf-8')
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
''')
# Use -S to not import the site module: get the proper configuration
# when test_embed is run from a venv (bpo-35313)
args = (sys.executable, '-S', '-c', code)
env = dict(env)
if not expected_global['Py_IsolatedFlag']:
env['PYTHONCOERCECLOCALE'] = '0'
env['PYTHONUTF8'] = '0'
proc = subprocess.run(args, env=env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
if proc.returncode:
raise Exception(f"failed to get the default config: "
f"stdout={proc.stdout!r} stderr={proc.stderr!r}")
stdout = proc.stdout.decode('utf-8')
config = json.loads(stdout)
for key, value in expected_core.items():
if value is self.GET_DEFAULT_CONFIG:
expected_core[key] = config[key]
for key, value in expected_global.items():
if value is self.GET_DEFAULT_CONFIG:
expected_global[key] = config[key]
return (expected_core, expected_global)