Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9decc0dc
Commit
9decc0dc
authored
Mar 07, 2007
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #1675471: convert test_pty to unittest.
parent
05c075d6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
123 additions
and
116 deletions
+123
-116
Lib/test/output/test_pty
Lib/test/output/test_pty
+0
-3
Lib/test/test_pty.py
Lib/test/test_pty.py
+123
-113
No files found.
Lib/test/output/test_pty
deleted
100644 → 0
View file @
05c075d6
test_pty
I wish to buy a fish license.
For my pet fish, Eric.
Lib/test/test_pty.py
View file @
9decc0dc
import
pty
,
os
,
sys
,
signal
from
test.test_support
import
verbose
,
TestFailed
,
TestSkipped
import
pty
import
os
import
signal
from
test.test_support
import
verbose
,
TestSkipped
,
run_unittest
import
unittest
TEST_STRING_1
=
"I wish to buy a fish license.
\
n
"
TEST_STRING_2
=
"For my pet fish, Eric.
\
n
"
...
...
@@ -11,6 +14,7 @@ else:
def
debug
(
msg
):
pass
def
normalize_output
(
data
):
# Some operating systems do conversions on newline. We could possibly
# fix that by doing the appropriate termios.tcsetattr()s. I couldn't
...
...
@@ -32,116 +36,122 @@ def normalize_output(data):
return
data
# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
# because pty code is not too portable.
def
test_basic_pty
():
try
:
debug
(
"Calling master_open()"
)
master_fd
,
slave_name
=
pty
.
master_open
()
debug
(
"Got master_fd '%d', slave_name '%s'"
%
(
master_fd
,
slave_name
))
debug
(
"Calling slave_open(%r)"
%
(
slave_name
,))
slave_fd
=
pty
.
slave_open
(
slave_name
)
debug
(
"Got slave_fd '%d'"
%
slave_fd
)
except
OSError
:
# " An optional feature could not be imported " ... ?
raise
TestSkipped
,
"Pseudo-terminals (seemingly) not functional."
if
not
os
.
isatty
(
slave_fd
):
raise
TestFailed
,
"slave_fd is not a tty"
debug
(
"Writing to slave_fd"
)
os
.
write
(
slave_fd
,
TEST_STRING_1
)
s1
=
os
.
read
(
master_fd
,
1024
)
sys
.
stdout
.
write
(
normalize_output
(
s1
))
debug
(
"Writing chunked output"
)
os
.
write
(
slave_fd
,
TEST_STRING_2
[:
5
])
os
.
write
(
slave_fd
,
TEST_STRING_2
[
5
:])
s2
=
os
.
read
(
master_fd
,
1024
)
sys
.
stdout
.
write
(
normalize_output
(
s2
))
os
.
close
(
slave_fd
)
os
.
close
(
master_fd
)
def
handle_sig
(
sig
,
frame
):
raise
TestFailed
,
"isatty hung"
# isatty() and close() can hang on some platforms
# set an alarm before running the test to make sure we don't hang forever
old_alarm
=
signal
.
signal
(
signal
.
SIGALRM
,
handle_sig
)
signal
.
alarm
(
10
)
try
:
test_basic_pty
()
finally
:
# remove alarm, restore old alarm handler
signal
.
alarm
(
0
)
signal
.
signal
(
signal
.
SIGALRM
,
old_alarm
)
# basic pty passed.
debug
(
"calling pty.fork()"
)
pid
,
master_fd
=
pty
.
fork
()
if
pid
==
pty
.
CHILD
:
# stdout should be connected to a tty.
if
not
os
.
isatty
(
1
):
debug
(
"Child's fd 1 is not a tty?!"
)
os
.
_exit
(
3
)
# After pty.fork(), the child should already be a session leader.
# (on those systems that have that concept.)
debug
(
"In child, calling os.setsid()"
)
try
:
os
.
setsid
()
except
OSError
:
# Good, we already were session leader
debug
(
"Good: OSError was raised."
)
pass
except
AttributeError
:
# Have pty, but not setsid() ?
debug
(
"No setsid() available ?"
)
pass
except
:
# We don't want this error to propagate, escaping the call to
# os._exit() and causing very peculiar behavior in the calling
# regrtest.py !
# Note: could add traceback printing here.
debug
(
"An unexpected error was raised."
)
os
.
_exit
(
1
)
else
:
debug
(
"os.setsid() succeeded! (bad!)"
)
os
.
_exit
(
2
)
os
.
_exit
(
4
)
else
:
debug
(
"Waiting for child (%d) to finish."
%
pid
)
##line = os.read(master_fd, 80)
##lines = line.replace('\r\n', '\n').split('\n')
##if False and lines != ['In child, calling os.setsid()',
## 'Good: OSError was raised.', '']:
## raise TestFailed("Unexpected output from child: %r" % line)
(
pid
,
status
)
=
os
.
waitpid
(
pid
,
0
)
res
=
status
>>
8
debug
(
"Child (%d) exited with status %d (%d)."
%
(
pid
,
res
,
status
))
if
res
==
1
:
raise
TestFailed
,
"Child raised an unexpected exception in os.setsid()"
elif
res
==
2
:
raise
TestFailed
,
"pty.fork() failed to make child a session leader."
elif
res
==
3
:
raise
TestFailed
,
"Child spawned by pty.fork() did not have a tty as stdout"
elif
res
!=
4
:
raise
TestFailed
,
"pty.fork() failed for unknown reasons."
##debug("Reading from master_fd now that the child has exited")
##try:
## s1 = os.read(master_fd, 1024)
##except os.error:
## pass
##else:
## raise TestFailed("Read from master_fd did not raise exception")
os
.
close
(
master_fd
)
# pty.fork() passed.
class
PtyTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
# isatty() and close() can hang on some platforms. Set an alarm
# before running the test to make sure we don't hang forever.
self
.
old_alarm
=
signal
.
signal
(
signal
.
SIGALRM
,
self
.
handle_sig
)
signal
.
alarm
(
10
)
def
tearDown
(
self
):
# remove alarm, restore old alarm handler
signal
.
alarm
(
0
)
signal
.
signal
(
signal
.
SIGALRM
,
self
.
old_alarm
)
def
handle_sig
(
self
,
sig
,
frame
):
self
.
fail
(
"isatty hung"
)
def
test_basic
(
self
):
try
:
debug
(
"Calling master_open()"
)
master_fd
,
slave_name
=
pty
.
master_open
()
debug
(
"Got master_fd '%d', slave_name '%s'"
%
(
master_fd
,
slave_name
))
debug
(
"Calling slave_open(%r)"
%
(
slave_name
,))
slave_fd
=
pty
.
slave_open
(
slave_name
)
debug
(
"Got slave_fd '%d'"
%
slave_fd
)
except
OSError
:
# " An optional feature could not be imported " ... ?
raise
TestSkipped
,
"Pseudo-terminals (seemingly) not functional."
self
.
assertTrue
(
os
.
isatty
(
slave_fd
),
'slave_fd is not a tty'
)
debug
(
"Writing to slave_fd"
)
os
.
write
(
slave_fd
,
TEST_STRING_1
)
s1
=
os
.
read
(
master_fd
,
1024
)
self
.
assertEquals
(
'I wish to buy a fish license.
\
n
'
,
normalize_output
(
s1
))
debug
(
"Writing chunked output"
)
os
.
write
(
slave_fd
,
TEST_STRING_2
[:
5
])
os
.
write
(
slave_fd
,
TEST_STRING_2
[
5
:])
s2
=
os
.
read
(
master_fd
,
1024
)
self
.
assertEquals
(
'For my pet fish, Eric.
\
n
'
,
normalize_output
(
s2
))
os
.
close
(
slave_fd
)
os
.
close
(
master_fd
)
def
test_fork
(
self
):
debug
(
"calling pty.fork()"
)
pid
,
master_fd
=
pty
.
fork
()
if
pid
==
pty
.
CHILD
:
# stdout should be connected to a tty.
if
not
os
.
isatty
(
1
):
debug
(
"Child's fd 1 is not a tty?!"
)
os
.
_exit
(
3
)
# After pty.fork(), the child should already be a session leader.
# (on those systems that have that concept.)
debug
(
"In child, calling os.setsid()"
)
try
:
os
.
setsid
()
except
OSError
:
# Good, we already were session leader
debug
(
"Good: OSError was raised."
)
pass
except
AttributeError
:
# Have pty, but not setsid()?
debug
(
"No setsid() available?"
)
pass
except
:
# We don't want this error to propagate, escaping the call to
# os._exit() and causing very peculiar behavior in the calling
# regrtest.py !
# Note: could add traceback printing here.
debug
(
"An unexpected error was raised."
)
os
.
_exit
(
1
)
else
:
debug
(
"os.setsid() succeeded! (bad!)"
)
os
.
_exit
(
2
)
os
.
_exit
(
4
)
else
:
debug
(
"Waiting for child (%d) to finish."
%
pid
)
##line = os.read(master_fd, 80)
##lines = line.replace('\r\n', '\n').split('\n')
##if False and lines != ['In child, calling os.setsid()',
## 'Good: OSError was raised.', '']:
## raise TestFailed("Unexpected output from child: %r" % line)
(
pid
,
status
)
=
os
.
waitpid
(
pid
,
0
)
res
=
status
>>
8
debug
(
"Child (%d) exited with status %d (%d)."
%
(
pid
,
res
,
status
))
if
res
==
1
:
self
.
fail
(
"Child raised an unexpected exception in os.setsid()"
)
elif
res
==
2
:
self
.
fail
(
"pty.fork() failed to make child a session leader."
)
elif
res
==
3
:
self
.
fail
(
"Child spawned by pty.fork() did not have a tty as stdout"
)
elif
res
!=
4
:
self
.
fail
(
"pty.fork() failed for unknown reasons."
)
##debug("Reading from master_fd now that the child has exited")
##try:
## s1 = os.read(master_fd, 1024)
##except os.error:
## pass
##else:
## raise TestFailed("Read from master_fd did not raise exception")
os
.
close
(
master_fd
)
# pty.fork() passed.
def
test_main
(
verbose
=
None
):
run_unittest
(
PtyTest
)
if
__name__
==
"__main__"
:
test_main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment