Skip to content

Commit 733d865

Browse files
committed
Stop using Py_FrozenFlag
1 parent d764900 commit 733d865

File tree

7 files changed

+44
-30
lines changed

7 files changed

+44
-30
lines changed

Pythonwin/dllmain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ extern "C" __declspec(dllexport) int __stdcall DllMainwin32ui(HINSTANCE hInstanc
191191
// insert into resource chain.
192192
pDLL = new CDynLinkLibrary(extensionDLL);
193193

194-
#else // Frozen .EXE that embedds win32ui is initializing
194+
#else // Frozen .EXE that embeds win32ui is initializing
195195
TRACE("win32ui in frozen %s initializing.\n", path);
196196
#endif
197197
}

com/pythoncom.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,23 @@
22
import pywintypes
33

44
pywintypes.__import_pywin32_system_module__("pythoncom", globals())
5+
6+
7+
def __getattr__(name: str) -> "bool | str":
8+
if name == "frozen":
9+
import sys
10+
import warnings
11+
12+
warnings.warn(
13+
DeprecationWarning(
14+
f"`pythoncom.frozen` used to expose `Py_FrozenFlag` from the C API. "
15+
+ "`Py_FrozenFlag` is deprecated since Python 3.12. "
16+
+ "Ever since pywin32 b200, loading the `win32com` has silently been "
17+
+ "replacing `pythoncom.frozen` with `sys.frozen`. "
18+
+ 'Use `getattr(sys, "frozen", False)` directly instead.'
19+
),
20+
stacklevel=2,
21+
)
22+
return getattr(sys, "frozen", False)
23+
else:
24+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

com/win32com/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
import pythoncom
1010
import win32api
1111

12-
# flag if we are in a "frozen" build.
13-
_frozen = getattr(sys, "frozen", False)
14-
# pythoncom dumbly defaults this to zero - we believe sys.frozen over it.
15-
if _frozen and not getattr(pythoncom, "frozen", 0):
16-
pythoncom.frozen = sys.frozen
12+
__frozen: str | bool = getattr(sys, "frozen", False)
1713

1814
# Add support for an external "COM Extensions" path.
1915
# Concept is that you can register a seperate path to be used for
@@ -86,11 +82,11 @@ def SetupEnvironment():
8682
# (which the win32com developers do!)
8783
def __PackageSupportBuildPath__(package_path):
8884
# See if we have a special directory for the binaries (for developers)
89-
if not _frozen and __build_path__:
85+
if not __frozen and __build_path__:
9086
package_path.append(__build_path__)
9187

9288

93-
if not _frozen:
89+
if not __frozen:
9490
SetupEnvironment()
9591

9692
# If we don't have a special __gen_path__, see if we have a gen_py as a

com/win32com/server/register.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
88
"""
99

10+
from __future__ import annotations
11+
1012
import os
1113
import sys
1214

@@ -17,6 +19,8 @@
1719

1820
CATID_PythonCOMServer = "{B3EF80D0-68E2-11D0-A689-00C04FD658FF}"
1921

22+
__frozen: str | bool = getattr(sys, "frozen", False)
23+
2024

2125
def _set_subkeys(keyName, valueDict, base=win32con.HKEY_CLASSES_ROOT):
2226
hkey = win32api.RegCreateKey(base, keyName)
@@ -207,13 +211,9 @@ def RegisterServer(
207211
# Set default clsctx.
208212
if not clsctx:
209213
clsctx = pythoncom.CLSCTX_INPROC_SERVER | pythoncom.CLSCTX_LOCAL_SERVER
210-
# And if we are frozen, ignore the ones that don't make sense in this
211-
# context.
212-
if pythoncom.frozen:
213-
assert sys.frozen, (
214-
"pythoncom is frozen, but sys.frozen is not set - don't know the context!"
215-
)
216-
if sys.frozen == "dll":
214+
# And if we are frozen, ignore the ones that don't make sense in this context.
215+
if __frozen:
216+
if __frozen == "dll":
217217
clsctx &= pythoncom.CLSCTX_INPROC_SERVER
218218
else:
219219
clsctx &= pythoncom.CLSCTX_LOCAL_SERVER
@@ -223,7 +223,7 @@ def RegisterServer(
223223
# nod to Gordon's installer - if sys.frozen and sys.frozendllhandle
224224
# exist, then we are being registered via a DLL - use this DLL as the
225225
# file name.
226-
if pythoncom.frozen:
226+
if __frozen:
227227
if hasattr(sys, "frozendllhandle"):
228228
dllName = win32api.GetModuleFileName(sys.frozendllhandle)
229229
else:
@@ -261,9 +261,8 @@ def RegisterServer(
261261
_remove_key(keyNameRoot + "\\InprocServer32")
262262

263263
if clsctx & pythoncom.CLSCTX_LOCAL_SERVER:
264-
if pythoncom.frozen:
265-
# If we are frozen, we write "{exe} /Automate", just
266-
# like "normal" .EXEs do
264+
if __frozen:
265+
# If we are frozen, we write "{exe} /Automate", just like "normal" .EXEs do
267266
exeName = win32api.GetShortPathName(sys.executable)
268267
command = f"{exeName} /Automate"
269268
else:
@@ -302,7 +301,7 @@ def RegisterServer(
302301
_remove_key(keyNameRoot + "\\PythonCOMPath")
303302

304303
if addPyComCat is None:
305-
addPyComCat = pythoncom.frozen == 0
304+
addPyComCat = __frozen == False
306305
if addPyComCat:
307306
catids = catids + [CATID_PythonCOMServer]
308307

@@ -422,7 +421,7 @@ def RegisterClasses(*classes, **flags):
422421
clsctx = _get(cls, "_reg_clsctx_")
423422
tlb_filename = _get(cls, "_reg_typelib_filename_")
424423
# default to being a COM category only when not frozen.
425-
addPyComCat = not _get(cls, "_reg_disable_pycomcat_", pythoncom.frozen != 0)
424+
addPyComCat = not _get(cls, "_reg_disable_pycomcat_", __frozen)
426425
addnPath = None
427426
if debugging:
428427
# If the class has a debugging dispatcher specified, use it, otherwise
@@ -455,7 +454,7 @@ def RegisterClasses(*classes, **flags):
455454

456455
spec = moduleName + "." + cls.__name__
457456
# Frozen apps don't need their directory on sys.path
458-
if not pythoncom.frozen:
457+
if not __frozen:
459458
scriptDir = os.path.split(sys.argv[0])[0]
460459
if not scriptDir:
461460
scriptDir = "."
@@ -664,7 +663,7 @@ def RegisterPyComCategory():
664663
regCat.RegisterCategories([(CATID_PythonCOMServer, 0x0409, "Python COM Server")])
665664

666665

667-
if not pythoncom.frozen:
666+
if not __frozen:
668667
try:
669668
win32api.RegQueryValue(
670669
win32con.HKEY_CLASSES_ROOT,

com/win32com/src/PythonCOM.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,9 +2637,6 @@ PYWIN_MODULE_INIT_FUNC(pythoncom)
26372637

26382638
ADD_CONSTANT(DESCKIND_FUNCDESC);
26392639
ADD_CONSTANT(DESCKIND_VARDESC);
2640-
// Expose the frozen flag, as Python itself doesn't!!
2641-
// @prop int|frozen|1 if the host is a frozen program, else 0
2642-
AddConstant(dict, "frozen", Py_FrozenFlag);
26432640

26442641
// And finally some gross hacks relating to DCOM
26452642
// I'm really not sure what a better option is!
@@ -2678,7 +2675,10 @@ PYWIN_MODULE_INIT_FUNC(pythoncom)
26782675
// @prop int|dcom|1 if the system is DCOM aware, else 0. Only Win95 without DCOM extensions should return 0
26792676

26802677
// ### ALL THE @PROPERTY TAGS MUST COME AFTER THE LAST @PROP TAG!!
2681-
// @property int|pythoncom|frozen|1 if the host is a frozen program, else 0
2678+
// @property int|pythoncom|frozen|`pythoncom.frozen` used to expose `Py_FrozenFlag` from the C API.
2679+
// `Py_FrozenFlag` is deprecated since Python 3.12.
2680+
// Ever since pywin32 b200, loading the `win32com` has silently been replacing `pythoncom.frozen` with `sys.frozen`.
2681+
// Use `getattr(sys, "frozen", False)` directly instead.
26822682
// @property int|pythoncom|dcom|1 if the system is DCOM aware, else 0. Only Win95 without DCOM extensions should
26832683
// return 0
26842684

com/win32comext/mapi/src/exchangeguids.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#define USES_IID_IExchangeFolderACLs
44

55
#ifndef BUILD_FREEZE
6-
/* In a frozen environemt, these are likely to be picked
7-
up by the MAPI module */
6+
// In a frozen environemnt, these are likely to be picked up by the MAPI module
87

98
#define USES_IID_IMsgStore
109
#define USES_IID_IMAPISession

pycln.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ skip_imports = [
66
"IDLEenvironment", # Injects fast_readline into the IDLE auto-indent extension
77
"pythoncom", # pythoncomXX.dll loader
88
"pywintypes", # pywintypesXX.dll loader
9-
"win32com", # Sets pythoncom.frozen and adds modules to path based on the registry
9+
"win32com", # Adds modules to path based on the registry
1010
"win32traceutil", # Redirects output to win32trace remote collector
1111
"win32ui", # Must always be imported before dde
1212
]

0 commit comments

Comments
 (0)