|
| 1 | +""" |
| 2 | +A pure-python re-implementation of methods used by win32verstamp. |
| 3 | +This is to avoid a bootstraping problem where win32verstamp is used during build, |
| 4 | +but requires an installation of pywin32 to be present. |
| 5 | +We used to work around this by ignoring failure to verstamp, but that's easy to miss. |
| 6 | +
|
| 7 | +Implementations adapted, simplified and typed from: |
| 8 | +- https://github.com/enthought/pywin32-ctypes/blob/main/win32ctypes/core/ctypes/_util.py |
| 9 | +- https://github.com/enthought/pywin32-ctypes/blob/main/win32ctypes/core/cffi/_resource.py |
| 10 | +- https://github.com/enthought/pywin32-ctypes/blob/main/win32ctypes/pywin32/win32api.py |
| 11 | +
|
| 12 | +--- |
| 13 | +
|
| 14 | +(C) Copyright 2014 Enthought, Inc., Austin, TX |
| 15 | +All right reserved. |
| 16 | +
|
| 17 | +This file is open source software distributed according to the terms in |
| 18 | +https://github.com/enthought/pywin32-ctypes/blob/main/LICENSE.txt |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +from collections.abc import Callable, Iterable |
| 24 | +from ctypes import FormatError, WinDLL, _SimpleCData, get_last_error |
| 25 | +from ctypes.wintypes import ( |
| 26 | + BOOL, |
| 27 | + DWORD, |
| 28 | + HANDLE, |
| 29 | + LPCWSTR, |
| 30 | + LPVOID, |
| 31 | + WORD, |
| 32 | +) |
| 33 | +from typing import TYPE_CHECKING, Any |
| 34 | + |
| 35 | +if TYPE_CHECKING: |
| 36 | + from ctypes import _NamedFuncPointer |
| 37 | + |
| 38 | + from _typeshed import ReadableBuffer |
| 39 | + from typing_extensions import Literal, SupportsBytes, SupportsIndex |
| 40 | + |
| 41 | +kernel32 = WinDLL("kernel32", use_last_error=True) |
| 42 | + |
| 43 | +### |
| 44 | +# https://github.com/enthought/pywin32-ctypes/blob/main/win32ctypes/core/ctypes/_util.py |
| 45 | +### |
| 46 | + |
| 47 | + |
| 48 | +def function_factory( |
| 49 | + function: _NamedFuncPointer, |
| 50 | + argument_types: list[type[_SimpleCData[Any]]], |
| 51 | + return_type: type[_SimpleCData[Any]], |
| 52 | + error_checking: Callable[..., Any], # Simplified over errcheck's signature |
| 53 | +) -> _NamedFuncPointer: |
| 54 | + function.argtypes = argument_types |
| 55 | + function.restype = return_type |
| 56 | + function.errcheck = error_checking |
| 57 | + return function |
| 58 | + |
| 59 | + |
| 60 | +def make_error(function: _NamedFuncPointer) -> OSError: |
| 61 | + code = get_last_error() |
| 62 | + description = FormatError(code).strip() |
| 63 | + function_name = function.__name__ |
| 64 | + exception = OSError() |
| 65 | + exception.winerror = code |
| 66 | + exception.function = function_name |
| 67 | + exception.strerror = description |
| 68 | + return exception |
| 69 | + |
| 70 | + |
| 71 | +def check_null(result: int | None, function: _NamedFuncPointer, *_) -> int: |
| 72 | + if result is None: |
| 73 | + raise make_error(function) |
| 74 | + return result |
| 75 | + |
| 76 | + |
| 77 | +def check_false(result: int | None, function: _NamedFuncPointer, *_) -> Literal[True]: |
| 78 | + if not bool(result): |
| 79 | + raise make_error(function) |
| 80 | + else: |
| 81 | + return True |
| 82 | + |
| 83 | + |
| 84 | +### |
| 85 | +# https://github.com/enthought/pywin32-ctypes/blob/main/win32ctypes/core/cffi/_resource.py |
| 86 | +### |
| 87 | + |
| 88 | + |
| 89 | +def _UpdateResource( |
| 90 | + hUpdate: int, |
| 91 | + lpType: str | int, |
| 92 | + lpName: str | int, |
| 93 | + wLanguage: int, |
| 94 | + lpData: bytes, |
| 95 | + cbData: int, |
| 96 | +): |
| 97 | + lp_type = LPCWSTR(lpType) |
| 98 | + lp_name = LPCWSTR(lpName) |
| 99 | + _BaseUpdateResource(hUpdate, lp_type, lp_name, wLanguage, lpData, cbData) |
| 100 | + |
| 101 | + |
| 102 | +_BeginUpdateResource = function_factory( |
| 103 | + kernel32.BeginUpdateResourceW, |
| 104 | + [LPCWSTR, BOOL], |
| 105 | + HANDLE, |
| 106 | + check_null, |
| 107 | +) |
| 108 | + |
| 109 | + |
| 110 | +_EndUpdateResource = function_factory( |
| 111 | + kernel32.EndUpdateResourceW, |
| 112 | + [HANDLE, BOOL], |
| 113 | + BOOL, |
| 114 | + check_false, |
| 115 | +) |
| 116 | + |
| 117 | +_BaseUpdateResource = function_factory( |
| 118 | + kernel32.UpdateResourceW, |
| 119 | + [HANDLE, LPCWSTR, LPCWSTR, WORD, LPVOID, DWORD], |
| 120 | + BOOL, |
| 121 | + check_false, |
| 122 | +) |
| 123 | + |
| 124 | + |
| 125 | +### |
| 126 | +# https://github.com/enthought/pywin32-ctypes/blob/main/win32ctypes/pywin32/win32api.py |
| 127 | +### |
| 128 | + |
| 129 | +LANG_NEUTRAL = 0x00 |
| 130 | + |
| 131 | + |
| 132 | +def BeginUpdateResource(filename: str, delete: bool): |
| 133 | + """Get a handle that can be used by the :func:`UpdateResource`. |
| 134 | +
|
| 135 | + Parameters |
| 136 | + ---------- |
| 137 | + fileName : unicode |
| 138 | + The filename of the module to load. |
| 139 | + delete : bool |
| 140 | + When true all existing resources are deleted |
| 141 | +
|
| 142 | + Returns |
| 143 | + ------- |
| 144 | + result : hModule |
| 145 | + Handle of the resource. |
| 146 | +
|
| 147 | + """ |
| 148 | + return _BeginUpdateResource(filename, delete) |
| 149 | + |
| 150 | + |
| 151 | +def EndUpdateResource(handle: int, discard: bool) -> None: |
| 152 | + """End the update resource of the handle. |
| 153 | +
|
| 154 | + Parameters |
| 155 | + ---------- |
| 156 | + handle : hModule |
| 157 | + The handle of the resource as it is returned |
| 158 | + by :func:`BeginUpdateResource` |
| 159 | +
|
| 160 | + discard : bool |
| 161 | + When True all writes are discarded. |
| 162 | +
|
| 163 | + """ |
| 164 | + _EndUpdateResource(handle, discard) |
| 165 | + |
| 166 | + |
| 167 | +def UpdateResource( |
| 168 | + handle: int, |
| 169 | + type: str | int, |
| 170 | + name: str | int, |
| 171 | + data: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer, |
| 172 | + language=LANG_NEUTRAL, |
| 173 | +) -> None: |
| 174 | + """Update a resource. |
| 175 | +
|
| 176 | + Parameters |
| 177 | + ---------- |
| 178 | + handle : hModule |
| 179 | + The handle of the resource file as returned by |
| 180 | + :func:`BeginUpdateResource`. |
| 181 | +
|
| 182 | + type : str : int |
| 183 | + The type of resource to update. |
| 184 | +
|
| 185 | + name : str : int |
| 186 | + The name or Id of the resource to update. |
| 187 | +
|
| 188 | + data : bytes |
| 189 | + A bytes like object is expected. |
| 190 | +
|
| 191 | + .. note:: |
| 192 | + PyWin32 version 219, on Python 2.7, can handle unicode inputs. |
| 193 | + However, the data are stored as bytes and it is not really |
| 194 | + possible to convert the information back into the original |
| 195 | + unicode string. To be consistent with the Python 3 behaviour |
| 196 | + of PyWin32, we raise an error if the input cannot be |
| 197 | + converted to `bytes`. |
| 198 | +
|
| 199 | + language : int |
| 200 | + Language to use, default is LANG_NEUTRAL. |
| 201 | +
|
| 202 | + """ |
| 203 | + try: |
| 204 | + lp_data = bytes(data) |
| 205 | + except UnicodeEncodeError: |
| 206 | + raise TypeError("a bytes-like object is required, not a 'unicode'") |
| 207 | + _UpdateResource(handle, type, name, language, lp_data, len(lp_data)) |
0 commit comments