From 128725ee7442388bd7e8cc3f907ab5677c535694 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 30 Mar 2021 11:00:52 +0200 Subject: [PATCH 5/6] =?UTF-8?q?misc:=20remove=20some=20python2=C2=A0compat?= =?UTF-8?q?ibility=20code=20(#52457)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/authentic2/compat/misc.py | 6 ------ src/authentic2/crypto.py | 3 +-- src/authentic2/csv_import.py | 13 +++---------- src/authentic2_idp_oidc/views.py | 2 +- tests/test_commands.py | 10 ++-------- tests/test_import_export_site_cmd.py | 4 +--- tests/test_utils_lazy.py | 14 -------------- 7 files changed, 8 insertions(+), 44 deletions(-) diff --git a/src/authentic2/compat/misc.py b/src/authentic2/compat/misc.py index d3110716..6f49db1a 100644 --- a/src/authentic2/compat/misc.py +++ b/src/authentic2/compat/misc.py @@ -18,7 +18,6 @@ import inspect from datetime import datetime from django.conf import settings -from django.utils import six try: from django.contrib.auth import get_user_model @@ -36,11 +35,6 @@ except ImportError: user_model_label = getattr(settings, 'AUTH_USER_MODEL', 'auth.User') -if six.PY2: - Base64Error = TypeError -else: - from binascii import Error as Base64Error - if hasattr(inspect, 'signature'): def signature_parameters(func): diff --git a/src/authentic2/crypto.py b/src/authentic2/crypto.py index 091885c2..8b9f04bf 100644 --- a/src/authentic2/crypto.py +++ b/src/authentic2/crypto.py @@ -18,6 +18,7 @@ import base64 import hashlib import hmac import struct +from binascii import Error as Base64Error from Cryptodome import Random from Cryptodome.Cipher import AES @@ -27,8 +28,6 @@ from django.utils.crypto import constant_time_compare from django.utils.encoding import force_bytes from django.utils.six import text_type -from authentic2.compat.misc import Base64Error - class DecryptionError(Exception): pass diff --git a/src/authentic2/csv_import.py b/src/authentic2/csv_import.py index f4da5293..dbc029e4 100644 --- a/src/authentic2/csv_import.py +++ b/src/authentic2/csv_import.py @@ -83,10 +83,7 @@ class UTF8Recoder(object): return self def __next__(self): - if six.PY2: - return self.fd.next().encode('utf-8') - else: - return force_text(self.fd.__next__().encode('utf-8')) + return force_text(self.fd.__next__().encode('utf-8')) next = __next__ @@ -96,12 +93,8 @@ class UnicodeReader(object): self.reader = csv.reader(UTF8Recoder(fd), dialect=dialect, **kwargs) def __next__(self): - if six.PY2: - row = self.reader.next() - return [s.decode('utf-8') for s in row] - else: - row = self.reader.__next__() - return [force_bytes(s).decode('utf-8') for s in row] + row = self.reader.__next__() + return [force_bytes(s).decode('utf-8') for s in row] def __iter__(self): return self diff --git a/src/authentic2_idp_oidc/views.py b/src/authentic2_idp_oidc/views.py index c3833e89..a876305e 100644 --- a/src/authentic2_idp_oidc/views.py +++ b/src/authentic2_idp_oidc/views.py @@ -18,6 +18,7 @@ import base64 import datetime import logging import math +from binascii import Error as Base64Error try: from secrets import compare_digest @@ -45,7 +46,6 @@ from ratelimit.utils import is_ratelimited from authentic2 import app_settings as a2_app_settings from authentic2 import hooks -from authentic2.compat.misc import Base64Error from authentic2.decorators import setting_enabled from authentic2.exponential_retry_timeout import ExponentialRetryTimeout from authentic2.utils import ( diff --git a/tests/test_commands.py b/tests/test_commands.py index 24951f94..9056408d 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -17,12 +17,12 @@ import datetime import importlib import json +from io import BufferedReader, BufferedWriter, TextIOWrapper import py import pytest from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType -from django.utils import six from django.utils.timezone import now from authentic2.a2_rbac.models import MANAGE_MEMBERS_OP, VIEW_OP @@ -42,13 +42,6 @@ from .utils import call_command, login User = get_user_model() -if six.PY2: - FileType = file # noqa: F821 -else: - from io import BufferedReader, BufferedWriter, TextIOWrapper - - FileType = (TextIOWrapper, BufferedReader, BufferedWriter) - def test_changepassword(db, simple_user, monkeypatch): import getpass @@ -196,6 +189,7 @@ def test_cleanupauthentic(db): def test_load_ldif(db, monkeypatch, tmpdir): + FileType = (TextIOWrapper, BufferedReader, BufferedWriter) ldif = tmpdir.join('some.ldif') ldif.ensure() diff --git a/tests/test_import_export_site_cmd.py b/tests/test_import_export_site_cmd.py index 030f4ad1..9261d9f1 100644 --- a/tests/test_import_export_site_cmd.py +++ b/tests/test_import_export_site_cmd.py @@ -21,7 +21,6 @@ import pytest from django import VERSION from django.core import management from django.core.exceptions import ValidationError -from django.utils import six from django.utils.six.moves import builtins as __builtin__ from django_rbac.utils import get_role_model @@ -168,8 +167,7 @@ def test_import_site_confirm_prompt_yes(db, monkeypatch, json_fixture): def yes_raw_input(*args, **kwargs): return 'yes' - input_funcname = 'raw_input' if six.PY2 else 'input' - monkeypatch.setattr(__builtin__, input_funcname, yes_raw_input) + monkeypatch.setattr(__builtin__, input, yes_raw_input) management.call_command('import_site', json_fixture(content), stdin='yes') assert Role.objects.get(uuid='dqfewrvesvews2532') diff --git a/tests/test_utils_lazy.py b/tests/test_utils_lazy.py index 6f99eddb..737666b2 100644 --- a/tests/test_utils_lazy.py +++ b/tests/test_utils_lazy.py @@ -14,10 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from __future__ import unicode_literals - import pytest -from django.utils import six from authentic2.utils.lazy import lazy_join @@ -25,14 +22,3 @@ from authentic2.utils.lazy import lazy_join def test_lazy_join(): assert lazy_join(', ', ['\xe9']) == '\xe9' assert lazy_join(', ', ['\xe9'] * 2) == '\xe9, \xe9' - if six.PY2: - assert lazy_join(b', ', ['\xe9']) == '\xe9' - assert lazy_join(b', ', ['\xe9'] * 2) == '\xe9, \xe9' - with pytest.raises(UnicodeDecodeError): - assert lazy_join(b', ', [b'\xe9']) == '' - with pytest.raises(UnicodeDecodeError): - assert lazy_join(b', ', [b'\xe9'] * 2) == '' - with pytest.raises(UnicodeDecodeError): - assert lazy_join(', ', [b'\xe9']) == '' - with pytest.raises(UnicodeDecodeError): - assert lazy_join(', ', [b'\xe9'] * 2) == '' -- 2.20.1