Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleaner way of annotating test functions using a decorator
  • Loading branch information
ronaldoussoren committed Jan 14, 2024
commit 1fc244379dfe5fb518f14246b5a25a0980335758
98 changes: 47 additions & 51 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ def hunting_for_refleaks():
return _hunting_for_refleaks


def skipForRefleakHuntinIf(condition, issueref):
Copy link
Copy Markdown
Member

@Eclips4 Eclips4 Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's make sense to store it in test.support.refleak_helper (as you said above)? It's can be useful in future in other situations like this

if not condition:
def decorator(f):
f.client_skip = lambda f: f
return f

else:
def decorator(f):
@contextlib.wraps(f)
def wrapper(*args, **kwds):
if hunting_for_refleaks():
raise unittest.SkipTest(f"ignore while hunting for refleaks, see {issueref}")

return f(*args, **kwds)

def client_skip(f):
@contextlib.wraps(f)
def wrapper(*args, **kwds):
if hunting_for_refleaks():
return

return f(*args, **kwds)

return wrapper
wrapper.client_skip = client_skip
return wrapper

return decorator

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

@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncNoBufSize(self):
# Check that no ancillary data is received when no buffer size
# is specified.
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG)),
# BSD seems to set MSG_CTRUNC only
# if an item has been partially
# received.
ignoreflags=socket.MSG_CTRUNC)

@testCmsgTruncNoBufSize.client_skip
def _testCmsgTruncNoBufSize(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.createAndSendFDs(1)

@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTrunc0(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

# Check that no ancillary data is received when buffer size is 0.
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 0),
ignoreflags=socket.MSG_CTRUNC)

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

@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTrunc1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 1))

@testCmsgTrunc1.client_skip
def _testCmsgTrunc1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.createAndSendFDs(1)

@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTrunc2Int(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

# The cmsghdr structure has at least three members, two of
# which are ints, so we still shouldn't see any ancillary
# data.
self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG),
SIZEOF_INT * 2))

@testCmsgTrunc2Int.client_skip
def _testCmsgTrunc2Int(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.createAndSendFDs(1)

@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen0Minus1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG),
socket.CMSG_LEN(0) - 1))

@testCmsgTruncLen0Minus1.client_skip
def _testCmsgTruncLen0Minus1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.createAndSendFDs(1)

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

@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen0(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0), maxdata=0)

@testCmsgTruncLen0.client_skip
def _testCmsgTruncLen0(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.createAndSendFDs(1)

@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen0Plus1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0) + 1, maxdata=1)

@testCmsgTruncLen0Plus1.client_skip
def _testCmsgTruncLen0Plus1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.createAndSendFDs(2)

Comment thread
ronaldoussoren marked this conversation as resolved.
@skipForRefleakHuntinIf(sys.platform == "darwin", "#80931")
def testCmsgTruncLen1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(SIZEOF_INT),
maxdata=SIZEOF_INT)

@testCmsgTruncLen1.client_skip
def _testCmsgTruncLen1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.createAndSendFDs(2)

def testCmsgTruncLen2Minus1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

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

@testCmsgTruncLen2Minus1.client_skip
def _testCmsgTruncLen2Minus1(self):
if sys.platform == "darwin" and hunting_for_refleaks():
return

self.createAndSendFDs(2)


Expand Down