FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ipython/IPython/utils/io.py at python-3.14-dev · krassowski/ipython · GitHub
krassowski
ipython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ipython
/
IPython
/
utils
/
io.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
135 lines (108 loc) · 3.84 KB
Breadcrumbs
ipython
/
IPython
/
utils
/
io.py
Copy path
File metadata and controls
135 lines (108 loc) · 3.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# encoding: utf-8
"""
IO related utilities.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import
atexit
import
os
import
sys
import
tempfile
from
pathlib
import
Path
from
warnings
import
warn
from
IPython
.
utils
.
decorators
import
undoc
from
.
capture
import
CapturedIO
,
capture_output
class
Tee
:
"""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
def
__init__
(
self
,
file_or_name
,
mode
=
"w"
,
channel
=
'stdout'
):
"""Construct a new Tee object.
Parameters
----------
file_or_name : filename or open filehandle (writable)
File that will be duplicated
mode : optional, valid mode for open().
If a filename was give, open with this mode.
channel : str, one of ['stdout', 'stderr']
"""
if
channel
not
in
[
'stdout'
,
'stderr'
]:
raise
ValueError
(
'Invalid channel spec %s'
%
channel
)
if
hasattr
(
file_or_name
,
'write'
)
and
hasattr
(
file_or_name
,
'seek'
):
self
.
file
=
file_or_name
else
:
encoding
=
None
if
"b"
in
mode
else
"utf-8"
self
.
file
=
open
(
file_or_name
,
mode
,
encoding
=
encoding
)
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
()
def
isatty
(
self
):
return
False
def
ask_yes_no
(
prompt
,
default
=
None
,
interrupt
=
None
):
"""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
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.
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
:
ans
=
input
(
prompt
+
' '
).
lower
()
if
not
ans
:
# response was an empty string
ans
=
default
except
KeyboardInterrupt
:
if
interrupt
:
ans
=
interrupt
print
(
"
\r
"
)
except
EOFError
:
if
default
in
answers
.
keys
():
ans
=
default
print
()
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)
Source code to be written to the file.
ext : optional, string
Extension for the generated file.
Returns
-------
(filename, open filehandle)
It is the caller's responsibility to close the open file and unlink it.
"""
fname
=
tempfile
.
mkstemp
(
ext
)[
1
]
with
open
(
Path
(
fname
),
"w"
,
encoding
=
"utf-8"
)
as
f
:
f
.
write
(
src
)
f
.
flush
()
return
fname
Back
|
FazBrowse Home
|
New Git URL