From 43c211f1a7cb2870e9f2a4a5a6584b499b8c66b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 6 Dec 2022 18:44:46 +0100 Subject: [PATCH] extend and rename util.CustomNone --- gallery_dl/util.py | 24 +++++++++++++++++++++--- test/test_util.py | 11 +++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 8ce1fb40..23d5bc8e 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -528,8 +528,8 @@ def parse_inputfile(file, log): yield line -class UniversalNone(): - """None-style object that supports more operations than None itself""" +class CustomNone(): + """None-style type that supports more operations than regular None""" __slots__ = () def __getattribute__(self, _): @@ -538,10 +538,28 @@ class UniversalNone(): def __getitem__(self, _): return self + def __iter__(self): + return self + + def __call__(self, *args, **kwargs): + return self + + @staticmethod + def __next__(): + raise StopIteration + @staticmethod def __bool__(): return False + @staticmethod + def __len__(): + return 0 + + @staticmethod + def __format__(_): + return "None" + @staticmethod def __str__(): return "None" @@ -549,7 +567,7 @@ class UniversalNone(): __repr__ = __str__ -NONE = UniversalNone() +NONE = CustomNone() EPOCH = datetime.datetime(1970, 1, 1) SECOND = datetime.timedelta(0, 1) WINDOWS = (os.name == "nt") diff --git a/test/test_util.py b/test/test_util.py index 2921ea23..4b8f9ae4 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -618,10 +618,21 @@ class TestOther(unittest.TestCase): obj = util.NONE self.assertFalse(obj) + self.assertEqual(len(obj), 0) self.assertEqual(str(obj), str(None)) self.assertEqual(repr(obj), repr(None)) + self.assertEqual(format(obj), str(None)) + self.assertEqual(format(obj, "%F"), str(None)) self.assertIs(obj.attr, obj) self.assertIs(obj["key"], obj) + self.assertIs(obj(), obj) + self.assertIs(obj(1, "a"), obj) + self.assertIs(obj(foo="bar"), obj) + + i = 0 + for _ in obj: + i += 1 + self.assertEqual(i, 0) class TestExtractor():