8000 Merge branch 'main' of github.com:python/cpython · python/cpython@7e49f3e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e49f3e

Browse files
committed
Merge branch 'main' of github.com:python/cpython
2 parents 0e8d64a + 13e4659 commit 7e49f3e

File tree

10 files changed

+267
-286
lines changed

10 files changed

+267
-286
lines changed

Doc/library/unittest.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ Test Discovery
266266

267267
Unittest supports simple test discovery. In order to be compatible with test
268268
discovery, all of the test files must be :ref:`modules <tut-modules>` or
269-
:ref:`packages <tut-packages>` (including :term:`namespace packages
270-
<namespace package>`) importable from the top-level directory of
269+
:ref:`packages <tut-packages>` importable from the top-level directory of
271270
the project (this means that their filenames must be valid :ref:`identifiers
272271
<identifiers>`).
273272

@@ -340,6 +339,24 @@ the `load_tests protocol`_.
340339
directory too (e.g.
341340
``python -m unittest discover -s root/namespace -t root``).
342341

342+
.. versionchanged:: 3.11
343+
Python 3.11 dropped the :term:`namespace packages <namespace package>`
344+
support. It has been broken since Python 3.7. Start directory and
345+
subdirectories containing tests must be regular package that have
346+
``__init__.py`` file.
347+
348+
Directories containing start directory still can be a namespace package.
349 8000 +
In this case, you need to specify start directory as dotted package name,
350+
and target directory explicitly. For example::
351+
352+
# proj/ <-- current directory
353+
# namespace/
354+
# mypkg/
355+
# __init__.py
356+
# test_mypkg.py
357+
358+
python -m unittest discover -s namespace.mypkg -t .
359+
343360

344361
.. _organizing-tests:
345362

@@ -1858,6 +1875,10 @@ Loading and running tests
18581875
whether their path matches *pattern*, because it is impossible for
18591876
a package name to match the default pattern.
18601877

1878+
.. versionchanged:: 3.11
1879+
*start_dir* can not be a :term:`namespace packages <namespace package>`.
1880+
It has been broken since Python 3.7 and Python 3.11 officially remove it.
1881+
18611882

18621883
The following attributes of a :class:`TestLoader` can be configured either by
18631884
subclassing or assignment on an instance:

Doc/tutorial/datastructures.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,12 @@ More on Conditions
659659
The conditions used in ``while`` and ``if`` statements can contain any
660660
operators, not just comparisons.
661661

662-
The comparison operators ``in`` and ``not in`` check whether a value occurs
663-
(does not occur) in a sequence. The operators ``is`` and ``is not`` compare
664-
whether two objects are really the same object. All comparison operators have
665-
the same priority, which is lower than that of all numerical operators.
662+
663+
The comparison operators ``in`` and ``not in`` are membership tests that
664+
determine whether a value is in (or not in) a container. The operators ``is``
665+
and ``is not`` compare whether two objects are really the same object. All
666+
comparison operators have the same priority, which is lower than that of all
667+
numerical operators.
666668

667669
Comparisons can be chained. For example, ``a < b == c`` tests whether ``a`` is
668670
less than ``b`` and moreover ``b`` equals ``c``.

Doc/whatsnew/3.11.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ Removed
542542

543543
(Contributed by Hugo van Kemenade in :issue:`45320`.)
544544

545+
* Remove namespace package support from unittest discovery. It was introduced in
546+
Python 3.4 but has been broken since Python 3.7.
547+
(Contributed by Inada Naoki in :issue:`23882`.)
548+
545549

546550
Porting to Python 3.11
547551
======================

Lib/logging/config.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import logging.handlers
3131
import re
3232
import struct
33-
import sys
3433
import threading
3534
import traceback
3635

@@ -392,11 +391,9 @@ def resolve(self, s):
392391
self.importer(used)
393392
found = getattr(found, frag)
394393
return found
395-
except ImportError:
396-
e, tb = sys.exc_info()[1:]
394+
except ImportError as e:
397395
v = ValueError('Cannot resolve %r: %s' % (s, e))
398-
v.__cause__, v.__traceback__ = e, tb
399-
raise v
396+
raise v from e
400397

401398
def ext_convert(self, value):
402399
"""Default converter for the ext:// protocol."""

Lib/test/test_enum.py

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4449,30 +4449,34 @@ def test__all__(self):
44494449
COMPLEX_A = 2j
44504450
COMPLEX_B = 3j
44514451

4452-
class TestIntEnumConvert(unittest.TestCase):
4453-
def setUp(self):
4454-
# Reset the module-level test variables to their original integer
4455-
# values, otherwise the already created enum values get converted
4456-
# instead.
4457-
for suffix in ['A', 'B', 'C', 'D', 'E', 'F']:
4458-
globals()[f'CONVERT_TEST_NAME_{suffix}'] = 5
4459-
globals()[f'CONVERT_STRING_TEST_NAME_{suffix}'] = 5
4452+
class _ModuleWrapper:
4453+
"""We use this class as a namespace for swapping modules."""
4454+
4455+
def __init__(self, module):
4456+
self.__dict__.update(module.__dict__)
44604457

4458+
class TestIntEnumConvert(unittest.TestCase):
44614459
def test_convert_value_lookup_priority(self):
4462-
test_type = enum.IntEnum._convert_(
4463-
'UnittestConvert',
4464-
MODULE,
4465-
filter=lambda x: x.startswith('CONVERT_TEST_'))
4460+
with support.swap_item(
4461+
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
4462+
):
4463+
test_type = enum.IntEnum._convert_(
4464+
'UnittestConvert',
4465+
MODULE,
4466+
filter=lambda x: x.startswith('CONVERT_TEST_'))
44664467
# We don't want the reverse lookup value to vary when there are
44674468
# multiple possible names for a given value. It should always
44684469
# report the first lexigraphical name in that case.
44694470
self.assertEqual(test_type(5).name, 'CONVERT_TEST_NAME_A')
44704471

44714472
def test_convert(self):
4472-
test_type = enum.IntEnum._convert_(
4473-
'UnittestConvert',
4474-
MODULE,
4475-
filter=lambda x: x.startswith('CONVERT_TEST_'))
4473+
with support.swap_item(
4474+
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
4475+
):
4476+
test_type = enum.IntEnum._convert_(
4477+
'UnittestConvert',
4478+
MODULE,
4479+
filter=lambda x: x.startswith('CONVERT_TEST_'))
44764480
# Ensure that test_type has all of the desired names and values.
44774481
self.assertEqual(test_type.CONVERT_TEST_NAME_F,
44784482
test_type.CONVERT_TEST_NAME_A)
@@ -4487,11 +4491,16 @@ def test_convert(self):
44874491
[], msg='Names other than CONVERT_TEST_* found.')
44884492

44894493
def test_convert_uncomparable(self):
4490-
uncomp = enum.Enum._convert_(
4491-
'Uncomparable',
4492-
MODULE,
4493-
filter=lambda x: x.startswith('UNCOMPARABLE_'),
4494-
)
4494+
# We swap a module to some other object with `__dict__`
4495+
# because otherwise refleak is created.
4496+
# `_convert_` uses a module side effect that does this. See 30472
4497+
with support.swap_item(
4498+
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
4499+
):
4500+
uncomp = enum.Enum._convert_(
4501+
'Uncomparable',
4502+
MODULE,
4503+
filter=lambda x: x.startswith('UNCOMPARABLE_'))
44954504

44964505
# Should be ordered by `name` only:
44974506
self.assertEqual(
@@ -4500,29 +4509,20 @@ def test_convert_uncomparable(self):
45004509
)
45014510

45024511
def test_convert_complex(self):
4503-
uncomp = enum.Enum._convert_(
4504-
'Uncomparable',
4505-
MODULE,
4506-
filter=lambda x: x.startswith('COMPLEX_'),
4507-
)
4512+
with support.swap_item(
4513+
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
4514+
):
4515+
uncomp = enum.Enum._convert_(
4516+
'Uncomparable',
4517+
MODULE,
4518+
filter=lambda x: x.startswith('COMPLEX_'))
45084519

45094520
# Should be ordered by `name` only:
45104521
self.assertEqual(
45114522
list(uncomp),
45124523
[uncomp.COMPLEX_A, uncomp.COMPLEX_B, uncomp.COMPLEX_C],
45134524
)
45144525

4515-
@unittest.skipUnless(python_version == (3, 8),
4516-
'_convert was deprecated in 3.8')
4517-
def test_convert_warn(self):
4518-
with self.assertWarns(DeprecationWarning):
4519-
enum.IntEnum._convert(
4520-
'UnittestConvert',
4521-
MODULE,
4522-
filter=lambda x: x.startswith('CONVERT_TEST_'))
4523-
4524-
@unittest.skipUnless(python_version >= (3, 9),
4525-
'_convert was removed in 3.9')
45264526
def test_convert_raise(self):
45274527
with self.assertRaises(AttributeError):
45284528
enum.IntEnum._convert(
@@ -4531,10 +4531,13 @@ def test_convert_raise(self):
45314531
filter=lambda x: x.startswith('CONVERT_TEST_'))
45324532

45334533
def test_convert_repr_and_str(self):
4534-
test_type = enum.IntEnum._convert_(
4535-
'UnittestConvert',
4536-
MODULE,
4537-
filter=lambda x: x.startswith('CONVERT_STRING_TEST_'))
4534+
with support.swap_item(
4535+
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
4536+
):
4537+
test_type = enum.IntEnum._convert_(
4538+
'UnittestConvert',
4539+
MODULE,
4540+
filter=lambda x: x.startswith('CONVERT_STRING_TEST_'))
45384541
self.assertEqual(repr(test_type.CONVERT_STRING_TEST_NAME_A), '%s.CONVERT_STRING_TEST_NAME_A' % SHORT_MODULE)
45394542
self.assertEqual(str(test_type.CONVERT_STRING_TEST_NAME_A), 'CONVERT_STRING_TEST_NAME_A')
45404543
self.assertEqual(format(test_type.CONVERT_STRING_TEST_NAME_A), '5')
@@ -4544,17 +4547,14 @@ def test_convert_repr_and_str(self):
45444547
CONVERT_STR_TEST_1 = 'hello'
45454548

45464549
class TestStrEnumConvert(unittest.TestCase):
4547-
def setUp(self):
4548-
global CONVERT_STR_TEST_1
4549-
global CONVERT_STR_TEST_2
4550-
CONVERT_STR_TEST_2 = 'goodbye'
4551-
CONVERT_STR_TEST_1 = 'hello'
4552-
45534550
def test_convert(self):
4554-
test_type = enum.StrEnum._convert_(
4555-
'UnittestConvert',
4556-
MODULE,
4557-
filter=lambda x: x.startswith('CONVERT_STR_'))
4551+
with support.swap_item(
4552+
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
4553+
):
4554+
test_type = enum.StrEnum._convert_(
4555+
'UnittestConvert',
4556+
MODULE,
4557+
filter=lambda x: x.startswith('CONVERT_STR_'))
45584558
# Ensure that test_type has all of the desired names and values.
45594559
self.assertEqual(test_type.CONVERT_STR_TEST_1, 'hello')
45604560
self.assertEqual(test_type.CONVERT_STR_TEST_2, 'goodbye')
@@ -4565,10 +4565,13 @@ def test_convert(self):
45654565
[], msg='Names other than CONVERT_STR_* found.')
45664566

45674567
def test_convert_repr_and_str(self):
4568-
test_type = enum.StrEnum._convert_(
4569-
'UnittestConvert',
4570-
MODULE,
4571-
filter=lambda x: x.startswith('CONVERT_STR_'))
4568+
with support.swap_item(
4569+
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
4570+
):
4571+
test_type = enum.StrEnum._convert_(
4572+
'UnittestConvert',
4573+
MODULE,
4574+
filter=lambda x: x.startswith('CONVERT_STR_'))
45724575
self.assertEqual(repr(test_type.CONVERT_STR_TEST_1), '%s.CONVERT_STR_TEST_1' % SHORT_MODULE)
45734576
self.assertEqual(str(test_type.CONVERT_STR_TEST_2), 'goodbye')
45744577
self.assertEqual(format(test_type.CONVERT_STR_TEST_1), 'hello')

Lib/test/test_sys_settrace.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,21 @@ def func():
13881388
(19, 'line'),
13891389
(19, 'return')])
13901390

1391+
def test_notrace_lambda(self):
1392+
#Regression test for issue 46314
1393+
1394+
def func():
1395+
1
1396+
lambda x: 2
1397+
3
1398+
1399+
self.run_and_compare(func,
1400+
[(0, 'call'),
1401+
(1, 'line'),
1402+
(2, 'line'),
1403+
(3, 'line'),
1404+
(3, 'return')])
1405+
13911406

13921407
class SkipLineEventsTraceTestCase(TraceTestCase):
13931408
"""Repeat the trace tests, but with per-line events skipped"""

0 commit comments

Comments
 (0)
0