Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pywin32.pth: don't blindly append to PATH in case site is repeatedly reload()ed #1376

Merged
merged 1 commit into from
Jul 25, 2019

Conversation

ksmyth
Copy link
Contributor

@ksmyth ksmyth commented Jul 25, 2019

pywin32.pth appends to the PATH environment variable. When the site module is reload()ed, it re-evaluates .pth files. When an environment variable gets too long (~32k), CreateProcess fails.

This PR modifies pywin32.pth to append to PATH only if the entry is not already present.

POC:

import os
import subprocess
import site


for i in range(1000):
    old_path = os.environ['PATH']
    reload(site)
    if len(old_path) >= len(os.environ['PATH']):
        break
print i

subprocess.check_call('cmd /c set')

Output:

c:\Python27\python test.py
Error processing line 11 of c:\Python27\lib\site-packages\pywin32.pth:

  Traceback (most recent call last):
    File "c:\Python27\lib\site.py", line 152, in addpackage
      exec line
    File "<string>", line 1, in <module>
    File "c:\Python27\lib\os.py", line 422, in __setitem__
      putenv(key, item)
  ValueError: the environment variable is longer than 32767 bytes

Remainder of file ignored
660
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    subprocess.check_call('cmd /c set')
  File "c:\Python27\lib\subprocess.py", line 181, in check_call
    retcode = call(*popenargs, **kwargs)
  File "c:\Python27\lib\subprocess.py", line 168, in call
    return Popen(*popenargs, **kwargs).wait()
  File "c:\Python27\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "c:\Python27\lib\subprocess.py", line 640, in _execute_child
    startupinfo)
WindowsError: [Error 8] Not enough memory resources are available to process this command

@thopiekar
Copy link
Contributor

Love it. Never observed this, but your output definitely proofs that it might happen.
I'm sure that @mhammond will merge this as soon has he finds the time to review it 😉👍

Copy link
Owner

@mhammond mhammond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mhammond mhammond merged commit 857ad62 into mhammond:master Jul 25, 2019
vernondcole pushed a commit to vernondcole/pywin32 that referenced this pull request Nov 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants