From f7963f32097982f87d5d8ff9f14cf59aa9111215 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 13 Apr 2024 20:01:48 -0400 Subject: [PATCH] Handle __path__ --- .github/workflows/main.yml | 18 +++++++++--------- Pythonwin/pywin/framework/startup.py | 4 ++++ Pythonwin/pywin/scintilla/config.py | 4 ++-- Pythonwin/pywin/test/test_pywin.py | 2 +- com/win32com/__init__.py | 7 ++++--- com/win32com/server/register.py | 2 +- com/win32com/test/GenTestScripts.py | 2 +- com/win32com/test/testAXScript.py | 6 +++--- mypy.ini | 2 +- 9 files changed, 26 insertions(+), 21 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 160e45a372..670afd69db 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,8 +17,8 @@ jobs: strategy: fail-fast: false matrix: - python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12-dev'] - architecture: ['x64', 'x86'] + python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12-dev"] + architecture: ["x64", "x86"] steps: - uses: actions/checkout@v3 @@ -64,7 +64,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ['3.10', '3.11', '3.12-dev'] + python-version: ["3.10", "3.11", "3.12-dev"] steps: - uses: actions/checkout@v3 @@ -73,7 +73,7 @@ jobs: uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - architecture: 'x64' + architecture: "x64" - name: Setup Environment run: | @@ -103,12 +103,12 @@ jobs: - uses: actions/setup-python@v4 with: # This job only needs to target the oldest supported version (black@stable supports Python >=3.8) - python-version: '3.8' + python-version: "3.8" - run: pip install pycln - run: pycln . --config=pycln.toml --check - uses: chartboost/ruff-action@v1 with: - version: '0.3.4' + version: "0.3.4" - uses: psf/black@stable with: options: "--fast --check --diff --verbose" @@ -119,7 +119,7 @@ jobs: fail-fast: false matrix: # mypy 1.5 dropped support for Python 3.7 - python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] steps: - uses: actions/checkout@v3 - uses: actions/setup-python@v4 @@ -133,7 +133,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12'] + python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] steps: - uses: actions/checkout@v3 - uses: actions/setup-python@v4 @@ -144,5 +144,5 @@ jobs: - uses: jakebailey/pyright-action@v2 with: python-version: ${{ matrix.python-version }} - version: '1.1.354' + version: "1.1.358" annotate: errors diff --git a/Pythonwin/pywin/framework/startup.py b/Pythonwin/pywin/framework/startup.py index 5496063e38..d10eecf35a 100644 --- a/Pythonwin/pywin/framework/startup.py +++ b/Pythonwin/pywin/framework/startup.py @@ -43,6 +43,10 @@ import pywin import pywin.framework +# Ensure we're working on __path__ as list, not Iterable +pywin.__path__ = list(pywin.__path__) +pywin.framework.__path__ = list(pywin.framework.__path__) + pywin.__path__[0] = win32ui.FullPath(pywin.__path__[0]) pywin.framework.__path__[0] = win32ui.FullPath(pywin.framework.__path__[0]) diff --git a/Pythonwin/pywin/scintilla/config.py b/Pythonwin/pywin/scintilla/config.py index f6916a7331..8113a0a89c 100644 --- a/Pythonwin/pywin/scintilla/config.py +++ b/Pythonwin/pywin/scintilla/config.py @@ -64,7 +64,7 @@ def get_section_header(line): def find_config_file(f): - return os.path.join(pywin.__path__[0], f + ".cfg") + return os.path.join(next(iter(pywin.__path__)), f + ".cfg") def find_config_files(): @@ -72,7 +72,7 @@ def find_config_files(): os.path.split(x)[1] for x in [ os.path.splitext(x)[0] - for x in glob.glob(os.path.join(pywin.__path__[0], "*.cfg")) + for x in glob.glob(os.path.join(next(iter(pywin.__path__)), "*.cfg")) ] ] diff --git a/Pythonwin/pywin/test/test_pywin.py b/Pythonwin/pywin/test/test_pywin.py index 14b4430e24..3e505235a5 100644 --- a/Pythonwin/pywin/test/test_pywin.py +++ b/Pythonwin/pywin/test/test_pywin.py @@ -26,7 +26,7 @@ user_interaction = getattr(__main__, "user_interaction", False) # from all.py maybe file_abs = os.path.abspath(__file__) src_dir = os.path.dirname(file_abs) -pywin_path = pywin.__path__[0] +pywin_path = next(iter(pywin.__path__)) pythonwinpy_path = os.path.dirname(pywin_path) + "\\start_pythonwin.py" Object = argparse.Namespace _indebugger = "pywin.debugger" in sys.modules diff --git a/com/win32com/__init__.py b/com/win32com/__init__.py index 56d1b815cc..58030580ed 100644 --- a/com/win32com/__init__.py +++ b/com/win32com/__init__.py @@ -1,6 +1,7 @@ # # Initialization for the win32com package # +from __future__ import annotations import os import sys @@ -26,6 +27,8 @@ ### TODO - Load _all_ \\Extensions subkeys - for now, we only read the default ### Modules will work if loaded into "win32comext" path. +# Ensure we're working on __path__ as list, not Iterable +__path__: list[str] = list(__path__) def SetupEnvironment(): HKEY_LOCAL_MACHINE = -2147483646 # Avoid pulling in win32con for just these... @@ -96,9 +99,7 @@ def __PackageSupportBuildPath__(package_path): try: import win32com.gen_py - # hrmph - 3.3 throws: TypeError: '_NamespacePath' object does not support indexing - # attempting to get __path__[0] - but I can't quickly repro this stand-alone. - # Work around it by using an iterator. + # __path__ is only ensured to be an Iterable, not a list. __gen_path__ = next(iter(sys.modules["win32com.gen_py"].__path__)) except ImportError: # If a win32com\gen_py directory already exists, then we use it diff --git a/com/win32com/server/register.py b/com/win32com/server/register.py index d332912df9..ad9482b2bc 100644 --- a/com/win32com/server/register.py +++ b/com/win32com/server/register.py @@ -130,7 +130,7 @@ def _find_localserver_exe(mustfind): def _find_localserver_module(): import win32com.server - path = win32com.server.__path__[0] + path = next(iter(win32com.server.__path__)) baseName = "localserver" pyfile = os.path.join(path, baseName + ".py") try: diff --git a/com/win32com/test/GenTestScripts.py b/com/win32com/test/GenTestScripts.py index 627560fded..ea54014e08 100644 --- a/com/win32com/test/GenTestScripts.py +++ b/com/win32com/test/GenTestScripts.py @@ -19,7 +19,7 @@ def GetGenPath(): import win32api - return os.path.join(win32api.GetFullPathName(win32com.test.__path__[0]), genDir) + return os.path.join(win32api.GetFullPathName(next(iter(win32com.test.__path__))), genDir) def GenerateFromRegistered(fname, *loadArgs): diff --git a/com/win32com/test/testAXScript.py b/com/win32com/test/testAXScript.py index d7a4553ed9..88de4eb87a 100644 --- a/com/win32com/test/testAXScript.py +++ b/com/win32com/test/testAXScript.py @@ -13,7 +13,7 @@ class AXScript(win32com.test.util.TestCase): def setUp(self): file = win32api.GetFullPathName( - os.path.join(win32com.axscript.client.__path__[0], "pyscript.py") + os.path.join(next(iter(win32com.axscript.client.__path__)), "pyscript.py") ) from win32com.test.util import RegisterPythonServer @@ -22,7 +22,7 @@ def setUp(self): def testHost(self): file = win32api.GetFullPathName( - os.path.join(win32com.axscript.__path__[0], "test\\testHost.py") + os.path.join(next(iter(win32com.axscript.__path__)), "test\\testHost.py") ) cmd = f'{win32api.GetModuleFileName(0)} "{file}"' if verbose: @@ -31,7 +31,7 @@ def testHost(self): def testCScript(self): file = win32api.GetFullPathName( - os.path.join(win32com.axscript.__path__[0], "Demos\\Client\\wsh\\test.pys") + os.path.join(next(iter(win32com.axscript.__path__)), "Demos\\Client\\wsh\\test.pys") ) cmd = 'cscript.exe "%s"' % (file) if verbose: diff --git a/mypy.ini b/mypy.ini index 7f0506d85f..34ac87ef01 100644 --- a/mypy.ini +++ b/mypy.ini @@ -40,7 +40,7 @@ exclude = (?x)( | ^Pythonwin/Scintilla/ ; Forked IDLE extensions predating Python 2.3. They now live in idlelib in https://github.com/python/cpython/tree/main/Lib/idlelib | ^Pythonwin/pywin/idle/ - ; TODO: adodbapi should be updated and fixed separatly + ; TODO: adodbapi should be updated and fixed separately | ^adodbapi/ ; TODO: Ignoring non-public APIs until all public API is typed | ([Tt]est|[Dd]emos?)/