Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128731: fix ResourceWarning in test_robotparser #128746

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,7 @@ def close(self):
self.cleanup()

def __del__(self):
close_called = self.close_called
Copy link
Member

@picnixz picnixz Jan 12, 2025

Choose a reason for hiding this comment

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

This shouldn't be removed. We want to warn when there is a resource that is not closed. What should be fixed are the tests where we should explicitly close the corresponding resources.

self.cleanup()
if not close_called:
_warnings.warn(self.warn_message, ResourceWarning)


class _TemporaryFileWrapper:
Expand Down Expand Up @@ -554,6 +551,10 @@ def __iter__(self):
for line in self.file:
yield line

def __del__(self):
self._closer.cleanup()


def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
newline=None, suffix=None, prefix=None,
dir=None, delete=True, *, errors=None,
Expand Down
12 changes: 8 additions & 4 deletions Lib/test/test_robotparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,17 @@ def tearDown(self):
self.server.shutdown()
self.t.join()
self.server.server_close()
self.parser.close() # P9de7
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.


@threading_helper.reap_threads
def testPasswordProtectedSite(self):
addr = self.server.server_address
url = 'http://' + socket_helper.HOST + ':' + str(addr[1])
robots_url = url + "/robots.txt"
parser = urllib.robotparser.RobotFileParser()
parser.set_url(url)
parser.read()
self.assertFalse(parser.can_fetch("*", robots_url))
self.parser = urllib.robotparser.RobotFileParser() # P9de7
Copy link
Member

Choose a reason for hiding this comment

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

What's "P9de7"?

self.parser.set_url(url)
self.parser.read()
self.assertFalse(self.parser.can_fetch("*", robots_url))


@support.requires_working_socket()
Expand All @@ -364,6 +365,9 @@ def url(self, path):
self.base_url, path, '/' if not os.path.splitext(path)[1] else ''
)

def tearDown(self):
self.parser.close() # P080b
Copy link
Member

Choose a reason for hiding this comment

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

What's P080b?


def test_basic(self):
self.assertFalse(self.parser.disallow_all)
self.assertFalse(self.parser.allow_all)
Expand Down
Loading