Skip to content

bpo-32890: os: Use errno instead of GetLastError() where appropriate#5784

Merged
vstinner merged 2 commits into
python:masterfrom
izbyshev:bpo-32890
Oct 20, 2018
Merged

bpo-32890: os: Use errno instead of GetLastError() where appropriate#5784
vstinner merged 2 commits into
python:masterfrom
izbyshev:bpo-32890

Conversation

@izbyshev

@izbyshev izbyshev commented Feb 21, 2018

Copy link
Copy Markdown
Contributor

path_error() uses GetLastError() on Windows, but some os functions
are implemented via CRT APIs which report errors via errno.
This may result in raising OSError with invalid error code (such
as zero).

Introduce posix_path_error() function and use it where appropriate.

https://bugs.python.org/issue32890

path_error() uses GetLastError() on Windows, but some os functions
are implemented via CRT APIs which report errors via errno.
This may result in raising OSError with invalid error code (such
as zero).

Introduce posix_path_error() function and use it where appropriate.

@zooba zooba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just needs the NEWS.d entry so we can merge.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

posix_path_error() is used only twice, and posix_path_object_error() is used only twice including posix_path_error() implementation. Too much indirections makes harder reading the code and may hide some details. I would inline these function for improving readability and minimizing changes.

@izbyshev

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka My motivation for adding those functions was consistency with existing path_error and similar functions.
@zooba What do you think about it?

@serhiy-storchaka

Copy link
Copy Markdown
Member

path_error() is used 35 times. posix_error() is used 112 times. Rarely used functions with similar names can be confused with common functions. You need to find that posix_path_error() is expanded to posix_path_object_error() which is expanded to PyErr_SetFromErrnoWithFilenameObject(), and path_error() is expanded to path_object_error() which is expanded to PyErr_SetExcFromWindowsErrWithFilenameObject() or PyErr_SetFromErrnoWithFilenameObject() to figure out the difference. This is twice easier if inline rarely used functions.

@ned-deily

Copy link
Copy Markdown
Member

@zooba, @serhiy-storchaka Where are we with this PR?

@vstinner vstinner merged commit 8346031 into python:master Oct 20, 2018
@miss-islington

Copy link
Copy Markdown
Contributor

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

@bedevere-bot

Copy link
Copy Markdown

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 20, 2018
…uncate() (pythonGH-5784)

path_error() uses GetLastError() on Windows, but some os functions
are implemented via CRT APIs which report errors via errno.
This may result in raising OSError with invalid error code (such
as zero).

Introduce posix_path_error() function and use it where appropriate.
(cherry picked from commit 8346031)

Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
@bedevere-bot

Copy link
Copy Markdown

GH-9985 is a backport of this pull request to the 3.6 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 20, 2018
…uncate() (pythonGH-5784)

path_error() uses GetLastError() on Windows, but some os functions
are implemented via CRT APIs which report errors via errno.
This may result in raising OSError with invalid error code (such
as zero).

Introduce posix_path_error() function and use it where appropriate.
(cherry picked from commit 8346031)

Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
@vstinner

Copy link
Copy Markdown
Member

posix_path_error() is used only twice, and posix_path_object_error() is used only twice including posix_path_error() implementation. Too much indirections makes harder reading the code and may hide some details. I would inline these function for improving readability and minimizing changes.

IMHO practicality beats purity here. I concur with @izbyshev ("My motivation for adding those functions was consistency with existing path_error and similar functions.")

miss-islington added a commit that referenced this pull request Oct 20, 2018
…uncate() (GH-5784)

path_error() uses GetLastError() on Windows, but some os functions
are implemented via CRT APIs which report errors via errno.
This may result in raising OSError with invalid error code (such
as zero).

Introduce posix_path_error() function and use it where appropriate.
(cherry picked from commit 8346031)

Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
miss-islington added a commit that referenced this pull request Oct 20, 2018
…uncate() (GH-5784)

path_error() uses GetLastError() on Windows, but some os functions
are implemented via CRT APIs which report errors via errno.
This may result in raising OSError with invalid error code (such
as zero).

Introduce posix_path_error() function and use it where appropriate.
(cherry picked from commit 8346031)

Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
@serhiy-storchaka

Copy link
Copy Markdown
Member

Practicality beats purity, but it looks to me that this change opposed to this rule. New functions were introduced just for reasons of purity. It needlessly complicated the code and made it harder to read.

"A foolish consistency is the hobgoblin of little minds".

@izbyshev izbyshev deleted the bpo-32890 branch October 20, 2018 20:02
Comment thread Lib/test/test_os.py
except OSError as e:
self.assertTrue(e.winerror is None or e.winerror != 0)
else:
self.fail('No OSError raised')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note for future PRs: self.assertRaises should be used in such cases.
It makes the intent of the test clearer, and prevent adding the else block which lowers test coverage (as it is not reached unless the code is buggy).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, thanks for the advice.

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.

9 participants