Skip to content

Commit fa424f8

Browse files
committed
{Misc} Remove various bits of Python 2.7 code
1 parent 1676925 commit fa424f8

File tree

25 files changed

+34
-125
lines changed

25 files changed

+34
-125
lines changed

scripts/curl_install_pypi/install.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,8 @@
2323
import shutil
2424
import subprocess
2525
import hashlib
26-
try:
27-
# Attempt to load python 3 module
28-
from urllib.request import urlopen
29-
except ImportError:
30-
# Import python 2 version
31-
from urllib2 import urlopen
32-
33-
try:
34-
# Rename raw_input to input to support Python 2
35-
input = raw_input
36-
except NameError:
37-
# Python 3 doesn't have raw_input
38-
pass
26+
from urllib.request import urlopen
27+
3928

4029
AZ_DISPATCH_TEMPLATE = """#!/usr/bin/env bash
4130
{install_dir}/bin/python -m azure.cli "$@"

src/azure-cli-core/azure/cli/core/_help_loaders.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515

1616
logger = get_logger(__name__)
1717

18-
try:
19-
ABC = abc.ABC
20-
except AttributeError: # Python 2.7, abc exists, but not ABC
21-
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
22-
2318

2419
# BaseHelpLoader defining versioned loader interface. Also contains some helper methods.
25-
class BaseHelpLoader(ABC):
20+
class BaseHelpLoader(abc.ABC):
2621
def __init__(self, help_ctx=None):
2722
self.help_ctx = help_ctx
2823
self._entry_data = None

src/azure-cli-core/azure/cli/core/aaz/_operation.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import json
99

10+
from urllib.parse import quote
11+
1012
from azure.core.exceptions import ClientAuthenticationError, ResourceExistsError, ResourceNotFoundError, \
1113
HttpResponseError
1214
from azure.cli.core.azclierror import InvalidArgumentValueError
@@ -19,10 +21,6 @@
1921
from ._field_value import AAZList, AAZObject, AAZBaseDictValue
2022
from .exceptions import AAZInvalidValueError
2123

22-
try:
23-
from urllib import quote # type: ignore
24-
except ImportError:
25-
from urllib.parse import quote # type: ignore
2624

2725

2826
class AAZOperation:

src/azure-cli-core/azure/cli/core/commands/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@
4242
from knack.events import EVENT_INVOKER_TRANSFORM_RESULT
4343
from knack.validators import DefaultStr
4444

45-
try:
46-
t_JSONDecodeError = json.JSONDecodeError
47-
except AttributeError: # in Python 2.7
48-
t_JSONDecodeError = ValueError
45+
t_JSONDecodeError = json.JSONDecodeError
4946

5047
logger = get_logger(__name__)
5148
DEFAULT_CACHE_TTL = '10'
@@ -471,7 +468,7 @@ def _put_operation():
471468
obj_path = os.path.join(obj_dir, obj_file)
472469
try:
473470
os.remove(obj_path)
474-
except (OSError, IOError): # FileNotFoundError introduced in Python 3
471+
except FileNotFoundError:
475472
pass
476473
return result
477474

src/azure-cli-core/azure/cli/core/keys.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ def is_valid_ssh_rsa_public_key(openssh_pubkey):
1616
# http://stackoverflow.com/questions/2494450/ssh-rsa-public-key-validation-using-a-regular-expression # pylint: disable=line-too-long
1717
# A "good enough" check is to see if the key starts with the correct header.
1818
import struct
19-
try:
20-
from base64 import decodebytes as base64_decode
21-
except ImportError:
22-
# deprecated and redirected to decodebytes in Python 3
23-
from base64 import decodestring as base64_decode
19+
from base64 import decodestring as base64_decode
2420

2521
parts = openssh_pubkey.split()
2622
if len(parts) < 2:

src/azure-cli-core/azure/cli/core/tests/test_aaz_prompt.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import random
88

99
import unittest
10-
try:
11-
import mock
12-
except ImportError:
13-
from unittest import mock
10+
from unittest import mock
1411

1512
from azure.cli.core import azclierror
1613
from azure.cli.core.aaz import exceptions as aazerror

src/azure-cli-telemetry/azure/cli/telemetry/components/telemetry_client.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66
import json
77
import datetime
88

9+
import urllib.request as http_client_t
10+
from urllib.error import HTTPError
11+
912
from applicationinsights import TelemetryClient
1013
from applicationinsights.channel import SynchronousSender, SynchronousQueue, TelemetryChannel
1114

12-
try:
13-
# Python 2.x
14-
import urllib2 as http_client_t
15-
from urllib2 import HTTPError
16-
except ImportError:
17-
# Python 3.x
18-
import urllib.request as http_client_t
19-
from urllib.error import HTTPError
20-
2115

2216
class CliTelemetryClient:
2317
def __init__(self, batch=100, sender=None):

src/azure-cli-telemetry/azure/cli/telemetry/tests/test_telemetry_client.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88
import os
99
import unittest
1010

11-
from applicationinsights.channel import SynchronousSender
11+
import urllib.request as http_client_t
12+
from urllib.error import HTTPError
1213

13-
try:
14-
# Python 2.x
15-
import urllib2 as http_client_t
16-
from urllib2 import HTTPError
17-
except ImportError:
18-
# Python 3.x
19-
import urllib.request as http_client_t
20-
from urllib.error import HTTPError
14+
from applicationinsights.channel import SynchronousSender
2115

2216
from azure.cli.telemetry.components.telemetry_client import (CliTelemetryClient, _NoRetrySender, http_client_t)
2317

src/azure-cli-testsdk/azure/cli/testsdk/scenario_tests/patches.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def _shortcut_long_run_operation(*args, **kwargs): # pylint: disable=unused-arg
2323

2424

2525
def mock_in_unit_test(unit_test, target, replacement):
26-
try:
27-
import unittest.mock as mock
26+
from unittest.mock as mock
2827
except ImportError:
2928
import mock
3029
import unittest

src/azure-cli-testsdk/azure/cli/testsdk/scenario_tests/tests/test_utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --------------------------------------------------------------------------------------------
55
import os
66
import unittest
7-
import mock
7+
from unittest import mock
88
from azure.cli.testsdk.scenario_tests.utilities import (
99
create_random_name, get_sha1_hash, is_text_payload, is_json_payload)
1010

0 commit comments

Comments
 (0)