Skip to content

Commit

Permalink
Be compatible with Python 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
cc committed Feb 7, 2019
1 parent 7134c7d commit 00137ad
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See `example.py`.

## Dependencies

* Python 2.6 or later
* Python 2.7 or later
* requests
* pytest

Expand Down
4 changes: 2 additions & 2 deletions alluxio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

from .client import Client
import option
import wire
from . import option
from . import wire

__version__ = '0.1.1'
2 changes: 1 addition & 1 deletion alluxio/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ def raise_with_traceback(error_type, message):
"""Raise a new error with the most recent traceback."""

traceback = sys.exc_info()[2]
reraise(error_type, message, traceback)
reraise(error_type, error_type(message), traceback)
42 changes: 29 additions & 13 deletions alluxio/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import json
import random
import urllib
from urlparse import urlparse
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse

import alluxio

Expand All @@ -16,6 +19,13 @@
from util import random_str, random_int


def get_http_header(request, key):
try:
return request.headers.getheader(key, 0)
except AttributeError:
return request.headers.get(key, 0)


def get_free_port():
s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
s.bind(('localhost', 0))
Expand All @@ -27,7 +37,7 @@ def get_free_port():
def setup_client(handler):
host = 'localhost'
port = get_free_port()
print port
print(port)
server = HTTPServer((host, port), handler)
server_thread = Thread(target=server.serve_forever)
server_thread.setDaemon(True)
Expand All @@ -44,13 +54,16 @@ def handle_paths_request(request, path, action, params=None, input=None, output=
for i, (k, v) in enumerate(params.items()):
if i != 0:
expected_path += '&'
expected_path += '{}={}'.format(k,
urllib.quote(v, safe=''))
try:
quoted_v = urllib.quote(v, safe='')
except AttributeError:
quoted_v = urllib.parse.quote(v, safe='')
expected_path += '{}={}'.format(k, quoted_v)
assert request.path == expected_path

if input is not None:
# Assert that request body is expected.
content_len = int(request.headers.getheader('content-length', 0))
content_len = int(get_http_header(request, 'content-length'))
body = request.rfile.read(content_len)
assert json.loads(body) == input.json()

Expand All @@ -59,7 +72,9 @@ def handle_paths_request(request, path, action, params=None, input=None, output=
if output is not None:
request.send_header('Content-Type', 'application/json')
request.end_headers()
request.wfile.write(json.dumps(output))
request.wfile.write(json.dumps(output).encode())
else:
request.end_headers()


def paths_handler(path, action, params=None, input=None, output=None):
Expand Down Expand Up @@ -200,20 +215,20 @@ def handle_streams_request(request, file_id, action, input=None, output=None):
content_len = 0
if input is not None:
# Assert that request body is expected.
content_len = int(request.headers.getheader('content-length', 0))
body = request.rfile.read(content_len)
content_len = int(get_http_header(request, 'content-length'))
body = request.rfile.read(content_len).decode()
assert body == input

# Respond.
request.send_response(200)
if output is not None:
request.send_header('Content-Type', 'application/octet-stream')
request.end_headers()
request.wfile.write(output)
request.wfile.write(output.encode())
else:
request.send_header('Content-Type', 'application/json')
request.end_headers()
request.wfile.write(json.dumps(content_len))
request.wfile.write(json.dumps(content_len).encode())


def streams_handler(file_id, action, input=None, output=None):
Expand All @@ -240,7 +255,7 @@ def test_read():
reader = client.read(file_id)
got = reader.read()
reader.close()
assert got == message
assert got.decode() == message


def test_write():
Expand Down Expand Up @@ -270,6 +285,7 @@ def do_POST(self):
self, file_id, stream_action, input=stream_input, output=stream_output)
elif request_path == close_path:
self.send_response(200)
self.end_headers()

return _

Expand All @@ -285,15 +301,15 @@ def test_open_read():
with client.open(path, 'r') as f:
got = f.read()
cleanup()
assert got == message
assert got == message.encode()


def test_open_write():
path = '/foo'
file_id = random_int()
message = random_str()
handler = combined_handler(path, 'create-file', file_id, 'write',
path_output=file_id, stream_input=message, stream_output=len(message))
path_output=file_id, stream_input=message)
client, cleanup = setup_client(handler)
written_len = None
with client.open(path, 'w') as f:
Expand Down
2 changes: 1 addition & 1 deletion alluxio/tests/option_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def test_create_directory():
opt = random_create_directory()
print opt.json()
print(opt.json())
json = dict(allowExists=opt.allow_exists,
mode=opt.mode.json(),
recursive=opt.recursive,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests==2.20.0
requests==2.21.0
six==1.10.0
pytest==3.1.2
pytest-cov==2.5.1

0 comments on commit 00137ad

Please sign in to comment.