From 9ee4200afa3c1db6663a50b29351c1c0975d8f0a Mon Sep 17 00:00:00 2001 From: Python3pkg Date: Wed, 17 May 2017 23:53:17 -0700 Subject: [PATCH] Convert to python3 --- checkpy/__init__.py | 10 +++++----- checkpy/__main__.py | 4 ++-- checkpy/assertlib.py | 4 ++-- checkpy/downloader.py | 13 +++++++------ checkpy/lib.py | 22 ++++++++++++---------- checkpy/printer.py | 16 ++++++++-------- checkpy/tester.py | 7 ++++--- checkpy/tests.py | 2 +- 8 files changed, 41 insertions(+), 37 deletions(-) diff --git a/checkpy/__init__.py b/checkpy/__init__.py index cbd57e7..079bce6 100644 --- a/checkpy/__init__.py +++ b/checkpy/__init__.py @@ -2,9 +2,9 @@ def testModule(moduleName): """ Test all files from module """ - import caches + from . import caches caches.clearAllCaches() - import tester + from . import tester results = tester.testModule(moduleName) try: if __IPYTHON__: @@ -18,9 +18,9 @@ def test(fileName): """ Run tests for a single file """ - import caches + from . import caches caches.clearAllCaches() - import tester + from . import tester result = tester.test(fileName) try: if __IPYTHON__: @@ -30,4 +30,4 @@ def test(fileName): pass return result -from downloader import download, update \ No newline at end of file +from .downloader import download, update \ No newline at end of file diff --git a/checkpy/__main__.py b/checkpy/__main__.py index 1e4b88b..e3f3dd6 100644 --- a/checkpy/__main__.py +++ b/checkpy/__main__.py @@ -1,8 +1,8 @@ import sys import os import argparse -import downloader -import tester +from . import downloader +from . import tester import shutil import time diff --git a/checkpy/assertlib.py b/checkpy/assertlib.py index 1934f85..b29dcec 100644 --- a/checkpy/assertlib.py +++ b/checkpy/assertlib.py @@ -1,4 +1,4 @@ -import lib +from . import lib import re import os @@ -18,7 +18,7 @@ def contains(actual, expectedElement): return expectedElement in actual def containsOnly(actual, expectedElements): - return len(filter(lambda e : e not in expectedElements, actual)) == 0 + return len([e for e in actual if e not in expectedElements]) == 0 def sameType(actual, expected): return type(actual) is type(expected) diff --git a/checkpy/downloader.py b/checkpy/downloader.py index 6f3aa92..a23a21c 100644 --- a/checkpy/downloader.py +++ b/checkpy/downloader.py @@ -1,11 +1,12 @@ import requests import zipfile as zf -import StringIO +import io import os import shutil import tinydb -import caches -import printer +from . import caches +from . import printer +from functools import reduce class Folder(object): def __init__(self, name, path): @@ -60,7 +61,7 @@ def pathFromFolder(self, folderName): return Path(path) def __add__(self, other): - if isinstance(other, str) or isinstance(other, unicode): + if isinstance(other, str) or isinstance(other, str): return Path(os.path.join(self.asString(), other)) return Path(os.path.join(self.asString(), other.asString())) @@ -85,7 +86,7 @@ def __eq__(self, other): def __contains__(self, item): return item in [item for item in self] - def __nonzero__ (self): + def __bool__ (self): return len(self.asString()) != 0 @@ -133,7 +134,7 @@ def _download(githubUserName, githubRepoName): _addToDownloadLocations(githubUserName, githubRepoName) - with zf.ZipFile(StringIO.StringIO(r.content)) as z: + with zf.ZipFile(io.StringIO(r.content)) as z: destFolder = Folder(githubRepoName, TESTSFOLDER.path + githubRepoName) existingTests = set() diff --git a/checkpy/lib.py b/checkpy/lib.py index 2490326..49c83cb 100644 --- a/checkpy/lib.py +++ b/checkpy/lib.py @@ -1,19 +1,21 @@ import sys import re -import StringIO +import io import contextlib import importlib import imp -import cStringIO +import io import tokenize -import exception as excep -import caches +from . import exception as excep +from . import caches +import collections +from functools import reduce @contextlib.contextmanager def _stdoutIO(stdout=None): old = sys.stdout if stdout is None: - stdout = StringIO.StringIO() + stdout = io.StringIO() sys.stdout = stdout yield stdout sys.stdout = old @@ -27,7 +29,7 @@ def new_input(prompt = None): old = sys.stdin if stdin is None: - stdin = StringIO.StringIO() + stdin = io.StringIO() sys.stdin = stdin yield stdin @@ -94,13 +96,13 @@ def moduleAndOutputFromSource(fileName, source, stdinArgs = None): moduleName = fileName[:-3] if fileName.endswith(".py") else fileName try: mod = imp.new_module(moduleName) - exec source in mod.__dict__ + exec(source, mod.__dict__) sys.modules[moduleName] = mod except Exception as e: exception = excep.SourceException(e, "while trying to import the code") - for name, func in [(name, f) for name, f in mod.__dict__.iteritems() if callable(f)]: + for name, func in [(name, f) for name, f in mod.__dict__.items() if isinstance(f, collections.Callable)]: if func.__module__ == moduleName: setattr(mod, name, wrapFunctionWithExceptionHandler(func)) output = stdout.getvalue() @@ -129,7 +131,7 @@ def exceptionWrapper(*args, **kwargs): return func(*args, **kwargs) except Exception as e: argListRepr = reduce(lambda xs, x : xs + ", " + x, ["{}={}".format(func.__code__.co_varnames[i], args[i]) for i in range(len(args))]) if args else "" - for kwargName in func.__code__.co_varnames[len(args):func.func_code.co_argcount]: + for kwargName in func.__code__.co_varnames[len(args):func.__code__.co_argcount]: argListRepr += ", {}={}".format(kwargName, kwargs[kwargName]) if not argListRepr: @@ -155,7 +157,7 @@ def getLine(text, lineNumber): # inspiration from http://stackoverflow.com/questions/1769332/script-to-remove-python-comments-docstrings def removeComments(source): - io_obj = cStringIO.StringIO(source) + io_obj = io.StringIO(source) out = "" prev_toktype = tokenize.INDENT last_lineno = -1 diff --git a/checkpy/printer.py b/checkpy/printer.py index bb743a8..f12a8e5 100644 --- a/checkpy/printer.py +++ b/checkpy/printer.py @@ -1,4 +1,4 @@ -import exception as excep +from . import exception as excep import os import colorama colorama.init() @@ -20,36 +20,36 @@ def display(testResult): msg = "{}{} {}{}".format(color, smiley, testResult.description, _Colors.ENDC) if testResult.message: msg += "\n - {}".format(testResult.message) - print msg + print(msg) return msg def displayTestName(testName): msg = "{}Testing: {}{}".format(_Colors.NAME, testName, _Colors.ENDC) - print msg + print(msg) return msg def displayUpdate(fileName): msg = "{}Updated: {}{}".format(_Colors.WARNING, os.path.basename(fileName), _Colors.ENDC) - print msg + print(msg) return msg def displayRemoved(fileName): msg = "{}Removed: {}{}".format(_Colors.WARNING, os.path.basename(fileName), _Colors.ENDC) - print msg + print(msg) return msg def displayAdded(fileName): msg = "{}Added: {}{}".format(_Colors.WARNING, os.path.basename(fileName), _Colors.ENDC) - print msg + print(msg) return msg def displayCustom(message): - print message + print(message) return message def displayError(message): msg = "{}{} {}{}".format(_Colors.WARNING, _Smileys.CONFUSED, message, _Colors.ENDC) - print msg + print(msg) return msg def _selectColorAndSmiley(testResult): diff --git a/checkpy/tester.py b/checkpy/tester.py index 3de1d54..6ccde39 100644 --- a/checkpy/tester.py +++ b/checkpy/tester.py @@ -1,5 +1,5 @@ -import printer -import caches +from . import printer +from . import caches import os import sys import importlib @@ -7,6 +7,7 @@ import multiprocessing import time import dill +import collections HERE = os.path.abspath(os.path.dirname(__file__)) @@ -138,7 +139,7 @@ def run(self): return reservedNames = ["before", "after"] - testCreators = [method for method in self.module.__dict__.values() if callable(method) and method.__name__ not in reservedNames] + testCreators = [method for method in list(self.module.__dict__.values()) if isinstance(method, collections.Callable) and method.__name__ not in reservedNames] result.nTests = len(testCreators) diff --git a/checkpy/tests.py b/checkpy/tests.py index 72c565e..03dd455 100644 --- a/checkpy/tests.py +++ b/checkpy/tests.py @@ -1,4 +1,4 @@ -import caches +from . import caches class Test(object): def __init__(self, priority):