Projet

Général

Profil

0005-misc-remove-some-python2-compatibility-code-52457.patch

Valentin Deniaud, 30 mars 2021 11:39

Télécharger (6,96 ko)

Voir les différences:

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(-)
src/authentic2/compat/misc.py
18 18
from datetime import datetime
19 19

  
20 20
from django.conf import settings
21
from django.utils import six
22 21

  
23 22
try:
24 23
    from django.contrib.auth import get_user_model
......
36 35

  
37 36
user_model_label = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
38 37

  
39
if six.PY2:
40
    Base64Error = TypeError
41
else:
42
    from binascii import Error as Base64Error
43

  
44 38
if hasattr(inspect, 'signature'):
45 39

  
46 40
    def signature_parameters(func):
src/authentic2/crypto.py
18 18
import hashlib
19 19
import hmac
20 20
import struct
21
from binascii import Error as Base64Error
21 22

  
22 23
from Cryptodome import Random
23 24
from Cryptodome.Cipher import AES
......
27 28
from django.utils.encoding import force_bytes
28 29
from django.utils.six import text_type
29 30

  
30
from authentic2.compat.misc import Base64Error
31

  
32 31

  
33 32
class DecryptionError(Exception):
34 33
    pass
src/authentic2/csv_import.py
83 83
        return self
84 84

  
85 85
    def __next__(self):
86
        if six.PY2:
87
            return self.fd.next().encode('utf-8')
88
        else:
89
            return force_text(self.fd.__next__().encode('utf-8'))
86
        return force_text(self.fd.__next__().encode('utf-8'))
90 87

  
91 88
    next = __next__
92 89

  
......
96 93
        self.reader = csv.reader(UTF8Recoder(fd), dialect=dialect, **kwargs)
97 94

  
98 95
    def __next__(self):
99
        if six.PY2:
100
            row = self.reader.next()
101
            return [s.decode('utf-8') for s in row]
102
        else:
103
            row = self.reader.__next__()
104
            return [force_bytes(s).decode('utf-8') for s in row]
96
        row = self.reader.__next__()
97
        return [force_bytes(s).decode('utf-8') for s in row]
105 98

  
106 99
    def __iter__(self):
107 100
        return self
src/authentic2_idp_oidc/views.py
18 18
import datetime
19 19
import logging
20 20
import math
21
from binascii import Error as Base64Error
21 22

  
22 23
try:
23 24
    from secrets import compare_digest
......
45 46

  
46 47
from authentic2 import app_settings as a2_app_settings
47 48
from authentic2 import hooks
48
from authentic2.compat.misc import Base64Error
49 49
from authentic2.decorators import setting_enabled
50 50
from authentic2.exponential_retry_timeout import ExponentialRetryTimeout
51 51
from authentic2.utils import (
tests/test_commands.py
17 17
import datetime
18 18
import importlib
19 19
import json
20
from io import BufferedReader, BufferedWriter, TextIOWrapper
20 21

  
21 22
import py
22 23
import pytest
23 24
from django.contrib.auth import get_user_model
24 25
from django.contrib.contenttypes.models import ContentType
25
from django.utils import six
26 26
from django.utils.timezone import now
27 27

  
28 28
from authentic2.a2_rbac.models import MANAGE_MEMBERS_OP, VIEW_OP
......
42 42

  
43 43
User = get_user_model()
44 44

  
45
if six.PY2:
46
    FileType = file  # noqa: F821
47
else:
48
    from io import BufferedReader, BufferedWriter, TextIOWrapper
49

  
50
    FileType = (TextIOWrapper, BufferedReader, BufferedWriter)
51

  
52 45

  
53 46
def test_changepassword(db, simple_user, monkeypatch):
54 47
    import getpass
......
196 189

  
197 190

  
198 191
def test_load_ldif(db, monkeypatch, tmpdir):
192
    FileType = (TextIOWrapper, BufferedReader, BufferedWriter)
199 193
    ldif = tmpdir.join('some.ldif')
200 194
    ldif.ensure()
201 195

  
tests/test_import_export_site_cmd.py
21 21
from django import VERSION
22 22
from django.core import management
23 23
from django.core.exceptions import ValidationError
24
from django.utils import six
25 24
from django.utils.six.moves import builtins as __builtin__
26 25

  
27 26
from django_rbac.utils import get_role_model
......
168 167
    def yes_raw_input(*args, **kwargs):
169 168
        return 'yes'
170 169

  
171
    input_funcname = 'raw_input' if six.PY2 else 'input'
172
    monkeypatch.setattr(__builtin__, input_funcname, yes_raw_input)
170
    monkeypatch.setattr(__builtin__, input, yes_raw_input)
173 171

  
174 172
    management.call_command('import_site', json_fixture(content), stdin='yes')
175 173
    assert Role.objects.get(uuid='dqfewrvesvews2532')
tests/test_utils_lazy.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
from __future__ import unicode_literals
18

  
19 17
import pytest
20
from django.utils import six
21 18

  
22 19
from authentic2.utils.lazy import lazy_join
23 20

  
......
25 22
def test_lazy_join():
26 23
    assert lazy_join(', ', ['\xe9']) == '\xe9'
27 24
    assert lazy_join(', ', ['\xe9'] * 2) == '\xe9, \xe9'
28
    if six.PY2:
29
        assert lazy_join(b', ', ['\xe9']) == '\xe9'
30
        assert lazy_join(b', ', ['\xe9'] * 2) == '\xe9, \xe9'
31
        with pytest.raises(UnicodeDecodeError):
32
            assert lazy_join(b', ', [b'\xe9']) == ''
33
        with pytest.raises(UnicodeDecodeError):
34
            assert lazy_join(b', ', [b'\xe9'] * 2) == ''
35
        with pytest.raises(UnicodeDecodeError):
36
            assert lazy_join(', ', [b'\xe9']) == ''
37
        with pytest.raises(UnicodeDecodeError):
38
            assert lazy_join(', ', [b'\xe9'] * 2) == ''
39
-