Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
SMIRR Jean-Loup
igor
Commits
d2c87ab4
Commit
d2c87ab4
authored
Jul 21, 2012
by
W. Trevor King
Browse files
Add igor.util._ord for Python 3 compatibility.
parent
69e98dbe
Changes
1
Hide whitespace changes
Inline
Side-by-side
igor/util.py
View file @
d2c87ab4
...
...
@@ -22,21 +22,33 @@ import sys as _sys
import
numpy
as
_numpy
def
_ord
(
byte
):
r
"""Convert a byte to an integer.
>>> buffer = b'\x00\x01\x02'
>>> [_ord(b) for b in buffer]
[0, 1, 2]
"""
if
_sys
.
version_info
>=
(
3
,):
return
byte
else
:
return
ord
(
byte
)
def
hex_bytes
(
buffer
,
spaces
=
None
):
r
"""Pretty-printing for binary buffers.
>>> hex_bytes(b
uffer(
'\x00\x01\x02\x03\x04')
)
>>> hex_bytes(b'\x00\x01\x02\x03\x04')
'0001020304'
>>> hex_bytes(b
uffer(
'\x00\x01\x02\x03\x04'
)
, spaces=1)
>>> hex_bytes(b'\x00\x01\x02\x03\x04', spaces=1)
'00 01 02 03 04'
>>> hex_bytes(b
uffer(
'\x00\x01\x02\x03\x04'
)
, spaces=2)
>>> hex_bytes(b'\x00\x01\x02\x03\x04', spaces=2)
'0001 0203 04'
>>> hex_bytes(b
uffer(
'\x00\x01\x02\x03\x04\x05\x06'
)
, spaces=2)
>>> hex_bytes(b'\x00\x01\x02\x03\x04\x05\x06', spaces=2)
'0001 0203 0405 06'
>>> hex_bytes(b
uffer(
'\x00\x01\x02\x03\x04\x05\x06'
)
, spaces=3)
>>> hex_bytes(b'\x00\x01\x02\x03\x04\x05\x06', spaces=3)
'000102 030405 06'
"""
hex_bytes
=
[
'{:02x}'
.
format
(
ord
(
x
))
for
x
in
buffer
]
hex_bytes
=
[
'{:02x}'
.
format
(
_
ord
(
x
))
for
x
in
buffer
]
if
spaces
is
None
:
return
''
.
join
(
hex_bytes
)
elif
spaces
is
1
:
...
...
@@ -49,19 +61,19 @@ def assert_null(buffer, strict=True):
r
"""Ensure an input buffer is entirely zero.
>>> import sys
>>> assert_null(b
uffer(
'')
)
>>> assert_null(b
uffer(
'\x00\x00')
)
>>> assert_null(b
uffer(
'\x00\x01\x02\x03')
)
>>> assert_null(b'')
>>> assert_null(b'\x00\x00')
>>> assert_null(b'\x00\x01\x02\x03')
Traceback (most recent call last):
...
ValueError: 00 01 02 03
>>> stderr = sys.stderr
>>> sys.stderr = sys.stdout
>>> assert_null(b
uffer(
'\x00\x01\x02\x03'
)
, strict=False)
>>> assert_null(b'\x00\x01\x02\x03', strict=False)
warning: post-data padding not zero: 00 01 02 03
>>> sys.stderr = stderr
"""
if
buffer
and
ord
(
max
(
buffer
))
!=
0
:
if
buffer
and
_
ord
(
max
(
buffer
))
!=
0
:
hex_string
=
hex_bytes
(
buffer
,
spaces
=
1
)
if
strict
:
raise
ValueError
(
hex_string
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment