Skip to content

Commit

Permalink
Remove utcnow() deprecation warning.
Browse files Browse the repository at this point in the history
Python 3.12 deprecates utcnow(), but we cannot do the recommended thing and replace
utcnow() with datetime.now(timezone.utc) as the datetime objects that the cryptography
x509 library puts in certificates do not have any timezone info.  If we try to
compare them with something that has timezone info we will get an exception:

    TypeError: can't compare offset-naive and offset-aware datetimes

We must continue to make an explictly offset-naive timestamp.
  • Loading branch information
rthalley committed Dec 29, 2023
1 parent ac18ff6 commit e94f6c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/aioquic/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@

T = TypeVar("T")

# facilitate mocking for the test suite
utcnow = datetime.datetime.utcnow
# facilitate time mocking for the test suite
#
# Python 3.12 deprecates utcnow(), but we cannot do the recommended thing and replace
# utcnow() with datetime.now(timezone.utc) as the datetime objects that the cryptography
# x509 library puts in certificates do not have any timezone info. If we try to
# compare them with something that has timezone info we will get an exception:
#
# TypeError: can't compare offset-naive and offset-aware datetimes
#
# We must continue to make an explictly offset-naive timestamp.

Check failure on line 66 in src/aioquic/tls.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

explictly ==> explicitly
utcnow = partial(datetime.datetime.now, None)


class AlertDescription(IntEnum):
Expand Down
5 changes: 3 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ def generate_certificate(*, alternative_names, common_name, hash_algorithm, key)
[x509.NameAttribute(x509.NameOID.COMMON_NAME, common_name)]
)

# See tls.py's comment about utcnow() for why we call now() with None below.
builder = (
x509.CertificateBuilder()
.subject_name(subject)
.issuer_name(issuer)
.public_key(key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(datetime.datetime.utcnow())
.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=10))
.not_valid_before(datetime.datetime.now(None))
.not_valid_after(datetime.datetime.now(None) + datetime.timedelta(days=10))
)
if alternative_names:
builder = builder.add_extension(
Expand Down

0 comments on commit e94f6c3

Please sign in to comment.