本文整理汇总了Python中sys.std方法的典型用法代码示例。如果您正苦于以下问题:Python sys.std方法的具体用法?Python sys.std怎么用?Python sys.std使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sys
的用法示例。
在下文中一共展示了sys.std方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: import sys [as 别名]
# 或者: from sys import std [as 别名]
def __call__(self, string):
# the special argument "-" means sys.std{in,out}
if string == '-':
if 'r' in self._mode:
return sys.stdin
elif 'w' in self._mode:
return sys.stdout
else:
msg = _('argument "-" with mode %r') % self._mode
raise ValueError(msg)
# all other arguments are used as file names
try:
return open(string, self._mode, self._bufsize, self._encoding, self._errors)
except OSError as e:
message = _("can't open '%s': %s")
raise ArgumentTypeError(message % (string, e))
示例2: get_stream_enc
# 需要导入模块: import sys [as 别名]
# 或者: from sys import std [as 别名]
def get_stream_enc(stream, default=None):
"""Return the given stream's encoding or a default.
There are cases where ``sys.std*`` might not actually be a stream, so
check for the encoding attribute prior to returning it, and return
a default if it doesn't exist or evaluates as False. ``default``
is None if not provided.
"""
if not hasattr(stream, 'encoding') or not stream.encoding:
return default
else:
return stream.encoding
# Less conservative replacement for sys.getdefaultencoding, that will try
# to match the environment.
# Defined here as central function, so if we find better choices, we
# won't need to make changes all over IPython.
示例3: patch_sys
# 需要导入模块: import sys [as 别名]
# 或者: from sys import std [as 别名]
def patch_sys(stdin=True, stdout=True, stderr=True):
"""Patch sys.std[in,out,err] to use a cooperative IO via a threadpool.
This is relatively dangerous and can have unintended consequences such as hanging
the process or `misinterpreting control keys`_ when ``input`` and ``raw_input``
are used.
This method does nothing on Python 3. The Python 3 interpreter wants to flush
the TextIOWrapper objects that make up stderr/stdout at shutdown time, but
using a threadpool at that time leads to a hang.
.. _`misinterpreting control keys`: https://github.com/gevent/gevent/issues/274
"""
# test__issue6.py demonstrates the hang if these lines are removed;
# strangely enough that test passes even without monkey-patching sys
if PY3:
return
if stdin:
_patch_sys_std('stdin')
if stdout:
_patch_sys_std('stdout')
if stderr:
_patch_sys_std('stderr')
示例4: get_dropbox_client
# 需要导入模块: import sys [as 别名]
# 或者: from sys import std [as 别名]
def get_dropbox_client(username, setup=True, stdin=None, stdout=None):
"""
checks wether a dropbox.dropbox.Dropbox is available for username.
If it is, it is returned.
Otherwise, if setup is True, a command-line setup is shown.
The setup uses stdin and stout, both defaulting to the sys.std*.
If no client was found and setup is False, None will be returned.
"""
if stdout is None:
stdout = sys.stdout
if stdin is None:
stdin = sys.stdin
data = load_dropbox_data(username)
if data is None:
stdout.write("\n")
if not setup:
return None
dropbox_setup(username, stdin, stdout)
data = load_dropbox_data(username)
token = data["access_token"]
dbclient = dropbox.dropbox.Dropbox(token)
return dbclient
示例5: test_patching_std_streams
# 需要导入模块: import sys [as 别名]
# 或者: from sys import std [as 别名]
def test_patching_std_streams(self):
"""
Test if std streams are correctly redirected to the web-console
"""
time.sleep(1)
self.stdin.send_keys('n')
self.send_btn.click()
time.sleep(1)
self.assertIn('Enter something:', self.stdout_tag.text)
self.stdin.send_keys('spam')
self.send_btn.click()
time.sleep(1)
self.stdin.send_keys('n')
self.send_btn.click()
time.sleep(1)
self.assertIn('You have entered: spam', self.stdout_tag.text)
示例6: __call__
# 需要导入模块: import sys [as 别名]
# 或者: from sys import std [as 别名]
def __call__(self, string):
# the special argument "-" means sys.std{in,out}
if string == '-':
if 'r' in self._mode:
return sys.stdin
elif 'w' in self._mode:
return sys.stdout
else:
msg = 'argument "-" with mode %r' % self._mode
raise ValueError(msg)
# all other arguments are used as file names
try:
return open(string,
self._mode,
self._bufsize,
self._encoding,
self._errors,
newline=self._newline)
except OSError as e:
message = "can't open '%s': %s"
raise argparse.ArgumentTypeError(message % (string, e))
示例7: setUp
# 需要导入模块: import sys [as 别名]
# 或者: from sys import std [as 别名]
def setUp(self):
super().setUp()
self.patcher = MonkeyPatcher()
self.addCleanup(self.patcher.restore)
# Patch sys.std* and self.std*. Use TextIOWrapper to provide an
# identical API to the "real" stdin, stdout, and stderr objects.
self._addStream("stdin", self._wrapStream(self._buf_in))
self._addStream("stdout", self._wrapStream(self._buf_out))
self._addStream("stderr", self._wrapStream(self._buf_err))
self.patcher.patch()
示例8: __call__
# 需要导入模块: import sys [as 别名]
# 或者: from sys import std [as 别名]
def __call__(self, string):
from argparse import ArgumentTypeError
# the special argument "-" means sys.std{in,out}
if string == '-':
if 'r' in self._mode:
cfp = ConfFileProxy("<stdin>", "r", stream=sys.stdin, is_file=False)
if self._action == "load":
try:
d = cfp.data
del d
except ConfParserException as e:
raise ArgumentTypeError("failed to parse <stdin>: {}".format(e))
return cfp
elif 'w' in self._mode:
return ConfFileProxy("<stdout>", "w", stream=sys.stdout, is_file=False)
else:
raise ValueError('argument "-" with mode {}'.format(self._mode))
if self._accept_dir and os.path.isdir(string):
return ConfDirProxy(string, self._mode, parse_profile=self._parse_profile)
if self._action == "none":
return ConfFileProxy(string, self._mode, parse_profile=self._parse_profile)
else:
try:
# Another possible option is using: seekable() but that only works ONCE the stream is open
if os.path.isfile(string):
encoding = detect_by_bom(string)
stream = open(string, self._mode, encoding=encoding)
cfp = ConfFileProxy(string, self._mode, stream=stream,
parse_profile=self._parse_profile, is_file=True)
else:
# Could be an explicit link to /dev/stdin; or /dev/fd/63; a bash <(cmd) input
# Assume UTF-8 here because that's the default encoding expected by Splunk
stream = open(string, self._mode, encoding="utf-8")
cfp = ConfFileProxy(string, self._mode, stream=stream,
parse_profile=self._parse_profile, is_file=False)
if self._action == "load":
# Force file to be parsed by accessing the 'data' property
d = cfp.data
del d
return cfp
except IOError as e:
message = "can't open '%s': %s"
debug_traceback()
raise ArgumentTypeError(message % (string, e))
except ConfParserException as e:
debug_traceback()
raise ArgumentTypeError("failed to parse '%s': %s" % (string, e))
except TypeError as e:
debug_traceback()
raise ArgumentTypeError("Parser config error '%s': %s" % (string, e))