enthought / comtypes

A pure Python, lightweight COM client and server framework, based on the ctypes Python FFI package.
Other
292 stars 99 forks source link

comtypes 1.1.3 hangs when importing CreateObject as non admin user #124

Open woakesd opened 7 years ago

woakesd commented 7 years ago

This happens if you import first an admin user, as admin user in python run

from comtypes.client import CreateObject

This works, then as an ordinary user run the same command.

I get the following tracebacks when I interrupt with control c (I had to do this as the process hangs).

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

During handling of the above exception, another exception occurred:

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

Deleting the gen folder allows the non admin user to import CreateObject.

woakesd commented 7 years ago

A few more notes on this:

Python version is 64 bit 3.5.3 and is installed for all users in Program Files\Python35.

When importing CreateObject as an admin it creates the folder C:\Program Files\Python35\lib\site-packages\comtypes\gen.

Subsequently importing CreateObject hangs and I've just found that if you delete the gen folder while the process is hanging allows it to complete successfully. It creates a comtypes_cache folder in AppData\Roaming\Python\Python35.

ezradreisbach commented 6 years ago

Thanks, buddies.
Using the Windows file permissions dialog to give myself "Full Control" over C:\Program Files\Python35\Lib\site-packages\comtypes made everything work

vasily-v-ryabov commented 6 years ago

I've just reproduced the same issue. Going to fix it in next release. It should work without adding more permissions.

glungu commented 5 years ago

It still hangs on 1.1.7. I'm using venv, and Ctrl+С doesn't help. I am the same user that installed the package, and I am in administrators group. Does not help. Deleting gen does not help either.

1kastner commented 4 years ago

it is still an issue

Wyko commented 3 years ago

Still an issue...

vasily-v-ryabov commented 3 years ago

I can't reproduce the issue on my machine. Before my fix it was reproduced. So the issue is different.

Guys please describe your reproducing sequence in more details.

  1. Which Python version? Exact version and (32- or 64-bit)?
  2. How Python was installed? Which path? Which user? Is it in Administrators group?
  3. Is UAC disabled on your machine?
  4. Are you installing comtypes==1.1.10 under the same user as Python was installed?
  5. Are you running from comtypes.client import CreateObject under the same user as Python was installed?
  6. Which code hangs? Import statement above? Or something at next step?
  7. Is Ctrl+C working to interrupt hang?
Wyko commented 3 years ago

In my case, I think it relates to a permissions error when writing to that directory. I'm not near my computer right now, but I'll update it later with the exact details. It would be good if there was some kind of option to fall back to a user's home directory, or a manually specified directory that is writable.

On Fri, 23 Jul 2021, 12:50 pm Vasily Ryabov, @.***> wrote:

I can't reproduce the issue on my machine. Before my fix it was reproduced. So the issue is different.

Guys please describe your reproducing sequence in more details.

  1. Which Python version? Exact version and (32- or 64-bit)?
  2. How Python was installed? Which path? Which user? Is it in Administrators group?
  3. Is UAC disabled on your machine?
  4. Are you installing comtypes==1.1.10 under the same user as Python was installed?
  5. Are you running from comtypes.client import CreateObject under the same user as Python was installed?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/enthought/comtypes/issues/124#issuecomment-885557150, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADSBFR7JDTJCLLCDNGNDRLTZFCPPANCNFSM4DGFBVVA .

vasily-v-ryabov commented 3 years ago

For example, I used Python 3.6.5 32-bit which was installed in Program Files (x86) as Administrator (in privileged mode) as well as comtypes from master branch.

This is the output in non-privileged cmd.exe:

"C:\Program Files (x86)\Python36-32\python.exe"
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from comtypes.client import CreateObject
>>>
>>> from comtypes.client import GetModule
>>> GetModule("UIAutomationCore.dll")
<module 'comtypes.gen._944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'C:\\Users\\myuser\\AppData\\Roaming\\Python\\Python36\\comtypes_cache\\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>

Also I tried the same scenario for Python 3.9.6 64-bit which was installed in non-privileged mode as well as comtypes. It also works perfectly for me.

C:\Users\myuser\AppData\Local\Programs\Python\Python39\python.exe
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from comtypes.client import CreateObject
>>>
>>> from comtypes.client import CreateObject
>>> from comtypes.client import GetModule
>>> GetModule("UIAutomationCore.dll")
<module 'comtypes.gen._944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'C:\\Users\\myuser\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\comtypes\\gen\\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
Wyko commented 3 years ago

I'm working on a corporate laptop that unfortunately is very restricted. My normal user account has almost no permissions to write to program files. (Or most other folders)

vasily-v-ryabov commented 3 years ago

Restricted access to Program Files is not a problem. comtypes can fallback to user AppData folder which should be accessible. This is visible in my output. So let's wait for your sequence with more details. Maybe it's some non-trivial corner case.

Wyko commented 3 years ago

Which Python version? Exact version and (32- or 64-bit)? Python 3.9.5 How Python was installed? Which path? Which user? Is it in Administrators group? It was installed as an admin Is UAC disabled on your machine? No Are you installing comtypes==1.1.10 under the same user as Python was installed? Yes Are you running from comtypes.client import CreateObject under the same user as Python was installed? No. Non-admin Which code hangs? Import statement above? Or something at next step? Is Ctrl+C working to interrupt hang? Not hanging. Just exits.

Traceback (most recent call last):
  File "C:\Python39\Scripts\parasol-script.py", line 33, in <module>
    sys.exit(load_entry_point('parasol', 'console_scripts', 'parasol')())
  File "c:\python39\lib\site-packages\click\core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "c:\python39\lib\site-packages\click\core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "c:\python39\lib\site-packages\click\core.py", line 1668, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\python39\lib\site-packages\click\core.py", line 1668, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\python39\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\python39\lib\site-packages\click\core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "c:\python39\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "c:\users\au21fn\developer\coding\p08914-parasol\parasol\bigip\_commands.py", line 48, in cert
    c.generate()
  File "c:\users\au21fn\developer\coding\p08914-parasol\parasol\bigip\certificate_runbook\certificate_runbook.py", line 212, in generate
    self.openNewDocFromTemplate()
  File "c:\users\au21fn\developer\coding\p08914-parasol\parasol\helpers\utilities.py", line 319, in wrapper
    result = func(*args, **kwargs)
  File "c:\users\au21fn\developer\coding\p08914-parasol\parasol\bigip\certificate_runbook\certificate_runbook.py", line 480, in openNewDocFromTemplate
    self.word = comtypes.client.CreateObject("Word.Application")
  File "c:\python39\lib\site-packages\comtypes\client\__init__.py", line 250, in CreateObject
    return _manage(obj, clsid, interface=interface)
  File "c:\python39\lib\site-packages\comtypes\client\__init__.py", line 188, in _manage
    obj = GetBestInterface(obj)
  File "c:\python39\lib\site-packages\comtypes\client\__init__.py", line 110, in GetBestInterface
    mod = GetModule(tlib)
  File "c:\python39\lib\site-packages\comtypes\client\_generate.py", line 118, in GetModule
    mod = _CreateWrapper(tlib, pathname)
  File "c:\python39\lib\site-packages\comtypes\client\_generate.py", line 180, in _CreateWrapper
    ofi = open(os.path.join(comtypes.client.gen_dir, modname + ".py"), "w")
PermissionError: [Errno 13] Permission denied: 'c:\\python39\\lib\\site-packages\\comtypes\\gen\\_00020905_0000_0000_C000_000000000046_0_8_7.py'
Wyko commented 3 years ago

As a side note: Running the same code from a venv works fine.

junkmd commented 3 months ago

@Wyko

Is there an update on this issue?

Can this issue be reproduced with the newer versions of Python or comtypes?