Python "import comtypes.client" calls pwd.py indirectly
Question: I have not a deep experience in Python. Working on last application found very
interesting thing. I put the script with name pwd.py in the same directory as
the main script. I created pwd.py to test some basic modules and methods, no
more purpose.

But I was really surprised that later found that my main script indirectly
calls pwd.py! I put some debug printouts and found that the import statement
"import comtypes.client" calls pwd.py.

Well...I thought that it is probably some standard feature that I don't know
still, but:

  * recursive search in the PYTHON_HOME (C:\Python343 in my case) does not show pwd.py in the standard Python directories. I even tried to do recursive search by file content inside c:\Python343 to find who calls pwd.py, but this search returned nothing (I used Total Commander search by Ctrl+F7, probably it fails sometimes).

  * Google says nothing well-known regarding pwd.py

So, what it is the feature and why it is not described well anywhere? It is
even a kind of vulnerability. One can create pwd.py in the same directory
where the main script is located and put any code inside pwd.py...

May anybody check this behavior on own system? If it really works so, where I
can find the description of this feature?


Answer: I found that the line "import comtypes.client" causes this "issue". Here is
the content of the main script:

    
    
    #!C:\Python343\python
    import comtypes.client # this line causes pwd.py to be called indirectly !!!
    

And here is the content of pwd.py that was put to the same directory:

    
    
    #!C:\Python343\python
    print('pwd.py is called!')
    raise RuntimeError("We got here!") # I put an exception as Kevin asked me above, but I am not sure that Python knows "throw" (it does not work for me) so use "raise"
    

After that I got the following result:

    
    
    c:\dev>test.py
    pwd.py is called!
    Traceback (most recent call last):
      File "C:\dev\test.py", line 2, in <module>
        import comtypes.client # this line causes pwd.py to be called indirectly !!!
      File "C:\Python343\lib\site-packages\comtypes\client\__init__.py", line 31, in <module>
        from comtypes.client._code_cache import _find_gen_dir
      File "C:\Python343\lib\site-packages\comtypes\client\_code_cache.py", line 7, in <module>
        import ctypes, logging, os, sys, tempfile, types
      File "c:\Python343\Lib\tempfile.py", line 34, in <module>
        import shutil as _shutil
      File "c:\Python343\Lib\shutil.py", line 24, in <module>
        from pwd import getpwnam
      File "C:\dev\pwd.py", line 3, in <module>
        raise RuntimeError("We got here!")
    RuntimeError: We got here!
    

So the call chain is:

    
    
    C:\dev\test.py
    C:\Python343\lib\site-packages\comtypes\client\__init__.py
    C:\Python343\lib\site-packages\comtypes\client\_code_cache.py
    c:\Python343\Lib\tempfile.py
    c:\Python343\Lib\shutil.py
    C:\dev\pwd.py
    

and the line "from pwd import getpwnam" seems to be responsible for the call
of pwd.py. After that I changed my main script the following way:

    
    
    #!C:\Python343\python
    import pwd # I know it's you!
    

and really, it calls pwd.py! Thanks Kevin for this simple idea. The only
remaining question is is it correct that "import pwd" calls pwd.py from the
same directory? Very interesting feature :-)

