[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light
/ cpython Public
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-33687: uu: fix call to os.chmod #7282

Merged
merged 2 commits into from Jan 17, 2019

Conversation

Copy link
Contributor

timofurrer commented May 31, 2018

This change addresses the issue reported here: https://bugs.python.org/issue33687

Do I have to pull request this to other branches, too?
The issue exists on all actively maintained cpython branches.

https://bugs.python.org/issue33687

Lib/uu.py Outdated
@@ -134,7 +134,7 @@ def decode(in_file, out_file=None, mode=None, quiet=False):
elif isinstance(out_file, str):
fp = open(out_file, 'wb')
try:
os.path.chmod(out_file, mode)
os.chmod(out_file, mode)
except AttributeError:
Copy link
Member

berkerpeksag May 31, 2018

Choose a reason for hiding this comment

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

Perhaps I'm missing something but os.chmod() is defined undonditionally in Modules/posixmodule.c:

os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,

Do we still need the try...except AttributeError check? It was added in 1995 in 8b74512#diff-bd8ce21226a634ebc2701b92641e3424R108 and I think we can remove it now.

Copy link
Contributor Author

timofurrer May 31, 2018

Choose a reason for hiding this comment

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

Agree. I've removed the try ... except block.

Copy link
Member

serhiy-storchaka commented Jun 3, 2018

Why test_decodetwice was changed? Seems this breaks the purpose of this test.

It would be better to test with a real os.chmod instead of the mock. Try to create a file with a mode 0o444 and check that the resulting file is read-only. This will expose other error in using os.chmod. You need first call os.chmod with mode | stat.S_IWRITE and set the mode mode after successful decoding.

Copy link
Contributor Author

timofurrer commented Jun 3, 2018

Why test_decodetwice was changed? Seems this breaks the purpose of this test.

I see, there is something off with the current test implementation - I'll have a look at it.
However, all those tests have side-effects - that's why I needed to make this change in the first place.
The side-effect is actually confirmed by the current implementation, because the uu.Error is still raised ..

Try to create a file with a mode 0o444 and check that the resulting file is read-only

Why is that? I could argue that this would be a test for os.chmod itself and I don't want the uu test to fail just because something is off with os.chmod ...
Any advice on how you want to handle such cases usually?

timofurrer force-pushed the bugfix/issue-33687 branch 2 times, most recently from d8cea61 to 3fc26d2 Compare Jun 5, 2018
Copy link
Contributor Author

timofurrer commented Jun 5, 2018

@serhiy-storchaka I've seen that you've been working on the tests ..
I've rebased and implemented a test without using a mock.

Copy link
Member

vstinner commented Jun 6, 2018

It seems like your PR breaks test_uu on Windows (AppVeyor).

vstinner changed the title bpo-33687: fix call to os.chmod bpo-33687: uu: fix call to os.chmod Jun 6, 2018
Copy link
Contributor Author

timofurrer commented Jun 6, 2018

Unfortunately, I don't have access to a Windows. Do you know if os.access / os.W_OK behaves differently on a Windows? I couldn't find anything in the docs.
It's seems like os.chmod with 0o444 doesn't make the file read-only?

Copy link
Member

berkerpeksag commented Jun 6, 2018

The test has failed on Linux too: https://python.visualstudio.com/cpython/_build?buildId=6360&tab=details&_a=summary

This line returns True:

self.assertFalse(os.access(self.tmpout, os.W_OK))

Copy link
Member

berkerpeksag commented Jun 6, 2018

By the way, the os.chmod() call in decode() only triggered when out_file passed as str. I think you need to pass self.tmpout to decode() in line 228:

with open(self.tmpin, 'rb') as f:
    uu.decode(f)

Copy link
Contributor Author

timofurrer commented Jun 9, 2018

By the way, the os.chmod() call in decode() only triggered when out_file passed as str. I think you need to pass self.tmpout to decode() in line 228:

This is not always the case, because at

out_file = hdrfields[2].rstrip(b' \t\r\n\f').decode("ascii")
out_file is set to the out_file encoded into the in_file. And this actually happens before the check if out_file is str or not.

With which user are the tests running?
I can reproduce the failure on my machine by running the tests as root:

sudo ./python Lib/test/test_uu.py -v

... which would explain the seen behavior on CI.

Copy link
Contributor Author

timofurrer commented Jun 9, 2018

I've now replace the os.access() calls with os.stat() to verify the os.chmod call - Which is guess is fine for the test because for the test itself it doesn't matter what permissions are set - they just have to match.

Copy link
Member

serhiy-storchaka commented Jun 10, 2018

Just try to open a file for writing. It should fail.

Copy link
Contributor Author

timofurrer commented Jun 10, 2018

Just try to open a file for writing. It should fail.

That's actually not the case as you can see in: https://python.visualstudio.com/cpython/_build?buildId=8085

The only way I could explain this behavior is that the tests are run a root.

Copy link
Member

berkerpeksag commented Jun 10, 2018

According to https://python.visualstudio.com/cpython/_build?buildId=8085 (click to "Display build info") UID is indeed 0.


if __name__=="__main__":
if __name__ == "__main__":
Copy link
Member

berkerpeksag Jun 10, 2018

Choose a reason for hiding this comment

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

I know this may be done by your editor/IDE automatically, but please don't include cosmetic changes to the PR.

Copy link
Contributor Author

timofurrer Jun 10, 2018

Choose a reason for hiding this comment

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

I've reverted it. When I see something like that, should I create a separate PR?

Copy link
Member

berkerpeksag Jun 10, 2018

Choose a reason for hiding this comment

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

We tend to reject cosmetic-only PRs, but if you find similar cases in Lib/test/ (not in the standard library) and have time to prepare a PR, it may be accepted.

Copy link
Contributor Author

timofurrer commented Jun 10, 2018

According to https://python.visualstudio.com/cpython/_build?buildId=8085 (click to "Display build info") UID is indeed 0.

Would be nice to have a separate user to run the tests ..

timofurrer force-pushed the bugfix/issue-33687 branch 2 times, most recently from a67107d to f6f6e42 Compare Jun 10, 2018
Copy link
Member

berkerpeksag commented Jun 10, 2018

uu is an ancient module and no one has noticed this for the past ~20 years, so perhaps we can just skip the test if UID is 0?

timofurrer force-pushed the bugfix/issue-33687 branch 2 times, most recently from 8614a61 to e2d8bac Compare Jun 10, 2018
Lib/test/test_uu.py Outdated Show resolved Hide resolved
Copy link
Member

vstinner commented Oct 19, 2018

@berkerpeksag, @serhiy-storchaka: Berker approved this change, but it's still not merged. Is it ready to be merged? Since last Serhiy comments, updated have been pushed.

Copy link
Contributor Author

timofurrer commented Jan 17, 2019

Is there anything missing from my side to get this merged?

berkerpeksag merged commit 17f05bb into python:master Jan 17, 2019
Copy link
Contributor

miss-islington commented Jan 17, 2019

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

Copy link

bedevere-bot commented Jan 17, 2019

GH-11592 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 Jan 17, 2019
(cherry picked from commit 17f05bb)

Co-authored-by: Timo Furrer <tuxtimo@gmail.com>
Copy link
Member

berkerpeksag commented Jan 17, 2019

@timofurrer sorry for my late response and thank you for your contribution and patience! :)

berkerpeksag pushed a commit that referenced this pull request Jan 17, 2019
(cherry picked from commit 17f05bb)

Co-authored-by: Timo Furrer <tuxtimo@gmail.com>
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.

None yet

8 participants