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
pprint long non-printable bytes as hexdump #62068
Comments
Here is a patch with which pprint formats long bytes objects which contain non-ascii or non-printable bytes as a hexdump. Inspired by Antoine's wish (http://permalink.gmane.org/gmane.comp.python.ideas/20329). |
A couple of comments:
|
Yes, I think a separate function would be better. There's another issue for pprint() of bytes with line continuations: |
Some issues:
I like that this is not over-engineered. In my hexdump module I got too involved with problems of parsing/producing full dumps in a way compatible with Python 2/3. So I have to postpone my own user story until finally I run out of time. Probably hexdump.dump() returning string will make it a useful API for the primary user story. |
I don't think the general hexdump() function is worth to including in the stdlib. It should have too many options (How many bytes display in one line? How group hexdigits? What replacemant character for non-printables? Whether or not to display addresses? Whether or not to display chars? What are delimiters between hexdigits and chars, address and hexdigits? What are line prefix and suffix? How display last incomplete line? How display first incomplete line?) and this makes it complicated. An application which outputs a hexdump on more rich device (a html file or a ANSI-colored terminal) needs advanced options. However a simple specialized code can be used for special purposes, i.e. internally in the pprint module. I don't see how it can be reused and don't interested in a general function.
An alternative option is first output a bytes object as is (perhaps splitting it on multiple line as in bpo-17530) and then output a hexdamp as a comment. [b'\x7fELF\x01\x01\x01\x00\x00\n' |
Here is an alternative patch which outputs a bytes literal and a hexdump as a comment. |
Le samedi 04 mai 2013 à 09:42 +0000, Serhiy Storchaka a écrit :
I don't understand how it would be useful in the pprint module if it
This won't work very nicely in smaller display widths. You'll need too |
How can it be used besides pprint/pformat functions?
Then perhaps a new parameter for pprint/pformat needed (hex=True?). I think printing integers in hexadecimal can sometimes be useful too.
This is a nature of hexdumps. Every byte requires 4+ characters (or 3+ if group hexdigits tighter). |
I don't understand your question. Do you never print some data at the
Passing type-specific parameters to pprint/pformat sounds like a bad
Which is why the proposal doesn't fit well with pprint/pformat. |
The idea is that the output of pprint should be something like (once python/cpython#61732 is applied):
>>> pprint.pprint(b'\x7fELF\x01\x01\x01\x00\x00\n\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00\x01')
(b'\x7fELF\x01\x01\x01\x00\x00\n\x00\x00'
b'\x00\x00\x00\x00\x02\x00\x03\x00\x01') whereas the output of hexdump can be something like: hexdump() could accept some additional args too if required, but otherwise I don't think the details are so important as long as it produces something readable for a human. |
To be honest, I very rarely even use pprint. I'm too lazy to import it. If I want to quickly get a hexdump, I use something like
Agree. Of course it would be better to automatically determine a "nice" display (use hexdump only for large non-printable bytes).
If you want to print bytes in hex, why not ints and floats? ;) In fact I don't want to print data as hex, so shut up.
Perhaps I misunderstood your wish. I'm not against consider pprint as a black box, which does all good magic inside by default. The use of this feature does not require anything from the users and does not impose obligations on the maintainers. But I'm not interested in a separate function. |
Le samedi 04 mai 2013 à 15:41 +0000, Serhiy Storchaka a écrit :
The problem is the "good magic" will depend on the situation. Really, I |
Oh, I forgot about bytes.fromhex(). This of course looks better than base64.b16decode((...).replace(' ', '')). |
Withdrawn in favor of bpo-17530. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: