##// END OF EJS Templates
terminal interface: catch exception when standard stream is closed...
terminal interface: catch exception when standard stream is closed if a standard stream is closed, e.g. `import sys; sys.stdin.close()`, a ValueError is raised when the stream is read. this commit adds a try .. except statement to catch that exception in IPython/terminal/interactiveshell.py

File last commit:

r27495:1a9d9554
r27749:1dcbbc47
Show More
io.py
157 lines | 4.7 KiB | text/x-python | PythonLexer
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 # encoding: utf-8
"""
IO related utilities.
"""
Min RK
move io.unicode_std_stream to nbconvert.utils.io
r21111 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Min RK
move io.unicode_std_stream to nbconvert.utils.io
r21111
John Kirkham
IPython/utils/io.py: Make sure `devnull` is closed at exit to avoid a `ResourceWarning` being raised by Python 3.
r21762 import atexit
Brandon Parsons
moved getdefaultencoding from text to py3compat
r6653 import os
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 import sys
import tempfile
Min RK
fix some deprecations...
r22742 import warnings
Samreen Zarroug
use pathlib.Path in io.py
r26154 from pathlib import Path
Matthias Bussonnier
Fix some import....
r21298 from warnings import warn
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192
from IPython.utils.decorators import undoc
MinRK
capture rich output as well as stdout/err in capture_output...
r12223 from .capture import CapturedIO, capture_output
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Brandon Parsons
ensure a fallback exists, so use local std{in,out,err}...
r6652 # setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr
gousaiyang
Format code
r27495 devnull = open(os.devnull, "w", encoding="utf-8")
John Kirkham
IPython/utils/io.py: Make sure `devnull` is closed at exit to avoid a `ResourceWarning` being raised by Python 3.
r21762 atexit.register(devnull.close)
Min RK
fix some deprecations...
r22742
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
class Tee(object):
"""A class to duplicate an output stream to stdout/err.
This works in a manner very similar to the Unix 'tee' command.
When the object is closed or deleted, it closes the original file given to
it for duplication.
"""
# Inspired by:
# http://mail.python.org/pipermail/python-list/2007-May/442737.html
Thomas Kluyver
Fixes to utils.io.Tee
r4763 def __init__(self, file_or_name, mode="w", channel='stdout'):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """Construct a new Tee object.
Parameters
----------
file_or_name : filename or open filehandle (writable)
Matthias Bussonnier
reformat docstring in IPython utils
r26419 File that will be duplicated
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 mode : optional, valid mode for open().
Matthias Bussonnier
reformat docstring in IPython utils
r26419 If a filename was give, open with this mode.
Bernardo B. Marques
remove all trailling spaces
r4872 channel : str, one of ['stdout', 'stderr']
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """
if channel not in ['stdout', 'stderr']:
raise ValueError('Invalid channel spec %s' % channel)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Fixes to utils.io.Tee
r4763 if hasattr(file_or_name, 'write') and hasattr(file_or_name, 'seek'):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 self.file = file_or_name
else:
gousaiyang
Format code
r27495 encoding = None if "b" in mode else "utf-8"
gousaiyang
Fix EncodingWarning on Python 3.10
r27494 self.file = open(file_or_name, mode, encoding=encoding)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 self.channel = channel
self.ostream = getattr(sys, channel)
setattr(sys, channel, self)
self._closed = False
def close(self):
"""Close the file and restore the channel."""
self.flush()
setattr(sys, self.channel, self.ostream)
self.file.close()
self._closed = True
def write(self, data):
"""Write data to both channels."""
self.file.write(data)
self.ostream.write(data)
self.ostream.flush()
def flush(self):
"""Flush both channels."""
self.file.flush()
self.ostream.flush()
def __del__(self):
if not self._closed:
self.close()
Paul Ivanov
custom keyboard interrupt handling in ask_yes_no...
r13609 def ask_yes_no(prompt, default=None, interrupt=None):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """Asks a question and returns a boolean (y/n) answer.
If default is given (one of 'y','n'), it is used if the user input is
Paul Ivanov
custom keyboard interrupt handling in ask_yes_no...
r13609 empty. If interrupt is given (one of 'y','n'), it is used if the user
presses Ctrl-C. Otherwise the question is repeated until an answer is
given.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
An EOF is treated as the default answer. If there is no default, an
exception is raised to prevent infinite loops.
Valid answers are: y/yes/n/no (match is not case sensitive)."""
answers = {'y':True,'n':False,'yes':True,'no':False}
ans = None
while ans not in answers.keys():
try:
Thomas Kluyver
Fix references to raw_input()
r13355 ans = input(prompt+' ').lower()
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 if not ans: # response was an empty string
ans = default
except KeyboardInterrupt:
Paul Ivanov
custom keyboard interrupt handling in ask_yes_no...
r13609 if interrupt:
ans = interrupt
sachet-mittal
Insert a new line after catching KeyboardException in ask_yes_no...
r23031 print("\r")
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 except EOFError:
if default in answers.keys():
ans = default
Thomas Spura
Fix bare print calls that were missing '()'....
r4430 print()
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 else:
raise
return answers[ans]
def temp_pyfile(src, ext='.py'):
"""Make a temporary python file, return filename and filehandle.
Parameters
----------
src : string or list of strings (no need for ending newlines if list)
Matthias Bussonnier
reformat docstring in IPython utils
r26419 Source code to be written to the file.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 ext : optional, string
Matthias Bussonnier
reformat docstring in IPython utils
r26419 Extension for the generated file.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Returns
-------
(filename, open filehandle)
Matthias Bussonnier
reformat docstring in IPython utils
r26419 It is the caller's responsibility to close the open file and unlink it.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """
fname = tempfile.mkstemp(ext)[1]
gousaiyang
Format code
r27495 with open(Path(fname), "w", encoding="utf-8") as f:
Matthias Bussonnier
Do not return filehandle as unused
r25106 f.write(src)
f.flush()
return fname
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Matthias Bussonnier
Deprecate some methods from IPython.utils.io...
r24275 @undoc
Fernando Perez
Provide cleaner names for raw print utilities.
r2874 def raw_print(*args, **kw):
Matthias Bussonnier
Deprecate some methods from IPython.utils.io...
r24275 """DEPRECATED: Raw print to sys.__stdout__, otherwise identical interface to print()."""
warn("IPython.utils.io.raw_print has been deprecated since IPython 7.0", DeprecationWarning, stacklevel=2)
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improve io.rprint* interface, unify usage in ipkernel....
r2856 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
file=sys.__stdout__)
sys.__stdout__.flush()
Matthias Bussonnier
Deprecate some methods from IPython.utils.io...
r24275 @undoc
Fernando Perez
Provide cleaner names for raw print utilities.
r2874 def raw_print_err(*args, **kw):
Matthias Bussonnier
Deprecate some methods from IPython.utils.io...
r24275 """DEPRECATED: Raw print to sys.__stderr__, otherwise identical interface to print()."""
warn("IPython.utils.io.raw_print_err has been deprecated since IPython 7.0", DeprecationWarning, stacklevel=2)
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improve io.rprint* interface, unify usage in ipkernel....
r2856 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
file=sys.__stderr__)
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 sys.__stderr__.flush()