How to shadow python builtin pwd module

北城余情 提交于 2019-12-05 10:24:38

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.

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...

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