How to shadow python builtin pwd module

可紊 提交于 2019-12-07 05:40:54

问题


There is some python code that works under Linux. It uses the pwd module in a way like that:

import pwd
   ...
  def func():
      user=pwd.getpwnam(user)[2]

Now we have a specific need to cover this code with tests, and tests have to be runnable under Windows. The program itself is intended to run only under Linux. The problem is that pwd module is not available under Windows, so the code under test will fail with ImportError, even if the implementation of pwd functions is mocked using MagicMock.

The basic idea to solve this issue was to shadow the pwd module when running tests. So when running tests, the stub will shadow pwd and when running main program, original (Unix) pwd will be used. We created such stub at test PYTHONPATH:

# pwd.py
def getpwnam(user):
    print("YESSSSSSSS")

But it does not seem to shadow a pwd module, in a debugger we see that the built-in pwd is imported. I'm primarily a Java developer, so I'm sorry if the way of doing things is not "pythonic". Ideas are welcome.


回答1:


Rename your pwd.py to something else, such as winpwd.py. Then use:

try:
    import pwd
except ImportError:
    import winpwd as pwd

By importing pwd this way, you will get the built-in pwd on Linux, and winpwd on Windows. Then you should be able to run tests and mock it as you please.




回答2:


import os
if os.name == 'nt':
    class Pwd():
        def getpwnam(self, user):
            pass
    pwd = Pwd()
else:
    import pwd

Could something similar work? No need for extra .py files within your project.
I've used it for fchown myself a couple of times...



来源:https://stackoverflow.com/questions/16529166/how-to-shadow-python-builtin-pwd-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!