cleanup references of python 2 & <3.11 (#35799)

* chore: cleanup of old python references
This commit is contained in:
Irtaza Akram
2024-11-15 16:58:20 +05:00
committed by GitHub
parent 9de9f2648d
commit ec2a698604
18 changed files with 87 additions and 165 deletions

View File

@@ -106,9 +106,12 @@ def meili_id_from_opaque_key(usage_key: UsageKey) -> str:
we could use PublishableEntity's primary key / UUID instead.
"""
# The slugified key _may_ not be unique so we append a hashed string to make it unique:
key_bin = str(usage_key).encode()
suffix = blake2b(key_bin, digest_size=4).hexdigest() # When we use Python 3.9+, should add usedforsecurity=False
return slugify(str(usage_key)) + "-" + suffix
key_str = str(usage_key)
key_bin = key_str.encode()
suffix = blake2b(key_bin, digest_size=4, usedforsecurity=False).hexdigest()
return f"{slugify(key_str)}-{suffix}"
def _meili_access_id_from_context_key(context_key: LearningContextKey) -> int:

View File

@@ -86,11 +86,10 @@ class ContentLibrariesRestApiTest(APITransactionTestCase):
"""
Assert that the first dict contains at least all of the same entries as
the second dict.
Like python 2's assertDictContainsSubset, but with the arguments in the
correct order.
"""
assert big_dict.items() >= subset_dict.items()
for key, value in subset_dict.items():
assert key in big_dict, f"Missing key: {key}"
assert big_dict[key] == value, f"Value for key {key} does not match: expected {value}, got {big_dict[key]}"
def assertOrderEqual(self, libraries_list, expected_order):
"""

View File

@@ -270,14 +270,13 @@ class TestCsrfCrossDomainCookieMiddleware(TestCase):
if is_set:
assert self.COOKIE_NAME in response.cookies
cookie_header = str(response.cookies[self.COOKIE_NAME])
# lint-amnesty, pylint: disable=bad-option-value, unicode-format-string
expected = 'Set-Cookie: {name}={value}; Domain={domain};'.format(
name=self.COOKIE_NAME,
value=self.COOKIE_VALUE,
domain=self.COOKIE_DOMAIN
)
assert expected in cookie_header
# added lower function because in python 3 the value of cookie_header has Secure and secure in python 2
assert 'Max-Age=31449600; Path=/; secure'.lower() in cookie_header.lower()
assert 'Max-Age=31449600; Path=/; Secure' in cookie_header
else:
assert self.COOKIE_NAME not in response.cookies

View File

@@ -42,16 +42,14 @@ class CrawlersConfig(ConfigurationModel):
# If there was no user agent detected or no crawler agents configured,
# then just return False.
if (not req_user_agent) or (not crawler_agents):
if not req_user_agent or not crawler_agents:
return False
# The crawler_agents list we pull from our model always has unicode objects, but the
# req_user_agent we get from HTTP headers ultimately comes to us via WSGI. That
# value is an ISO-8859-1 encoded byte string in Python 2.7 (and in the HTTP spec), but
# it will be a unicode str when we move to Python 3.x. This code should work under
# either version.
# Decode req_user_agent if it's bytes, so we can work with consistent string types.
if isinstance(req_user_agent, bytes):
crawler_agents = [crawler_agent.encode('iso-8859-1') for crawler_agent in crawler_agents]
req_user_agent = req_user_agent.decode('iso-8859-1')
crawler_agents = [crawler_agent.strip() for crawler_agent in crawler_agents]
# We perform prefix matching of the crawler agent here so that we don't
# have to worry about version bumps.

View File

@@ -207,7 +207,7 @@ class CacheInvalidationManager:
def zpickle(data):
"""Given any data structure, returns a zlib compressed pickled serialization."""
return zlib.compress(pickle.dumps(data, 4)) # Keep this constant as we upgrade from python 2 to 3.
return zlib.compress(pickle.dumps(data, 4))
def zunpickle(zdata):

View File

@@ -48,11 +48,9 @@ def is_score_higher_or_equal(earned1, possible1, earned2, possible2, treat_undef
def round_away_from_zero(number, digits=0):
"""
Round numbers using the 'away from zero' strategy as opposed to the
'Banker's rounding strategy.' The strategy refers to how we round when
a number is half way between two numbers. eg. 0.5, 1.5, etc. In python 2
positive numbers in this category would be rounded up and negative numbers
would be rounded down. ie. away from zero. In python 3 numbers round
towards even. So 0.5 would round to 0 but 1.5 would round to 2.
'Banker's rounding strategy.' The strategy refers to how we round when
a number is half way between two numbers. eg. 0.5, 1.5, etc. In python 3
numbers round towards even. So 0.5 would round to 0 but 1.5 would round to 2.
See here for more on floating point rounding strategies:
https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules