Skip to content

Commit b28639b

Browse files
authored
Merge branch 'main' into gh-124951-neon
2 parents b45a6da + badf54f commit b28639b

19 files changed

Lines changed: 579 additions & 180 deletions

‎Doc/library/warnings.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ class, to turn a warning into an error we simply raise ``category(message)``.
188188
If a warning is reported and doesn't match any registered filter then the
189189
"default" action is applied (hence its name).
190190

191+
.. versionchanged:: 3.15
192+
Added regular expression support for *message* and *module*.
191193

192194

193195
.. _repeated-warning-suppression-criteria:

‎Lib/asyncio/tasks.py‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -841,17 +841,12 @@ def gather(*coros_or_futures, return_exceptions=False):
841841
return outer
842842

843843
loop = events._get_running_loop()
844-
if loop is not None:
845-
cur_task = current_task(loop)
846-
else:
847-
cur_task = None
848844

849-
def _done_callback(fut, cur_task=cur_task):
845+
def _done_callback(fut):
850846
nonlocal nfinished
851847
nfinished += 1
852848

853-
if cur_task is not None:
854-
futures.future_discard_from_awaited_by(fut, cur_task)
849+
futures.future_discard_from_awaited_by(fut, outer)
855850

856851
if outer is None or outer.done():
857852
if not fut.cancelled():
@@ -866,13 +861,13 @@ def _done_callback(fut, cur_task=cur_task):
866861
# instead of returning it.
867862
exc = fut._make_cancelled_error()
868863
# gh-157213: children outliving gather() must lose the edge
869-
_discard_awaited_by(children, cur_task)
864+
_discard_awaited_by(children, outer)
870865
outer.set_exception(exc)
871866
return
872867
else:
873868
exc = fut.exception()
874869
if exc is not None:
875-
_discard_awaited_by(children, cur_task)
870+
_discard_awaited_by(children, outer)
876871
outer.set_exception(exc)
877872
return
878873

@@ -939,10 +934,9 @@ def _done_callback(fut, cur_task=cur_task):
939934

940935
outer = _GatheringFuture(children, loop=loop)
941936
# Add the awaited-by edges only after all arguments are validated
942-
if cur_task is not None and len(done_futs) < nfuts:
943-
for fut in arg_to_fut.values():
944-
if not fut.done():
945-
futures.future_add_to_awaited_by(fut, cur_task)
937+
for fut in arg_to_fut.values():
938+
if not fut.done():
939+
futures.future_add_to_awaited_by(fut, outer)
946940
# Run done callbacks after GatheringFuture created so any post-processing
947941
# can be performed at this point
948942
# optimization: in the special case that *all* futures finished eagerly,

‎Lib/test/test_asyncio/test_graph.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,32 @@ async def main():
526526
'T<anon>',
527527
['s capture_test_stack', 'a deep', 'a c1'],
528528
[
529-
['T<anon>', ['a main', 'a test_stack_gather'], []]
529+
['F', [], [
530+
['T<anon>', ['a main', 'a test_stack_gather'], []],
531+
]],
530532
]
531533
])
532534

535+
async def test_gather_awaited_by_different_task(self):
536+
# gh-162061
537+
async def consume(fut):
538+
await fut
539+
540+
release = asyncio.Event()
541+
child = asyncio.create_task(release.wait())
542+
gathered = asyncio.gather(child)
543+
consumer = asyncio.create_task(consume(gathered), eager_start=True)
544+
graph = asyncio.capture_call_graph(child, limit=0)
545+
release.set()
546+
await consumer
547+
self.assertFalse(asyncio.capture_call_graph(child, limit=0).awaited_by)
548+
549+
waiter, = graph.awaited_by
550+
self.assertIs(waiter.future, gathered)
551+
parent, = waiter.awaited_by
552+
self.assertIs(parent.future, consumer)
553+
self.assertFalse(parent.awaited_by)
554+
533555
async def test_stack_gather_survivor(self):
534556
# gh-157213: a child that outlives gather() must not be shown as awaited
535557

@@ -1048,6 +1070,17 @@ def setUp(self):
10481070
self._Future = asyncio.Future
10491071
asyncio.Future = futures.Future = futures._PyFuture
10501072

1073+
# The real _GatheringFuture subclasses the C Future, which the
1074+
# pure-Python awaited-by helpers ignore.
1075+
class _PyGatheringFuture(futures._PyFuture):
1076+
def __init__(self, children, *, loop):
1077+
super().__init__(loop=loop)
1078+
self._children = children
1079+
self._cancel_requested = False
1080+
cancel = tasks._GatheringFuture.cancel
1081+
self.enterContext(mock.patch.object(tasks, '_GatheringFuture',
1082+
_PyGatheringFuture))
1083+
10511084
self._Task = asyncio.Task
10521085
asyncio.Task = tasks.Task = tasks._PyTask
10531086

‎Lib/test/test_clinic.py‎

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright 2012-2013 by Larry Hastings.
33
# Licensed to the PSF under a contributor agreement.
44

5-
from functools import partial
5+
from functools import partial, wraps
66
from test import support, test_tools
77
from test.support import force_not_colorized_test_class
88
from test.support import os_helper
@@ -3570,6 +3570,18 @@ class Foo "FooObject *" "Foo_Type"
35703570
"""
35713571
self.expect_failure(block, err, lineno=7)
35723572

3573+
def test_vectorcall_with_deprecated_parameters(self):
3574+
err = "@vectorcall does not support deprecated parameters"
3575+
block = """
3576+
module m
3577+
class Foo "FooObject *" "Foo_Type"
3578+
@vectorcall
3579+
Foo.__init__
3580+
a: object = None
3581+
/ [from 3.14]
3582+
"""
3583+
self.expect_failure(block, err, lineno=5)
3584+
35733585
def test_unused_param(self):
35743586
block = self.parse("""
35753587
module foo
@@ -4621,6 +4633,16 @@ def test_file_dest(self):
46214633
except ImportError:
46224634
ac_tester = None
46234635

4636+
def static_and_heap(test):
4637+
# Run test with both a static type and a heap type.
4638+
@wraps(test)
4639+
def wrapper(self):
4640+
for cls in ac_tester.TestClass, ac_tester.HeapTestClass:
4641+
with self.subTest(cls=cls):
4642+
test(self, cls)
4643+
return wrapper
4644+
4645+
46244646
@unittest.skipIf(ac_tester is None, "_testclinic is missing")
46254647
class ClinicFunctionalTest(unittest.TestCase):
46264648
locals().update((name, getattr(ac_tester, name))
@@ -5295,53 +5317,56 @@ def test_cloned_func_with_converter_exception_message(self):
52955317
func = getattr(ac_tester, name)
52965318
self.assertEqual(func(), name)
52975319

5298-
def test_get_defining_class(self):
5299-
obj = ac_tester.TestClass()
5320+
@static_and_heap
5321+
def test_get_defining_class(self, cls):
5322+
obj = cls()
53005323
meth = obj.get_defining_class
5301-
self.assertIs(obj.get_defining_class(), ac_tester.TestClass)
5324+
self.assertIs(obj.get_defining_class(), cls)
53025325

53035326
# 'defining_class' argument is a positional only argument
53045327
with self.assertRaises(TypeError):
5305-
obj.get_defining_class_arg(cls=ac_tester.TestClass)
5328+
obj.get_defining_class_arg(cls=cls)
53065329

53075330
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
53085331
check(meth, 1)
53095332
check(meth, a=1)
53105333

5311-
def test_get_defining_class_capi(self):
5334+
@static_and_heap
5335+
def test_get_defining_class_capi(self, cls):
53125336
from _testcapi import pyobject_vectorcall
5313-
obj = ac_tester.TestClass()
5337+
obj = cls()
53145338
meth = obj.get_defining_class
53155339
pyobject_vectorcall(meth, None, None)
53165340
pyobject_vectorcall(meth, (), None)
53175341
pyobject_vectorcall(meth, (), ())
53185342
pyobject_vectorcall(meth, None, ())
5319-
self.assertIs(pyobject_vectorcall(meth, (), ()), ac_tester.TestClass)
5343+
self.assertIs(pyobject_vectorcall(meth, (), ()), cls)
53205344

53215345
check = partial(self.assertRaisesRegex, TypeError, "no arguments")
53225346
check(pyobject_vectorcall, meth, (1,), None)
53235347
check(pyobject_vectorcall, meth, (1,), ("a",))
53245348

5325-
def test_get_defining_class_arg(self):
5326-
obj = ac_tester.TestClass()
5349+
@static_and_heap
5350+
def test_get_defining_class_arg(self, cls):
5351+
obj = cls()
53275352
self.assertEqual(obj.get_defining_class_arg("arg"),
5328-
(ac_tester.TestClass, "arg"))
5353+
(cls, "arg"))
53295354
self.assertEqual(obj.get_defining_class_arg(arg=123),
5330-
(ac_tester.TestClass, 123))
5355+
(cls, 123))
53315356

53325357
# 'defining_class' argument is a positional only argument
53335358
with self.assertRaises(TypeError):
5334-
obj.get_defining_class_arg(cls=ac_tester.TestClass, arg="arg")
5359+
obj.get_defining_class_arg(cls=cls, arg="arg")
53355360

53365361
# wrong number of arguments
53375362
with self.assertRaises(TypeError):
53385363
obj.get_defining_class_arg()
53395364
with self.assertRaises(TypeError):
53405365
obj.get_defining_class_arg("arg1", "arg2")
53415366

5342-
def test_defclass_varpos(self):
5367+
@static_and_heap
5368+
def test_defclass_varpos(self, cls):
53435369
# fn(*args)
5344-
cls = ac_tester.TestClass
53455370
obj = cls()
53465371
fn = obj.defclass_varpos
53475372
self.assertEqual(fn(), (cls, ()))
@@ -5351,9 +5376,9 @@ def test_defclass_varpos(self):
53515376
self.assertEqual(fn(obj), (cls, ()))
53525377
self.assertEqual(fn(obj, 1, 2), (cls, (1, 2)))
53535378

5354-
def test_defclass_posonly_varpos(self):
5379+
@static_and_heap
5380+
def test_defclass_posonly_varpos(self, cls):
53555381
# fn(a, b, /, *args)
5356-
cls = ac_tester.TestClass
53575382
obj = cls()
53585383
fn = obj.defclass_posonly_varpos
53595384
errmsg = 'expected at least 2 arguments'

‎Lib/test/test_external_inspection.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,11 +1391,14 @@ async def main():
13911391
relationships = self._get_awaited_by_relationships(
13921392
stack_trace
13931393
)
1394+
future_name, = (node.task_name for node in stack_trace[0].awaited_by
1395+
if node.is_future)
13941396
self.assertEqual(
13951397
relationships,
13961398
{
13971399
"Task-1": set(),
1398-
"Task-2": {"Task-1"},
1400+
"Task-2": {future_name},
1401+
future_name: {"Task-1"},
13991402
},
14001403
)
14011404

@@ -1409,6 +1412,7 @@ async def main():
14091412
coroutine_stacks,
14101413
{
14111414
"Task-1": [((script_name, 21, "main", None),)],
1415+
future_name: [()],
14121416
"Task-2": [
14131417
(
14141418
(script_name, 11, "deep", None),
@@ -1436,7 +1440,8 @@ async def main():
14361440
awaited_by_coroutine_stacks,
14371441
{
14381442
"Task-1": [],
1439-
"Task-2": [
1443+
"Task-2": [(future_name, ())],
1444+
future_name: [
14401445
("Task-1", ((script_name, 21, "main", None),))
14411446
],
14421447
},

‎Lib/test/test_robotparser.py‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,5 +875,80 @@ def test_read_404(self):
875875
self.assertIsNone(parser.crawl_delay('*'))
876876
self.assertIsNone(parser.request_rate('*'))
877877

878+
879+
class RepeatedUserAgentTest(unittest.TestCase):
880+
881+
def test_many_duplicate_user_agents(self):
882+
# gh-162282: a User-agent repeated within one group must not be
883+
# merged into that group once per repetition (quadratic time).
884+
body = "User-agent: evil\n" * 1000 + "Disallow: /\n"
885+
parser = urllib.robotparser.RobotFileParser()
886+
parser.parse(body.splitlines())
887+
entry = parser.groups["evil"]
888+
self.assertIs(entry, parser.entries[0])
889+
self.assertEqual(len(entry.rulelines), 1)
890+
self.assertFalse(parser.can_fetch("evil", "/"))
891+
892+
def test_many_groups_for_same_user_agent(self):
893+
body = "User-agent: evil\nDisallow: /x\n\n" * 999
894+
parser = urllib.robotparser.RobotFileParser()
895+
parser.parse(body.splitlines())
896+
entry = parser.groups["evil"]
897+
self.assertEqual(len(entry.rulelines), 999)
898+
# The merged entry is extended in place rather than copied.
899+
parser.parse("User-agent: evil\nDisallow: /x\nCrawl-delay: 5".splitlines())
900+
self.assertIs(parser.groups["evil"], entry)
901+
self.assertEqual(len(entry.rulelines), 1000)
902+
self.assertEqual(entry.delay, 5)
903+
# The original entries are left intact.
904+
self.assertEqual([len(e.rulelines) for e in parser.entries], [1] * 1000)
905+
self.assertIsNone(parser.entries[-2].delay)
906+
907+
def test_merged_group_shared_by_user_agents(self):
908+
parser = urllib.robotparser.RobotFileParser()
909+
parser.parse("""\
910+
User-agent: a
911+
User-agent: b
912+
User-agent: c
913+
Disallow: /x
914+
915+
User-agent: a
916+
User-agent: b
917+
Disallow: /y
918+
919+
User-agent: a
920+
Disallow: /z
921+
""".splitlines())
922+
groups = parser.groups
923+
self.assertIs(groups["c"], parser.entries[0])
924+
self.assertEqual(list(map(str, groups["a"].rulelines)),
925+
["Disallow: /x", "Disallow: /y", "Disallow: /z"])
926+
self.assertEqual(list(map(str, groups["b"].rulelines)),
927+
["Disallow: /x", "Disallow: /y"])
928+
self.assertEqual(list(map(str, groups["c"].rulelines)),
929+
["Disallow: /x"])
930+
self.assertFalse(parser.can_fetch("a", "/z"))
931+
self.assertTrue(parser.can_fetch("b", "/z"))
932+
self.assertTrue(parser.can_fetch("c", "/y"))
933+
934+
def test_many_user_agents_in_one_group(self):
935+
body = "".join(f"User-agent: bot{i}\n" for i in range(1000))
936+
body += "Disallow: /\n\nUser-agent: other\nDisallow: /x\n"
937+
parser = urllib.robotparser.RobotFileParser()
938+
parser.parse(body.splitlines())
939+
self.assertIs(parser.groups["bot0"], parser.groups["bot999"])
940+
calls = []
941+
orig = urllib.robotparser.Entry.applies_to
942+
def applies_to(entry, useragent):
943+
calls.append(entry)
944+
return orig(entry, useragent)
945+
with support.swap_attr(urllib.robotparser.Entry, "applies_to",
946+
applies_to):
947+
self.assertTrue(parser.can_fetch("unknown", "/"))
948+
self.assertFalse(parser.can_fetch("xbot5x", "/"))
949+
# Each distinct entry is checked at most once per lookup.
950+
self.assertEqual(len(calls), 2 + 1)
951+
952+
878953
if __name__=='__main__':
879954
unittest.main()

0 commit comments

Comments
 (0)