Skip to content

Commit 4a852fe

Browse files
authored
tree-wide: replace utcnow and utcfromtimestamp (#4462)
They were deprecated in python/cpython#103857. Closes #4460
1 parent d7ae655 commit 4a852fe

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

scapy/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _version_from_git_archive():
6868
return _parse_tag(tag)
6969
elif tstamp:
7070
# archived revision is not tagged, use the commit date
71-
d = datetime.datetime.utcfromtimestamp(int(tstamp))
71+
d = datetime.datetime.fromtimestamp(int(tstamp), datetime.timezone.utc)
7272
return d.strftime('%Y.%m.%d')
7373

7474
raise ValueError("invalid git archive format")
@@ -162,7 +162,9 @@ def _version():
162162
# Fallback
163163
try:
164164
# last resort, use the modification date of __init__.py
165-
d = datetime.datetime.utcfromtimestamp(os.path.getmtime(__file__))
165+
d = datetime.datetime.fromtimestamp(
166+
os.path.getmtime(__file__), datetime.timezone.utc
167+
)
166168
return d.strftime('%Y.%m.%d')
167169
except Exception:
168170
pass

scapy/layers/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def self_build(self, **kwargs):
434434
# Add Content-Length anyways
435435
val = str(len(self.payload or b""))
436436
elif f.name == "Date" and isinstance(self, HTTPResponse):
437-
val = datetime.datetime.utcnow().strftime(
437+
val = datetime.datetime.now(datetime.timezone.utc).strftime(
438438
'%a, %d %b %Y %H:%M:%S GMT'
439439
)
440440
else:

scapy/layers/kerberos.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,7 +2643,7 @@ def _base_kdc_req(self, now_time):
26432643
return kdcreq
26442644

26452645
def as_req(self):
2646-
now_time = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc)
2646+
now_time = datetime.now(timezone.utc).replace(microsecond=0)
26472647

26482648
kdc_req = self._base_kdc_req(now_time=now_time)
26492649
kdc_req.addresses = [
@@ -2690,7 +2690,7 @@ def as_req(self):
26902690
return asreq
26912691

26922692
def tgs_req(self):
2693-
now_time = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc)
2693+
now_time = datetime.now(timezone.utc).replace(microsecond=0)
26942694

26952695
kdc_req = self._base_kdc_req(now_time=now_time)
26962696

@@ -3836,7 +3836,7 @@ def GSS_Init_sec_context(
38363836
authenticator=EncryptedData(),
38373837
)
38383838
# Build the authenticator
3839-
now_time = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc)
3839+
now_time = datetime.now(timezone.utc).replace(microsecond=0)
38403840
Context.KrbSessionKey = Key.random_to_key(
38413841
self.SKEY_TYPE,
38423842
os.urandom(16),
@@ -3929,7 +3929,7 @@ def GSS_Init_sec_context(
39293929
# The client MUST generate an additional AP exchange reply message
39303930
# exactly as the server would as the final message to send to the
39313931
# server.
3932-
now_time = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc)
3932+
now_time = datetime.now(timezone.utc).replace(microsecond=0)
39333933
cli_ap_rep = KRB_AP_REP(encPart=EncryptedData())
39343934
cli_ap_rep.encPart.encrypt(
39353935
Context.STSessionKey,
@@ -4018,7 +4018,7 @@ def GSS_Accept_sec_context(self, Context: CONTEXT, val=None):
40184018
# Required but not provided. Return an error
40194019
self._setup_u2u()
40204020
Context.U2U = True
4021-
now_time = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc)
4021+
now_time = datetime.now(timezone.utc).replace(microsecond=0)
40224022
err = KRB_GSSAPI_Token(
40234023
innerToken=KRB_InnerToken(
40244024
TOK_ID=b"\x03\x00",
@@ -4039,7 +4039,7 @@ def GSS_Accept_sec_context(self, Context: CONTEXT, val=None):
40394039
tkt = ap_req.ticket.encPart.decrypt(self.KEY)
40404040
except ValueError as ex:
40414041
warning("KerberosSSP: %s (bad KEY?)" % ex)
4042-
now_time = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc)
4042+
now_time = datetime.now(timezone.utc).replace(microsecond=0)
40434043
err = KRB_GSSAPI_Token(
40444044
innerToken=KRB_InnerToken(
40454045
TOK_ID=b"\x03\x00",

scapy/modules/ticketer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ def create_ticket(self, **kwargs):
645645
duration = kwargs.get(
646646
"duration", int(self._prompt("Expires in (h) [10]: ") or "10")
647647
)
648-
now_time = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc)
648+
now_time = datetime.now(timezone.utc).replace(microsecond=0)
649649
rand = random.SystemRandom()
650650
key = Key.random_to_key(
651651
EncryptionType.AES256_CTS_HMAC_SHA1_96, rand.randbytes(32)

test/regression.uts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5136,10 +5136,10 @@ assert pl[1][Ether].dst == '00:22:33:44:55:66'
51365136
= _version()
51375137

51385138
import os
5139-
from datetime import datetime
5139+
from datetime import datetime, timezone
51405140

51415141
version_filename = os.path.join(scapy._SCAPY_PKG_DIR, "VERSION")
5142-
mtime = datetime.utcfromtimestamp(os.path.getmtime(scapy.__file__))
5142+
mtime = datetime.fromtimestamp(os.path.getmtime(scapy.__file__), timezone.utc)
51435143
version = "2.0.0"
51445144
with open(version_filename, "w") as fd:
51455145
fd.write(version)

test/scapy/layers/kerberos.uts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ def fake_choice(x):
12661266
return x[0]
12671267

12681268
date_mock = mock.MagicMock()
1269-
date_mock.utcnow.return_value = datetime(2024, 3, 5, 16, 52, 55, 424801)
1269+
date_mock.now.side_effect = lambda tz=None: datetime(2024, 3, 5, 16, 52, 55, 424801, tz)
12701270

12711271
_patches = [
12721272
# Patch all the random

0 commit comments

Comments
 (0)