Projet

Général

Profil

0034-misc-fix-consider-using-with-pylint-error-56982.patch

Valentin Deniaud, 21 septembre 2021 17:09

Télécharger (6,29 ko)

Voir les différences:

Subject: [PATCH 34/59] misc: fix consider-using-with pylint error (#56982)

 src/authentic2/csv_import.py                  |  2 ++
 .../management/commands/export_site.py        |  8 ++---
 src/authentic2/nonce/utils.py                 |  1 +
 src/authentic2/saml/x509utils.py              | 29 ++++++++-----------
 src/authentic2_auth_fc/utils.py               |  6 ++--
 5 files changed, 22 insertions(+), 24 deletions(-)
src/authentic2/csv_import.py
113 113
            input_fd = io.StringIO(fd_or_str)
114 114
        elif not hasattr(fd_or_str, 'read1'):
115 115
            try:
116
                # pylint: disable=consider-using-with
116 117
                input_fd = open(fd_or_str.fileno(), closefd=False, mode='rb')
117 118
            except Exception:
118 119
                try:
......
151 152
                    input_fd.seek(0)
152 153

  
153 154
            if not hasattr(input_fd, 'readable'):
155
                # pylint: disable=consider-using-with
154 156
                input_fd = open(input_fd.fileno(), 'rb', closefd=False)
155 157
            return io.TextIOWrapper(input_fd, encoding=encoding)
156 158

  
src/authentic2/management/commands/export_site.py
32 32

  
33 33
    def handle(self, *args, **options):
34 34
        if options['output']:
35
            output, close = open(options['output'], 'w'), True
35
            with open(options['output'], 'w') as f:
36
                json.dump(export_site(), f, indent=4)
36 37
        else:
37
            output, close = sys.stdout, False
38
        json.dump(export_site(), output, indent=4)
39
        if close:
40
            output.close()
38
            json.dump(export_site(), sys.stdout, indent=4)
src/authentic2/nonce/utils.py
79 79
            # not too old, the nonce is unacceptable
80 80
            return False
81 81

  
82
    # pylint: disable=consider-using-with
82 83
    temp_file = tempfile.NamedTemporaryFile(dir=path, delete=False)
83 84
    temp_file.close()
84 85

  
src/authentic2/saml/x509utils.py
41 41
    Return a tuple made of the return code and the stdout output
42 42
    """
43 43
    try:
44
        process = subprocess.Popen(
44
        with subprocess.Popen(
45 45
            args=[_openssl] + args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True
46
        )
47
        output = process.communicate()[0]
48
        return process.returncode, output
46
        ) as process:
47
            output = process.communicate()[0]
48
            return process.returncode, output
49 49
    except OSError:
50 50
        return 1, None
51 51

  
......
57 57
    if not publickey or not privatekey:
58 58
        return None
59 59

  
60
    privatekey_file = tempfile.NamedTemporaryFile(mode='w')
61
    publickey_file = tempfile.NamedTemporaryFile(mode='w')
62
    with privatekey_file, publickey_file:
60
    with tempfile.NamedTemporaryFile(mode='w') as privatekey_file, tempfile.NamedTemporaryFile(
61
        mode='w'
62
    ) as publickey_file:
63 63

  
64 64
        privatekey_file.write(privatekey)
65 65
        privatekey_file.flush()
......
87 87

  
88 88
def generate_rsa_keypair(numbits=1024):
89 89
    """Generate simple RSA public and private key files"""
90
    privatekey_file = tempfile.NamedTemporaryFile(mode='r')
91
    publickey_file = tempfile.NamedTemporaryFile(mode='r')
92

  
93
    with privatekey_file, publickey_file:
90
    with tempfile.NamedTemporaryFile(mode='r') as privatekey_file, tempfile.NamedTemporaryFile(
91
        mode='r'
92
    ) as publickey_file:
94 93
        rc1, _ = _call_openssl(['genrsa', '-out', privatekey_file.name, '-passout', 'pass:', str(numbits)])
95 94
        if rc1 != 0:
96 95
            raise Exception('Failed to generate a key')
......
101 100

  
102 101

  
103 102
def get_rsa_public_key_modulus(publickey):
104
    publickey_file = tempfile.NamedTemporaryFile(mode='w')
105

  
106
    with publickey_file:
103
    with tempfile.NamedTemporaryFile(mode='w') as publickey_file:
107 104
        publickey_file.write(publickey)
108 105
        publickey_file.flush()
109 106

  
......
124 121

  
125 122

  
126 123
def get_rsa_public_key_exponent(publickey):
127
    publickey_file = tempfile.NamedTemporaryFile(mode='w')
128

  
129
    with publickey_file:
124
    with tempfile.NamedTemporaryFile(mode='w') as publickey_file:
130 125
        publickey_file.write(publickey)
131 126
        publickey_file.flush()
132 127

  
src/authentic2_auth_fc/utils.py
101 101
def resolve_insee_commune(insee_code):
102 102
    global _insee_communes
103 103
    if not _insee_communes:
104
        _insee_communes = json.load(open(os.path.join(os.path.dirname(__file__), 'insee-communes.json')))
104
        with open(os.path.join(os.path.dirname(__file__), 'insee-communes.json')) as f:
105
            _insee_communes = json.load(f)
105 106
    return _insee_communes.get(insee_code, _('Unknown INSEE code'))
106 107

  
107 108

  
......
112 113
    global _insee_countries
113 114

  
114 115
    if not _insee_countries:
115
        _insee_countries = json.load(open(os.path.join(os.path.dirname(__file__), 'insee-countries.json')))
116
        with open(os.path.join(os.path.dirname(__file__), 'insee-countries.json')) as f:
117
            _insee_countries = json.load(f)
116 118
    return _insee_countries.get(insee_code, _('Unknown INSEE code'))
117 119

  
118 120

  
119
-