Skip to content

bpo-40638: Check for attribute lookup failure in builtin_input_impl #20125

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tekknolagi
Copy link
Contributor

@tekknolagi tekknolagi commented May 15, 2020

Add tests to cover this case.

https://bugs.python.org/issue40638

@tekknolagi tekknolagi force-pushed the mb-fix-builtin-input branch from 153b866 to 8eaabdb Compare May 27, 2020 23:59
Copy link
Contributor

@remilapeyre remilapeyre left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this @tekknolagi, the change looks correct but does not conform to the coding style.

@@ -2010,16 +2010,16 @@ builtin_input_impl(PyObject *module, PyObject *prompt)

/* stdin is a text stream, so it must have an encoding. */
stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
if (!stdin_encoding || !PyUnicode_Check(stdin_encoding))
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be

if (!stdin_encoding || !PyUnicode_Check(stdin_encoding)) {
    goto _readline_disable_tty_and_fall_back;
}

as it is specified in PEP 7 (https://www.python.org/dev/peps/pep-0007/#code-lay-out):

All new C code requires braces

@tekknolagi
Copy link
Contributor Author

tekknolagi commented Jun 6, 2020 via email

@tekknolagi tekknolagi force-pushed the mb-fix-builtin-input branch from 8eaabdb to 400bef9 Compare June 6, 2020 01:30
Copy link
Contributor

@remilapeyre remilapeyre left a comment

Choose a reason for hiding this comment

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

Thanks, can you add a blurb to document the change? You can add one by using https://blurb-it.herokuapp.com/

Copy link
Contributor

@taleinat taleinat left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to fix this, @tekknolagi, and sorry for the long delay in reviewing!

I've got a few minor comments.

Comment on lines +1322 to +1328
# input() in tty mode with a malformed input stream should attempt
# to call .readline()
sys.stdin = MalformedInputStream()
sys.stdout = MalformedOutputStream()
self.assertEqual(input("baz"), "foobar") # strips \n
self.assertEqual(sys.stdout.value, "baz")

Copy link
Contributor

Choose a reason for hiding this comment

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

Having this here is slightly confusing, as it appears like it may affect the following assertion as well. Perhaps put this at the end of this method or in a separate method.

# input() in tty mode with a malformed input stream should attempt
# to call .readline()
sys.stdin = MalformedInputStream()
sys.stdout = MalformedOutputStream()
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you considered using a StringIO object for sys.stdout here?

@@ -75,6 +75,23 @@ class BitBucket:
def write(self, line):
pass

class MalformedInputStream:
Copy link
Contributor

Choose a reason for hiding this comment

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

"Malformed" in what way? I think this name could be better.

Suggested change
class MalformedInputStream:
class InputStreamWithReadlineButNoRead:

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

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

The following commit authors need to sign the Contributor License Agreement:

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants