summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYusuke Iwase <iwase.yusuke0@gmail.com>2015-09-09 10:17:01 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2015-09-10 11:14:58 +0900
commit27befc18faf98463604e4b42442431da0a29ea55 (patch)
treef4dd32099f8274ce1c308c5fc36673afd8eba5c7
parent97906ee7403e7b50695b583585bda32241557594 (diff)
utils: Unify output str format of hex_array()
This patch simplifies hex_array() and unifies its output into '0x%02x' format. Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/tests/unit/test_utils.py28
-rw-r--r--ryu/utils.py30
2 files changed, 23 insertions, 35 deletions
diff --git a/ryu/tests/unit/test_utils.py b/ryu/tests/unit/test_utils.py
index 64886c3c..303e2918 100644
--- a/ryu/tests/unit/test_utils.py
+++ b/ryu/tests/unit/test_utils.py
@@ -31,19 +31,27 @@ class Test_utils(unittest.TestCase):
pass
def test_hex_array_string(self):
- ''' Test string conversion into array of hexes '''
- expected_result = '0x1 0x2 0x3 0x4'
- data = b'\01\02\03\04'
+ """
+ Test hex_array() with str type.
+ """
+ expected_result = '0x01 0x02 0x03 0x04'
+ data = b'\x01\x02\x03\x04'
eq_(expected_result, utils.hex_array(data))
def test_hex_array_bytearray(self):
- ''' Test bytearray conversion into array of hexes '''
- expected_result = '0x1 0x2 0x3 0x4'
- data = bytearray(b'\01\02\03\04')
+ """
+ Test hex_array() with bytearray type.
+ """
+ expected_result = '0x01 0x02 0x03 0x04'
+ data = bytearray(b'\x01\x02\x03\x04')
eq_(expected_result, utils.hex_array(data))
- def test_hex_array_invalid(self):
- ''' Test conversion into array of hexes with invalid data type '''
- expected_result = None
- data = 1234
+ def test_hex_array_bytes(self):
+ """
+ Test hex_array() with bytes type. (Python3 only)
+ """
+ if six.PY2:
+ return
+ expected_result = '0x01 0x02 0x03 0x04'
+ data = bytes(b'\x01\x02\x03\x04')
eq_(expected_result, utils.hex_array(data))
diff --git a/ryu/utils.py b/ryu/utils.py
index 4a5bc445..fd8225ea 100644
--- a/ryu/utils.py
+++ b/ryu/utils.py
@@ -33,7 +33,6 @@
import inspect
import logging
import os
-import six
import sys
import re
@@ -99,31 +98,12 @@ def round_up(x, y):
return ((x + y - 1) // y) * y
-def _str_to_hex(data):
- """Convert str into array of hexes to be printed. (Python2 only)"""
- return ' '.join(hex(ord(char)) for char in data)
-
-
-def _bytearray_to_hex(data):
- """Convert bytearray into array of hexes to be printed.
- In Python3, this function works for binary_types, too.
- """
- return ' '.join(hex(byte) for byte in data)
-
-
def hex_array(data):
- """Convert binary_type or bytearray into array of hexes to be printed."""
- if six.PY3:
- to_hex = {six.binary_type: _bytearray_to_hex,
- bytearray: _bytearray_to_hex}
- else:
- to_hex = {six.binary_type: _str_to_hex,
- bytearray: _bytearray_to_hex}
- try:
- return to_hex[type(data)](data)
- except KeyError:
- LOG.exception('%s is invalid data type', type(data))
- return None
+ """
+ Convert six.binary_type or bytearray into array of hexes to be printed.
+ """
+ # convert data into bytearray explicitly
+ return ' '.join('0x%02x' % byte for byte in bytearray(data))
# the following functions are taken from OpenStack