Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/asyncio/proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ async def _sock_sendfile_native(self, sock, file, offset, count):
async def _sendfile_native(self, transp, file, offset, count):
resume_reading = transp.is_reading()
transp.pause_reading()
await transp._make_empty_waiter()
try:
await transp._make_empty_waiter()
return await self.sock_sendfile(transp._sock, file, offset, count,
fallback=False)
finally:
Expand Down
2 changes: 1 addition & 1 deletion Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,8 @@ async def _sendfile_native(self, transp, file, offset, count):
del self._transports[transp._sock_fd]
resume_reading = transp.is_reading()
transp.pause_reading()
await transp._make_empty_waiter()
try:
await transp._make_empty_waiter()
return await self.sock_sendfile(transp._sock, file, offset, count,
fallback=False)
finally:
Expand Down
4 changes: 2 additions & 2 deletions Lib/idlelib/idle_test/test_configdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ def test_fontlist_key(self):
font = d.fontlist.get('active')

# Test Down key.
fontlist.focus_force()
fontlist.update()
fontlist.focus_force()
fontlist.event_generate('<Key-Down>')
fontlist.event_generate('<KeyRelease-Down>')

Expand All @@ -160,8 +160,8 @@ def test_fontlist_key(self):
self.assertIn(d.font_name.get(), down_font.lower())

# Test Up key.
fontlist.focus_force()
fontlist.update()
fontlist.focus_force()
fontlist.event_generate('<Key-Up>')
fontlist.event_generate('<KeyRelease-Up>')

Expand Down
41 changes: 41 additions & 0 deletions Lib/test/test_asyncio/test_sendfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,47 @@ def test_sendfile(self):
self.assertEqual(srv_proto.data, self.DATA)
self.assertEqual(self.file.tell(), len(self.DATA))

def test_sendfile_cancel_empty_waiter(self):
for reading in (True, False):
with self.subTest(reading=reading):
srv_proto, cli_proto = self.prepare_sendfile()
transport = cli_proto.transport
if not reading:
transport.pause_reading()
waiter = self.loop.create_future()

def make_empty_waiter():
transport._empty_waiter = waiter
return waiter

with mock.patch.object(transport, '_make_empty_waiter',
side_effect=make_empty_waiter):
task = self.loop.create_task(
self.loop.sendfile(transport, self.file))
test_utils.run_briefly(self.loop)
self.assertIs(transport._empty_waiter, waiter)
self.assertFalse(waiter.done())
self.assertFalse(transport.is_reading())
task.cancel()
with self.assertRaises(asyncio.CancelledError):
self.run_loop(task)

try:
self.assertIsNone(transport._empty_waiter)
self.assertEqual(transport.is_reading(), reading)
if isinstance(self.loop, asyncio.SelectorEventLoop):
self.assertIs(
self.loop._transports[transport._sock_fd],
transport)
finally:
transport._reset_empty_waiter()

ret = self.run_loop(self.loop.sendfile(transport, self.file))
transport.close()
self.run_loop(srv_proto.done)
self.assertEqual(ret, len(self.DATA))
self.assertEqual(srv_proto.data, self.DATA)

def test_sendfile_force_fallback(self):
srv_proto, cli_proto = self.prepare_sendfile()

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_io/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,18 @@ def test_cow_mutable(self):
memio = self.ioclass(ba)
self.assertEqual(sys.getrefcount(ba), old_rc)

def test_write_with_export(self):
memio = self.ioclass(b"abcd")
memio.seek(2)
with memio.getbuffer() as view:
self.assertRaises(BufferError, memio.__init__, b"replacement")
self.assertEqual(memio.tell(), 2)
self.assertEqual(memio.getvalue(), b"abcd")
self.assertEqual(bytes(view), b"abcd")
memio.write(b"X")
self.assertEqual(memio.getvalue(), b"abXd")


class CStringIOTest(PyStringIOTest):
ioclass = io.StringIO
UnsupportedOperation = io.UnsupportedOperation
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_tkinter/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def require_mapped(self, widget, timeout=None):
f'(timed out after {timeout:g}s)')


class AbstractDialogTest(AbstractTkTest):
# Tk delivers generated keyboard events to the focused window. Hide the
# root window, otherwise the window manager can take the focus back from
# the dialog (gh-154357).

def setUp(self):
super().setUp()
self.root.withdraw()


class AbstractDefaultRootTest:

def setUp(self):
Expand Down Expand Up @@ -112,6 +122,7 @@ def wait_until_mapped(widget, timeout=None, *, full_size=False):
timeout = support.LOOPBACK_TIMEOUT
deadline = time.monotonic() + timeout
widget.update_idletasks()
reset = False
while True:
widget.update() # drain pending Map/Configure events
if widget.winfo_ismapped():
Expand All @@ -123,6 +134,11 @@ def wait_until_mapped(widget, timeout=None, *, full_size=False):
h_ok = widget.winfo_height() > 1
if w_ok and h_ok:
return True
if full_size and not reset:
# Tk no longer resizes the toplevel to fit its content if
# the window manager has resized it. Undo this.
widget.winfo_toplevel().wm_geometry('')
reset = True
if time.monotonic() >= deadline:
return False
time.sleep(0.01)
Expand Down
12 changes: 7 additions & 5 deletions Lib/test/test_tkinter/test_filedialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tkinter.commondialog import Dialog
from test.support import requires, swap_attr
from test.test_tkinter.support import setUpModule # noqa: F401
from test.test_tkinter.support import AbstractTkTest
from test.test_tkinter.support import AbstractDialogTest, AbstractTkTest

requires('gui')

Expand Down Expand Up @@ -72,7 +72,7 @@ def test_results_preserved(self):
('/a', '/b'))


class FileDialogTest(AbstractTkTest, unittest.TestCase):
class FileDialogTest(AbstractDialogTest, unittest.TestCase):
# The pure-Python FileDialog runs its own modal loop in go(); its logic is
# exercised here without entering the loop.

Expand Down Expand Up @@ -164,8 +164,8 @@ def test_alt_key(self):
d = self.open()
invoked = []
d.cancel_button.configure(command=lambda: invoked.append(True))
d.top.focus_force()
d.top.update()
d.top.focus_force()
d.top.event_generate('<Alt-c>') # "&Cancel"
d.top.update()
self.assertTrue(invoked)
Expand All @@ -174,8 +174,8 @@ def test_escape_cancels(self):
# The Escape key cancels the dialog.
d = self.open()
d.how = 'spam'
d.top.focus_force()
d.top.update()
d.top.focus_force()
d.top.event_generate('<Escape>')
d.top.update()
self.assertIsNone(d.how)
Expand All @@ -195,8 +195,10 @@ def test_type_ahead(self):
d.files.delete(0, 'end')
for name in ('alpha', 'bravo', 'charlie'):
d.files.insert('end', name)
d.files.focus_force()
d.top.update()
# Force the focus right before generating the event: the window
# manager can take it back.
d.files.focus_force()
d.files.event_generate('<Key>', keysym='c')
d.top.update()
sel = d.files.curselection()
Expand Down
26 changes: 20 additions & 6 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from test.test_tkinter.support import setUpModule # noqa: F401
from test.test_tkinter.support import (AbstractTkTest, AbstractDefaultRootTest,
requires_tk, get_tk_patchlevel,
tcl_version, tk_version)
tcl_version, tk_version,
wait_until_mapped)

support.requires('gui')

Expand Down Expand Up @@ -508,15 +509,20 @@ def test_focus_methods(self):
self.root.update_idletasks()
f.focus_force()
self.root.update()
self.assertIs(self.root.focus_get(), f)
self.assertIs(self.root.focus_displayof(), f)
# The window manager can take the focus away, and then focus_get()
# and focus_displayof() return None.
if self.root.focus_displayof() is not None:
self.assertIs(self.root.focus_get(), f)
self.assertIs(self.root.focus_displayof(), f)
self.assertIs(f.focus_lastfor(), f)
b = tkinter.Button(f)
b.pack()
self.root.update()
b.focus_set()
self.root.update()
self.assertIs(self.root.focus_get(), b)
if self.root.focus_displayof() is not None:
self.assertIs(self.root.focus_get(), b)
self.assertIs(f.focus_lastfor(), b)

def test_focus_methods_unresolvable(self):
# The focus may be on a widget that tkinter did not create and so
Expand Down Expand Up @@ -1319,9 +1325,15 @@ def test_wm_transient(self):
def test_wm_stackorder(self):
t1 = tkinter.Toplevel(self.root)
t2 = tkinter.Toplevel(self.root)
if self.root._windowingsystem == 'x11':
# Bypass the window manager, which may ignore lift() or reorder
# the windows while they are being mapped.
t1.overrideredirect(True)
t2.overrideredirect(True)
t1.deiconify()
t2.deiconify()
self.root.update()
wait_until_mapped(t1)
wait_until_mapped(t2)
t1.lift(t2) # Raise t1 above t2.
self.root.update()
order = self.root.wm_stackorder()
Expand Down Expand Up @@ -1361,7 +1373,9 @@ def test_focus(self):

f.focus_force()
self.root.update()
self.assertEqual(len(events), 1, events)
# The window manager can take the focus away and give it back,
# which makes Tk generate additional focus events.
self.assertGreaterEqual(len(events), 1, events)
e = events[0]
self.assertIs(e.type, tkinter.EventType.FocusIn)
self.assertIs(e.widget, f)
Expand Down
Loading
Loading