Skip to content

Commit b55cc6d

Browse files
authored
Do not call signal.SIG_DFL when forwarding SIGWINCH (#30)
- fix ReaderConsole.run_user_init_file for Python 3 - revisit testing/test_functional.py Fixes 0f07d6d (#29).
1 parent 4d788a9 commit b55cc6d

File tree

3 files changed

+90
-17
lines changed

3 files changed

+90
-17
lines changed

pyrepl/python_reader.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ def run_user_init_file(self):
179179
else:
180180
return
181181
try:
182-
execfile(initfile, self.locals, self.locals)
182+
with open(initfile, "r") as f:
183+
exec(compile(f.read(), initfile, "exec"), self.locals, self.locals)
183184
except:
184185
etype, value, tb = sys.exc_info()
185186
traceback.print_exception(etype, value, tb.tb_next)

pyrepl/unix_console.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ def restore(self):
401401
def __sigwinch(self, signum, frame):
402402
self.height, self.width = self.getheightwidth()
403403
self.event_queue.insert(Event('resize', None))
404-
self.old_sigwinch(signum, frame)
404+
if self.old_sigwinch != signal.SIG_DFL:
405+
self.old_sigwinch(signum, frame)
405406

406407
def push_char(self, char):
407408
trace('push char {char!r}', char=char)

testing/test_functional.py

+86-15
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,98 @@
33
# License: MIT
44
# some functional tests, to see if this is really working
55

6-
import pytest
6+
import os
7+
import signal
78
import sys
9+
import textwrap
10+
11+
import pytest
12+
13+
try:
14+
import pexpect
15+
except ImportError as exc:
16+
pytest.skip("could not import pexpect: {}".format(exc),
17+
allow_module_level=True)
818

919

1020
@pytest.fixture
11-
def child(request):
12-
try:
13-
pexpect = pytest.importorskip('pexpect')
14-
except SyntaxError:
15-
pytest.skip('pexpect wont work on py3k')
16-
child = pexpect.spawn(sys.executable, ['-S'], timeout=10)
17-
if sys.version_info >= (3, ):
18-
child.logfile = sys.stdout.buffer
19-
else:
20-
child.logfile = sys.stdout
21-
child.sendline('from pyrepl.python_reader import main')
22-
child.sendline('main()')
21+
def start_child():
22+
ret_childs = []
23+
24+
def start_child_func(env_update=None):
25+
assert not ret_childs, "child started already"
26+
27+
env = {k: v for k, v in os.environ.items() if k in (
28+
"TERM",
29+
)}
30+
if env_update:
31+
env.update(env_update)
32+
child = pexpect.spawn(sys.executable, timeout=5, env=env)
33+
if sys.version_info >= (3, ):
34+
child.logfile = sys.stdout.buffer
35+
else:
36+
child.logfile = sys.stdout
37+
child.expect_exact(">>> ")
38+
child.sendline('from pyrepl.python_reader import main')
39+
ret_childs.append(child)
40+
return child
41+
42+
yield start_child_func
43+
44+
assert ret_childs, "child was not started"
45+
child = ret_childs[0]
46+
47+
child.sendeof()
48+
child.expect_exact(">>> ")
49+
# Verify there's no error, e.g. when signal.SIG_DFL would be called.
50+
before = child.before.decode()
51+
assert "Traceback (most recent call last):" not in before
52+
child.sendeof()
53+
assert child.wait() == 0
54+
55+
56+
@pytest.fixture
57+
def child(start_child):
58+
child = start_child()
59+
child.sendline("main()")
2360
return child
2461

2562

2663
def test_basic(child):
27-
child.sendline('a = 3')
64+
child.expect_exact("->> ")
65+
child.sendline('a = 40 + 2')
66+
child.expect_exact("->> ")
2867
child.sendline('a')
29-
child.expect('3')
68+
child.expect_exact('42')
69+
child.expect_exact("->> ")
70+
71+
72+
def test_sigwinch_default(child):
73+
child.expect_exact("->> ")
74+
os.kill(child.pid, signal.SIGWINCH)
75+
76+
77+
def test_sigwinch_forwarded(start_child, tmpdir):
78+
with open(str(tmpdir.join("initfile")), "w") as initfile:
79+
initfile.write(textwrap.dedent(
80+
"""
81+
import signal
82+
83+
called = []
84+
85+
def custom_handler(signum, frame):
86+
called.append([signum, frame])
87+
88+
signal.signal(signal.SIGWINCH, custom_handler)
89+
90+
print("PYREPLSTARTUP called")
91+
"""
92+
))
93+
94+
child = start_child(env_update={"PYREPLSTARTUP": initfile.name})
95+
child.sendline("main()")
96+
child.expect_exact("PYREPLSTARTUP called")
97+
child.expect_exact("->> ")
98+
os.kill(child.pid, signal.SIGWINCH)
99+
child.sendline('"called={}".format(len(called))')
100+
child.expect_exact("called=1")

0 commit comments

Comments
 (0)