_win.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:GoGuru 作者: alvarolm 项目源码 文件源码
def get_env(shell=None, for_subprocess=False):
    """
    Return environment variables for the current user

    :param shell:
        The shell to get the env from - unused on Windows

    :param for_subprocess:
        If True, and the code is being run in Sublime Text 2, the result will
        be byte strings instead of unicode strings

    :return:
        A 2-element tuple:

         - [0] unicode string shell path
         - [1] env dict with keys and values as unicode strings
    """

    shell = os.environ['ComSpec']
    if not isinstance(shell, str_cls) and for_subprocess is False:
        shell = shell.decode(_sys_encoding)

    if sys.version_info < (3,) and for_subprocess is False:
        str_pointer = kernel32.GetEnvironmentStringsW()
        string = ctypes.wstring_at(str_pointer)

        values = {}
        while string != '':
            if string[0].isalpha():
                name, value = string.split(u'=', 1)
                values[name.upper()] = value
            # Include the trailing null byte, and measure each
            # char as 2 bytes since Windows uses UTF-16 for
            # wide chars
            str_pointer += (len(string) + 1) * 2
            string = ctypes.wstring_at(str_pointer)
    else:
        values = dict(os.environ)

    return (shell, values)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号