Skip to content
/ cpython Public
  • Rate limit · GitHub

    Access has been restricted

    You have triggered a rate limit.

    Please wait a few minutes before you try again;
    in some cases this may take up to an hour.

  • Notifications You must be signed in to change notification settings
  • Fork 31.4k
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-28557: error message for bad raw readinto #7496

Merged
merged 3 commits into from
Jun 15, 2020

Conversation

davidszotten
Copy link
Contributor

@davidszotten davidszotten commented Jun 7, 2018

@davidszotten
Copy link
Contributor Author

Would prefer to still return an OSError with the TypeError as a cause, but couldn't figure out how to do that. Hints/help would be appreciated.

@csabella csabella requested a review from benjaminp May 17, 2019 23:58
Copy link
Contributor

@benjaminp benjaminp left a comment

Choose a reason for hiding this comment

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

Yeah, I don't think you should change the exception type.

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

@davidszotten davidszotten force-pushed the fix-issue-28557 branch 2 times, most recently from 5157566 to e72ab00 Compare September 12, 2019 16:36
@benjaminp
Copy link
Contributor

So, I just noticed other similar places in BufferedReader have code like this:

    if (n < 0) {
        if (!PyErr_Occurred())
            PyErr_Format(PyExc_OSError,
                         "Raw stream returned invalid position %" PY_PRIdOFF,
                         (PY_OFF_T_COMPAT)n);
        return -1;
    }

So that will propagate the exception for PyNumber_AsSsize_t directly. I probably wouldn't have designed it this way, but we should follow that existing convention I think.

Sorry for the whiplash.

@davidszotten
Copy link
Contributor Author

So that will propagate the exception for PyNumber_AsSsize_t directly.

Sorry, i'm not sure i quite follow what you mean here

@benjaminp
Copy link
Contributor

I'm saying the error handling code should look like this.

@davidszotten
Copy link
Contributor Author

i see your point about following conventions in the file but i think that makes this a lot less ergonomic and helpful for debugging

without cause:

Traceback (most recent call last):
  File "/Users/david/dev/cpython/try.py", line 7, in <module>
    bufio.readline()
OSError: raw readinto() returned invalid value

with cause:

TypeError: 'bytes' object cannot be interpreted as an integer

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/david/dev/cpython/try.py", line 7, in <module>
    bufio.readline()
OSError: raw readinto() returned invalid value

@davidszotten
Copy link
Contributor Author

friendly bump @benjaminp

@csabella csabella requested a review from benjaminp January 12, 2020 13:48
@davidszotten
Copy link
Contributor Author

@csabella should we change this from awaiting changes to awaiting review again?

@csabella csabella requested review from benjaminp and removed request for benjaminp March 8, 2020 18:58
bufio = self.tp(rawio)
with self.assertRaises(OSError) as cm:
bufio.readline()
assert cm.exception.__cause__ is None
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert cm.exception.__cause__ is None
self.assertIsNone(cm.exception.__cause__)

bufio = self.tp(rawio)
with self.assertRaises(OSError) as cm:
bufio.readline()
assert isinstance(cm.exception.__cause__, TypeError)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert isinstance(cm.exception.__cause__, TypeError)
self.assertIsInstance(cm.exception.__cause__, TypeError)

@davidszotten
Copy link
Contributor Author

thanks for the suggestions @remilapeyre . updated

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 @davidszotten ! Just one small nit, I'm pretty sure an error must be set when n == 1 so you don't have to check PyErr_Occurred() too. Otherwise it looks good to me!

@@ -1483,6 +1483,15 @@ _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
}
n = PyNumber_AsSsize_t(res, PyExc_ValueError);
Py_DECREF(res);

if (n == -1 && PyErr_Occurred()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (n == -1 && PyErr_Occurred()) {
if (n == -1) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i don't quite understand why, but removing that check i get a segfault

here's the bottom of the stack from lldb:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00000001001542f5 python.exe`_PyErr_FormatVFromCause [inlined] _Py_DECREF(op=0x0000000000000000) at object.h:441:9 [opt]
    frame #1: 0x00000001001542f5 python.exe`_PyErr_FormatVFromCause(tstate=0x0000000100506680, exception=0x0000000100292fc0, format="raw readinto() returned invalid value", vargs=0x00007ffeefbf9a40) at errors.c:586 [opt]
  * frame #2: 0x0000000100154460 python.exe`_PyErr_FormatFromCause(exception=<unavailable>, format=<unavailable>) at errors.c:626:5 [opt]
    frame #3: 0x00000001001e8508 python.exe`_bufferedreader_raw_read(self=<unavailable>, start=<unavailable>, len=8192) at bufferedio.c:1488:9 [opt]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hang on, i think i get it now. the point is that n is the return value of the (user-defined) readinto which means it can sometimes be -1 even without PyErr_Occurred. this is then caught in the next if block (and becomes "raw readinto() returned invalid length [..]")

the whole point of this pr is to help users debug bad readinto implementations by providing more granular error messages

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes sorry, this was a mistake :/

@davidszotten
Copy link
Contributor Author

@remilapeyre no worries

does that mean this is ready? pr is marked as "requested changes" from @benjaminp though he never replied to my comment about his suggested change

what's the next step?

@remilapeyre
Copy link
Contributor

@remilapeyre no worries

does that mean this is ready? pr is marked as "requested changes" from @benjaminp though he never replied to my comment about his suggested change

what's the next step?

I think your change looks good, new it needs to be reviewed by a core developer like @benjaminp and they will either merge it or ask you for changes. There is a lot of work going on on Python and they have a lot of PR to review so it may take them some time but they will get there :)

@davidszotten
Copy link
Contributor Author

is there a difference between the tags for "awaiting review" and "awaiting core review" that's applicable here?

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.

The first is when nobody reviewed, the second is when someone who is not a core developer has made a review. It should have switched but the bot seems to have missed it.

@benjaminp benjaminp merged commit 8666356 into python:master Jun 15, 2020
@davidszotten
Copy link
Contributor Author

thank you!

arun-mani-j pushed a commit to arun-mani-j/cpython that referenced this pull request Jul 21, 2020
Co-authored-by: Benjamin Peterson <benjamin@python.org>
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.

Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

6 participants