我在浏览来源时注意到,在定义变量environ之前,它引用了方法中的变量:
def _createenviron():
if name == 'nt':
# Where Env Var Names Must Be UPPERCASE
def check_str(value):
if not isinstance(value, str):
raise TypeError("str expected, not %s" % type(value).__name__)
return value
encode = check_str
decode = str
def encodekey(key):
return encode(key).upper()
data = {}
for key, value in environ.items():
data[encodekey(key)] = value
else:
# Where Env Var Names Can Be Mixed Case
encoding = sys.getfilesystemencoding()
def encode(value):
if not isinstance(value, str):
raise TypeError("str expected, not %s" % type(value).__name__)
return value.encode(encoding, 'surrogateescape')
def decode(value):
return value.decode(encoding, 'surrogateescape')
encodekey = encode
data = environ
return _Environ(data,
encodekey, decode,
encode, decode)
# unicode environ
environ = _createenviron()
del _createenviron那么environ是如何设置的呢?我似乎无法推理它的初始化和声明的位置,以便_createenviron可以使用它?
发布于 2022-03-30 18:51:49
搜索from posix import *模块内容中的os。
os模块从posix (Unix)或nt ( os.py )低级模块导入os.py开头的所有公共符号。
posix将environ公开为普通的Python dict。os将其封装在类似于_Environ的对象中,以更新_Environ项上的环境变量。
https://stackoverflow.com/questions/71641609
复制相似问题