Skip to content

Commit

Permalink
Fix nbios.NCBStruct packing
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Oct 15, 2024
1 parent c717bfa commit 88d35b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions win32/Lib/netbios.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

import struct
from collections.abc import Iterable

import win32wnet

Expand Down Expand Up @@ -191,28 +194,26 @@ 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))

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_)
Expand Down

0 comments on commit 88d35b7

Please sign in to comment.