FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bpython/bpython/test/test_args.py at main · AND2797/bpython · GitHub
AND2797
/
bpython
Public
forked from
bpython/bpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
bpython
/
bpython
/
test
/
test_args.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
122 lines (107 loc) · 3.41 KB
Breadcrumbs
bpython
/
bpython
/
test
/
test_args.py
Copy path
File metadata and controls
122 lines (107 loc) · 3.41 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
import
errno
import
os
import
pty
import
re
import
select
import
subprocess
import
sys
import
tempfile
import
unittest
from
textwrap
import
dedent
from
bpython
import
args
from
bpython
.
config
import
getpreferredencoding
from
bpython
.
test
import
FixLanguageTestCase
as
TestCase
def
run_with_tty
(
command
):
# based on https://stackoverflow.com/questions/52954248/capture-output-as-a-tty-in-python
master_stdout
,
slave_stdout
=
pty
.
openpty
()
master_stderr
,
slave_stderr
=
pty
.
openpty
()
master_stdin
,
slave_stdin
=
pty
.
openpty
()
p
=
subprocess
.
Popen
(
command
,
stdout
=
slave_stdout
,
stderr
=
slave_stderr
,
stdin
=
slave_stdin
,
close_fds
=
True
,
)
for
fd
in
(
slave_stdout
,
slave_stderr
,
slave_stdin
):
os
.
close
(
fd
)
readable
=
[
master_stdout
,
master_stderr
]
result
=
{
master_stdout
:
b""
,
master_stderr
:
b""
}
try
:
while
readable
:
ready
,
_
,
_
=
select
.
select
(
readable
, [], [],
1
)
for
fd
in
ready
:
try
:
data
=
os
.
read
(
fd
,
512
)
except
OSError
as
e
:
if
e
.
errno
!=
errno
.
EIO
:
raise
# EIO means EOF on some systems
readable
.
remove
(
fd
)
else
:
if
not
data
:
# EOF
readable
.
remove
(
fd
)
result
[
fd
]
+=
data
finally
:
for
fd
in
(
master_stdout
,
master_stderr
,
master_stdin
):
os
.
close
(
fd
)
if
p
.
poll
()
is
None
:
p
.
kill
()
p
.
wait
()
if
p
.
returncode
:
raise
RuntimeError
(
f"Subprocess exited with
{
p
.
returncode
}
"
)
return
(
result
[
master_stdout
].
decode
(
getpreferredencoding
()),
result
[
master_stderr
].
decode
(
getpreferredencoding
()),
)
class
TestExecArgs
(
unittest
.
TestCase
):
def
test_exec_dunder_file
(
self
):
with
tempfile
.
NamedTemporaryFile
(
mode
=
"w"
)
as
f
:
f
.
write
(
dedent
(
"""
\
import sys
sys.stderr.write(__file__)
sys.stderr.flush()"""
)
)
f
.
flush
()
_
,
stderr
=
run_with_tty
(
[
sys
.
executable
]
+
[
"-m"
,
"bpython.curtsies"
,
f
.
name
]
)
self
.
assertEqual
(
stderr
.
strip
(),
f
.
name
)
def
test_exec_nonascii_file
(
self
):
with
tempfile
.
NamedTemporaryFile
(
mode
=
"w"
)
as
f
:
f
.
write
(
dedent
(
"""
\
# coding: utf-8
"你好 # nonascii"
"""
)
)
f
.
flush
()
_
,
stderr
=
run_with_tty
(
[
sys
.
executable
,
"-m"
,
"bpython.curtsies"
,
f
.
name
],
)
self
.
assertEqual
(
len
(
stderr
),
0
)
def
test_exec_nonascii_file_linenums
(
self
):
with
tempfile
.
NamedTemporaryFile
(
mode
=
"w"
)
as
f
:
f
.
write
(
dedent
(
"""
\
1/0
"""
)
)
f
.
flush
()
_
,
stderr
=
run_with_tty
(
[
sys
.
executable
,
"-m"
,
"bpython.curtsies"
,
f
.
name
],
)
self
.
assertIn
(
"line 1"
,
clean_colors
(
stderr
))
def
clean_colors
(
s
):
return
re
.
sub
(
r"\x1b[^m]*m"
,
""
,
s
)
class
TestParse
(
TestCase
):
def
test_version
(
self
):
with
self
.
assertRaises
(
SystemExit
):
args
.
parse
([
"--version"
])
Back
|
FazBrowse Home
|
New Git URL