]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Python 3: pass bytes to base64.encode{string,bytes}
authorCyril Roelandt <cyril@redhat.com>
Tue, 28 Jul 2015 14:43:00 +0000 (16:43 +0200)
committerCyril Roelandt <cyril@redhat.com>
Fri, 31 Jul 2015 15:50:46 +0000 (17:50 +0200)
In Python 3, base64.encodestring expects bytes. Also, base64.encodebytes should
be used in order not to trigger a DeprecationWarning.

Change-Id: Iaae377b612feda7e01963f0d527c898bc0fd21d1
Blueprint: neutron-python3

neutron/plugins/cisco/n1kv/n1kv_client.py

index f7a54f50fc8ebe00c10abb3175a1b7d2638fa619..edbf5e7db55b9c880907b5e6dc3d41c7144c4387 100644 (file)
@@ -19,6 +19,7 @@ import netaddr
 from oslo_log import log as logging
 from oslo_serialization import jsonutils
 import requests
+import six
 
 from neutron.common import exceptions as n_exc
 from neutron.extensions import providernet
@@ -32,6 +33,22 @@ from neutron.plugins.cisco.extensions import n1kv
 LOG = logging.getLogger(__name__)
 
 
+def safe_b64_encode(s):
+    if six.PY3:
+        method = base64.encodebytes
+    else:
+        method = base64.encodestring
+
+    if isinstance(s, six.text_type):
+        s = s.encode('utf-8')
+    encoded_string = method(s).rstrip()
+
+    if six.PY3:
+        return encoded_string.decode('utf-8')
+    else:
+        return encoded_string
+
+
 class Client(object):
 
     """
@@ -502,7 +519,7 @@ class Client(object):
         """
         username = c_cred.Store.get_username(host_ip)
         password = c_cred.Store.get_password(host_ip)
-        auth = base64.encodestring("%s:%s" % (username, password)).rstrip()
+        auth = safe_b64_encode("%s:%s" % (username, password))
         header = {"Authorization": "Basic %s" % auth}
         return header