Skip to content

Commit a6ca37a

Browse files
authored
Fix win32timezone.TimeZoneInfo.__init_from_other and added tests (#2339)
1 parent dbd0843 commit a6ca37a

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

CHANGES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ https://mhammond.github.io/pywin32_installers.html.
1414
Coming in build 309, as yet unreleased
1515
--------------------------------------
1616

17-
* Add runtime deprecation warning of `win2kras`, use `win32ras` instead (#2356, @Avasam)
17+
* Fixed `win32timezone.TimeZoneInfo` initialization from a `[DYNAMIC_]TIME_ZONE_INFORMATION` (#2339, @Avasam)
18+
* Added runtime deprecation warning of `win2kras`, use `win32ras` instead (#2356, @Avasam)
1819
* Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2332, #2330, @Avasam)
1920
* Fixed "Open GL Demo" (`Pythonwin/pywin/Demos/openGLDemo.py`) and restore "Font" demo in `Pythonwin/pywin/Demos/guidemo.py` (#2345, @Avasam)
2021
* Fixed accidentally trying to raise an undefined name instead of an `Exception` in `Pythonwin/pywin/debugger/debugger.py` (#2326, @Avasam)

win32/Lib/win32timezone.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ class TIME_ZONE_INFORMATION(_SimpleStruct):
316316
]
317317

318318

319-
class DYNAMIC_TIME_ZONE_INFORMATION(_SimpleStruct):
319+
class DYNAMIC_TIME_ZONE_INFORMATION(TIME_ZONE_INFORMATION):
320320
_fields_ = TIME_ZONE_INFORMATION._fields_ + [
321321
("key_name", str),
322322
("dynamic_daylight_time_disabled", bool),
@@ -334,10 +334,25 @@ class TimeZoneDefinition(DYNAMIC_TIME_ZONE_INFORMATION):
334334

335335
def __init__(self, *args, **kwargs):
336336
"""
337+
>>> test_args = [1] * 44
338+
337339
Try to construct a TimeZoneDefinition from
340+
338341
a) [DYNAMIC_]TIME_ZONE_INFORMATION args
339-
b) another TimeZoneDefinition
342+
>>> TimeZoneDefinition(*test_args).bias
343+
datetime.timedelta(seconds=60)
344+
345+
b) another TimeZoneDefinition or [DYNAMIC_]TIME_ZONE_INFORMATION
346+
>>> TimeZoneDefinition(TimeZoneDefinition(*test_args)).bias
347+
datetime.timedelta(seconds=60)
348+
>>> TimeZoneDefinition(DYNAMIC_TIME_ZONE_INFORMATION(*test_args)).bias
349+
datetime.timedelta(seconds=60)
350+
>>> TimeZoneDefinition(TIME_ZONE_INFORMATION(*test_args)).bias
351+
datetime.timedelta(seconds=60)
352+
340353
c) a byte structure (using _from_bytes)
354+
>>> TimeZoneDefinition(bytes(test_args)).bias
355+
datetime.timedelta(days=11696, seconds=46140)
341356
"""
342357
try:
343358
super().__init__(*args, **kwargs)
@@ -357,7 +372,7 @@ def __init__(self, *args, **kwargs):
357372
except TypeError:
358373
pass
359374

360-
raise TypeError("Invalid arguments for %s" % self.__class__)
375+
raise TypeError(f"Invalid arguments for {self.__class__}")
361376

362377
def __init_from_bytes(
363378
self,
@@ -389,7 +404,7 @@ def __init_from_other(self, other):
389404
raise TypeError("Not a TIME_ZONE_INFORMATION")
390405
for name in other.field_names():
391406
# explicitly get the value from the underlying structure
392-
value = super(TimeZoneDefinition, other).__getattribute__(other, name)
407+
value = super(TIME_ZONE_INFORMATION, other).__getattribute__(name)
393408
setattr(self, name, value)
394409
# consider instead of the loop above just copying the memory directly
395410
# size = max(ctypes.sizeof(DYNAMIC_TIME_ZONE_INFO), ctypes.sizeof(other))

0 commit comments

Comments
 (0)