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-29741: make some BytesIO methods accept integer types #560

Conversation

orenmn
Copy link
Contributor

@orenmn orenmn commented Mar 8, 2017

according to http://bugs.python.org/issue29741:

  • change some functions in Modules/_io/stringio.c and in Modules/_io/bytesio.c to use _PyIO_ConvertSsize_t (which supports integer types)
  • change _io_BytesIO_truncate_impl so that it would accept integer types
  • add tests to test_memoryio
  • change stuff in Lib/_pyio.py to behave like the C implementaiton

(I ran the test module again, and on my Windows 10, the same tests failed with
and without my patches. However, on my Ubuntu 16.04 VM, none of the tests
failed.)

https://bugs.python.org/issue29741

@mention-bot
Copy link

@orenmn, thanks for your PR! By analyzing the history of the files in this pull request, we identified @serhiy-storchaka, @benjaminp, @avassalotti, @vadmium and @rhettinger to be potential reviewers.

@orenmn
Copy link
Contributor Author

orenmn commented Mar 8, 2017

with regard to continuous-integration/appveyor/pr build fail - I see in the logs that test_site failed, but on my Windows 10, test_site passed.
(6 tests did fail on my Windows 10, but they failed both on cpython with and without my patches.)

@brettcannon brettcannon added the type-feature A feature request or enhancement label Mar 8, 2017
@orenmn
Copy link
Contributor Author

orenmn commented Mar 10, 2017

Does the AppVeyor build fail prevent core devs from reviewing this patch?
And if it does, what can I do about it?

@orenmn
Copy link
Contributor Author

orenmn commented Mar 13, 2017

merged changes from PR #606 and PR #650.
that leaves this PR with changes only in Lib/_pyio.py and Lib/test/test_memoryio.py.

Copy link
Member

@zooba zooba left a comment

Choose a reason for hiding this comment

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

It seems like with the native module changes the rest of the _pyio.py changes may be unnecessary. The enhanced tests are definitely valuable, but it would be nice to see if they pass without adding the extra error messages to _pyio.

Lib/_pyio.py Outdated
try:
size = size.__index__()
except AttributeError as err:
raise TypeError("an integer is required") from err
Copy link
Member

Choose a reason for hiding this comment

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

Given how size gets used after this, there shouldn't be any need to reassign it by calling __index__. Similarly for the other changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

when I remove the reassignment, I get TypeError: '<' not supported between instances of 'int' and 'IntLike' somewhere later, when the size var is compared to an int.
Did I misunderstand your comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Steve?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry - GitHub messages go to my work email, which means I don't see them unless I'm sneakily doing Python stuff on work time. Conversation on the bug is better.

There shouldn't be any need for the from err - chaining will happen automatically (that syntax is there for explicit non-trivial chaining). It's probably also a good idea to separate the attribute access from the invocation to ensure we wrap up the right exception:

try:
    size_index = size.__index__
except AttributeError:
    raise TypeError(f"{size!r} is not int-like enough")
else:
    size = size_index()   # now AttributeErrors from inside __index__ will be raised cleanly

@orenmn
Copy link
Contributor Author

orenmn commented May 24, 2017

I am really busy nowadays (this is my 2nd term), and so I won't be able to work on CPython until August.. if anyone wishes to take it from where I left, please do :)

Copy link
Member

@zooba zooba left a comment

Choose a reason for hiding this comment

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

Just some relatively minor style fixes, but we need to get these right within the stdlib.

Lib/_pyio.py Outdated
try:
size = size.__index__()
except AttributeError as err:
raise TypeError("an integer is required") from err
Copy link
Member

Choose a reason for hiding this comment

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

Sorry - GitHub messages go to my work email, which means I don't see them unless I'm sneakily doing Python stuff on work time. Conversation on the bug is better.

There shouldn't be any need for the from err - chaining will happen automatically (that syntax is there for explicit non-trivial chaining). It's probably also a good idea to separate the attribute access from the invocation to ensure we wrap up the right exception:

try:
    size_index = size.__index__
except AttributeError:
    raise TypeError(f"{size!r} is not int-like enough")
else:
    size = size_index()   # now AttributeErrors from inside __index__ will be raised cleanly

@@ -11,6 +11,13 @@
import pickle
import sys

class IntLike():
Copy link
Member

Choose a reason for hiding this comment

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

No parens after class names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my bad.
BTW, do you think this should be added to PEP8, or is it obvious?
(i grepped and found only 7 places in the codebase doing this.)

Copy link
Member

Choose a reason for hiding this comment

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

Hmm... I found more than that, though they're all in tests. Maybe it's just my personal preference - don't worry about it if you don't want to

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not worried at all, and I would certainly follow your advice.
just wondered whether this should be added to the PEP..

and it seems that the docs think like you - https://docs.python.org/3.7/tutorial/classes.html#class-definition-syntax.

@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 didn't expect the Spanish Inquisition!. 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.

@orenmn
Copy link
Contributor Author

orenmn commented Aug 22, 2017

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

Nobody expects the Spanish Inquisition!

@zooba: please review the changes made to this pull request.

try:
size_index = size.__index__
except AttributeError:
raise TypeError(f"{size!r} is not an integer")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

hope you are OK with this phrasing.. I chose it because even though 'int-like'
is intuitive, it seems that 'integer' is much more common in the docs and in
error messages (including in _pyio.py).

Copy link
Member

@zooba zooba left a comment

Choose a reason for hiding this comment

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

Yes, I'm happy with this now. Just needs the news.d item - click "Details" next to the check below and it'll give you the instructions to add this.

@zooba
Copy link
Member

zooba commented Aug 24, 2017

Thanks! I removed my name from the news item, since it doesn't have to go there, and once the build completes I'll merge.

@orenmn
Copy link
Contributor Author

orenmn commented Aug 24, 2017

I added your name because most of the code in the patch was written by you.
thanks for the review :)

@zooba
Copy link
Member

zooba commented Aug 24, 2017

Heh, maybe, but there's a lot of code in Python that doesn't have my name on it :) That's what happens when you get merge permissions - you stop getting explicit credit for every little change.

@zooba zooba merged commit de50360 into python:master Aug 24, 2017
GadgetSteve pushed a commit to GadgetSteve/cpython that referenced this pull request Sep 10, 2017
jaraco pushed a commit that referenced this pull request Dec 2, 2022
Bumps [sentry-sdk](https://github.com/getsentry/sentry-python) from 1.5.12 to 1.6.0.
- [Release notes](https://github.com/getsentry/sentry-python/releases)
- [Changelog](https://github.com/getsentry/sentry-python/blob/master/CHANGELOG.md)
- [Commits](getsentry/sentry-python@1.5.12...1.6.0)

---
updated-dependencies:
- dependency-name: sentry-sdk
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants