From 88d35b7283181b3ae71a9d2550cfb7dfafcf74a9 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 15 Oct 2024 13:07:14 -0400 Subject: [PATCH] Fix nbios.NCBStruct packing --- CHANGES.txt | 1 + win32/Lib/netbios.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index a88445e9b..24778c956 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -14,6 +14,7 @@ https://mhammond.github.io/pywin32_installers.html. Coming in build 309, as yet unreleased -------------------------------------- +* Fixed `nbios.NCBStruct` packing (#2406, @Avasam) * Fail sooner on invalid `win32timezone.TimeZoneInfo` creation (#2338, @Avasam) * Removed temporary `win32com.server.policy` reexports hack (#2344, @Avasam) Import `DispatcherWin32trace` and `DispatcherTrace` from `win32com.server.dispatcher` instead. diff --git a/win32/Lib/netbios.py b/win32/Lib/netbios.py index 3da1291c9..7503db642 100644 --- a/win32/Lib/netbios.py +++ b/win32/Lib/netbios.py @@ -1,4 +1,7 @@ +from __future__ import annotations + import struct +from collections.abc import Iterable import win32wnet @@ -191,7 +194,7 @@ def Netbios(ncb): class NCBStruct: - def __init__(self, items): + def __init__(self, items: Iterable[tuple[str, str]]) -> None: self._format = "".join([item[0] for item in items]) self._items = items self._buffer_ = win32wnet.NCBBuffer(struct.calcsize(self._format)) @@ -199,20 +202,18 @@ def __init__(self, items): for format, name in self._items: if len(format) == 1: if format == "c": - val = "\0" + val: bytes | int = b"\0" else: val = 0 else: l = int(format[:-1]) - val = "\0" * l + val = b"\0" * l self.__dict__[name] = val def _pack(self): - vals = [] - for format, name in self._items: - vals.append(self.__dict__.get(name)) + vals = [self.__dict__.get(name) for format, name in self._items] - self._buffer_[:] = struct.pack(*(self._format,) + tuple(vals)) + self._buffer_[:] = struct.pack(self._format, *vals) def _unpack(self): items = struct.unpack(self._format, self._buffer_)