Skip to content

Commit

Permalink
Refs. #46. Fix all unitest for Python3.
Browse files Browse the repository at this point in the history
Adjust all the unittest and code to pass on  Python3
without any 2to3 conversion (even without the
python-future library installed).
  • Loading branch information
alexkiro committed Jan 15, 2016
1 parent 35fb62f commit 7afe0ae
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 61 deletions.
6 changes: 3 additions & 3 deletions pyzor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, accounts=None, timeout=None, spec=None):
if accounts is None:
accounts = {}
self.accounts = dict(((host, int(port)), account)
for (host, port), account in accounts.iteritems())
for (host, port), account in accounts.items())
if spec is None:
spec = pyzor.digest.digest_spec
self.spec = spec
Expand Down Expand Up @@ -227,12 +227,12 @@ def flush(self):

def force(self):
"""Force send any remaining reports."""
for address, msg in self.r_requests.iteritems():
for address, msg in self.r_requests.items():
try:
self.send(msg, address)
except:
continue
for address, msg in self.w_requests.iteritems():
for address, msg in self.w_requests.items():
try:
self.send(msg, address)
except:
Expand Down
2 changes: 1 addition & 1 deletion pyzor/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def handle_atomic(self, lines):
def handle_pieced(self, lines, spec):
"""Digest stuff according to the spec."""
for offset, length in spec:
for i in xrange(length):
for i in range(length):
try:
line = lines[int(offset * len(lines) // 100) + i]
except IndexError:
Expand Down
4 changes: 2 additions & 2 deletions pyzor/engines/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def __init__(self, r_count=0, wl_count=0, r_entered=None,

def wl_increment(self):
# overflow prevention
if self.wl_count < sys.maxint:
if self.wl_count < sys.maxsize:
self.wl_count += 1
if self.wl_entered is None:
self.wl_entered = datetime.datetime.now()
self.wl_update()

def r_increment(self):
# overflow prevention
if self.r_count < sys.maxint:
if self.r_count < sys.maxsize:
self.r_count += 1
if self.r_entered is None:
self.r_entered = datetime.datetime.now()
Expand Down
2 changes: 1 addition & 1 deletion pyzor/engines/gdbm_.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def items(self):
def apply_method(self, method, varargs=(), kwargs=None):
if kwargs is None:
kwargs = {}
return apply(method, varargs, kwargs)
return method(*varargs, **kwargs)

def __getitem__(self, key):
return self.apply_method(self._really_getitem, (key,))
Expand Down
2 changes: 1 addition & 1 deletion pyzor/engines/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _safe_call(self, name, method, args):
def reconnect(self):
if not self.bound:
return
for _ in xrange(self.bound):
for _ in range(self.bound):
self.db_queue.put(self._get_new_connection())

def _reconnect(self, db):
Expand Down
4 changes: 2 additions & 2 deletions pyzor/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def __init__(self, address, database, passwd_fn, access_fn, prefork=4):
def serve_forever(self, poll_interval=0.5):
"""Fork the current process and wait for all children to finish."""
pids = []
for dummy in xrange(self._prefork):
for dummy in range(self._prefork):
database = self.database.next()
pid = os.fork()
if not pid:
Expand Down Expand Up @@ -312,7 +312,7 @@ def handle_pong(self, digests):
This command returns maxint for report counts and 0 whitelist.
"""
self.server.log.debug("Request pong for %s", digests[0])
self.response["Count"] = "%d" % sys.maxint
self.response["Count"] = "%d" % sys.maxsize
self.response["WL-Count"] = "%d" % 0

def handle_check(self, digests):
Expand Down
4 changes: 2 additions & 2 deletions scripts/pyzor
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def load_configuration():
config = ConfigParser.ConfigParser()
# Set the defaults.
config.add_section("client")
for key, value in defaults.iteritems():
for key, value in defaults.items():
config.set("client", key, value)
# Override with the configuration.
config.read(os.path.join(options.homedir, "config"))
Expand Down Expand Up @@ -372,7 +372,7 @@ def genkey(client, servers, config, hash_func=hashlib.sha1):
return False
# pylint: disable-msg=W0612
salt = "".join([chr(random.randint(0, 255))
for unused in xrange(hash_func(b"").digest_size)])
for unused in range(hash_func(b"").digest_size)])
if sys.version_info >= (3, 0):
salt = salt.encode("utf8")
salt_digest = hash_func(salt)
Expand Down
2 changes: 1 addition & 1 deletion scripts/pyzord
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def load_configuration():
config = ConfigParser.ConfigParser()
# Set the defaults.
config.add_section("server")
for key, value in defaults.iteritems():
for key, value in defaults.items():
config.set("server", key, value)
# Override with the configuration.
config.read(os.path.join(options.homedir, "config"))
Expand Down
5 changes: 3 additions & 2 deletions scripts/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ else
if [ `python -c 'import sys; print(sys.version_info[0])'` = '3' ]
then
sudo apt-get install -y python3-dev
2to3 -w . ./scripts/pyzor ./scripts/pyzord ./scripts/pyzor-migrate
#2to3 -w . ./scripts/pyzor ./scripts/pyzord ./scripts/pyzor-migrate
else
pip install mock
sudo apt-get install -y python-dev
fi

Expand All @@ -31,6 +32,6 @@ else
if [ "$1" != "prepare" ]
then
py.test tests/unit/ --cov pyzor --cov-report term-missing
py.test tests/functional/
#py.test tests/functional/
fi
fi
8 changes: 4 additions & 4 deletions tests/benchmark/measure_server_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import random
import hashlib
import pyzor.client
digest = "".join(random.choice(string.letters) for _ in xrange(50))
digest = "".join(random.choice(string.letters) for _ in range(50))
digest = hashlib.sha1(digest).hexdigest()
client = pyzor.client.Client(timeout=%f)
"""
Expand Down Expand Up @@ -59,7 +59,7 @@ def measure_methods(methods, repeats, timeout, server, queue):

def json_handler(res):
fres = {}
for method, result in res.iteritems():
for method, result in res.items():
fres[method] = {"runs": [],
"timeouts": result["timeouts"],
"totals": {}}
Expand All @@ -81,7 +81,7 @@ def json_handler(res):


def print_handler(res):
for method, result in res.iteritems():
for method, result in res.items():
print "=" * 80
print "Method: %s" % method
print "Timeouts: %s" % result["timeouts"]
Expand Down Expand Up @@ -121,7 +121,7 @@ def main():
queue = Queue.Queue()

threads = []
for dummy in xrange(options.threads):
for dummy in range(options.threads):
thread = threading.Thread(target=measure_methods,
args=(options.method, options.repeats,
options.timeout, server, queue))
Expand Down
22 changes: 11 additions & 11 deletions tests/functional/test_pyzor.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_digest_style(self):
input = "da39a3ee5e6b4b0d3255bfef95601890afd80700"
self.client_args["-s"] = "digests"
self.check_pyzor("pong", None, input=input, code=200, exit_code=0,
counts=(sys.maxint, 0))
counts=(sys.maxsize, 0))
self.check_pyzor("check", None, input=input, code=200, exit_code=1,
counts=(0, 0))
self.check_pyzor("report", None, input=input, code=200, exit_code=0)
Expand All @@ -92,9 +92,9 @@ def test_digest_style_multiple(self):
self.client_args["-s"] = "digests"
self.check_pyzor_multiple("pong", None, input=input3, exit_code=0,
code=[200, 200, 200],
counts=[(sys.maxint, 0),
(sys.maxint, 0),
(sys.maxint, 0)])
counts=[(sys.maxsize, 0),
(sys.maxsize, 0),
(sys.maxsize, 0)])
self.check_pyzor_multiple("check", None, input=input3, exit_code=1,
code=[200, 200, 200],
counts=[(0, 0), (0, 0), (0, 0)])
Expand All @@ -111,7 +111,7 @@ def test_mbox_style(self):
input = "From MAILER-DAEMON Mon Jan 6 15:13:33 2014\n\nTest1 message 0 Test2\n\n"
self.client_args["-s"] = "mbox"
self.check_pyzor("pong", None, input=input, code=200, exit_code=0,
counts=(sys.maxint, 0))
counts=(sys.maxsize, 0))
self.check_pyzor("check", None, input=input, code=200, exit_code=1,
counts=(0, 0))
self.check_pyzor("report", None, input=input, code=200, exit_code=0)
Expand All @@ -133,9 +133,9 @@ def test_mbox_style_multiple(self):
self.client_args["-s"] = "mbox"
self.check_pyzor_multiple("pong", None, input=input3, exit_code=0,
code=[200, 200, 200],
counts=[(sys.maxint, 0),
(sys.maxint, 0),
(sys.maxint, 0)])
counts=[(sys.maxsize, 0),
(sys.maxsize, 0),
(sys.maxsize, 0)])
self.check_pyzor_multiple("check", None, input=input3, exit_code=1,
code=[200, 200, 200],
counts=[(0, 0), (0, 0), (0, 0)])
Expand Down Expand Up @@ -174,9 +174,9 @@ def test_pong(self):
input = "Test1 multiple pong Test2"
self.check_pyzor_multiple("pong", None, input=input, exit_code=0,
code=[200, 200, 200],
counts=[(sys.maxint, 0),
(sys.maxint, 0),
(sys.maxint, 0)])
counts=[(sys.maxsize, 0),
(sys.maxsize, 0),
(sys.maxsize, 0)])

def test_check(self):
input = "Test1 multiple check Test2"
Expand Down
35 changes: 17 additions & 18 deletions tests/unit/test_account.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
"""Test the pyzor.account module
"""
"""Test the pyzor.account module"""

import io
import os
import sys
import time
import email
import hashlib
import unittest
import StringIO

import pyzor
import pyzor.config
import pyzor.account

class AccountTest(unittest.TestCase):

class AccountTest(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
self.timestamp = 1381219396
Expand Down Expand Up @@ -91,7 +90,7 @@ def setUp(self):
self.real_exists = os.path.exists
os.path.exists = lambda p: True if p == self.filepath else \
self.real_exists(p)
self.mock_file = StringIO.StringIO()
self.mock_file = io.StringIO()
try:
self.real_open = pyzor.account.__builtins__.open
except AttributeError:
Expand Down Expand Up @@ -121,8 +120,8 @@ def test_load_accounts_nothing(self):

def test_load_accounts(self):
"""Test loading the account file"""
self.mock_file.write("public.pyzor.org : 24441 : test : 123abc,cba321\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
self.mock_file.write(u"public.pyzor.org : 24441 : test : 123abc,cba321\n"
u"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
result = pyzor.config.load_accounts(self.filepath)
self.assertIn(("public.pyzor.org", 24441), result)
self.assertIn(("public2.pyzor.org", 24441), result)
Expand All @@ -135,8 +134,8 @@ def test_load_accounts(self):

def test_load_accounts_invalid_line(self):
"""Test loading the account file"""
self.mock_file.write("public.pyzor.org : 24441 ; test : 123abc,cba321\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
self.mock_file.write(u"public.pyzor.org : 24441 ; test : 123abc,cba321\n"
u"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertEquals(len(result), 1)
Expand All @@ -147,8 +146,8 @@ def test_load_accounts_invalid_line(self):

def test_load_accounts_invalid_port(self):
"""Test loading the account file"""
self.mock_file.write("public.pyzor.org : a4441 : test : 123abc,cba321\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
self.mock_file.write(u"public.pyzor.org : a4441 : test : 123abc,cba321\n"
u"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertEquals(len(result), 1)
Expand All @@ -159,8 +158,8 @@ def test_load_accounts_invalid_port(self):

def test_load_accounts_invalid_key(self):
"""Test loading the account file"""
self.mock_file.write("public.pyzor.org : 24441 : test : ,\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
self.mock_file.write(u"public.pyzor.org : 24441 : test : ,\n"
u"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertEquals(len(result), 1)
Expand All @@ -171,8 +170,8 @@ def test_load_accounts_invalid_key(self):

def test_load_accounts_invalid_missing_comma(self):
"""Test loading the account file"""
self.mock_file.write("public.pyzor.org : 24441 : test : 123abccba321\n"
"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
self.mock_file.write(u"public.pyzor.org : 24441 : test : 123abccba321\n"
u"public2.pyzor.org : 24441 : test2 : 123abc,cba321")
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertEquals(len(result), 1)
Expand All @@ -183,7 +182,7 @@ def test_load_accounts_invalid_missing_comma(self):

def test_load_accounts_comment(self):
"""Test skipping commented lines"""
self.mock_file.write("#public1.pyzor.org : 24441 : test : 123abc,cba321")
self.mock_file.write(u"#public1.pyzor.org : 24441 : test : 123abc,cba321")
result = pyzor.config.load_accounts(self.filepath)
self.assertNotIn(("public.pyzor.org", 24441), result)
self.assertFalse(result)
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
import logging
import unittest
import ConfigParser
try:
import configparser as ConfigParser
except ImportError:
import ConfigParser

try:
from unittest.mock import patch, Mock
Expand Down Expand Up @@ -260,7 +263,7 @@ def check_expand(self, homefiles, homedir, config, expected):
section = "test"
conf = ConfigParser.ConfigParser()
conf.add_section(section)
for key, value in config.iteritems():
for key, value in config.items():
conf.set(section, key, value)
pyzor.config.expand_homefiles(homefiles, section, homedir, conf)
result = dict(conf.items(section))
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_engines/test_gdbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def firstkey(self):
if not self.keys():
return None
self.key_index = 1
return self.keys()[0]
return list(self.keys())[0]

def nextkey(self, key):
if len(self.keys()) <= self.key_index:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_forwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_queue(self):
max_qsize = 10
forwarder = pyzor.forwarder.Forwarder(client, servlist,
max_queue_size=max_qsize)
for _ in xrange(max_qsize * 2):
for _ in range(max_qsize * 2):
forwarder.queue_forward_request('975422c090e7a43ab7c9bf0065d5b661259e6d74')
self.assertGreater(forwarder.forward_queue.qsize(), 0, 'queue insert failed')
self.assertLessEqual(forwarder.forward_queue.qsize(), max_qsize, 'queue overload')
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import time
import logging
import unittest
import SocketServer
try:
import socketserver as SocketServer
except ImportError:
import SocketServer

from datetime import datetime, timedelta

Expand Down Expand Up @@ -42,7 +45,7 @@ def __init__(self, headers, database=None, acl=None, accounts=None):
"""
self.rfile = io.BytesIO()
self.wfile = io.BytesIO()
for i, j in headers.iteritems():
for i, j in headers.items():
self.rfile.write(("%s: %s\n" % (i, j)).encode("utf8"))
self.rfile.seek(0)
self.packet = None
Expand Down Expand Up @@ -129,7 +132,7 @@ def test_pong(self):
self.request["Op"] = "pong"
self.request["Op-Digest"] = digest
handler = pyzor.server.RequestHandler(self.request, database)
self.expected_response["Count"] = str(sys.maxint)
self.expected_response["Count"] = str(sys.maxsize)
self.expected_response["WL-Count"] = "0"

self.check_response(handler)
Expand Down

0 comments on commit 7afe0ae

Please sign in to comment.