|

楼主 |
发表于 2017-9-16 17:36:52
|
显示全部楼层
本帖最后由 哲学三信 于 2017-9-16 20:20 编辑
取自os.py代码
- import sys, errno
- import stat as st
- _names = sys.builtin_module_names
- # Note: more names are added to __all__ later.
- __all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
- "defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR",
- "SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen",
- "popen", "extsep"]
- def _exists(name):
- return name in globals()
- def _get_exports_list(module):
- try:
- return list(module.__all__)
- except AttributeError:
- return [n for n in dir(module) if n[0] != '_']
- ###省略
- elif 'nt' in _names:
- name = 'nt'
- linesep = '\r\n'
- from nt import *
- try:
- from nt import _exit
- __all__.append('_exit')
- except ImportError:
- pass
- import ntpath as path
- import nt
- __all__.extend(_get_exports_list(nt))
- del nt
- try:
- from nt import _have_functions
- except ImportError:
- pass
复制代码
以上分别为判断操作系统之后调用模组
- sys.modules['os.path'] = path
- from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep, devnull)
复制代码
这里os从os.path(位置)调用了很多模组???
以及
- if _exists("_have_functions"):
- _globals = globals()
- def _add(str, fn):
- if (fn in _globals) and (str in _have_functions):
- _set.add(_globals[fn])
- _set = set()
- _add("HAVE_OPENAT", "open")
- supports_dir_fd = _set
- _set = set()
- _add("HAVE_FACCESSAT", "access")
- supports_effective_ids = _set
- _set = set()
- _add("HAVE_FCHDIR", "chdir")
- supports_fd = _set
- _set = set()
- _add("HAVE_FACCESSAT", "access")
复制代码
全局变量中存在"chdir"变量和存在_have_functions模组中方法向集合 _set 中添加元素"chdir"而且下文找不到"chdir"关键字
只在以下位置用了这个集合
- if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd:
- def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None):
- orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
- topfd = open(top, O_RDONLY, dir_fd=dir_fd)
- def _fwalk(topfd, toppath, topdown, onerror, follow_symlinks):
- names = listdir(topfd)
- dirs, nondirs = [], []
- __all__.append("fwalk")
- # Make sure os.environ exists, at least
复制代码
==========================================
自己跑一遍
- import sys, errno
- import stat as st
- from nt import *
- from nt import _exit
- import ntpath as path
- import nt
- from nt import _have_functions
- sys.modules['os.path'] = path
- from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep, devnull)
- print(globals())
复制代码
摘选结果有
{'path': <module 'ntpath' from 'D:\\Python34\\lib\\ntpath.py'>, 'O_CREAT': 256, 'chdir': <built-in function chdir>, 'listdir': <built-in function listdir>, 'rename': <built-in function rename>, 'replace': <built-in function replace>, 'read': <built-in function read>, 'O_TRUNC': 512, 'O_EXCL': 1024, 'P_OVERLAY': 2, 'O_TEMPORARY': 64, 'O_NOINHERIT': 128, 'O_BINARY': 32768, 'error': <class 'OSError'>, 'open': <built-in function open>, 'TMP_MAX': 32767, 'system': <built-in function system>, 'O_TEXT': 16384, 'P_WAIT': 0, 'P_NOWAIT': 1, 'P_NOWAITO': 3, '_exit': <built-in function _exit>, 'errno': <module 'errno' (built-in)>}
怀疑是
'O_CREAT': 256
的问题
2017.09.16 折腾和研究一圈还是没有用(然而其实还是没看懂) O_等值并不是限制操作长度的
另外求"chdir"这些python内建函数的源码(这里加载出来显示是内建函数,help(os.chdir)可以看到说明却不是在os.py文件里定义的,os.py只有help(os)的一部分说明的源码的位置) |
|