aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_asyncio/test_base_events.py88
-rw-r--r--Lib/test/test_dbm.py3
-rw-r--r--Lib/test/test_decimal.py13
-rw-r--r--Lib/test/test_fractions.py13
-rw-r--r--Lib/test/test_sqlite3/test_dbapi.py26
-rw-r--r--Lib/test/test_typing.py31
-rw-r--r--Lib/test/typinganndata/fwdref_module.py6
7 files changed, 142 insertions, 38 deletions
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
index 12179eb0c9e..22ae0ef3581 100644
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -150,6 +150,29 @@ class BaseEventTests(test_utils.TestCase):
socket.SOCK_STREAM,
socket.IPPROTO_TCP))
+ def test_interleave_addrinfos(self):
+ self.maxDiff = None
+ SIX_A = (socket.AF_INET6, 0, 0, '', ('2001:db8::1', 1))
+ SIX_B = (socket.AF_INET6, 0, 0, '', ('2001:db8::2', 2))
+ SIX_C = (socket.AF_INET6, 0, 0, '', ('2001:db8::3', 3))
+ SIX_D = (socket.AF_INET6, 0, 0, '', ('2001:db8::4', 4))
+ FOUR_A = (socket.AF_INET, 0, 0, '', ('192.0.2.1', 5))
+ FOUR_B = (socket.AF_INET, 0, 0, '', ('192.0.2.2', 6))
+ FOUR_C = (socket.AF_INET, 0, 0, '', ('192.0.2.3', 7))
+ FOUR_D = (socket.AF_INET, 0, 0, '', ('192.0.2.4', 8))
+
+ addrinfos = [SIX_A, SIX_B, SIX_C, FOUR_A, FOUR_B, FOUR_C, FOUR_D, SIX_D]
+ expected = [SIX_A, FOUR_A, SIX_B, FOUR_B, SIX_C, FOUR_C, SIX_D, FOUR_D]
+
+ self.assertEqual(expected, base_events._interleave_addrinfos(addrinfos))
+
+ expected_fafc_2 = [SIX_A, SIX_B, FOUR_A, SIX_C, FOUR_B, SIX_D, FOUR_C, FOUR_D]
+ self.assertEqual(
+ expected_fafc_2,
+ base_events._interleave_addrinfos(addrinfos, first_address_family_count=2),
+ )
+
+
class BaseEventLoopTests(test_utils.TestCase):
@@ -1053,6 +1076,71 @@ class BaseEventLoopTests(test_utils.TestCase):
test_utils.run_briefly(self.loop)
self.assertTrue(status['finalized'])
+ @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'no IPv6 support')
+ @patch_socket
+ def test_create_connection_happy_eyeballs(self, m_socket):
+
+ class MyProto(asyncio.Protocol):
+ pass
+
+ async def getaddrinfo(*args, **kw):
+ return [(socket.AF_INET6, 0, 0, '', ('2001:db8::1', 1)),
+ (socket.AF_INET, 0, 0, '', ('192.0.2.1', 5))]
+
+ async def sock_connect(sock, address):
+ if address[0] == '2001:db8::1':
+ await asyncio.sleep(1)
+ sock.connect(address)
+
+ loop = asyncio.new_event_loop()
+ loop._add_writer = mock.Mock()
+ loop._add_writer = mock.Mock()
+ loop._add_reader = mock.Mock()
+ loop.getaddrinfo = getaddrinfo
+ loop.sock_connect = sock_connect
+
+ coro = loop.create_connection(MyProto, 'example.com', 80, happy_eyeballs_delay=0.3)
+ transport, protocol = loop.run_until_complete(coro)
+ try:
+ sock = transport._sock
+ sock.connect.assert_called_with(('192.0.2.1', 5))
+ finally:
+ transport.close()
+ test_utils.run_briefly(loop) # allow transport to close
+ loop.close()
+
+ @patch_socket
+ def test_create_connection_happy_eyeballs_ipv4_only(self, m_socket):
+
+ class MyProto(asyncio.Protocol):
+ pass
+
+ async def getaddrinfo(*args, **kw):
+ return [(socket.AF_INET, 0, 0, '', ('192.0.2.1', 5)),
+ (socket.AF_INET, 0, 0, '', ('192.0.2.2', 6))]
+
+ async def sock_connect(sock, address):
+ if address[0] == '192.0.2.1':
+ await asyncio.sleep(1)
+ sock.connect(address)
+
+ loop = asyncio.new_event_loop()
+ loop._add_writer = mock.Mock()
+ loop._add_writer = mock.Mock()
+ loop._add_reader = mock.Mock()
+ loop.getaddrinfo = getaddrinfo
+ loop.sock_connect = sock_connect
+
+ coro = loop.create_connection(MyProto, 'example.com', 80, happy_eyeballs_delay=0.3)
+ transport, protocol = loop.run_until_complete(coro)
+ try:
+ sock = transport._sock
+ sock.connect.assert_called_with(('192.0.2.2', 6))
+ finally:
+ transport.close()
+ test_utils.run_briefly(loop) # allow transport to close
+ loop.close()
+
class MyProto(asyncio.Protocol):
done = None
diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py
index 7e8d78b8940..ae9faabd536 100644
--- a/Lib/test/test_dbm.py
+++ b/Lib/test/test_dbm.py
@@ -274,7 +274,8 @@ class WhichDBTestCase(unittest.TestCase):
@unittest.skipUnless(ndbm, reason='Test requires ndbm')
def test_whichdb_ndbm(self):
# Issue 17198: check that ndbm which is referenced in whichdb is defined
- with open(_fname + '.db', 'wb'): pass
+ with open(_fname + '.db', 'wb') as f:
+ f.write(b'spam')
_bytes_fname = os.fsencode(_fname)
fnames = [_fname, os_helper.FakePath(_fname),
_bytes_fname, os_helper.FakePath(_bytes_fname)]
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index ef64b878805..08a8f4c3b36 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -1089,6 +1089,15 @@ class FormatTest:
('07_', '1234.56', '1_234.56'),
('_', '1.23456789', '1.23456789'),
('_%', '123.456789', '12_345.6789%'),
+ # and now for something completely different...
+ ('.,', '1.23456789', '1.234,567,89'),
+ ('._', '1.23456789', '1.234_567_89'),
+ ('.6_f', '12345.23456789', '12345.234_568'),
+ (',._%', '123.456789', '12,345.678_9%'),
+ (',._e', '123456', '1.234_56e+5'),
+ (',.4_e', '123456', '1.234_6e+5'),
+ (',.3_e', '123456', '1.235e+5'),
+ (',._E', '123456', '1.234_56E+5'),
# negative zero: default behavior
('.1f', '-0', '-0.0'),
@@ -1162,6 +1171,10 @@ class FormatTest:
# bytes format argument
self.assertRaises(TypeError, Decimal(1).__format__, b'-020')
+ # precision or fractional part separator should follow after dot
+ self.assertRaises(ValueError, format, Decimal(1), '.f')
+ self.assertRaises(ValueError, format, Decimal(1), '._6f')
+
def test_negative_zero_format_directed_rounding(self):
with self.decimal.localcontext() as ctx:
ctx.rounding = ROUND_CEILING
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 1875a2f529c..cf42b86358d 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -1322,6 +1322,8 @@ class FractionTest(unittest.TestCase):
# Thousands separators
(F('1234567.123456'), ',.5e', '1.23457e+06'),
(F('123.123456'), '012_.2e', '0_001.23e+02'),
+ # Thousands separators for fractional part (or for integral too)
+ (F('1234567.123456'), '.5_e', '1.234_57e+06'),
# z flag is legal, but never makes a difference to the output
(F(-1, 7**100), 'z.6e', '-3.091690e-85'),
]
@@ -1447,6 +1449,12 @@ class FractionTest(unittest.TestCase):
(F('1234567'), ',.2f', '1,234,567.00'),
(F('12345678'), ',.2f', '12,345,678.00'),
(F('12345678'), ',f', '12,345,678.000000'),
+ # Thousands separators for fractional part (or for integral too)
+ (F('123456.789123123'), '._f', '123456.789_123'),
+ (F('123456.789123123'), '.7_f', '123456.789_123_1'),
+ (F('123456.789123123'), '.9_f', '123456.789_123_123'),
+ (F('123456.789123123'), '.,f', '123456.789,123'),
+ (F('123456.789123123'), '_.,f', '123_456.789,123'),
# Underscore as thousands separator
(F(2, 3), '_.2f', '0.67'),
(F(2, 3), '_.7f', '0.6666667'),
@@ -1620,6 +1628,11 @@ class FractionTest(unittest.TestCase):
'.f',
'.g',
'.%',
+ # Thousands separators before precision
+ '._6e',
+ '._6f',
+ '._6g',
+ '._6%',
# Z instead of z for negative zero suppression
'Z.2f'
# z flag not supported for general formatting
diff --git a/Lib/test/test_sqlite3/test_dbapi.py b/Lib/test/test_sqlite3/test_dbapi.py
index 291e0356253..3602726437d 100644
--- a/Lib/test/test_sqlite3/test_dbapi.py
+++ b/Lib/test/test_sqlite3/test_dbapi.py
@@ -31,8 +31,7 @@ import urllib.parse
import warnings
from test.support import (
- SHORT_TIMEOUT, check_disallow_instantiation, requires_subprocess,
- is_apple, is_emscripten, is_wasi
+ SHORT_TIMEOUT, check_disallow_instantiation, requires_subprocess
)
from test.support import gc_collect
from test.support import threading_helper, import_helper
@@ -641,14 +640,21 @@ class OpenTests(unittest.TestCase):
self.assertTrue(os.path.exists(path))
cx.execute(self._sql)
+ def get_undecodable_path(self):
+ path = TESTFN_UNDECODABLE
+ if not path:
+ self.skipTest("only works if there are undecodable paths")
+ try:
+ open(path, 'wb').close()
+ except OSError:
+ self.skipTest(f"can't create file with undecodable path {path!r}")
+ unlink(path)
+ return path
+
@unittest.skipIf(sys.platform == "win32", "skipped on Windows")
- @unittest.skipIf(is_apple, "skipped on Apple platforms")
- @unittest.skipIf(is_emscripten or is_wasi, "not supported on Emscripten/WASI")
- @unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
def test_open_with_undecodable_path(self):
- path = TESTFN_UNDECODABLE
+ path = self.get_undecodable_path()
self.addCleanup(unlink, path)
- self.assertFalse(os.path.exists(path))
with contextlib.closing(sqlite.connect(path)) as cx:
self.assertTrue(os.path.exists(path))
cx.execute(self._sql)
@@ -688,14 +694,10 @@ class OpenTests(unittest.TestCase):
cx.execute(self._sql)
@unittest.skipIf(sys.platform == "win32", "skipped on Windows")
- @unittest.skipIf(is_apple, "skipped on Apple platforms")
- @unittest.skipIf(is_emscripten or is_wasi, "not supported on Emscripten/WASI")
- @unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
def test_open_undecodable_uri(self):
- path = TESTFN_UNDECODABLE
+ path = self.get_undecodable_path()
self.addCleanup(unlink, path)
uri = "file:" + urllib.parse.quote(path)
- self.assertFalse(os.path.exists(path))
with contextlib.closing(sqlite.connect(uri, uri=True)) as cx:
self.assertTrue(os.path.exists(path))
cx.execute(self._sql)
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index bef6773ad6c..b1615bbff38 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -6309,31 +6309,6 @@ class NoTypeCheckTests(BaseTestCase):
class InternalsTests(BaseTestCase):
- def test_deprecation_for_no_type_params_passed_to__evaluate(self):
- with self.assertWarnsRegex(
- DeprecationWarning,
- (
- "Failing to pass a value to the 'type_params' parameter "
- "of 'typing._eval_type' is deprecated"
- )
- ) as cm:
- self.assertEqual(typing._eval_type(list["int"], globals(), {}), list[int])
-
- self.assertEqual(cm.filename, __file__)
-
- f = ForwardRef("int")
-
- with self.assertWarnsRegex(
- DeprecationWarning,
- (
- "Failing to pass a value to the 'type_params' parameter "
- "of 'typing.ForwardRef._evaluate' is deprecated"
- )
- ) as cm:
- self.assertIs(f._evaluate(globals(), {}, recursive_guard=frozenset()), int)
-
- self.assertEqual(cm.filename, __file__)
-
def test_collect_parameters(self):
typing = import_helper.import_fresh_module("typing")
with self.assertWarnsRegex(
@@ -7351,6 +7326,12 @@ class EvaluateForwardRefTests(BaseTestCase):
list[EqualToForwardRef('A')],
)
+ def test_with_module(self):
+ from test.typinganndata import fwdref_module
+
+ typing.evaluate_forward_ref(
+ fwdref_module.fw,)
+
class CollectionsAbcTests(BaseTestCase):
diff --git a/Lib/test/typinganndata/fwdref_module.py b/Lib/test/typinganndata/fwdref_module.py
new file mode 100644
index 00000000000..7347a7a4245
--- /dev/null
+++ b/Lib/test/typinganndata/fwdref_module.py
@@ -0,0 +1,6 @@
+from typing import ForwardRef
+
+MyList = list[int]
+MyDict = dict[str, 'MyList']
+
+fw = ForwardRef('MyDict', module=__name__)