Drop support for python2

This commit is contained in:
Alexandre Aubin 2021-01-20 05:35:35 +01:00
parent 19a40b6e9a
commit 570e53234a
9 changed files with 29 additions and 128 deletions

View file

@ -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

View file

@ -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:

View file

@ -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"

View file

@ -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

View file

@ -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__"

View file

@ -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__))

View file

@ -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"))

View file

@ -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})

View file

@ -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)