Skip to content

Commit 1fc2443

Browse files
Cleaner way of annotating test functions using a decorator
1 parent 87330e7 commit 1fc2443

File tree

1 file changed

+47
-51
lines changed

1 file changed

+47
-51
lines changed

Lib/test/test_socket.py

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,34 @@ def hunting_for_refleaks():
7373
return _hunting_for_refleaks
7474

7575

76+
def skipForRefleakHuntinIf(condition, issueref):
77+
if not condition:
78+
def decorator(f):
79+
f.client_skip = lambda f: f
80+
return f
81+
82+
else:
83+
def decorator(f):
84+
@contextlib.wraps(f)
85+
def wrapper(*args, **kwds):
86+
if hunting_for_refleaks():
87+
raise unittest.SkipTest(f"ignore while hunting for refleaks, see {issueref}")
88+
89+
return f(*args, **kwds)
90+
91+
def client_skip(f):
92+
@contextlib.wraps(f)
93+
def wrapper(*args, **kwds):
94+
if hunting_for_refleaks():
95+
return
96+
97+
return f(*args, **kwds)
98+
99+
return wrapper
100+
wrapper.client_skip = client_skip
101+
return wrapper
102+
103+
return decorator
76104

77105
def get_cid():
78106
if fcntl is None:
@@ -3836,32 +3864,27 @@ def checkTruncatedHeader(self, result, ignoreflags=0):
38363864
self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC,
38373865
ignore=ignoreflags)
38383866

3867+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
38393868
def testCmsgTruncNoBufSize(self):
38403869
# Check that no ancillary data is received when no buffer size
38413870
# is specified.
3842-
if sys.platform == "darwin" and hunting_for_refleaks():
3843-
return
3844-
38453871
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG)),
38463872
# BSD seems to set MSG_CTRUNC only
38473873
# if an item has been partially
38483874
# received.
38493875
ignoreflags=socket.MSG_CTRUNC)
38503876

3877+
@testCmsgTruncNoBufSize.client_skip
38513878
def _testCmsgTruncNoBufSize(self):
3852-
if sys.platform == "darwin" and hunting_for_refleaks():
3853-
return
3854-
38553879
self.createAndSendFDs(1)
38563880

3881+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
38573882
def testCmsgTrunc0(self):
3858-
if sys.platform == "darwin" and hunting_for_refleaks():
3859-
return
3860-
38613883
# Check that no ancillary data is received when buffer size is 0.
38623884
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 0),
38633885
ignoreflags=socket.MSG_CTRUNC)
38643886

3887+
@testCmsgTrunc0.client_skip
38653888
def _testCmsgTrunc0(self):
38663889
if sys.platform == "darwin" and hunting_for_refleaks():
38673890
return
@@ -3871,45 +3894,33 @@ def _testCmsgTrunc0(self):
38713894
# Check that no ancillary data is returned for various non-zero
38723895
# (but still too small) buffer sizes.
38733896

3897+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
38743898
def testCmsgTrunc1(self):
3875-
if sys.platform == "darwin" and hunting_for_refleaks():
3876-
return
3877-
38783899
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 1))
38793900

3901+
@testCmsgTrunc1.client_skip
38803902
def _testCmsgTrunc1(self):
3881-
if sys.platform == "darwin" and hunting_for_refleaks():
3882-
return
3883-
38843903
self.createAndSendFDs(1)
38853904

3905+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
38863906
def testCmsgTrunc2Int(self):
3887-
if sys.platform == "darwin" and hunting_for_refleaks():
3888-
return
3889-
38903907
# The cmsghdr structure has at least three members, two of
38913908
# which are ints, so we still shouldn't see any ancillary
38923909
# data.
38933910
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG),
38943911
SIZEOF_INT * 2))
38953912

3913+
@testCmsgTrunc2Int.client_skip
38963914
def _testCmsgTrunc2Int(self):
3897-
if sys.platform == "darwin" and hunting_for_refleaks():
3898-
return
3899-
39003915
self.createAndSendFDs(1)
39013916

3917+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
39023918
def testCmsgTruncLen0Minus1(self):
3903-
if sys.platform == "darwin" and hunting_for_refleaks():
3904-
return
3905-
39063919
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG),
39073920
socket.CMSG_LEN(0) - 1))
39083921

3922+
@testCmsgTruncLen0Minus1.client_skip
39093923
def _testCmsgTruncLen0Minus1(self):
3910-
if sys.platform == "darwin" and hunting_for_refleaks():
3911-
return
3912-
39133924
self.createAndSendFDs(1)
39143925

39153926
# The following tests try to truncate the control message in the
@@ -3939,54 +3950,39 @@ def checkTruncatedArray(self, ancbuf, maxdata, mindata=0):
39393950
len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
39403951
self.checkFDs(fds)
39413952

3953+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
39423954
def testCmsgTruncLen0(self):
3943-
if sys.platform == "darwin" and hunting_for_refleaks():
3944-
return
3945-
39463955
self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0), maxdata=0)
39473956

3957+
@testCmsgTruncLen0.client_skip
39483958
def _testCmsgTruncLen0(self):
3949-
if sys.platform == "darwin" and hunting_for_refleaks():
3950-
return
3951-
39523959
self.createAndSendFDs(1)
39533960

3961+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
39543962
def testCmsgTruncLen0Plus1(self):
3955-
if sys.platform == "darwin" and hunting_for_refleaks():
3956-
return
3957-
39583963
self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0) + 1, maxdata=1)
39593964

3965+
@testCmsgTruncLen0Plus1.client_skip
39603966
def _testCmsgTruncLen0Plus1(self):
3961-
if sys.platform == "darwin" and hunting_for_refleaks():
3962-
return
3963-
39643967
self.createAndSendFDs(2)
39653968

3969+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
39663970
def testCmsgTruncLen1(self):
3967-
if sys.platform == "darwin" and hunting_for_refleaks():
3968-
return
3969-
39703971
self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(SIZEOF_INT),
39713972
maxdata=SIZEOF_INT)
39723973

3974+
@testCmsgTruncLen1.client_skip
39733975
def _testCmsgTruncLen1(self):
3974-
if sys.platform == "darwin" and hunting_for_refleaks():
3975-
return
3976-
39773976
self.createAndSendFDs(2)
39783977

3979-
def testCmsgTruncLen2Minus1(self):
3980-
if sys.platform == "darwin" and hunting_for_refleaks():
3981-
return
39823978

3979+
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
3980+
def testCmsgTruncLen2Minus1(self):
39833981
self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(2 * SIZEOF_INT) - 1,
39843982
maxdata=(2 * SIZEOF_INT) - 1)
39853983

3984+
@testCmsgTruncLen2Minus1.client_skip
39863985
def _testCmsgTruncLen2Minus1(self):
3987-
if sys.platform == "darwin" and hunting_for_refleaks():
3988-
return
3989-
39903986
self.createAndSendFDs(2)
39913987

39923988

0 commit comments

Comments
 (0)