Skip to content

Conversation

@tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented May 17, 2019

Fix NameError in urllib.request.URLopener.retrieve by using correct imports.

https://bugs.python.org/issue36948

Copy link
Member

@isidentical isidentical left a comment

Choose a reason for hiding this comment

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

Do this PR needs a news entry?

@tirkarthi
Copy link
Member Author

Do this PR needs a news entry?

@isidentical I have added one already at https://github.com/python/cpython/pull/13389/files#diff-1f45e92fbc7f2721e27165810e333552

@isidentical
Copy link
Member

I'm just asking, i dont think this is a change that requires a news entry.

@tirkarthi
Copy link
Member Author

I'm just asking, i dont think this is a change that requires a news entry.

This is a user impacting bug where even though the function was deprecated it's now causing NameError that used to work fine. IMO, it's a bug fix and requires a NEWS entry.

def test_urlopener_retrieve_file(self):
with self.assertWarns(DeprecationWarning):
try:
fd, tmp_file = tempfile.mkstemp()
Copy link
Member

Choose a reason for hiding this comment

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

Can't we use tempfile.NamedTemporaryFile and context management protocol to avoid handling resources manually?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess I used it due to Windows permission error in previous CI runs where the created file was not readable but with mkstemp it's possible as seen from the docstring. It seems to be a windows only case but it seems to have other problem where the directory could be used by another process as seen in current CI run. Is there a reliable way to generate a temp file for this usecase?

    The file is readable and writable only by the creating user ID.
    If the operating system uses permission bits to indicate whether a
    file is executable, the file is executable by no one. The file
    descriptor is not inherited by children of this process.

Copy link
Member

Choose a reason for hiding this comment

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

I think support.unlink() can be used to handle "it's being used by another process" errors under Windows.

Also, the order of self.addCleanup() calls is important. You might be bitten by a similar issue like d0f5bab.

Copy link
Member Author

Choose a reason for hiding this comment

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

NamedTemporaryFile gives me that the file is not readable. Hence I used temp_dir and mkstemp to generate a more safer version of temp file for this test but I am sure I am not the only one having this problem in the testsuite.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think calling os.close() just after tempfile.mkstemp() makes sense. The temp directory will be cleaned up after the support.temp_dir() context manager and fd will be effectively invalid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Without calling os.close(fd) there will be an open file handle to the tempfile inside the directory and there will be a permission error that file handle is used by another process cannot be cleaned up as support.temp_dir context manager exits and due to unclosed fd. I guess Windows doesn't allow deleting directory when there is an open file handle to one of the files inside.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, now I understood what you are talking about, and, yes, your analysis is correct. I think the current version of the test is good and reliable enough. Thanks!

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@tirkarthi
Copy link
Member Author

I have resorted to using support.temp_dir to create a temporary directory for mkstemp . I guess the file being used error was to adding os.close(fd) to Cleanup where it's closed at the end of tests though the context manager support.temp_dir tries to clean it up on __exit__ causing error on cleaning up tmp_dir with an open file handle. So I have added os.close(fd) on the next line itself. I must admit that I resolved the issue with trial and error since I don't have access to Windows. Suggestions if any windows compatible test would be helpful too.

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@berkerpeksag: please review the changes made to this pull request.

@berkerpeksag berkerpeksag merged commit c661b30 into python:master May 19, 2019
@miss-islington
Copy link
Contributor

Thanks @tirkarthi for the PR, and @berkerpeksag for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 19, 2019
…GH-13389)

(cherry picked from commit c661b30)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
@bedevere-bot
Copy link

GH-13422 is a backport of this pull request to the 3.7 branch.

@tirkarthi
Copy link
Member Author

Thanks @berkerpeksag for the review. This is a 3.8 only issue so I guess the backport PR to 3.7 can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants