在ipython中使用re模块时,我注意到了一个未记录的template函数:
In [420]: re.template?
Type: function
Base Class: <type 'function'>
String Form: <function template at 0xb7eb8e64>
Namespace: Interactive
File: /usr/tideway/lib/python2.7/re.py
Definition: re.template(pattern, flags=0)
Docstring:
Compile a template pattern, returning a pattern object
Run Code Online (Sandbox Code Playgroud)
还有一面旗帜re.TEMPLATE及其别名re.T.
在2.7或3.2的文档中都没有提到这一点.他们在做什么?它们是早期版本的Python过时的宿醉,还是未来可能正式添加的实验性功能?
NPE*_*NPE 11
在CPython 2.7.1中,re.template()定义为:
def template(pattern, flags=0):
"Compile a template pattern, returning a pattern object"
return _compile(pattern, flags|T)
Run Code Online (Sandbox Code Playgroud)
_compile打电话给_compile_typed谁sre_compile.compile.检查T(aka SRE_FLAG_TEMPLATE)标志的代码中唯一的位置是该函数:
elif op in REPEATING_CODES:
if flags & SRE_FLAG_TEMPLATE:
raise error, "internal: unsupported template operator"
emit(OPCODES[REPEAT])
skip = _len(code); emit(0)
emit(av[0])
emit(av[1])
_compile(code, av[2], flags)
emit(OPCODES[SUCCESS])
code[skip] = _len(code) - skip
...
Run Code Online (Sandbox Code Playgroud)
这将有禁用所有重复操作符(的效果*,+,?,{}等):
In [10]: re.template('a?')
---------------------------------------------------------------------------
.....
error: internal: unsupported template operator
Run Code Online (Sandbox Code Playgroud)
代码的结构方式(无条件地raise在一堆死代码之前)使我认为该功能要么从未完全实现,要么由于某些问题而被关闭.我只能猜出预期的语义可能是什么.
最终结果是该函数没有任何用处.
| 归档时间: |
|
| 查看次数: |
2272 次 |
| 最近记录: |