Skip to content

Commit 188b239

Browse files
authored
Merge branch 'main' into cmaloney/pyio_fileio_readall
2 parents 869a31b + a1a4e9f commit 188b239

File tree

13 files changed

+180
-135
lines changed

13 files changed

+180
-135
lines changed

Doc/library/logging.handlers.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module,
613613
supports sending logging messages to a remote or local Unix syslog.
614614

615615

616-
.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
616+
.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM, timeout=None)
617617

618618
Returns a new instance of the :class:`SysLogHandler` class intended to
619619
communicate with a remote Unix machine whose address is given by *address* in
@@ -626,6 +626,11 @@ supports sending logging messages to a remote or local Unix syslog.
626626
*socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus
627627
opens a UDP socket. To open a TCP socket (for use with the newer syslog
628628
daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`.
629+
If *timeout* is specified, it sets a timeout (in seconds) for the socket operations.
630+
This can help prevent the program from hanging indefinitely if the syslog server is
631+
unreachable. By default, *timeout* is ``None``, meaning no timeout is applied.
632+
633+
629634

630635
Note that if your server is not listening on UDP port 514,
631636
:class:`SysLogHandler` may appear not to work. In that case, check what
@@ -645,6 +650,8 @@ supports sending logging messages to a remote or local Unix syslog.
645650
.. versionchanged:: 3.2
646651
*socktype* was added.
647652

653+
.. versionchanged:: 3.14
654+
*timeout* was added.
648655

649656
.. method:: close()
650657

Lib/logging/handlers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ class SysLogHandler(logging.Handler):
855855
}
856856

857857
def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
858-
facility=LOG_USER, socktype=None):
858+
facility=LOG_USER, socktype=None, timeout=None):
859859
"""
860860
Initialize a handler.
861861
@@ -872,6 +872,7 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
872872
self.address = address
873873
self.facility = facility
874874
self.socktype = socktype
875+
self.timeout = timeout
875876
self.socket = None
876877
self.createSocket()
877878

@@ -933,6 +934,8 @@ def createSocket(self):
933934
err = sock = None
934935
try:
935936
sock = socket.socket(af, socktype, proto)
937+
if self.timeout:
938+
sock.settimeout(self.timeout)
936939
if socktype == socket.SOCK_STREAM:
937940
sock.connect(sa)
938941
break

Lib/test/test_call.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from test.support import (cpython_only, is_wasi, requires_limited_api, Py_DEBUG,
3-
set_recursion_limit, skip_on_s390x, skip_emscripten_stack_overflow)
3+
set_recursion_limit, skip_on_s390x, skip_emscripten_stack_overflow, import_helper)
44
try:
55
import _testcapi
66
except ImportError:
@@ -616,9 +616,6 @@ def testfunction_kw(self, *, kw):
616616
return self
617617

618618

619-
ADAPTIVE_WARMUP_DELAY = 2
620-
621-
622619
@unittest.skipIf(_testcapi is None, "requires _testcapi")
623620
class TestPEP590(unittest.TestCase):
624621

@@ -802,17 +799,18 @@ def __call__(self, *args):
802799

803800
def test_setvectorcall(self):
804801
from _testcapi import function_setvectorcall
802+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
805803
def f(num): return num + 1
806804
assert_equal = self.assertEqual
807805
num = 10
808806
assert_equal(11, f(num))
809807
function_setvectorcall(f)
810-
# make sure specializer is triggered by running > 50 times
811-
for _ in range(10 * ADAPTIVE_WARMUP_DELAY):
808+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
812809
assert_equal("overridden", f(num))
813810

814811
def test_setvectorcall_load_attr_specialization_skip(self):
815812
from _testcapi import function_setvectorcall
813+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
816814

817815
class X:
818816
def __getattribute__(self, attr):
@@ -824,11 +822,12 @@ def __getattribute__(self, attr):
824822
function_setvectorcall(X.__getattribute__)
825823
# make sure specialization doesn't trigger
826824
# when vectorcall is overridden
827-
for _ in range(ADAPTIVE_WARMUP_DELAY):
825+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
828826
assert_equal("overridden", x.a)
829827

830828
def test_setvectorcall_load_attr_specialization_deopt(self):
831829
from _testcapi import function_setvectorcall
830+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
832831

833832
class X:
834833
def __getattribute__(self, attr):
@@ -840,12 +839,12 @@ def get_a(x):
840839
assert_equal = self.assertEqual
841840
x = X()
842841
# trigger LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN specialization
843-
for _ in range(ADAPTIVE_WARMUP_DELAY):
842+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
844843
assert_equal("a", get_a(x))
845844
function_setvectorcall(X.__getattribute__)
846845
# make sure specialized LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN
847846
# gets deopted due to overridden vectorcall
848-
for _ in range(ADAPTIVE_WARMUP_DELAY):
847+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
849848
assert_equal("overridden", get_a(x))
850849

851850
@requires_limited_api

Lib/test/test_dis.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,6 @@ def extended_arg_quick():
931931
"""% (extended_arg_quick.__code__.co_firstlineno,
932932
extended_arg_quick.__code__.co_firstlineno + 1,)
933933

934-
ADAPTIVE_WARMUP_DELAY = 2
935-
936934
class DisTestBase(unittest.TestCase):
937935
"Common utilities for DisTests and TestDisTraceback"
938936

@@ -1259,8 +1257,9 @@ def test__try_compile_no_context_exc_on_error(self):
12591257
self.assertIsNone(e.__context__)
12601258

12611259
@staticmethod
1262-
def code_quicken(f, times=ADAPTIVE_WARMUP_DELAY):
1263-
for _ in range(times):
1260+
def code_quicken(f):
1261+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
1262+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
12641263
f()
12651264

12661265
@cpython_only
@@ -1306,7 +1305,7 @@ def test_call_specialize(self):
13061305
@requires_specialization
13071306
def test_loop_quicken(self):
13081307
# Loop can trigger a quicken where the loop is located
1309-
self.code_quicken(loop_test, 4)
1308+
self.code_quicken(loop_test)
13101309
got = self.get_disassembly(loop_test, adaptive=True)
13111310
jit = import_helper.import_module("_testinternalcapi").jit_enabled()
13121311
expected = dis_loop_test_quickened_code.format("JIT" if jit else "NO_JIT")
@@ -1315,8 +1314,9 @@ def test_loop_quicken(self):
13151314
@cpython_only
13161315
@requires_specialization
13171316
def test_loop_with_conditional_at_end_is_quickened(self):
1317+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
13181318
def for_loop_true(x):
1319-
for i in range(10):
1319+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
13201320
if x:
13211321
pass
13221322

@@ -1325,7 +1325,7 @@ def for_loop_true(x):
13251325
self.get_disassembly(for_loop_true, adaptive=True))
13261326

13271327
def for_loop_false(x):
1328-
for i in range(10):
1328+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
13291329
if x:
13301330
pass
13311331

@@ -1335,7 +1335,7 @@ def for_loop_false(x):
13351335

13361336
def while_loop():
13371337
i = 0
1338-
while i < 10:
1338+
while i < _testinternalcapi.SPECIALIZATION_THRESHOLD:
13391339
i += 1
13401340

13411341
while_loop()

Lib/test/test_embed.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ def test_simple_initialization_api(self):
384384
def test_specialized_static_code_gets_unspecialized_at_Py_FINALIZE(self):
385385
# https://github.com/python/cpython/issues/92031
386386

387-
code = textwrap.dedent("""\
387+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
388+
389+
code = textwrap.dedent(f"""\
388390
import dis
389391
import importlib._bootstrap
390392
import opcode
@@ -411,7 +413,7 @@ def is_specialized(f):
411413
412414
assert not is_specialized(func), "specialized instructions found"
413415
414-
for i in range(test.test_dis.ADAPTIVE_WARMUP_DELAY):
416+
for _ in range({_testinternalcapi.SPECIALIZATION_THRESHOLD}):
415417
func(importlib._bootstrap, ["x"], lambda *args: None)
416418
417419
assert is_specialized(func), "no specialized instructions found"

Lib/test/test_logging.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import logging.handlers
2323
import logging.config
2424

25+
2526
import codecs
2627
import configparser
2728
import copy
@@ -2095,6 +2096,18 @@ def test_udp_reconnection(self):
20952096
self.handled.wait(support.LONG_TIMEOUT)
20962097
self.assertEqual(self.log_output, b'<11>sp\xc3\xa4m\x00')
20972098

2099+
@patch('socket.socket')
2100+
def test_tcp_timeout(self, mock_socket):
2101+
instance_mock_sock = mock_socket.return_value
2102+
instance_mock_sock.connect.side_effect = socket.timeout
2103+
2104+
with self.assertRaises(socket.timeout):
2105+
logging.handlers.SysLogHandler(address=('localhost', 514),
2106+
socktype=socket.SOCK_STREAM,
2107+
timeout=1)
2108+
2109+
instance_mock_sock.close.assert_called()
2110+
20982111
@unittest.skipUnless(hasattr(socket, "AF_UNIX"), "Unix sockets required")
20992112
class UnixSysLogHandlerTest(SysLogHandlerTest):
21002113

Lib/test/test_monitoring.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from test.support import requires_specialization_ft, script_helper
1515

1616
_testcapi = test.support.import_helper.import_module("_testcapi")
17+
_testinternalcapi = test.support.import_helper.import_module("_testinternalcapi")
1718

1819
PAIR = (0,1)
1920

@@ -897,13 +898,13 @@ def implicit_stop_iteration(iterator=None):
897898
# re-specialize immediately, so that we can we can test the
898899
# unspecialized version of the loop first.
899900
# Note: this assumes that we don't specialize loops over sets.
900-
implicit_stop_iteration(set(range(100)))
901+
implicit_stop_iteration(set(range(_testinternalcapi.SPECIALIZATION_THRESHOLD)))
901902

902903
# This will record a RAISE event for the StopIteration.
903904
self.check_events(implicit_stop_iteration, expected, recorders=recorders)
904905

905906
# Now specialize, so that we see a STOP_ITERATION event.
906-
for _ in range(100):
907+
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN):
907908
implicit_stop_iteration()
908909

909910
# This will record a STOP_ITERATION event for the StopIteration.
@@ -1057,7 +1058,7 @@ def f():
10571058
except ValueError:
10581059
pass
10591060

1060-
for _ in range(100):
1061+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
10611062
f()
10621063
recorders = (
10631064
ReturnRecorder,
@@ -2033,8 +2034,8 @@ def __init__(self, set_event):
20332034
sys.monitoring.set_events(TEST_TOOL, E.PY_RESUME)
20342035

20352036
def make_foo_optimized_then_set_event():
2036-
for i in range(100):
2037-
Foo(i == 99)
2037+
for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD + 1):
2038+
Foo(i == _testinternalcapi.SPECIALIZATION_THRESHOLD)
20382039

20392040
try:
20402041
make_foo_optimized_then_set_event()
@@ -2106,9 +2107,9 @@ def test_func(recorder):
21062107
set_events = sys.monitoring.set_events
21072108
line = E.LINE
21082109
i = 0
2109-
for i in range(551):
2110-
# Turn on events without branching once i reaches 500.
2111-
set_events(TEST_TOOL, line * int(i >= 500))
2110+
for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD + 51):
2111+
# Turn on events without branching once i reaches _testinternalcapi.SPECIALIZATION_THRESHOLD.
2112+
set_events(TEST_TOOL, line * int(i >= _testinternalcapi.SPECIALIZATION_THRESHOLD))
21122113
pass
21132114
pass
21142115
pass

0 commit comments

Comments
 (0)