Skip to content

Commit

Permalink
Use six to fix imports on Python 3
Browse files Browse the repository at this point in the history
Replace Python 2 imports with six.moves import to make code compatible
with Python 2 and Python 3.

Replaced imports:

* BaseHTTPServer
* __builtin__
* cookielib
* httplib

This patch was generated by the six_moves operation of the sixer tool
version 0.4:
https://pypi.python.org/pypi/sixer

Manual changes:

* Disable pylint warnings on the HTTPSConnection classes because pylint
  doesn't support importing from six.moves yet, see:
  https://bitbucket.org/logilab/pylint/issue/550/

Blueprint cinder-python3
Change-Id: Ide6d4e3480f2c0a7eb4500aa88affe152ecc0401
  • Loading branch information
vstinner committed Jun 11, 2015
1 parent 6feb65d commit f484310
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 187 deletions.
4 changes: 2 additions & 2 deletions cinder/api/v1/limits.py
Expand Up @@ -19,13 +19,13 @@

import collections
import copy
import httplib
import math
import re
import time

from oslo_serialization import jsonutils
from oslo_utils import importutils
from six.moves import http_client
import webob.dec
import webob.exc

Expand Down Expand Up @@ -439,7 +439,7 @@ def check_for_delay(self, verb, path, username=None):
body = jsonutils.dumps({"verb": verb, "path": path})
headers = {"Content-Type": "application/json"}

conn = httplib.HTTPConnection(self.limiter_address)
conn = http_client.HTTPConnection(self.limiter_address)

if username:
conn.request("POST", "/%s" % (username), body, headers)
Expand Down
4 changes: 2 additions & 2 deletions cinder/api/v2/limits.py
Expand Up @@ -19,13 +19,13 @@

import collections
import copy
import httplib
import math
import re
import time

from oslo_serialization import jsonutils
from oslo_utils import importutils
from six.moves import http_client
import webob.dec
import webob.exc

Expand Down Expand Up @@ -437,7 +437,7 @@ def check_for_delay(self, verb, path, username=None):
body = jsonutils.dumps({"verb": verb, "path": path})
headers = {"Content-Type": "application/json"}

conn = httplib.HTTPConnection(self.limiter_address)
conn = http_client.HTTPConnection(self.limiter_address)

if username:
conn.request("POST", "/%s" % (username), body, headers)
Expand Down
4 changes: 2 additions & 2 deletions cinder/tests/unit/__init__.py
Expand Up @@ -29,10 +29,10 @@
"""

import eventlet
from six.moves import builtins

eventlet.monkey_patch()

# See http://code.google.com/p/python-nose/issues/detail?id=373
# The code below enables nosetests to work with i18n _() blocks
import __builtin__
setattr(__builtin__, '_', lambda x: x)
setattr(builtins, '_', lambda x: x)
21 changes: 11 additions & 10 deletions cinder/tests/unit/api/v1/test_limits.py
Expand Up @@ -17,12 +17,12 @@
Tests dealing with HTTP rate-limiting.
"""

import httplib
from xml.dom import minidom

from lxml import etree
from oslo_serialization import jsonutils
import six
from six.moves import http_client
import webob

from cinder.api.v1 import limits
Expand Down Expand Up @@ -631,7 +631,7 @@ def test_response_to_delays_usernames(self):


class FakeHttplibSocket(object):
"""Fake `httplib.HTTPResponse` replacement."""
"""Fake `http_client.HTTPResponse` replacement."""

def __init__(self, response_string):
"""Initialize new `FakeHttplibSocket`."""
Expand All @@ -643,7 +643,7 @@ def makefile(self, _mode, _other):


class FakeHttplibConnection(object):
"""Fake `httplib.HTTPConnection`."""
"""Fake `http_client.HTTPConnection`."""

def __init__(self, app, host):
"""Initialize `FakeHttplibConnection`."""
Expand All @@ -655,7 +655,7 @@ def request(self, method, path, body="", headers=None):
Requests made via this connection actually get translated and
routed into our WSGI app, we then wait for the response and turn
it back into an `httplib.HTTPResponse`.
it back into an `http_client.HTTPResponse`.
"""
if not headers:
headers = {}
Expand All @@ -669,7 +669,7 @@ def request(self, method, path, body="", headers=None):
resp = str(req.get_response(self.app))
resp = "HTTP/1.0 %s" % resp
sock = FakeHttplibSocket(resp)
self.http_response = httplib.HTTPResponse(sock)
self.http_response = http_client.HTTPResponse(sock)
self.http_response.begin()

def getresponse(self):
Expand All @@ -683,7 +683,7 @@ def wire_HTTPConnection_to_WSGI(host, app):
After calling this method, when any code calls
httplib.HTTPConnection(host)
http_client.HTTPConnection(host)
the connection object will be a fake. Its requests will be sent directly
to the given WSGI app rather than through a socket.
Expand All @@ -710,8 +710,9 @@ def __call__(self, connection_host, *args, **kwargs):
else:
return self.wrapped(connection_host, *args, **kwargs)

oldHTTPConnection = httplib.HTTPConnection
httplib.HTTPConnection = HTTPConnectionDecorator(httplib.HTTPConnection)
oldHTTPConnection = http_client.HTTPConnection
new_http_connection = HTTPConnectionDecorator(http_client.HTTPConnection)
http_client.HTTPConnection = new_http_connection
return oldHTTPConnection


Expand All @@ -722,7 +723,7 @@ def setUp(self):
"""setUp for test suite.
Do some nifty HTTP/WSGI magic which allows for WSGI to be called
directly by something like the `httplib` library.
directly by something like the `http_client` library.
"""
super(WsgiLimiterProxyTest, self).setUp()
self.app = limits.WsgiLimiter(TEST_LIMITS)
Expand All @@ -733,7 +734,7 @@ def setUp(self):

def _restore(self, oldHTTPConnection):
# restore original HTTPConnection object
httplib.HTTPConnection = oldHTTPConnection
http_client.HTTPConnection = oldHTTPConnection

def test_200(self):
"""Successful request test."""
Expand Down
21 changes: 11 additions & 10 deletions cinder/tests/unit/api/v2/test_limits.py
Expand Up @@ -17,12 +17,12 @@
Tests dealing with HTTP rate-limiting.
"""

import httplib
from xml.dom import minidom

from lxml import etree
from oslo_serialization import jsonutils
import six
from six.moves import http_client
import webob

from cinder.api.v2 import limits
Expand Down Expand Up @@ -634,7 +634,7 @@ def test_response_to_delays_usernames(self):

class FakeHttplibSocket(object):

"""Fake `httplib.HTTPResponse` replacement."""
"""Fake `http_client.HTTPResponse` replacement."""

def __init__(self, response_string):
"""Initialize new `FakeHttplibSocket`."""
Expand All @@ -647,7 +647,7 @@ def makefile(self, _mode, _other):

class FakeHttplibConnection(object):

"""Fake `httplib.HTTPConnection`."""
"""Fake `http_client.HTTPConnection`."""

def __init__(self, app, host):
"""Initialize `FakeHttplibConnection`."""
Expand All @@ -659,7 +659,7 @@ def request(self, method, path, body="", headers=None):
Requests made via this connection actually get translated and
routed into our WSGI app, we then wait for the response and turn
it back into an `httplib.HTTPResponse`.
it back into an `http_client.HTTPResponse`.
"""
if not headers:
headers = {}
Expand All @@ -673,7 +673,7 @@ def request(self, method, path, body="", headers=None):
resp = str(req.get_response(self.app))
resp = "HTTP/1.0 %s" % resp
sock = FakeHttplibSocket(resp)
self.http_response = httplib.HTTPResponse(sock)
self.http_response = http_client.HTTPResponse(sock)
self.http_response.begin()

def getresponse(self):
Expand All @@ -687,7 +687,7 @@ def wire_HTTPConnection_to_WSGI(host, app):
After calling this method, when any code calls
httplib.HTTPConnection(host)
http_client.HTTPConnection(host)
the connection object will be a fake. Its requests will be sent directly
to the given WSGI app rather than through a socket.
Expand All @@ -714,8 +714,9 @@ def __call__(self, connection_host, *args, **kwargs):
else:
return self.wrapped(connection_host, *args, **kwargs)

oldHTTPConnection = httplib.HTTPConnection
httplib.HTTPConnection = HTTPConnectionDecorator(httplib.HTTPConnection)
oldHTTPConnection = http_client.HTTPConnection
new_http_connection = HTTPConnectionDecorator(http_client.HTTPConnection)
http_client.HTTPConnection = new_http_connection
return oldHTTPConnection


Expand All @@ -727,7 +728,7 @@ def setUp(self):
"""setUp() for WsgiLimiterProxyTest.
Do some nifty HTTP/WSGI magic which allows for WSGI to be called
directly by something like the `httplib` library.
directly by something like the `http_client` library.
"""
super(WsgiLimiterProxyTest, self).setUp()
self.app = limits.WsgiLimiter(TEST_LIMITS)
Expand All @@ -738,7 +739,7 @@ def setUp(self):

def _restore(self, oldHTTPConnection):
# restore original HTTPConnection object
httplib.HTTPConnection = oldHTTPConnection
http_client.HTTPConnection = oldHTTPConnection

def test_200(self):
"""Successful request test."""
Expand Down
10 changes: 5 additions & 5 deletions cinder/tests/unit/backup/drivers/test_backup_nfs.py
Expand Up @@ -16,7 +16,6 @@
Tests for Backup NFS driver.
"""
import __builtin__
import bz2
import exceptions
import filecmp
Expand All @@ -30,6 +29,7 @@
from os_brick.remotefs import remotefs as remotefs_brick
from oslo_config import cfg
from oslo_log import log as logging
from six.moves import builtins

from cinder.backup.drivers import nfs
from cinder import context
Expand Down Expand Up @@ -214,20 +214,20 @@ def test_get_container_entries_no_match(self):
self.assertEqual([], result)

def test_get_object_writer(self):
self.mock_object(__builtin__, 'open', mock.mock_open())
self.mock_object(builtins, 'open', mock.mock_open())
self.mock_object(os, 'chmod')

self.driver.get_object_writer(FAKE_CONTAINER, FAKE_OBJECT_NAME)

os.chmod.assert_called_once_with(FAKE_OBJECT_PATH, 0o660)
__builtin__.open.assert_called_once_with(FAKE_OBJECT_PATH, 'w')
builtins.open.assert_called_once_with(FAKE_OBJECT_PATH, 'w')

def test_get_object_reader(self):
self.mock_object(__builtin__, 'open', mock.mock_open())
self.mock_object(builtins, 'open', mock.mock_open())

self.driver.get_object_reader(FAKE_CONTAINER, FAKE_OBJECT_NAME)

__builtin__.open.assert_called_once_with(FAKE_OBJECT_PATH, 'r')
builtins.open.assert_called_once_with(FAKE_OBJECT_PATH, 'r')

def test_delete_object(self):
self.mock_object(os, 'remove')
Expand Down
6 changes: 3 additions & 3 deletions cinder/tests/unit/backup/fake_swift_client.py
Expand Up @@ -13,13 +13,13 @@
# License for the specific language governing permissions and limitations
# under the License.

import httplib
import json
import os
import socket
import zlib

from oslo_log import log as logging
from six.moves import http_client
from swiftclient import client as swift

LOG = logging.getLogger(__name__)
Expand All @@ -45,10 +45,10 @@ def head_container(self, container):
LOG.debug("fake head_container(%s)" % container)
if container == 'missing_container':
raise swift.ClientException('fake exception',
http_status=httplib.NOT_FOUND)
http_status=http_client.NOT_FOUND)
elif container == 'unauthorized_container':
raise swift.ClientException('fake exception',
http_status=httplib.UNAUTHORIZED)
http_status=http_client.UNAUTHORIZED)
elif container == 'socket_error_on_head':
raise socket.error(111, 'ECONNREFUSED')
pass
Expand Down
6 changes: 3 additions & 3 deletions cinder/tests/unit/backup/fake_swift_client2.py
Expand Up @@ -15,12 +15,12 @@
# under the License.

import hashlib
import httplib
import os
import socket
import tempfile

from oslo_log import log as logging
from six.moves import http_client
from swiftclient import client as swift

from cinder.openstack.common import fileutils
Expand Down Expand Up @@ -48,10 +48,10 @@ def head_container(self, container):
LOG.debug("fake head_container(%s)", container)
if container == 'missing_container':
raise swift.ClientException('fake exception',
http_status=httplib.NOT_FOUND)
http_status=http_client.NOT_FOUND)
elif container == 'unauthorized_container':
raise swift.ClientException('fake exception',
http_status=httplib.UNAUTHORIZED)
http_status=http_client.UNAUTHORIZED)
elif container == 'socket_error_on_head':
raise socket.error(111, 'ECONNREFUSED')

Expand Down
11 changes: 5 additions & 6 deletions cinder/tests/unit/test_api.py
Expand Up @@ -17,14 +17,13 @@

"""Unit tests for the API endpoint."""

import httplib

import six
from six.moves import http_client
import webob


class FakeHttplibSocket(object):
"""A fake socket implementation for httplib.HTTPResponse, trivial."""
"""A fake socket implementation for http_client.HTTPResponse, trivial."""
def __init__(self, response_string):
self.response_string = response_string
self._buffer = six.StringIO(response_string)
Expand All @@ -35,11 +34,11 @@ def makefile(self, _mode, _other):


class FakeHttplibConnection(object):
"""A fake httplib.HTTPConnection for boto.
"""A fake http_client.HTTPConnection for boto.
requests made via this connection actually get translated and routed into
our WSGI app, we then wait for the response and turn it back into
the httplib.HTTPResponse that boto expects.
the http_client.HTTPResponse that boto expects.
"""
def __init__(self, app, host, is_secure=False):
self.app = app
Expand All @@ -58,7 +57,7 @@ def request(self, method, path, data, headers):
# guess that's a function the web server usually provides.
resp = "HTTP/1.0 %s" % resp
self.sock = FakeHttplibSocket(resp)
self.http_response = httplib.HTTPResponse(self.sock)
self.http_response = http_client.HTTPResponse(self.sock)
# NOTE(vish): boto is accessing private variables for some reason
self._HTTPConnection__response = self.http_response
self.http_response.begin()
Expand Down

0 comments on commit f484310

Please sign in to comment.