comtypes icon indicating copy to clipboard operation
comtypes copied to clipboard

after _check_version as admin: it stalls for user forever

Open nitzel opened this issue 8 years ago • 3 comments

For comtypes 1.1.2 and 1.1.3 (pip install -U comtypes) the following command does work as a normal user. from comtypes import _check_version; _check_version('') To reproduce:

  • Execute it as admin.
  • Try again as user: The console seems to idle forever, waiting for _os.open.

Deleting .../site-packages/comtypes/gen/ helped me. This folder seems to be created whenever the admin executes the command. It contains an __init__.py file with this content

# comtypes.gen package, directory for generated files.

and ./__pycache__/__init__.cpython-35.pyc.

Here's the traceback when I ctrl-c the stalled program:

Traceback (most recent call last):
  File "C:\Program Files\Anaconda3\lib\tempfile.py", line 260, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
PermissionError: [Errno 13] Permission denied: 'C:\\Program Files\\Anaconda3\\lib\\site-packages\\comtypes\\gen\\tmpfa2kcl2d'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Anaconda3\lib\site-packages\comtypes\__init__.py", line 27, in _check_version
    from comtypes.tools.codegenerator import version as required
  File "C:\Program Files\Anaconda3\lib\site-packages\comtypes\tools\codegenerator.py", line 7, in <module>
    import comtypes.client
  File "C:\Program Files\Anaconda3\lib\site-packages\comtypes\client\__init__.py", line 33, in <module>
    gen_dir = _find_gen_dir()
  File "C:\Program Files\Anaconda3\lib\site-packages\comtypes\client\_code_cache.py", line 30, in _find_gen_dir
    if not _is_writeable(gen.__path__):
  File "C:\Program Files\Anaconda3\lib\site-packages\comtypes\client\_code_cache.py", line 110, in _is_writeable
    tempfile.TemporaryFile(dir=path[0])
  File "C:\Program Files\Anaconda3\lib\tempfile.py", line 547, in NamedTemporaryFile
    (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "C:\Program Files\Anaconda3\lib\tempfile.py", line 260, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
KeyboardInterrupt

Python version on Windows 10 x64: Python 3.5.2 |Anaconda 4.3.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32

nitzel avatar Apr 05 '17 11:04 nitzel

Seems to be similar to

  • #124 (comtypes 1.1.3 hangs when importing CreateObject as non admin user)
  • #102 (TypeError: '_NamespacePath' object does not support indexing)

nitzel avatar Apr 05 '17 11:04 nitzel

Actually it's this bug over here: bugs.python.org/issue22107

in tempfile.py TemporaryFile() will be called. This is roughly what you'll find in there:

# global constant TMP_MAX
if hasattr(_os, 'TMP_MAX'):
    TMP_MAX = _os.TMP_MAX  # = 2147483647 on my machine
else:
    TMP_MAX = 10000

# in _mkstemp_inner(), called by TemporaryFile
for seq in range(TMP_MAX):
   try:
      os.open(filename+seq)
   except PermissionError:
      if os.is_windows():
         continue

Which leads to tempfile trying 2147483647 different filenames before giving up. A workaround until this is fixed in python and you have the newest version is:

import os
os.TMP_MAX = 1000  # choose a number reached quicker than 2**31
# del os  # if you don't want it to in your namespace
from comtypes import _check_version; _check_version('')
# now it tries a 1000 times and happily chooses another directory.

Edit: You can do the same workaround with tempfile.TMP_MAX if you do not want to mess with os: import tempfile; tempfile.TMP_MAX = 1000; del tempfile - of course, before importing comtypes!

nitzel avatar Apr 05 '17 12:04 nitzel

The suggested workaround is effective for #124.

woakesd avatar Apr 06 '17 09:04 woakesd

@nitzel @woakesd

If the issue remains, please re-open.

junkmd avatar Dec 06 '22 08:12 junkmd