From 8891f9cea297ab73f68d287e806de68edf589d22 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Tue, 30 Apr 2024 11:26:24 +0200 Subject: [PATCH] Replace deprecated datetime.utcnow() with datetime.now(datetime.UTC) datetime.utcnow is deprecated since Python 3.12 --- picard/script/serializer.py | 4 ++-- setup.py | 4 ++-- test/test_script_serializer.py | 26 ++++++++++---------------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/picard/script/serializer.py b/picard/script/serializer.py index 4f5d85544a..65763bfb97 100644 --- a/picard/script/serializer.py +++ b/picard/script/serializer.py @@ -3,7 +3,7 @@ # Picard, the next-generation MusicBrainz tagger # # Copyright (C) 2021, 2023 Bob Swift -# Copyright (C) 2021-2023 Philipp Wolfer +# Copyright (C) 2021-2024 Philipp Wolfer # Copyright (C) 2021-2024 Laurent Monin # # This program is free software; you can redistribute it and/or @@ -152,7 +152,7 @@ def make_last_updated(): Returns: str: Last updated string from current date and time """ - return datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC') + return datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC') def update_last_updated(self): """Update the last updated attribute to the current UTC date and time. diff --git a/setup.py b/setup.py index 5226e60e01..66897e148f 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ # Copyright (C) 2006-2008, 2011-2014, 2017 Lukáš Lalinský # Copyright (C) 2007 Santiago M. Mola # Copyright (C) 2008 Robert Kaye -# Copyright (C) 2008-2009, 2018-2022 Philipp Wolfer +# Copyright (C) 2008-2009, 2018-2024 Philipp Wolfer # Copyright (C) 2009 Carlin Mangar # Copyright (C) 2011-2012, 2014, 2016-2018 Wieland Hoffmann # Copyright (C) 2011-2014 Michael Wiencek @@ -716,7 +716,7 @@ def patch_version(self, filename): regex = re.compile(r'^PICARD_BUILD_VERSION_STR\s*=.*$', re.MULTILINE) with open(filename, 'r+b') as f: source = (f.read()).decode() - build = self.platform + '.' + datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S') + build = self.platform + '.' + datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%d%H%M%S') patched_source = regex.sub('PICARD_BUILD_VERSION_STR = "%s"' % build, source).encode() f.seek(0) f.write(patched_source) diff --git a/test/test_script_serializer.py b/test/test_script_serializer.py index 9e5c286529..943c06bf32 100644 --- a/test/test_script_serializer.py +++ b/test/test_script_serializer.py @@ -3,7 +3,7 @@ # Picard, the next-generation MusicBrainz tagger # # Copyright (C) 2021 Bob Swift -# Copyright (C) 2021 Philipp Wolfer +# Copyright (C) 2021, 2024 Philipp Wolfer # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -21,6 +21,7 @@ import datetime +from unittest.mock import patch import yaml @@ -33,26 +34,17 @@ ) -class _DateTime(datetime.datetime): +class MockDateTime(datetime.datetime): @classmethod - def utcnow(cls): - return cls(year=2020, month=1, day=2, hour=12, minute=34, second=56, microsecond=789, tzinfo=None) + def now(cls, tz=None): + if tz == datetime.timezone.utc: + return cls(year=2020, month=1, day=2, hour=12, minute=34, second=56, microsecond=789, tzinfo=None) + else: + raise Exception("Unexpected parameter tz=%r" % tz) class PicardScriptTest(PicardTestCase): - original_datetime = datetime.datetime - - def setUp(self): - super().setUp() - # Save original datetime object and substitute one returning - # a fixed utcnow() value for testing. - datetime.datetime = _DateTime - - def tearDown(self): - # Restore original datetime object - datetime.datetime = self.original_datetime - def assertYamlEquals(self, yaml_str, obj, msg=None): self.assertEqual(obj, yaml.safe_load(yaml_str), msg) @@ -88,6 +80,7 @@ def test_script_object_3(self): self.assertEqual(test_script.last_updated, '2021-04-26') self.assertEqual(test_script['last_updated'], '2021-04-26') + @patch('datetime.datetime', MockDateTime) def test_script_object_4(self): # Check updating values that modify `last_updated`. test_script = PicardScript(title='Script 1', script='Script text', id='12345', last_updated='2021-04-26') @@ -97,6 +90,7 @@ def test_script_object_4(self): self.assertEqual(test_script.last_updated, '2020-01-02 12:34:56 UTC') self.assertEqual(test_script['last_updated'], '2020-01-02 12:34:56 UTC') + @patch('datetime.datetime', MockDateTime) def test_script_object_5(self): # Check updating values from dict that modify `last_updated`. test_script = PicardScript(title='Script 1', script='Script text', id='12345', last_updated='2021-04-26')