GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update mmap stubs for newer Python versions by bmerry · Pull Request #4244 · python/typeshed

Conversation

@bmerry

@bmerry bmerry commented Jun 19, 2020

Copy link
Copy Markdown
Contributor

Based on the Python stdlib documentation:

  • Since Python 3.5, mmap.{find,rfind,write} all accept any bytes-like.
    I've used the _typeshed.ReadableBuffer alias defined in Introduce ReadableBuffer and WriteableBuffer Union aliases #4232.
  • Since Python 3.6, mmap.write returns the number of bytes written.
  • Since Python 3.3, mmap.read allows None as the parameter; while in
    Python 2 the argument cannot be omitted.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bmerry
Update mmap stubs for newer Python versions
82254a2
Based on the Python stdlib documentation:
- Since Python 3.5, mmap.{find,rfind,write} all accept any bytes-like.
  I've used the _typeshed.ReadableBuffer alias defined in python#4232.
- Since Python 3.6, mmap.write returns the number of bytes written.
- Since Python 3.3, mmap.read allows None as the parameter; while in
  Python 2 the argument cannot be omitted.
@bmerry

bmerry commented Jun 19, 2020

Copy link
Copy Markdown
Contributor Author

Incidentally, I don't really follow why master defines rfind separately for Python 2/3 while it defines find in the base class. I'd be happy to clean that up too if there is no good reason for it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

srittau
srittau reviewed Jun 19, 2020
View reviewed changes
Comment thread stdlib/2and3/mmap.pyi Outdated
Comment on lines +66 to +70
if sys.version_info >= (3, 5):
def find(self, sub: ReadableBuffer, start: int = ..., stop: int = ...) -> int: ...
def rfind(self, sub: ReadableBuffer, start: int = ..., stop: int = ...) -> int: ...
else:
def rfind(self, sub: bytes, start: int = ..., stop: int = ...) -> int: ...

@srittau srittau Jun 19, 2020

Copy link
Copy Markdown
Collaborator

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.

Since typeshed only supports Python 2.7 and 3.5+, you can leave out the version check and the else branch.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@bmerry bmerry Jun 19, 2020

Copy link
Copy Markdown
Contributor Author

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.

Thanks, I've used that to simplify/clean up things a bit further.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@srittau

srittau commented Jun 19, 2020

Copy link
Copy Markdown
Collaborator

Generally, some type stubs are rather old and use conventions that predate modern typeshed conventions. They also often have been patched several times by multiple people. So if you find something amiss, it's often due to these factors, and cleanups are most welcome. Checking the output of git log and git blame for the file in question can also give hints.

JelleZijlstra reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bmerry added a commit to bmerry/typeshed that referenced this pull request Jun 19, 2020
@bmerry
Use the ReadableBuffer type in more places
cf1507d
This is a follow-up on python#4232. memoryview, hashlib, and hmac are updated
to use ReadableBuffer type instead of their own home-spun unions of
bytes, bytearray and whatever else each use case used. mmap is being
handled in python#4244, and I'll leave BinaryIO for another day (or possibly
another person) because it's going to require some messy code
duplication because the relevant methods are defined in IO[AnyStr].

There's one corner case I'm not quite sure how best to handle: the
documentation for hmac.digest claim that the parmaeters have the same
meanings as in hmac.new, but in CPython the latter has an explicit check
that `key` is bytes or bytearray while the former works with a
memory-view. For now I've matched the documentation.

Also, the documentation for HMAC.update says that `msg` can be any type
supported by hashlib from Python 3.4; but I can't see anything in the
Python 2.7 implementation that would prevent it also taking bytes-like
objects, so I've not tried to treat Python 2 any different to Python 3.
@bmerry bmerry mentioned this pull request Jun 19, 2020
Use the ReadableBuffer type in more places #4245
Merged
@bmerry
Further clean up mmap.pyi
f575952
Use the fact that Python 3.0-3.4 are no longer supported to clean up the
version-dependent logic. Functions that always have different signatures
in Python 2/3 are moved from the base _mmap[bytes] to the mmap subclass.
JelleZijlstra
JelleZijlstra approved these changes Jun 21, 2020
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

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.

This all looks correct.

I'm wondering what's going on with the "_mmap" class. It doesn't exist at runtime in either Python 2 or 3—the mmap.mmap type inherits only from object. Maybe it exists so that we can share some methods between Python 2 and 3, but that doesn't explain why it's generic over AnyStr.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@JelleZijlstra
JelleZijlstra merged commit ec6116d into python:master Jun 21, 2020
@bmerry
bmerry deleted the mmap-readable-buffer branch June 22, 2020 06:56
@bmerry

bmerry commented Jun 22, 2020

Copy link
Copy Markdown
Contributor Author

I'm wondering what's going on with the "_mmap" class.

Glad it's not just me that's confused. I'm pretty tempted to just reorganise it as a single class with if sys_version blocks.

JelleZijlstra reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra pushed a commit that referenced this pull request Jun 22, 2020
@bmerry
Use the ReadableBuffer type in more places (#4245)
fb398b1
This is a follow-up on #4232. memoryview, hashlib, and hmac are updated
to use ReadableBuffer type instead of their own home-spun unions of
bytes, bytearray and whatever else each use case used. mmap is being
handled in #4244, and I'll leave BinaryIO for another day (or possibly
another person) because it's going to require some messy code
duplication because the relevant methods are defined in IO[AnyStr].

There's one corner case I'm not quite sure how best to handle: the
documentation for hmac.digest claim that the parmaeters have the same
meanings as in hmac.new, but in CPython the latter has an explicit check
that `key` is bytes or bytearray while the former works with a
memory-view. For now I've matched the documentation.

Also, the documentation for HMAC.update says that `msg` can be any type
supported by hashlib from Python 3.4; but I can't see anything in the
Python 2.7 implementation that would prevent it also taking bytes-like
objects, so I've not tried to treat Python 2 any different to Python 3.
vishalkuo pushed a commit to vishalkuo/typeshed that referenced this pull request Jun 26, 2020
@bmerry Vishal Kuo
Update mmap stubs for newer Python versions (python#4244)
8cb9764
* Update mmap stubs for newer Python versions

Based on the Python stdlib documentation:
- Since Python 3.5, mmap.{find,rfind,write} all accept any bytes-like.
  I've used the _typeshed.ReadableBuffer alias defined in python#4232.
- Since Python 3.6, mmap.write returns the number of bytes written.
- Since Python 3.3, mmap.read allows None as the parameter; while in
  Python 2 the argument cannot be omitted.

* Further clean up mmap.pyi

Use the fact that Python 3.0-3.4 are no longer supported to clean up the
version-dependent logic. Functions that always have different signatures
in Python 2/3 are moved from the base _mmap[bytes] to the mmap subclass.
vishalkuo pushed a commit to vishalkuo/typeshed that referenced this pull request Jun 26, 2020
@bmerry Vishal Kuo
Use the ReadableBuffer type in more places (python#4245)
4166f3c
This is a follow-up on python#4232. memoryview, hashlib, and hmac are updated
to use ReadableBuffer type instead of their own home-spun unions of
bytes, bytearray and whatever else each use case used. mmap is being
handled in python#4244, and I'll leave BinaryIO for another day (or possibly
another person) because it's going to require some messy code
duplication because the relevant methods are defined in IO[AnyStr].

There's one corner case I'm not quite sure how best to handle: the
documentation for hmac.digest claim that the parmaeters have the same
meanings as in hmac.new, but in CPython the latter has an explicit check
that `key` is bytes or bytearray while the former works with a
memory-view. For now I've matched the documentation.

Also, the documentation for HMAC.update says that `msg` can be any type
supported by hashlib from Python 3.4; but I can't see anything in the
Python 2.7 implementation that would prevent it also taking bytes-like
objects, so I've not tried to treat Python 2 any different to Python 3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@srittau srittau srittau left review comments
@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@bmerry @srittau @JelleZijlstra
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.