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

bpo-40459: Fix NameError in platform.py #19855

Merged
merged 8 commits into from
May 5, 2020
Merged

Conversation

sweeneyde
Copy link
Member

@sweeneyde sweeneyde commented May 2, 2020

Lib/platform.py Show resolved Hide resolved
@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.

@corona10 corona10 requested a review from vstinner May 2, 2020 16:33
@sweeneyde
Copy link
Member Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

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

Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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

Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Lib/platform.py Outdated
with winreg.OpenKeyEx(HKEY_LOCAL_MACHINE, cvkey) as key:
ptype = QueryValueEx(key, 'CurrentType')[0]
with winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, cvkey) as key:
ptype = winreg.QueryValueEx(key, 'CurrentType')[0]
except:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this bug was never seend because of "except: pass". It's maybe time to use more precise errors?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is except OSError: acceptable? Or are there any situations where this would raise an exception other than an OSError? And if so, is there a good reason why those are currently passing silently?

Copy link
Member

Choose a reason for hiding this comment

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

win32_edition() doesn't have the typo and it uses "except OSError: pass", so yeah I think that "except OSError:" is reasonable here.

@@ -0,0 +1 @@
Fix :exc:`NameError` caused from :func:`platform.win32_ver`.
Copy link
Member

Choose a reason for hiding this comment

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

The NEWS entry doesn't really explain the behavior change for users.

It seems like previously, ptype was always an empty string. ptype is now filled with the value of the "SOFTWARE\Microsoft\Windows NT\CurrentVersion" registry entry.

Can someone test on Windows to see how this string look like? I'm curious :-)

The documentation says:

As a hint: ptype is 'Uniprocessor Free' on single processor NT machines and 'Multiprocessor Free' on multi processor machines. The ‘Free’ refers to the OS version being free of debugging code. It could also state ‘Checked’ which means the OS version uses debugging code, i.e. code that checks arguments, ranges, etc.

https://docs.python.org/dev/library/platform.html#platform.win32_ver

Copy link
Member

Choose a reason for hiding this comment

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

Please explain that this change fix the ptype part of :func:platform.win32_ver result.

@sweeneyde
Copy link
Member Author

@vstinner On my machine, this was before the change:

.\python.bat -c "import platform; print(platform.win32_ver())"
('10', '10.0.18362', 'SP0', '')

and after the change:

.\python.bat -c "import platform; print(platform.win32_ver())"
('10', '10.0.18362', 'SP0', 'Multiprocessor Free')

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

ptype is empty since this change:

commit d307d05350e26a7a5f8f74db9af632a15215b50f
Author: Steve Dower <steve.dower@microsoft.com>
Date:   Mon Apr 22 11:40:12 2019 -0700

    Fixes platform.win32_ver on non-Windows platforms (GH-12912)

Hum, it seems like it's a regression introduced in Python 3.7.4.

In this case, it sounds ok to backport the change to Python 3.7 and 3.8.

Lib/platform.py Outdated
with winreg.OpenKeyEx(HKEY_LOCAL_MACHINE, cvkey) as key:
ptype = QueryValueEx(key, 'CurrentType')[0]
with winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, cvkey) as key:
ptype = winreg.QueryValueEx(key, 'CurrentType')[0]
except:
Copy link
Member

Choose a reason for hiding this comment

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

win32_edition() doesn't have the typo and it uses "except OSError: pass", so yeah I think that "except OSError:" is reasonable here.

@@ -0,0 +1 @@
Fix :exc:`NameError` caused from :func:`platform.win32_ver`.
Copy link
Member

Choose a reason for hiding this comment

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

Please explain that this change fix the ptype part of :func:platform.win32_ver result.

@vstinner
Copy link
Member

vstinner commented May 4, 2020

@corona10: Are you ok to take this change as the opportunity to replace "except: pass" with "except OSError: pass" to detect similar regressions in the future?

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

Ok, now it LGTM.

@corona10: Would you mind to review the updated PR? If you are ok with it, please merge it.

@sweeneyde
Copy link
Member Author

Would it be worth it to add a test case like

    @unittest.skipUnless(sys.platform.startswith('win'), "windows only test")
    def test_win32_ver(self):
        release, version, csd, ptype = platform.win32_ver()
        # are these all of the possibilities?
        ptype_options = ["Multiprocessor Free",
                         "Multiprocessor Checked",
                         "Uniprocessor Free",
                         "Uniprocessor Checked"]
        self.assertIn(ptype, ptype_options)

in test_platform?

@vstinner
Copy link
Member

vstinner commented May 5, 2020

I expect that such test will likely fail on some specific Windows versions. I don't think that it's worth it.

@vstinner
Copy link
Member

vstinner commented May 5, 2020

I approved the PR, there is no need to modify it anymore. Since there was a disagreement previously on the except, I'm now waiting for @corona10.

Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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

LGTM again :)

Thanks for the work @sweeneyde and for the review @vstinner

@corona10 corona10 merged commit 1e7e451 into python:master May 5, 2020
@miss-islington
Copy link
Contributor

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

@bedevere-bot
Copy link

GH-19912 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 5, 2020
(cherry picked from commit 1e7e451)

Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 5, 2020
(cherry picked from commit 1e7e451)

Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
@bedevere-bot
Copy link

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

miss-islington added a commit that referenced this pull request May 5, 2020
(cherry picked from commit 1e7e451)

Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
miss-islington added a commit that referenced this pull request May 5, 2020
(cherry picked from commit 1e7e451)

Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
@vstinner
Copy link
Member

vstinner commented May 5, 2020

Thanks for the fix @sweeneyde! I didn't expect that it would fix a real bug ;-) It's even better!

@sweeneyde sweeneyde deleted the platformfix branch January 25, 2022 23:10
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