diff --git a/moulinette/actionsmap.py b/moulinette/actionsmap.py index c13605cd..d36c59f7 100644 --- a/moulinette/actionsmap.py +++ b/moulinette/actionsmap.py @@ -5,26 +5,7 @@ import re import logging import yaml import glob - -import sys -if sys.version_info[0] == 3: - # python 3 - import pickle as pickle -else: - # python 2 - import cPickle as pickle - import codecs - import warnings - def open(file, mode='r', buffering=-1, encoding=None, - errors=None, newline=None, closefd=True, opener=None): - if newline is not None: - warnings.warn('newline is not supported in py2') - if not closefd: - warnings.warn('closefd is not supported in py2') - if opener is not None: - warnings.warn('opener is not supported in py2') - return codecs.open(filename=file, mode=mode, encoding=encoding, - errors=errors, buffering=buffering) +import pickle as pickle from time import time from collections import OrderedDict diff --git a/moulinette/core.py b/moulinette/core.py index 2ce9e2d7..036fd3d5 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -8,24 +8,6 @@ import logging import moulinette from moulinette.globals import init_moulinette_env -import sys -if sys.version_info[0] == 3: - pass -else: - # python 2 - import codecs - import warnings - def open(file, mode='r', buffering=-1, encoding=None, - errors=None, newline=None, closefd=True, opener=None): - if newline is not None: - warnings.warn('newline is not supported in py2') - if not closefd: - warnings.warn('closefd is not supported in py2') - if opener is not None: - warnings.warn('opener is not supported in py2') - return codecs.open(filename=file, mode=mode, encoding=encoding, - errors=errors, buffering=buffering) - logger = logging.getLogger("moulinette.core") @@ -189,9 +171,6 @@ class Translator(object): try: with open("%s/%s.json" % (self.locale_dir, locale), "r", encoding='utf-8') as f: j = json.load(f) - import sys - if sys.version_info[0] == 2: - j = {k.encode("utf-8"): v.encode("utf-8") for k, v in j.items()} except IOError: return False else: diff --git a/moulinette/interfaces/__init__.py b/moulinette/interfaces/__init__.py index 3f77e3d7..624ab7fd 100644 --- a/moulinette/interfaces/__init__.py +++ b/moulinette/interfaces/__init__.py @@ -9,13 +9,6 @@ from collections import deque, OrderedDict from moulinette import msettings, m18n from moulinette.core import MoulinetteError -import sys -if sys.version_info[0] == 3: - pass -else: - # python 2 - range = xrange - logger = logging.getLogger("moulinette.interface") GLOBAL_SECTION = "_global" diff --git a/moulinette/utils/text.py b/moulinette/utils/text.py index 61c7f0f6..8e959a78 100644 --- a/moulinette/utils/text.py +++ b/moulinette/utils/text.py @@ -3,24 +3,6 @@ import re import mmap import binascii -import sys -if sys.version_info[0] == 3: - pass -else: - # python 2 - import codecs - import warnings - def open(file, mode='r', buffering=-1, encoding=None, - errors=None, newline=None, closefd=True, opener=None): - if newline is not None: - warnings.warn('newline is not supported in py2') - if not closefd: - warnings.warn('closefd is not supported in py2') - if opener is not None: - warnings.warn('opener is not supported in py2') - return codecs.open(filename=file, mode=mode, encoding=encoding, - errors=errors, buffering=buffering) - # Pattern searching ---------------------------------------------------- @@ -66,10 +48,7 @@ def searchf(pattern, path, count=0, flags=re.MULTILINE): """ with open(path, "rb+") as f: data = mmap.mmap(f.fileno(), 0) - if sys.version_info[0] == 3: - match = search(pattern, data.read().decode(), count, flags) - else: - match = search(pattern, data, count, flags) + match = search(pattern, data.read().decode(), count, flags) data.close() return match diff --git a/test/conftest.py b/test/conftest.py index 2586ff5a..2f903d8e 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -6,7 +6,6 @@ import json import os import shutil import pytest -import sys from .src.ldap_server import LDAPServer @@ -220,11 +219,3 @@ def ldap_server(): server.start() yield server server.stop() - - -@pytest.fixture -def builtin_str(): - if sys.version_info[0] == 3: - return "builtins" - else: - return "__builtin__" \ No newline at end of file diff --git a/test/src/ldap_server.py b/test/src/ldap_server.py index 8cf30cfc..232b8d37 100644 --- a/test/src/ldap_server.py +++ b/test/src/ldap_server.py @@ -5,24 +5,6 @@ except ImportError: import os from moulinette.authenticators import ldap as m_ldap -import sys -if sys.version_info[0] == 3: - pass -else: - # python 2 - import codecs - import warnings - def open(file, mode='r', buffering=-1, encoding=None, - errors=None, newline=None, closefd=True, opener=None): - if newline is not None: - warnings.warn('newline is not supported in py2') - if not closefd: - warnings.warn('closefd is not supported in py2') - if opener is not None: - warnings.warn('opener is not supported in py2') - return codecs.open(filename=file, mode=mode, encoding=encoding, - errors=errors, buffering=buffering) - HERE = os.path.abspath(os.path.dirname(__file__)) diff --git a/test/test_actionsmap.py b/test/test_actionsmap.py index edf175a1..deaf52f5 100644 --- a/test/test_actionsmap.py +++ b/test/test_actionsmap.py @@ -244,7 +244,7 @@ def test_actions_map_api(): assert ("POST", "/test-auth/subcat/post") in amap.parser.routes -def test_actions_map_import_error(mocker, builtin_str): +def test_actions_map_import_error(mocker): from moulinette.interfaces.api import ActionsMapParser amap = ActionsMap(ActionsMapParser()) @@ -261,7 +261,7 @@ def test_actions_map_import_error(mocker, builtin_str): raise ImportError return orig_import(name, globals, locals, fromlist, level) - mocker.patch(builtin_str + ".__import__", side_effect=import_mock) + mocker.patch("builtins.__import__", side_effect=import_mock) with pytest.raises(MoulinetteError) as exception: amap.process({}, timeout=30, route=("GET", "/test-auth/none")) diff --git a/test/test_filesystem.py b/test/test_filesystem.py index 88fe6273..b0dd3754 100644 --- a/test/test_filesystem.py +++ b/test/test_filesystem.py @@ -39,10 +39,10 @@ def test_read_file_missing_file(): assert expected_msg in str(exception) -def test_read_file_cannot_read_ioerror(test_file, mocker, builtin_str): +def test_read_file_cannot_read_ioerror(test_file, mocker): error = "foobar" - mocker.patch(builtin_str + ".open", side_effect=IOError(error)) + mocker.patch("builtins.open", side_effect=IOError(error)) with pytest.raises(MoulinetteError) as exception: read_file(str(test_file)) @@ -51,10 +51,10 @@ def test_read_file_cannot_read_ioerror(test_file, mocker, builtin_str): assert expected_msg in str(exception) -def test_read_file_cannot_read_exception(test_file, mocker, builtin_str): +def test_read_file_cannot_read_exception(test_file, mocker): error = "foobar" - mocker.patch(builtin_str + ".open", side_effect=Exception(error)) + mocker.patch("builtins.open", side_effect=Exception(error)) with pytest.raises(MoulinetteError) as exception: read_file(str(test_file)) @@ -133,10 +133,10 @@ def test_read_ldif(test_ldif): assert entry["cn"] == ["Alice Alison".encode("utf-8")] -def test_read_ldif_cannot_ioerror(test_ldif, mocker, builtin_str): +def test_read_ldif_cannot_ioerror(test_ldif, mocker): error = "foobar" - mocker.patch(builtin_str + ".open", side_effect=IOError(error)) + mocker.patch("builtins.open", side_effect=IOError(error)) with pytest.raises(MoulinetteError) as exception: read_ldif(str(test_ldif)) @@ -145,10 +145,10 @@ def test_read_ldif_cannot_ioerror(test_ldif, mocker, builtin_str): assert expected_msg in str(exception) -def test_read_ldif_cannot_exception(test_ldif, mocker, builtin_str): +def test_read_ldif_cannot_exception(test_ldif, mocker): error = "foobar" - mocker.patch(builtin_str + ".open", side_effect=Exception(error)) + mocker.patch("builtins.open", side_effect=Exception(error)) with pytest.raises(MoulinetteError) as exception: read_ldif(str(test_ldif)) @@ -171,10 +171,10 @@ def test_write_to_new_file(tmp_path): assert read_file(str(new_file)) == "yolo\nswag" -def test_write_to_existing_file_bad_perms(test_file, mocker, builtin_str): +def test_write_to_existing_file_bad_perms(test_file, mocker): error = "foobar" - mocker.patch(builtin_str + ".open", side_effect=IOError(error)) + mocker.patch("builtins.open", side_effect=IOError(error)) with pytest.raises(MoulinetteError) as exception: write_to_file(str(test_file), "yolo\nswag") @@ -183,10 +183,10 @@ def test_write_to_existing_file_bad_perms(test_file, mocker, builtin_str): assert expected_msg in str(exception) -def test_write_to_file_exception(test_file, mocker, builtin_str): +def test_write_to_file_exception(test_file, mocker): error = "foobar" - mocker.patch(builtin_str + ".open", side_effect=Exception(error)) + mocker.patch("builtins.open", side_effect=Exception(error)) with pytest.raises(MoulinetteError) as exception: write_to_file(str(test_file), "yolo\nswag") @@ -238,12 +238,12 @@ def test_write_dict_to_json(tmp_path): assert _json["bar"] == ["a", "b", "c"] -def test_write_json_to_existing_file_bad_perms(test_file, mocker, builtin_str): +def test_write_json_to_existing_file_bad_perms(test_file, mocker): error = "foobar" dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]} - mocker.patch(builtin_str + ".open", side_effect=IOError(error)) + mocker.patch("builtins.open", side_effect=IOError(error)) with pytest.raises(MoulinetteError) as exception: write_to_json(str(test_file), dummy_dict) @@ -252,12 +252,12 @@ def test_write_json_to_existing_file_bad_perms(test_file, mocker, builtin_str): assert expected_msg in str(exception) -def test_write_json_to_file_exception(test_file, mocker, builtin_str): +def test_write_json_to_file_exception(test_file, mocker): error = "foobar" dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]} - mocker.patch(builtin_str + ".open", side_effect=Exception(error)) + mocker.patch("builtins.open", side_effect=Exception(error)) with pytest.raises(MoulinetteError) as exception: write_to_json(str(test_file), dummy_dict) @@ -276,10 +276,10 @@ def text_write_list_to_json(tmp_path): assert _json == ["foo", "bar", "baz"] -def test_write_to_json_bad_perms(test_json, mocker, builtin_str): +def test_write_to_json_bad_perms(test_json, mocker): error = "foobar" - mocker.patch(builtin_str + ".open", side_effect=IOError(error)) + mocker.patch("builtins.open", side_effect=IOError(error)) with pytest.raises(MoulinetteError) as exception: write_to_json(str(test_json), {"a": 1}) @@ -307,12 +307,12 @@ def test_write_dict_to_yaml(tmp_path): assert _yaml["bar"] == ["a", "b", "c"] -def test_write_yaml_to_existing_file_bad_perms(test_file, mocker, builtin_str): +def test_write_yaml_to_existing_file_bad_perms(test_file, mocker): error = "foobar" dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]} - mocker.patch(builtin_str + ".open", side_effect=IOError(error)) + mocker.patch("builtins.open", side_effect=IOError(error)) with pytest.raises(MoulinetteError) as exception: write_to_yaml(str(test_file), dummy_dict) @@ -321,12 +321,12 @@ def test_write_yaml_to_existing_file_bad_perms(test_file, mocker, builtin_str): assert expected_msg in str(exception) -def test_write_yaml_to_file_exception(test_file, mocker, builtin_str): +def test_write_yaml_to_file_exception(test_file, mocker): error = "foobar" dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]} - mocker.patch(builtin_str + ".open", side_effect=Exception(error)) + mocker.patch("builtins.open", side_effect=Exception(error)) with pytest.raises(MoulinetteError) as exception: write_to_yaml(str(test_file), dummy_dict) @@ -345,10 +345,10 @@ def text_write_list_to_yaml(tmp_path): assert _yaml == ["foo", "bar", "baz"] -def test_write_to_yaml_bad_perms(test_yaml, mocker, builtin_str): +def test_write_to_yaml_bad_perms(test_yaml, mocker): error = "foobar" - mocker.patch(builtin_str + ".open", side_effect=IOError(error)) + mocker.patch("builtins.open", side_effect=IOError(error)) with pytest.raises(MoulinetteError) as exception: write_to_yaml(str(test_yaml), {"a": 1}) diff --git a/test/test_text.py b/test/test_text.py index f569fafb..1bf1f896 100644 --- a/test/test_text.py +++ b/test/test_text.py @@ -19,8 +19,4 @@ def test_prependlines(): def test_random_ascii(): - import sys - if sys.version_info[0] == 3: - assert isinstance(random_ascii(length=2), str) - else: - assert isinstance(random_ascii(length=2), unicode) + assert isinstance(random_ascii(length=2), str)