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-25588: Fix regrtest when run inside IDLE #3962

Merged
merged 4 commits into from
Oct 13, 2017
Merged

bpo-25588: Fix regrtest when run inside IDLE #3962

merged 4 commits into from
Oct 13, 2017

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Oct 12, 2017

When regrtest in run inside IDLE, sys.stdout and sys.stderr are not
TextIOWrapper objects and have no file descriptor associated:
sys.stderr.fileno() raises io.UnsupportedOperation.

Disable faulthandler and don't replace sys.stdout in that case.

https://bugs.python.org/issue25588

When regrtest in run inside IDLE, sys.stdout and sys.stderr are not
TextIOWrapper objects and have no file descriptor associated:
sys.stderr.fileno() raises io.UnsupportedOperation.

Disable faulthandler and don't replace sys.stdout in that case.
@vstinner
Copy link
Member Author

I chose to not add a NEWS entry since running the Python test suite in IDLE causes many tests to fail. I don't think that we should promote running tests in IDLE.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM even if it doesn't fix all tests.

except io.UnsupportedOperation:
# On IDLE, sys.stdout has no file descriptor and is not a TextIOWrapper
# object. Leaving sys.stdout unchanged.
return
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we could use codecs.StreamRecoder here. But this looks like a more complex issue.

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 expect that the IDLE shell handles correctly Unicode. replace_stdout() was written for terminals using ASCII/strict for stdout.

faulthandler.register(signum, chain=True)
try:
stderr_fd = sys.stderr.fileno()
except io.UnsupportedOperation:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe catch ValueError? This would cover the case of closed files too. And if support the case sys.stderr is None we should catch AttributeError.

Copy link
Member Author

Choose a reason for hiding this comment

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

ValueError: ok, I made the change. It avoids "import io", so I like it :-) Even if I'm not sure that we are supposed to support running tests with closed stderr...

AtttributeError: I'm not sure that it makes sense to run the test suite if there is no sys.stderr. Where are you supposed to write the output if there is not stdout nor stderr?

@serhiy-storchaka serhiy-storchaka added needs backport to 2.7 tests Tests in the Lib/test dir labels Oct 12, 2017
if hasattr(signal, 'SIGUSR1'):
signals.append(signal.SIGUSR1)
for signum in signals:
faulthandler.register(signum, chain=True, file=stderr_fd)
Copy link
Member

Choose a reason for hiding this comment

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

Do the faulthandler functions absolutely need an fd int rather than the existing output object?

Copy link
Member Author

Choose a reason for hiding this comment

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

faulthandler really needs a FD internally. The API accepts a file and calls .fileno() for you. Since we already know the FD, I prefer to directly pass the FD.

@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.

terryjreedy
terryjreedy previously approved these changes Oct 13, 2017
#
# Catch ValueError to catch io.UnsupportedOperation on TextIOBase
# and ValueError on a closed stream.
stderr_fd = None
Copy link
Member

Choose a reason for hiding this comment

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

Replace line with

        try:
            stderr_fd = sys.__stderr__.fileno()
        except ValueError:
            stderr_fd = None

If IDLE is started from a command line or python (by import idlelib.idle), the backup call returns 2.

sys.stderr
<_io.TextIOWrapper name='' mode='w' encoding='utf-8'>
sys.stderr.fileno()
2

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@terryjreedy terryjreedy dismissed their stale review October 13, 2017 01:37

I meant to request changes

Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

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

see previous comment

@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.

@vstinner
Copy link
Member Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@serhiy-storchaka, @terryjreedy: please review the changes made to this pull request.

try:
stderr_fd = sys.__stderr__.fileno()
except (ValueError, AttributeError):
# In IDLE, sys.stderr.fileno() raises io.UnsupportedOperation: the
Copy link
Member

Choose a reason for hiding this comment

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

If you use sys.__stderr__.fileno() this comment looks outdated.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, removed.

@terryjreedy
Copy link
Member

Apveyor failure looks random and unrelated to this patch. Tests pass on my machine with a fresh build.

ERROR: setUpClass (test.test_nntplib.NetworkedNNTP_SSLTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\cpython\lib\test\test_nntplib.py", line 295, in setUpClass
    usenetrc=False)
  File "C:\projects\cpython\lib\nntplib.py", line 1077, in __init__
    self.sock = _encrypt_on(self.sock, ssl_context, host)
  File "C:\projects\cpython\lib\nntplib.py", line 292, in _encrypt_on
    return context.wrap_socket(sock, server_hostname=hostname)
  File "C:\projects\cpython\lib\ssl.py", line 410, in wrap_socket
    _session=session
  File "C:\projects\cpython\lib\ssl.py", line 821, in __init__
    self.do_handshake()
  File "C:\projects\cpython\lib\ssl.py", line 1075, in do_handshake
    self._sslobj.do_handshake()
  File "C:\projects\cpython\lib\ssl.py", line 696, in do_handshake
    self._sslobj.do_handshake()
OSError: [Errno 0] Error

@vstinner vstinner merged commit ccef823 into python:master Oct 13, 2017
@miss-islington
Copy link
Contributor

Thanks @Haypo for the PR 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6.
🐍🍒⛏🤖

@vstinner vstinner deleted the regrtest_idle branch October 13, 2017 19:59
@bedevere-bot
Copy link

GH-3987 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 13, 2017
When regrtest in run inside IDLE, sys.stdout and sys.stderr are not
TextIOWrapper objects and have no file descriptor associated:
sys.stderr.fileno() raises io.UnsupportedOperation.

Disable faulthandler and don't replace sys.stdout in that case.
(cherry picked from commit ccef823)
@vstinner
Copy link
Member Author

Apveyor failure looks random and unrelated to this patch. Tests pass on my machine with a fresh build.

"ERROR: setUpClass (test.test_nntplib.NetworkedNNTP_SSLTests)"

That's an old but unfixed bug: https://bugs.python.org/issue30188

@miss-islington
Copy link
Contributor

Sorry, @Haypo, I could not cleanly backport this to 2.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker ccef823939d4ef602f2d8d13d0bfec29eda597a5 2.7

@vstinner
Copy link
Member Author

@serhiy-storchaka serhiy-storchaka added needs backport to 2.7 needs backport to 3.6 type-tests labels a day ago

faulthandler doesn't exist in Python 2.7, not replace_stdout(). I tested: "import test.autotest" works in Python 2.7.

vstinner pushed a commit that referenced this pull request Oct 13, 2017
When regrtest in run inside IDLE, sys.stdout and sys.stderr are not
TextIOWrapper objects and have no file descriptor associated:
sys.stderr.fileno() raises io.UnsupportedOperation.

Disable faulthandler and don't replace sys.stdout in that case.
(cherry picked from commit ccef823)
@serhiy-storchaka
Copy link
Member

Thanks @Haypo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants