-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix defaultEncoding in base.py, uncovered by Buildbot regression tests #1502
Conversation
work. This is required for Buildbot's test infrastructure to work properly.
|
With help from @p12tic (Buildbot release manager), we found the following. sys.stdout.encoding is always populated in CPython as per:
However, as part of it's test suite, Buildbot overrides This is not ideal, but there could be other projects out there using Twisted which do the same. So I'm taking the conservative approach here, and always populating defaultEncoding. |
|
I tested Twisted's |
|
The PR looks good to me. I'm not an official Twisted reviewer, just coming from Buildbot side. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rodrigc, thanks for continuing to work on this.
I'm afraid that this doesn't look right to me. In addition to the None issue highlighted inline, it's unclear to me why the stdout encoding is relevant for environment variables, and furthermore given that change was originally made for Windows, why are we encoding environment variables to bytes at all on that platform?
I've spelunked the CPython sources a bit, but have to head to bed now. I'll follow up with you again tomorrow.
| # value of sys.stdout.encoding. | ||
| # If a client application patches sys.stdout so that encoding is not | ||
| # set properly, try to fall back to sys.__stdout__.encoding. | ||
| defaultEncoding = sys.stdout.encoding or sys.__stdout__.encoding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
So it looks like there are two issues here:
I'm a little concerned to release Twisted with the use of Overall I'm unsure if @rodrigc What do you want to do? |
|
Thanks for the update. The CI looks green, but I only see some mocked unit tests without an integration tests passing actual Unicode arguments. So I think that this fixed the error from Buildbot, but I am not sure if this add proper UTF-8 / UTF-16 / Unicode handling in spawnProcess. My suggestion is to use at least the "str with only ascii" argument passing tests and the "str with unicode" tests... and add another test in which the arguments have invalid UTF-8 encoding. In python 2.7 popen was complicated .... but I think that things are better in Python 3. So as suggested by Tom, maybe just remove the encoding handling in Twisted and let stdlib do its magic. |
|
@twm The patch in this PR is minimal, and my preference is to merge it as-is. I do not want to complicate this I spent a lot of time testing the code in base.py and changing it due to the various problems I saw on Windows in different versions of Python from 3.5-3.7. After this is merged, I want to do a new release of Twisted.
The original motivation for In Python 3, environment variables and command-line arguments are Unicode strings on all platforms, and these are passed all the way down to the lowest levels of the reactor. So there is no need to play these bytes/unicode games when passing args/environment variables to the reactor. I do not want to hold up the current release cycle on that, but I want to get this change in to not break Buildbot with the latest release. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rodrigc I am okay with an incremental step that doesn't address the larger issues here. However, I think that some minor changes are still required here before we can land and release.
It would be a regression to do a release that doesn't work in a Windows GUI app, where sys.stdout may be None, as I noted before. Twisted 20.3.0 would not fail in this case. I do not think that we should merge without addressing this (and adding a test, of course).
Given the dubious value this codepath offers on Windows I think that it would be reasonable to address this case by skipping bytes coercion entirely if sys.stdout/sys.__stdout__ are not present. The argChecker codepath is only intended to give an early error, after all. Cryptic post-fork() errors aren't great, but they're probably better than not being able to spawn processes at all.
I'll file a ticket about the whole arg/environment encoding situation based on my comments above once this PR is in. While there's a minor mess here most of this code has been around a while, so I don't think that it qualifies as a release blocker. I also agree that it would be easier to address once Python 3.5 is no longer a factor.
I hope this clarifies my earlier feedback. Thanks for your work on this!
|
@twm Thanks for the feedback. I don't understand what you are asking for. Are you saying that if |
|
@rodrigc Yes, I think that it is okay if
|
|
@twm I'm still don't understand what you are suggesting. Are you suggesting that _checkProcessArgs should be skipped in these places if sys.stdout is or are you suggesting that inside _checkProcessArgs(), if sys.stdout is None, |
Yes, that would be acceptable. |
|
I wrote the following program test.py: I then ran it on Windows 10, with Python 3.5.4's pythonw.exe as: The contents of b.txt was: You are indeed correct. Thanks for pointing out this corner case. |
This scenario occurs in Windows GUI Applications which have no console.
922fbf2
to
ae65122
Compare
|
@twm I have added a check in _checkProcessArgs() is sys.stdout is None, and added a test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rodrigc Sorry I missed your message until now. This looks okay to me.
I think we'll want to revisit all this in the next release cycle. Once we drop Python 3.5 support all of this will be simpler, but I don't think making it perfect need block the release any further.
|
@twm thanks for the review, and especially for bringing up the corner cases. |
Scope and purpose
In buildbot/buildbot#5591 , we found that Twisted trunk started breaking some of Buildbot's tests:
Contributor Checklist:
tox -e lintto format my patch to meet the Twisted Coding Standardreviewto the keywords field in Trac, and putting a link to this PR in the comment; it shows up in https://twisted.reviews/ now.