def test_exception_path2(self):
"""Test exception path in exception_validate, expecting windows line endings.
"""
self.mktmp("from __future__ import print_function\n"
"import sys\n"
"print('A')\n"
"print('B')\n"
"print('C', file=sys.stderr)\n"
"print('D', file=sys.stderr)\n"
)
out = "A\r\nB"
tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\r\nD")
python类print_function()的实例源码
def _patchTestCase(self, case):
if case:
case._dt_test.globs['print_function'] = print_function
case._dt_checker = _checker
return case
def _patchTestCase(self, case):
if case:
case._dt_test.globs['print_function'] = print_function
case._dt_checker = _checker
return case
def test_all_exports_correspond_to_plugins(self):
exports = [name for name in dir(tb.summary) if not name.startswith('_')]
futures = frozenset(('absolute_import', 'division', 'print_function'))
bad_exports = [
name for name in exports
if name not in futures and not any(
name == plugin or name.startswith('%s_' % plugin)
for plugin in STANDARD_PLUGINS)
]
if bad_exports:
self.fail(
'The following exports do not correspond to known standard '
'plugins: %r. Please mark these as private by prepending an '
'underscore to their names, or, if they correspond to a new '
'plugin that you are certain should be part of the public API '
'forever, add that plugin to the STANDARD_PLUGINS set in this '
'module.' % bad_exports)
def transform(self, node, results):
future_import(u"unicode_literals", node)
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:UMOG
作者: hsab
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def transform(self, node, results):
# Reverse order:
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
def transform(self, node, results):
# Add the __future__ import first. (Otherwise any shebang or encoding
# comment line attached as a prefix to the print statement will be
# copied twice and appear twice.)
future_import(u'print_function', node)
n_stmt = super(FixPrintWithImport, self).transform(node, results)
return n_stmt
def strip_future_imports(self, code):
"""
Strips any of these import lines:
from __future__ import <anything>
from future <anything>
from future.<anything>
from builtins <anything>
or any line containing:
install_hooks()
or:
install_aliases()
Limitation: doesn't handle imports split across multiple lines like
this:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
"""
output = []
# We need .splitlines(keepends=True), which doesn't exist on Py2,
# so we use this instead:
for line in code.split('\n'):
if not (line.startswith('from __future__ import ')
or line.startswith('from future ')
or line.startswith('from builtins ')
or 'install_hooks()' in line
or 'install_aliases()' in line
# but don't match "from future_builtins" :)
or line.startswith('from future.')):
output.append(line)
return '\n'.join(output)
def _patchTestCase(self, case):
if case:
case._dt_test.globs['print_function'] = print_function
case._dt_checker = _checker
return case
def transform(self, node, results):
future_import(u"unicode_literals", node)
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
def test_importfrom_future(self):
binding = FutureImportation('print_function', None, None)
assert binding.source_statement == 'from __future__ import print_function'
assert str(binding) == '__future__.print_function'
def test_futureImportUsed(self):
"""__future__ is special, but names are injected in the namespace."""
self.flakes('''
from __future__ import division
from __future__ import print_function
assert print_function is not division
''')
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:blackmamba
作者: zrzka
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def transform(self, node, results):
# Reverse order:
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
def transform(self, node, results):
# Add the __future__ import first. (Otherwise any shebang or encoding
# comment line attached as a prefix to the print statement will be
# copied twice and appear twice.)
future_import(u'print_function', node)
n_stmt = super(FixPrintWithImport, self).transform(node, results)
return n_stmt
def strip_future_imports(self, code):
"""
Strips any of these import lines:
from __future__ import <anything>
from future <anything>
from future.<anything>
from builtins <anything>
or any line containing:
install_hooks()
or:
install_aliases()
Limitation: doesn't handle imports split across multiple lines like
this:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
"""
output = []
# We need .splitlines(keepends=True), which doesn't exist on Py2,
# so we use this instead:
for line in code.split('\n'):
if not (line.startswith('from __future__ import ')
or line.startswith('from future ')
or line.startswith('from builtins ')
or 'install_hooks()' in line
or 'install_aliases()' in line
# but don't match "from future_builtins" :)
or line.startswith('from future.')):
output.append(line)
return '\n'.join(output)
def transform(self, node, results):
future_import(u"unicode_literals", node)
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:beepboop
作者: nicolehe
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def transform(self, node, results):
# Reverse order:
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
def transform(self, node, results):
# Add the __future__ import first. (Otherwise any shebang or encoding
# comment line attached as a prefix to the print statement will be
# copied twice and appear twice.)
future_import(u'print_function', node)
n_stmt = super(FixPrintWithImport, self).transform(node, results)
return n_stmt
def strip_future_imports(self, code):
"""
Strips any of these import lines:
from __future__ import <anything>
from future <anything>
from future.<anything>
from builtins <anything>
or any line containing:
install_hooks()
or:
install_aliases()
Limitation: doesn't handle imports split across multiple lines like
this:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
"""
output = []
# We need .splitlines(keepends=True), which doesn't exist on Py2,
# so we use this instead:
for line in code.split('\n'):
if not (line.startswith('from __future__ import ')
or line.startswith('from future ')
or line.startswith('from builtins ')
or 'install_hooks()' in line
or 'install_aliases()' in line
# but don't match "from future_builtins" :)
or line.startswith('from future.')):
output.append(line)
return '\n'.join(output)
def _patchTestCase(self, case):
if case:
case._dt_test.globs['print_function'] = print_function
case._dt_checker = _checker
return case