Python sys.std() Examples
The following are 13
code examples of sys.std().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sys
, or try the search function
.
Example #1
Source File: dbutils.py From stash with MIT License | 7 votes |
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
Example #2
Source File: ksp_compiler.py From SublimeKSP with GNU General Public License v3.0 | 6 votes |
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))
Example #3
Source File: encoding.py From Computable with MIT License | 6 votes |
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.
Example #4
Source File: monkey.py From satori with Apache License 2.0 | 6 votes |
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')
Example #5
Source File: encoding.py From pySINDy with MIT License | 6 votes |
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.
Example #6
Source File: tests.py From python-web-pdb with MIT License | 6 votes |
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)
Example #7
Source File: monkey.py From PhonePi_SampleServer with MIT License | 6 votes |
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')
Example #8
Source File: plaid2text.py From plaid2text with GNU General Public License v3.0 | 6 votes |
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))
Example #9
Source File: encoding.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
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.
Example #10
Source File: monkey.py From PokemonGo-DesktopMap with MIT License | 6 votes |
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')
Example #11
Source File: monkey.py From PokemonGo-DesktopMap with MIT License | 6 votes |
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')
Example #12
Source File: fixtures.py From maas with GNU Affero General Public License v3.0 | 5 votes |
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()
Example #13
Source File: __init__.py From ksconf with Apache License 2.0 | 4 votes |
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))