From cfdba5c12dde50fa691faec8331fa68da7a09599 Mon Sep 17 00:00:00 2001 From: Satoshi Kobayashi Date: Fri, 10 Apr 2015 15:43:43 +0900 Subject: [PATCH] Fix ImportError(s) with Python3 Signed-off-by: FUJITA Tomonori --- ryu/lib/stringify.py | 8 +++++++- ryu/ofproto/nicira_ext.py | 2 +- ryu/ofproto/ofproto_protocol.py | 20 +++++++++---------- ryu/ofproto/ofproto_v1_0.py | 2 +- ryu/ofproto/ofproto_v1_0_parser.py | 2 +- ryu/ofproto/ofproto_v1_2_parser.py | 2 +- ryu/ofproto/ofproto_v1_3_parser.py | 8 ++++---- ryu/ofproto/ofproto_v1_4_parser.py | 2 +- ryu/ofproto/ofproto_v1_5_parser.py | 2 +- ryu/ofproto/oxm_fields.py | 5 ++--- ryu/tests/unit/lib/test_ofctl.py | 7 ++----- ryu/tests/unit/ofproto/test_parser.py | 4 +--- ryu/tests/unit/ofproto/test_parser_compat.py | 4 +--- .../unit/ofproto/test_parser_ofpmatch.py | 4 +--- 14 files changed, 34 insertions(+), 38 deletions(-) diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py index 451c4b7b..d279fad7 100644 --- a/ryu/lib/stringify.py +++ b/ryu/lib/stringify.py @@ -37,7 +37,13 @@ import inspect # grep __init__ *.py | grep '[^_]_\>' showed that # 'len', 'property', 'set', 'type' # A bit more generic way is adopted -import __builtin__ +try: + # Python 2 + import __builtin__ +except ImportError: + # Python 3 + import builtins as __builtin__ + _RESERVED_KEYWORD = dir(__builtin__) diff --git a/ryu/ofproto/nicira_ext.py b/ryu/ofproto/nicira_ext.py index 03a2d951..5c261949 100644 --- a/ryu/ofproto/nicira_ext.py +++ b/ryu/ofproto/nicira_ext.py @@ -19,7 +19,7 @@ from struct import calcsize -from ofproto_common import OFP_HEADER_SIZE +from ryu.ofproto.ofproto_common import OFP_HEADER_SIZE # Action subtypes NXAST_RESUBMIT = 1 diff --git a/ryu/ofproto/ofproto_protocol.py b/ryu/ofproto/ofproto_protocol.py index 9e54597a..280e7a1b 100644 --- a/ryu/ofproto/ofproto_protocol.py +++ b/ryu/ofproto/ofproto_protocol.py @@ -14,16 +14,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import ofproto_v1_0 -from . import ofproto_v1_0_parser -from . import ofproto_v1_2 -from . import ofproto_v1_2_parser -from . import ofproto_v1_3 -from . import ofproto_v1_3_parser -from . import ofproto_v1_4 -from . import ofproto_v1_4_parser -from . import ofproto_v1_5 -from . import ofproto_v1_5_parser +from ryu.ofproto import ofproto_v1_0 +from ryu.ofproto import ofproto_v1_0_parser +from ryu.ofproto import ofproto_v1_2 +from ryu.ofproto import ofproto_v1_2_parser +from ryu.ofproto import ofproto_v1_3 +from ryu.ofproto import ofproto_v1_3_parser +from ryu.ofproto import ofproto_v1_4 +from ryu.ofproto import ofproto_v1_4_parser +from ryu.ofproto import ofproto_v1_5 +from ryu.ofproto import ofproto_v1_5_parser _versions = { diff --git a/ryu/ofproto/ofproto_v1_0.py b/ryu/ofproto/ofproto_v1_0.py index bab5cc87..8da99706 100644 --- a/ryu/ofproto/ofproto_v1_0.py +++ b/ryu/ofproto/ofproto_v1_0.py @@ -592,4 +592,4 @@ def nxm_nx_reg_w(idx): NXM_HEADER_PACK_STRING = '!I' -from nicira_ext import * # For API compat +from ryu.ofproto.nicira_ext import * # For API compat diff --git a/ryu/ofproto/ofproto_v1_0_parser.py b/ryu/ofproto/ofproto_v1_0_parser.py index 477082a3..2cabc08d 100644 --- a/ryu/ofproto/ofproto_v1_0_parser.py +++ b/ryu/ofproto/ofproto_v1_0_parser.py @@ -21,7 +21,7 @@ Decoder/Encoder implementations of OpenFlow 1.0. import struct import binascii -from ofproto_parser import StringifyMixin, MsgBase, msg_str_attr +from ryu.ofproto.ofproto_parser import StringifyMixin, MsgBase, msg_str_attr from ryu.lib import addrconv from ryu.lib import mac from ryu.lib.pack_utils import msg_pack_into diff --git a/ryu/ofproto/ofproto_v1_2_parser.py b/ryu/ofproto/ofproto_v1_2_parser.py index 7d00dc42..333dfa6a 100644 --- a/ryu/ofproto/ofproto_v1_2_parser.py +++ b/ryu/ofproto/ofproto_v1_2_parser.py @@ -25,7 +25,7 @@ from ryu.lib import addrconv from ryu.lib import mac from ryu.lib.pack_utils import msg_pack_into from ryu import utils -from ofproto_parser import StringifyMixin, MsgBase, msg_str_attr +from ryu.ofproto.ofproto_parser import StringifyMixin, MsgBase, msg_str_attr from . import ether from . import ofproto_parser from . import ofproto_v1_2 as ofproto diff --git a/ryu/ofproto/ofproto_v1_3_parser.py b/ryu/ofproto/ofproto_v1_3_parser.py index ea69c8cd..827c48a9 100644 --- a/ryu/ofproto/ofproto_v1_3_parser.py +++ b/ryu/ofproto/ofproto_v1_3_parser.py @@ -47,7 +47,7 @@ from ryu.lib import addrconv from ryu.lib import mac from ryu.lib.pack_utils import msg_pack_into from ryu import utils -from ofproto_parser import StringifyMixin, MsgBase, msg_str_attr +from ryu.ofproto.ofproto_parser import StringifyMixin, MsgBase, msg_str_attr from . import ether from . import nicira_ext from . import ofproto_parser @@ -829,9 +829,9 @@ class OFPMatch(StringifyMixin): # OFPMatch(eth_src=('ff:ff:ff:00:00:00'), eth_type=0x800, # ipv4_src='10.0.0.1') kwargs = dict(ofproto.oxm_normalize_user(k, v) for - (k, v) in kwargs.iteritems()) + (k, v) in kwargs.items()) fields = [ofproto.oxm_from_user(k, v) for (k, v) - in kwargs.iteritems()] + in kwargs.items()] # assumption: sorting by OXM type values makes fields # meet ordering requirements (eg. eth_type before ipv4_src) fields.sort() @@ -6039,7 +6039,7 @@ class OFPSetAsync(MsgBase): self.flow_removed_mask[0], self.flow_removed_mask[1]) -import nx_actions +from ryu.ofproto import nx_actions nx_actions.generate( 'ryu.ofproto.ofproto_v1_3', diff --git a/ryu/ofproto/ofproto_v1_4_parser.py b/ryu/ofproto/ofproto_v1_4_parser.py index c4c9054c..95339d8a 100644 --- a/ryu/ofproto/ofproto_v1_4_parser.py +++ b/ryu/ofproto/ofproto_v1_4_parser.py @@ -25,7 +25,7 @@ from ryu.lib import addrconv from ryu.lib import mac from ryu.lib.pack_utils import msg_pack_into from ryu import utils -from ofproto_parser import StringifyMixin, MsgBase, MsgInMsgBase, msg_str_attr +from ryu.ofproto.ofproto_parser import StringifyMixin, MsgBase, MsgInMsgBase, msg_str_attr from . import ether from . import ofproto_parser from . import ofproto_common diff --git a/ryu/ofproto/ofproto_v1_5_parser.py b/ryu/ofproto/ofproto_v1_5_parser.py index b351ebe4..d3a89707 100644 --- a/ryu/ofproto/ofproto_v1_5_parser.py +++ b/ryu/ofproto/ofproto_v1_5_parser.py @@ -25,7 +25,7 @@ from ryu.lib import addrconv from ryu.lib import mac from ryu.lib.pack_utils import msg_pack_into from ryu import utils -from ofproto_parser import StringifyMixin, MsgBase, MsgInMsgBase, msg_str_attr +from ryu.ofproto.ofproto_parser import StringifyMixin, MsgBase, MsgInMsgBase, msg_str_attr from . import ether from . import ofproto_parser from . import ofproto_common diff --git a/ryu/ofproto/oxm_fields.py b/ryu/ofproto/oxm_fields.py index b7936b7d..c538f492 100644 --- a/ryu/ofproto/oxm_fields.py +++ b/ryu/ofproto/oxm_fields.py @@ -62,7 +62,7 @@ import itertools import struct -import ofproto_common +from ryu.ofproto import ofproto_common from ryu.lib.pack_utils import msg_pack_into from ryu.lib import type_desc @@ -138,7 +138,6 @@ class NiciraExtended1(_OxmClass): def generate(modname): import sys - import string import functools mod = sys.modules[modname] @@ -147,7 +146,7 @@ def generate(modname): setattr(mod, k, v) for i in mod.oxm_types: - uk = string.upper(i.name) + uk = i.name.upper() if isinstance(i.num, tuple): continue oxm_class = i.num >> 7 diff --git a/ryu/tests/unit/lib/test_ofctl.py b/ryu/tests/unit/lib/test_ofctl.py index bfc8b4c7..877694e5 100644 --- a/ryu/tests/unit/lib/test_ofctl.py +++ b/ryu/tests/unit/lib/test_ofctl.py @@ -17,7 +17,6 @@ import unittest import logging import netaddr import functools -import new from nose.tools import * @@ -666,8 +665,7 @@ def _add_tests_actions(cls): func = functools.partial(_run, name=method_name, act=act, cls=cls) func.func_name = method_name func.__name__ = method_name - im = new.instancemethod(func, None, Test_ofctl) - setattr(Test_ofctl, method_name, im) + setattr(Test_ofctl, method_name, func) def _add_tests_match(cls): @@ -686,8 +684,7 @@ def _add_tests_match(cls): _run, name=method_name, attr=attr, cls=cls) func.func_name = method_name func.__name__ = method_name - im = new.instancemethod(func, None, Test_ofctl) - setattr(Test_ofctl, method_name, im) + setattr(Test_ofctl, method_name, func) """ Test case """ diff --git a/ryu/tests/unit/ofproto/test_parser.py b/ryu/tests/unit/ofproto/test_parser.py index ba878240..e0da6121 100644 --- a/ryu/tests/unit/ofproto/test_parser.py +++ b/ryu/tests/unit/ofproto/test_parser.py @@ -227,7 +227,6 @@ def _add_tests(): import os import os.path import fnmatch - import new import functools this_dir = os.path.dirname(sys.modules[__name__].__file__) @@ -258,7 +257,6 @@ def _add_tests(): json_str=json_str) f.func_name = method_name f.__name__ = method_name - im = new.instancemethod(f, None, Test_Parser) - setattr(Test_Parser, method_name, im) + setattr(Test_Parser, method_name, f) _add_tests() diff --git a/ryu/tests/unit/ofproto/test_parser_compat.py b/ryu/tests/unit/ofproto/test_parser_compat.py index f88d6b09..2e2e85e5 100644 --- a/ryu/tests/unit/ofproto/test_parser_compat.py +++ b/ryu/tests/unit/ofproto/test_parser_compat.py @@ -134,7 +134,6 @@ class Test_Parser_Compat(unittest.TestCase): def _add_tests(): - import new import functools import itertools @@ -152,7 +151,6 @@ def _add_tests(): f.func_name = method_name f.__name__ = method_name cls = Test_Parser_Compat - im = new.instancemethod(f, None, cls) - setattr(cls, method_name, im) + setattr(cls, method_name, f) _add_tests() diff --git a/ryu/tests/unit/ofproto/test_parser_ofpmatch.py b/ryu/tests/unit/ofproto/test_parser_ofpmatch.py index 4b2d0338..d801f8ab 100644 --- a/ryu/tests/unit/ofproto/test_parser_ofpmatch.py +++ b/ryu/tests/unit/ofproto/test_parser_ofpmatch.py @@ -62,7 +62,6 @@ class Test_Parser_OFPMatch(unittest.TestCase): def _add_tests(): - import new import functools import itertools @@ -235,7 +234,6 @@ def _add_tests(): f.func_name = method_name f.__name__ = method_name cls = Test_Parser_OFPMatch - im = new.instancemethod(f, None, cls) - setattr(cls, method_name, im) + setattr(cls, method_name, f) _add_tests()