Skip to content

Commit 781147e

Browse files
committed
Replace cdll with CDLL and windll with WinDLL
As noted in [this comment](python/cpython#125788 (comment)), it is preferable to use `ctypes.WinDLL` instead of `ctypes.windll`. This approach avoids modifying shared DLL objects and ensures better isolation.
1 parent 1676644 commit 781147e

10 files changed

Lines changed: 20 additions & 20 deletions

File tree

find_system_fonts_filename/android/android.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ctypes import c_char_p, c_void_p, cdll, util
1+
from ctypes import c_char_p, c_void_p, CDLL, util
22
from ..exceptions import AndroidLibraryNotFound, OSNotSupported
33

44
__all__ = ["Android"]
@@ -12,7 +12,7 @@ def __init__(self) -> None:
1212
if android_library_name is None:
1313
raise AndroidLibraryNotFound("You need to have the libandroid library. It is only available since the SDK/API level 29.")
1414

15-
android = cdll.LoadLibrary(android_library_name)
15+
android = CDLL(android_library_name)
1616

1717
try:
1818
# https://developer.android.com/ndk/reference/group/font#asystemfontiterator_open

find_system_fonts_filename/mac/core_foundation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .types import CFIndex, CFNumberRef, CFStringEncoding
2-
from ctypes import c_bool, c_char_p, c_void_p, cdll, util
2+
from ctypes import c_bool, c_char_p, c_void_p, CDLL, util
33
from enum import IntEnum
44

55
__all__ = [
@@ -63,7 +63,7 @@ def __init__(self) -> None:
6363
# From: https://github.com/pyglet/pyglet/blob/a44e83a265e7df8ece793de865bcf3690f66adbd/pyglet/libs/darwin/cocoapy/cocoalibs.py#L10-L14
6464
if core_foundation_library_name is None:
6565
core_foundation_library_name = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
66-
core_foundation = cdll.LoadLibrary(core_foundation_library_name)
66+
core_foundation = CDLL(core_foundation_library_name)
6767

6868

6969
# https://developer.apple.com/documentation/corefoundation/1521153-cfrelease

find_system_fonts_filename/mac/core_text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .types import CFIndex
2-
from ctypes import c_bool, c_uint32, c_void_p, cdll, util
2+
from ctypes import c_bool, c_uint32, c_void_p, CDLL, util
33
from enum import IntEnum
44

55
__all__ = [
@@ -35,7 +35,7 @@ def __init__(self) -> None:
3535
# From: https://github.com/pyglet/pyglet/blob/a44e83a265e7df8ece793de865bcf3690f66adbd/pyglet/libs/darwin/cocoapy/cocoalibs.py#L520-L524
3636
if core_text_library_name is None:
3737
core_text_library_name = "/System/Library/Frameworks/CoreText.framework/CoreText"
38-
core_text = cdll.LoadLibrary(core_text_library_name)
38+
core_text = CDLL(core_text_library_name)
3939

4040
self.kCTFontURLAttribute = c_void_p.in_dll(core_text, "kCTFontURLAttribute")
4141
self.kCTFontFormatAttribute = c_void_p.in_dll(core_text, "kCTFontFormatAttribute")

find_system_fonts_filename/unix/fontconfig.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ctypes import c_char_p, c_int, c_void_p, cdll, POINTER, Structure, util
1+
from ctypes import c_char_p, c_int, c_void_p, CDLL, POINTER, Structure, util
22
from enum import Enum, IntEnum
33
from ..exceptions import FontConfigNotFound
44

@@ -50,7 +50,7 @@ def __init__(self) -> None:
5050
if font_config_library_name is None:
5151
raise FontConfigNotFound("You need to install FontConfig to get the fonts filename")
5252

53-
font_config = cdll.LoadLibrary(font_config_library_name)
53+
font_config = CDLL(font_config_library_name)
5454

5555
self.FC_FONTFORMAT = FontConfig.string_to_cstring("fontformat")
5656
self.FC_FILE = FontConfig.string_to_cstring("file")

find_system_fonts_filename/windows/advapi32.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ctypes import windll, wintypes
1+
from ctypes import WinDLL, wintypes
22
from enum import Enum
33
from ..exceptions import SystemApiError
44

@@ -26,7 +26,7 @@ class RegistryDataType(Enum):
2626

2727
class Advapi32():
2828
def __init__(self) -> None:
29-
advapi32 = windll.LoadLibrary("advapi32")
29+
advapi32 = WinDLL("advapi32")
3030

3131
self.HKEY_CURRENT_USER = wintypes.HKEY(0x80000001)
3232
self.KEY_SET_VALUE = 0x0002

find_system_fonts_filename/windows/dwrite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .gdi32 import LOGFONTW
22
from comtypes import GUID, HRESULT, IID, IUnknown, STDMETHOD
3-
from ctypes import POINTER, windll, wintypes
3+
from ctypes import POINTER, WinDLL, wintypes
44
from enum import IntEnum, IntFlag
55

66
__all__ = [
@@ -342,7 +342,7 @@ class IDWriteFactory3(IDWriteFactory2):
342342

343343
class DWrite:
344344
def __init__(self) -> None:
345-
dwrite = windll.LoadLibrary("dwrite")
345+
dwrite = WinDLL("dwrite")
346346

347347
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nf-dwrite-dwritecreatefactory
348348
self.DWriteCreateFactory = dwrite.DWriteCreateFactory

find_system_fonts_filename/windows/gdi32.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ctypes import POINTER, WINFUNCTYPE, c_ubyte, Structure, windll, wintypes
1+
from ctypes import POINTER, WINFUNCTYPE, c_ubyte, Structure, WinDLL, wintypes
22
from ..exceptions import SystemApiError
33

44
__all__ = ["GDI32"]
@@ -62,7 +62,7 @@ class ENUMLOGFONTEXW(Structure):
6262

6363
class GDI32:
6464
def __init__(self) -> None:
65-
gdi = windll.LoadLibrary("gdi32")
65+
gdi = WinDLL("gdi32")
6666

6767
self.LF_FACESIZE = 32
6868
self.RASTER_FONTTYPE = 0x0001

find_system_fonts_filename/windows/kernel32.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from ctypes import windll, wintypes
1+
from ctypes import WinDLL, wintypes
22
from ..exceptions import SystemApiError
33

44
__all__ = ["Kernel32"]
55

66

77
class Kernel32():
88
def __init__(self) -> None:
9-
kernel32 = windll.LoadLibrary("kernel32")
9+
kernel32 = WinDLL("kernel32")
1010

1111
self.LOCALE_NAME_MAX_LENGTH = 85
1212

find_system_fonts_filename/windows/msvcrt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from ctypes import c_size_t, windll, wintypes
1+
from ctypes import c_size_t, WinDLL, wintypes
22
from ..exceptions import SystemApiError
33

44
__all__ = ["MSVCRT"]
55

66

77
class MSVCRT:
88
def __init__(self) -> None:
9-
msvcrt = windll.LoadLibrary("msvcrt")
9+
msvcrt = WinDLL("msvcrt")
1010

1111
# https://learn.microsoft.com/fr-fr/cpp/c-runtime-library/reference/strncpy-s-strncpy-s-l-wcsncpy-s-wcsncpy-s-l-mbsncpy-s-mbsncpy-s-l?view=msvc-170
1212
self.wcsncpy_s = msvcrt.wcsncpy_s

find_system_fonts_filename/windows/user32.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from ctypes import windll, wintypes
1+
from ctypes import WinDLL, wintypes
22
from ..exceptions import SystemApiError
33

44
__all__ = ["User32"]
55

66

77
class User32():
88
def __init__(self) -> None:
9-
user32 = windll.LoadLibrary("user32")
9+
user32 = WinDLL("user32")
1010

1111
self.HWND_BROADCAST = wintypes.HWND(0xFFFF)
1212
self.WM_FONTCHANGE = wintypes.UINT(0x001D)

0 commit comments

Comments
 (0)