This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Implement `mmap.mmap.__repr__`
Type: enhancement Stage: resolved
Components: Extension Modules Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: cool-RR, eric.smith, pablogsal, serhiy.storchaka, thautwarm, xiang.zhang, xtreak
Priority: normal Keywords: patch

Created on 2018-10-10 16:53 by cool-RR, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9891 merged thautwarm, 2018-10-15 11:40
Messages (10)
msg327503 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-10-10 23:48
What do you propose the repr would look like?
msg327517 - (view) Author: Ram Rachum (cool-RR) * Date: 2018-10-11 04:57
There are a few ways we can go with this, depending on how verbose we want to be and how willing we are to make calls to the data. Here are a few pieces of information we could expose:

 - Length
 - Whether it's a file or anonymous memory
 - The entire contents
 - The clipped contents
 - Whether it's opened or closed
 - The access type

So here's an example:

    <mmap.mmap object; length=514 access=ACCESS_READ>

And another: 

    <mmap.mmap object; length=514 content=b'This is my' ... b'file.'>

I don't know whether we're able to include the file name on there, that would be nice.
msg327734 - (view) Author: thautwarm (thautwarm) * Date: 2018-10-15 06:59
How about this:

```
# opened
<mmap.mmap is_closed=False fileno=-1 access=ACCESS_READ length=514 offset=0 entire_contents=b"This is my"...b"file">

# closed
<mmap.mmap is_closed=True fileno=-1 access=ACCESS_READ>

```
msg327759 - (view) Author: Ram Rachum (cool-RR) * Date: 2018-10-15 15:03
thautwarm, your suggestions look good. I'm not sure why you used
`entire_contents`, do you want to show the entire content there? Because I saw that the sample you used had truncation. It might be unsafe to show the entire mmap, because it could be huge. I'd truncate in some way or other. (Either show just the beginning, or just the beginning and the end.)
msg327767 - (view) Author: thautwarm (thautwarm) * Date: 2018-10-15 17:58
@Ram Rachum

Yes yes yes, actually the entire contents will be truncated when its length is bigger than a specific integer(now it's 50). For we use `...` to represent the ignored contents, `start ... end` could also be called *entire_contents* :-)
msg327805 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2018-10-16 02:59
I don't like the idea to display the contents in the repr, no matter entire or clipped. I think it's not suitable and for contents, just read the object manually. For me, a closed status plus the constructor arguments is a good choice.
msg327806 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-10-16 05:16
I agree with Xiang Zhang: I don't think we should show any content. Especially since basically every file will be truncated, and more often than not the interesting content isn't at the start or end of the file.
msg327807 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-16 05:30
I think that performing any IO operations in repr() is not a good idea.
msg327833 - (view) Author: thautwarm (thautwarm) * Date: 2018-10-16 14:10
I think it depends on the use case. If `repr` wouldn't be invoked except debugging,some IO operations might not be that bad.

I'm to make some adjustments to avoid displaying the contents if nothing evidence that IO operations is not bad.
msg354838 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2019-10-17 10:41
New changeset d8ca2354ed30c12b9ce37c4535222b700a727b32 by Xiang Zhang (Taine Zhao) in branch 'master':
bpo-34953: Implement `mmap.mmap.__repr__` (GH-9891)
https://github.com/python/cpython/commit/d8ca2354ed30c12b9ce37c4535222b700a727b32
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79134
2019-10-17 10:42:49xiang.zhangsetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
components: + Extension Modules, - Interpreter Core
versions: + Python 3.9, - Python 3.8
2019-10-17 10:41:41xiang.zhangsetmessages: + msg354838
2018-10-16 14:10:41thautwarmsetmessages: + msg327833
2018-10-16 05:30:28serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg327807
2018-10-16 05:16:09eric.smithsetmessages: + msg327806
2018-10-16 02:59:09xiang.zhangsetnosy: + xiang.zhang
messages: + msg327805
2018-10-15 17:58:39thautwarmsetmessages: + msg327767
2018-10-15 15:03:39cool-RRsetmessages: + msg327759
2018-10-15 11:40:04thautwarmsetkeywords: + patch
stage: patch review
pull_requests: + pull_request9254
2018-10-15 06:59:21thautwarmsetnosy: + thautwarm
messages: + msg327734
2018-10-13 12:44:32pablogsalsetnosy: + pablogsal
2018-10-11 04:57:46cool-RRsetmessages: + msg327517
2018-10-10 23:48:52eric.smithsetnosy: + eric.smith
messages: + msg327503
components: + Interpreter Core, - Library (Lib)
2018-10-10 17:50:41xtreaksetnosy: + xtreak
2018-10-10 16:53:18cool-RRcreate