def ten2eleven(*files): """Convert MDAnalysis 0.10.0 scripts to 0.11.0 Usage: ten2eleven('myscript.py') """ main('MDAnalysis.migration.fixes', ['-w'] + list(files))
def execute_2to3(dialog, cmd): try: args = _construct_options(dialog) except Exception as e: message(str(e), "Error") return b = Buffer() import logging logger = logging.getLogger("RefactoringTool") logger.addHandler(b) _stdout = sys.stdout _stderr = sys.stderr sys.stdout = b sys.stderr = b try: sys.argv = args main("lib2to3.fixes", args=args) except: traceback.print_exc() finally: sys.stdout = _stdout sys.stderr = _stderr del sys.argv logger.removeHandler(b) write_to_writer(b.lines)
def test_check_fixture(in_file, fixer, tmpdir): if fixer: main("unittest2pytest.fixes", args=[ '--no-diffs', '--fix', fixer, '-w', in_file, '--nobackups', '--output-dir', str(tmpdir) ]) else: main("unittest2pytest.fixes", args=[ '--no-diffs', '--fix', 'all', '-w', in_file, '--nobackups', '--output-dir', str(tmpdir) ]) result_file_name = tmpdir.join(os.path.basename(in_file)) assert result_file_name.exists(), '%s is missing' % result_file_name result_file_contents = result_file_name.readlines() expected_file = in_file.replace("_in.py", "_out.py") with open(expected_file) as fh: expected_contents = fh.readlines() if result_file_contents != expected_contents: text = "in_file doesn't match out_file\n" text += ''.join( unified_diff(expected_contents, result_file_contents, 'expected', 'refactured result')) raise AssertionError(text)
def check_fixture(in_file, out_file, fixer): if fixer: main("pep8ify.fixes", args=['--fix', fixer, '-w', in_file]) else: main("pep8ify.fixes", args=['--fix', 'all', '--fix', 'maximum_line_length', '-w', in_file]) in_file_contents = open(in_file, u'r').readlines() out_file_contents = open(out_file, u'r').readlines() assert in_file_contents == out_file_contents, \ "in_file doesn't match out_file with \n%s\n:\n%s" \ % (in_file_contents, out_file_contents)
def check_fixture(in_file, out_file, fixer): if fixer: main("pep8ify.fixes", args=["--fix", fixer, "-w", in_file]) else: main("pep8ify.fixes", args=["--fix", "all", "--fix", "maximum_line_length", "-w", in_file]) in_file_contents = open(in_file, "r").readlines() out_file_contents = open(out_file, "r").readlines() assert in_file_contents == out_file_contents, "in_file doesn't match out_file with \n%s\n:\n%s" % ( in_file_contents, out_file_contents, )
def check_fixture(in_file, out_file, fixer): if fixer: main("pep8ify.fixes", args=['--fix', fixer, '-w', in_file]) else: main("pep8ify.fixes", args=[ '--fix', 'all', '--fix', 'maximum_line_length', '-w', in_file ]) in_file_contents = open(in_file, 'r').readlines() out_file_contents = open(out_file, 'r').readlines() assert in_file_contents == out_file_contents, \ "in_file doesn't match out_file with \n%s\n:\n%s" \ % (in_file_contents, out_file_contents)
def check_fixture(in_file, out_file, fixer): if fixer: main("pep8ify.fixes", args=['--no-diffs', '--fix', fixer, '-w', in_file]) else: main("pep8ify.fixes", args=['--no-diffs', '--fix', 'all', '--fix', 'maximum_line_length', '-w', in_file]) in_file_contents = open(in_file, 'r').readlines() out_file_contents = open(out_file, 'r').readlines() if in_file_contents != out_file_contents: text = "in_file doesn't match out_file\n" text += ''.join(unified_diff(out_file_contents, in_file_contents, 'expected', 'refactured result')) raise AssertionError(text)
def may_convert_2_to_3(code): """Convert code from 2 to 3 if run with Python3, nothing otherwise.""" if sys.version_info.major == 3: from tempfile import NamedTemporaryFile from lib2to3 import main as lib2to3 tmp_py = NamedTemporaryFile(suffix='.py', delete=False) tmp_py.write(code.encode('utf8')) tmp_py.close() lib2to3.main('lib2to3.fixes', [tmp_py.name, '-w', '-n']) code = open(tmp_py.name).read() os.remove(tmp_py.name) return code
def may_convert_2_to_3(code): """Convert code from 2 to 3 if run with Python3, nothing otherwise.""" if sys.version_info.major == 3: from tempfile import NamedTemporaryFile from lib2to3 import main as lib2to3 tmp_py = NamedTemporaryFile(suffix='.py', delete=False) tmp_py.write(code.encode('ascii')) tmp_py.close() lib2to3.main('lib2to3.fixes', [tmp_py.name, '-w', '-n']) code = open(tmp_py.name).read() os.remove(tmp_py.name) return code
def command_py2to3(args): """ Apply '2to3' tool (Python2 to Python3 conversion tool) to Python sources. """ from lib2to3.main import main sys.exit(main("lib2to3.fixes", args=args.sources))
def prepare3(): """Prepare files for installing on Python 3. If you have distribute for Python 3, then we don't need to run this. """ import os try: os.mkdir('code3') except OSError: pass # Note: -W was added in 2.7.3. from lib2to3.main import main main("lib2to3.fixes", ["-w", "-W", "-n", "-o", "code3", conf['package_dir']['']]) conf['package_dir'] = {'': 'code3'}
def script_to_py3(script): """Convert a script to Python3 syntax if required.""" if sys.version_info[0] < 3: return script import tempfile f = tempfile.NamedTemporaryFile(suffix=".py", delete=False) f.write(script.encode()) f.flush() filename = f.name f.close() # 2to3 is way too chatty import logging logging.basicConfig(filename=os.devnull) from lib2to3.main import main if main("lib2to3.fixes", ['--no-diffs', '-w', '-n', filename]): raise Exception('py3 conversion failed') f2 = open(filename) try: return f2.read() finally: f2.close() os.remove(filename)
def to3(self, script): """Convert a script to Python3 if required.""" if not IS_PY3K: return script script = script.encode() f = tempfile.NamedTemporaryFile(suffix=".py") try: f.write(script) f.flush() # 2to3 is way too chatty import logging logging.basicConfig(filename=os.devnull) from lib2to3.main import main if main("lib2to3.fixes", ['--no-diffs', '-w', '-n', f.name]): raise Exception('py3 conversion failed') ff = open(f.name) try: return ff.read() finally: ff.close() finally: f.close()
def prepare3(): """Prepare files for installing on Python 3. If you have distribute for Python 3, then we don't need to run this. """ import os try: os.mkdir("code3") except OSError: pass # Note: -W was added in 2.7.3. from lib2to3.main import main main("lib2to3.fixes", ["-w", "-W", "-n", "-o", "code3", conf["package_dir"][""]]) conf["package_dir"] = {"": "code3"}
def firecheck(filename): # reduce verbosity of logging logging.basicConfig(filename=os.devnull) try: # create in-memory text stream fixes = io.StringIO() # redirect standard output to text stream with redirect_stdout(fixes): # call 2to3 fixers main("lib2to3.fixes", [filename]) # get fixes string from text stream fixes_str = fixes.getvalue() except IOError as err: print("I/O error({0}): {1}".format(err.errno, err.strerror)) else: return fixes_str
def makeadopackage(testfolder): adoName = os.path.normpath(os.getcwd() + '/../adodbapi.py') adoPath = os.path.dirname(adoName) if os.path.exists(adoName): newpackage = os.path.join(testfolder, 'adodbapi') os.mkdir(newpackage) for f in os.listdir(adoPath): if f.endswith('.py'): shutil.copy(os.path.join(adoPath, f), newpackage) if sys.version_info >= (3, 0): # only when running Py3.n save = sys.stdout sys.stdout = None from lib2to3.main import main # use 2to3 to make test package main("lib2to3.fixes", args=['-n', '-w', newpackage]) sys.stdout = save return testfolder else: raise EnvironmentError('Connot find source of adodbapi to test.')
def makeadopackage(testfolder): adoName = os.path.normpath(os.getcwd() + '/../adodbapi.py') adoPath = os.path.dirname(adoName) if os.path.exists(adoName): newpackage = os.path.join(testfolder,'adodbapi') os.mkdir(newpackage) for f in os.listdir(adoPath): if f.endswith('.py'): shutil.copy(os.path.join(adoPath, f), newpackage) if sys.version_info >= (3,0): # only when running Py3.n save = sys.stdout sys.stdout = None from lib2to3.main import main # use 2to3 to make test package main("lib2to3.fixes",args=['-n','-w', newpackage]) sys.stdout = save return testfolder else: raise EnvironmentError('Connot find source of adodbapi to test.')
def check_fixture(in_file, out_file, fixer): if fixer: main("pep8ify.fixes", args=['--no-diffs', '--fix', fixer, '-w', in_file]) else: main("pep8ify.fixes", args=[ '--no-diffs', '--fix', 'all', '--fix', 'maximum_line_length', '-w', in_file ]) in_file_contents = open(in_file, 'r').readlines() out_file_contents = open(out_file, 'r').readlines() if in_file_contents != out_file_contents: text = "in_file doesn't match out_file\n" text += ''.join( unified_diff(out_file_contents, in_file_contents, 'expected', 'refactured result')) raise AssertionError(text)
def convert2to3(): """ Convert source to Python 3.x syntax using lib2to3. """ # create a new 2to3 directory for converted source files dst_path = os.path.join(LOCAL_PATH, '2to3') shutil.rmtree(dst_path, ignore_errors=True) # copy original tree into 2to3 folder ignoring some unneeded files def ignored_files(_adir, filenames): return ['.svn', '2to3', 'debian', 'build', 'dist'] + \ [fn for fn in filenames if fn.startswith('distribute')] + \ [fn for fn in filenames if fn.endswith('.egg-info')] shutil.copytree(LOCAL_PATH, dst_path, ignore=ignored_files) os.chdir(dst_path) sys.path.insert(0, dst_path) # run lib2to3 script on duplicated source from lib2to3.main import main print("Converting to Python3 via lib2to3...") main("lib2to3.fixes", ["-w", "-n", "--no-diffs", "obspy"])
def convert2to3(): """ Convert source to Python 3.x syntax using lib2to3. """ # create a new 2to3 directory for converted source files dst_path = os.path.join(LOCAL_PATH, '2to3') shutil.rmtree(dst_path, ignore_errors=True) # copy original tree into 2to3 folder ignoring some unneeded files def ignored_files(adir, filenames): # @UnusedVariable return ['.git', '2to3', 'build', 'dist'] + \ [fn for fn in filenames if fn.startswith('distribute')] + \ [fn for fn in filenames if fn.endswith('.egg-info')] shutil.copytree(LOCAL_PATH, dst_path, ignore=ignored_files) os.chdir(dst_path) sys.path.insert(0, dst_path) # run lib2to3 script on duplicated source from lib2to3.main import main print('Converting to Python3 via lib2to3...') main('lib2to3.fixes', ['-w', '-n', '--no-diffs', 'smsl.py'])
def run_2to3_capture(self, args, in_capture, out_capture, err_capture): save_stdin = sys.stdin save_stdout = sys.stdout save_stderr = sys.stderr sys.stdin = in_capture sys.stdout = out_capture sys.stderr = err_capture try: return main.main("lib2to3.fixes", args) finally: sys.stdin = save_stdin sys.stdout = save_stdout sys.stderr = save_stderr
def test_check_fixture(in_file, fixer, tmpdir): if fixer: main("unittest2pytest.fixes", args=['--no-diffs', '--fix', fixer, '-w', in_file, '--nobackups', '--output-dir', str(tmpdir)]) else: main("unittest2pytest.fixes", args=['--no-diffs', '--fix', 'all', '-w', in_file, '--nobackups', '--output-dir', str(tmpdir)]) result_file_name = tmpdir.join(os.path.basename(in_file)) assert result_file_name.exists(), '%s is missing' % result_file_name result_file_contents = result_file_name.readlines() expected_file = in_file.replace("_in.py", "_out.py") with open(expected_file) as fh: expected_contents = fh.readlines() if result_file_contents != expected_contents: text = "in_file doesn't match out_file\n" text += ''.join(unified_diff(expected_contents, result_file_contents, 'expected', 'refactured result')) raise AssertionError(text)
def test_check_fixture(in_file, fixer, tmpdir): if fixer: main("unittest2pytest.fixes", args=[ '--no-diffs', '--fix', fixer, '-w', in_file, '--nobackups', '--output-dir', str(tmpdir) ]) else: main("unittest2pytest.fixes", args=[ '--no-diffs', '--fix', 'all', '-w', in_file, '--nobackups', '--output-dir', str(tmpdir) ]) result_file_name = tmpdir.join(os.path.basename(in_file)) assert result_file_name.exists(), '%s is missing' % result_file_name result_file_contents = result_file_name.readlines() expected_file = in_file.replace("_in.py", "_out.py") with open(expected_file) as fh: expected_contents = fh.readlines() # ensure the expected code is actually correct and compiles try: compile(''.join(expected_contents), expected_file, 'exec') except Exception as e: pytest.fail("FATAL: %s does not compile: %s" % (expected_file, e), False) if result_file_contents != expected_contents: text = "Refactured code doesn't match expected outcome\n" text += ''.join( unified_diff(expected_contents, result_file_contents, 'expected', 'refactured result')) pytest.fail(text, False)
def command_py2to3(args): """ Apply '2to3' tool (Python2 to Python3 conversion tool) to Python sources. """ from lib2to3.main import main args2 = [] if command_py2to3_work_around3k: if args.no_diffs: args2.append("--no-diffs") if args.write: args2.append("-w") if args.nobackups: args2.append("-n") args2.extend(args.sources) sys.exit(main("lib2to3.fixes", args=args2))
def makeadopackage(testfolder): adoName = os.path.normpath(os.getcwd() + "/../adodbapi.py") adoPath = os.path.dirname(adoName) if os.path.exists(adoName): newpackage = os.path.join(testfolder, "adodbapi") try: os.mkdir(newpackage) except OSErrors: print( "*Note: temporary adodbapi package already exists: may be two versions running?" ) for f in os.listdir(adoPath): if f.endswith(".py"): shutil.copy(os.path.join(adoPath, f), newpackage) if sys.version_info >= (3, 0): # only when running Py3.n save = sys.stdout sys.stdout = None from lib2to3.main import main # use 2to3 to make test package main("lib2to3.fixes", args=["-n", "-w", newpackage]) sys.stdout = save return testfolder else: raise EnvironmentError("Connot find source of adodbapi to test.")
def run_2to3(path, show_diffs=False): from lib2to3.main import main with modified_file(path, no2to3): cmd = [ '-f', 'all', '-f', 'buffer', '-f', 'idioms', '-f', 'set_literal', '-x', 'future', path, ] if not show_diffs: cmd.append('--no-diffs') ret = main('lib2to3.fixes', cmd + [path]) return ret
def _testFixer(self, fixer_name): """check fixer is applied on given path """ HERE = os.path.dirname(__file__) if os.environ['TESTED_PRODUCT'] == "bt5": path = HERE + '/../../../' else: path = HERE + '/../../' path = os.path.normpath(path + os.environ['TESTED_PRODUCT']) orig_stdout = sys.stdout try: # XXX: not thread-safe sys.stdout = stdout = StringIO() returncode = main("lib2to3.fixes", ["--fix", fixer_name, path]) finally: sys.stdout = orig_stdout error = stdout.getvalue() if error: self.fail(error)
def convert_to_py3(script): """将一段Python2代码转化成Python3代码""" if sys.version_info[0] < 3: return script f = tempfile.NamedTemporaryFile(suffix=".py", delete=False) f.write(script.encode()) f.flush() filename = f.name f.close() logging.basicConfig(filename=os.devnull) if main("lib2to3.fixes", ['--no-diffs', '-w', '-n', filename]): raise Exception('py3 conversion failed') f2 = open(filename, 'r') try: return f2.read() finally: f2.close() os.remove(filename)
import sys from lib2to3.main import main sys.exit(main("lib2to3.fixes"))
'Environment :: Console', 'Intended Audience :: Developers', 'License :: OSI Approved', 'Operating System :: OS Independent', 'Programming Language :: C++', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Software Development :: Localization', 'Topic :: Software Development :: Internationalization' ], ext_modules=[ Extension('_icu', [ filename for filename in sorted(os.listdir(os.curdir)) if filename.endswith('.cpp') ], include_dirs=_includes, extra_compile_args=_cflags, extra_link_args=_lflags, libraries=_libraries) ], py_modules=['icu', 'PyICU', 'docs']) if sys.version_info >= (3, ): path = os.path.join('test', '2to3.note') if not os.path.exists(path): from lib2to3.main import main main("lib2to3.fixes", ['-w', '-n', '--no-diffs', 'test']) output = open(path, 'w') output.write('tests auto-converted by 2to3 during setup\n') output.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys from lib2to3.main import main sys.exit(main("pygtk2gtk3.fixes"))
fl = FileList() manifest_file = open("MANIFEST.in") for line in manifest_file: fl.process_template_line(line) manifest_file.close() dir_util.create_tree(tmp_src, fl.files) outfiles_2to3 = [] dist_script = os.path.join("build", "src", "ez_setup.py") for f in fl.files: outf, copied = file_util.copy_file(f, os.path.join(tmp_src, f), update=1) if copied and outf.endswith(".py") and outf != dist_script: outfiles_2to3.append(outf) if copied and outf.endswith('api_tests.txt'): # XXX support this in distutils as well from lib2to3.main import main main('lib2to3.fixes', ['-wd', os.path.join(tmp_src, 'tests', 'api_tests.txt')]) util.run_2to3(outfiles_2to3) # arrange setup to use the copy sys.path.insert(0, os.path.abspath(tmp_src)) src_root = tmp_src from distutils.util import convert_path d = {} init_path = convert_path('setuptools/command/__init__.py') init_file = open(init_path) exec(init_file.read(), d) init_file.close()
import unittest import doctest from lib2to3 import main import shutil import os import sys from test_fixers import IndentFixerTest, Name1FixerTest, Name2FixerTest, ConstantFixerTest from shouldraise import shouldRaise files = [] for filename in ('test-3.11.txt', 'test-4.9.txt',): name2 = os.path.join('source/_tests', filename) name3 = name2 + '.2to3' shutil.copy(name2, name3) files.append(name3) main.main('lib2to3.fixes', args=['-d', '-w', '--no-diffs'] + files) try: 1/0 except ZeroDivisionError: traceback = sys.exc_info()[2] suite = doctest.DocFileSuite('test-1.1.txt', 'test-1.3.txt', 'test-1.5.txt', 'test-1.6.txt', 'test-1.7.txt', 'test-1.12.txt', 'test-2.1.txt', 'test-2.3.txt', 'test-2.4.txt',
line = line.rstrip() if line != '': fl.process_template_line(line) tmp.close() dir_util.create_tree(package_prefix, fl.files) outfiles_2to3 = [] #dist_script = os.path.join("build", "src", "distribute_setup.py") for f in fl.files: outf, copied = file_util.copy_file(f, os.path.join(package_prefix, f), update=1) if copied and outf.endswith(".py"): #and outf != dist_script: outfiles_2to3.append(outf) if copied and outf.endswith('api_tests.txt'): # XXX support this in distutils as well from lib2to3.main import main main('lib2to3.fixes', ['-wd', os.path.join(package_prefix, 'tests', 'api_tests.txt')]) util.run_2to3(outfiles_2to3) # arrange setup to use the copy sys.path.insert(0, package_prefix) src_root = package_prefix print('done.') else: from distutils.core import setup from distutils.core import Extension class build(_build): user_options = _build.user_options + \ [
from __future__ import absolute_import import sys from lib2to3.main import main if __name__ == '__main__': sys.exit(main("fixes")) def ten2eleven(*files): """Convert MDAnalysis 0.10.0 scripts to 0.11.0 Usage: ten2eleven('myscript.py') """ main('MDAnalysis.migration.fixes', ['-w'] + list(files))
import shutil import os import sys from test_fixers import IndentFixerTest, Name1FixerTest, Name2FixerTest, ConstantFixerTest from shouldraise import shouldRaise files = [] for filename in ( 'test-3.11.txt', 'test-4.9.txt', ): name2 = os.path.join('source/_tests', filename) name3 = name2 + '.2to3' shutil.copy(name2, name3) files.append(name3) main.main('lib2to3.fixes', args=['-d', '-w', '--no-diffs'] + files) try: 1 / 0 except ZeroDivisionError: traceback = sys.exc_info()[2] suite = doctest.DocFileSuite( 'test-1.1.txt', 'test-1.3.txt', 'test-1.5.txt', 'test-1.6.txt', 'test-1.7.txt', 'test-1.12.txt', 'test-2.1.txt', 'test-2.3.txt',
def _main(): sys.exit(main("pep8ify.fixes"))
#!/usr/bin/env python import sys from lib2to3.main import main from pathlib import Path if __name__ == "__main__": cur_path = Path().cwd() sys.path.append(str(cur_path / "pytup2019")) sys.exit(main("pytup2019.fixers_2to3"))
fl.process_template_line(line) tmp.close() dir_util.create_tree(package_prefix, fl.files) outfiles_2to3 = [] #dist_script = os.path.join("build", "src", "distribute_setup.py") for f in fl.files: outf, copied = file_util.copy_file(f, os.path.join(package_prefix, f), update=1) if copied and outf.endswith(".py"): #and outf != dist_script: outfiles_2to3.append(outf) if copied and outf.endswith('api_tests.txt'): # XXX support this in distutils as well from lib2to3.main import main main('lib2to3.fixes', [ '-wd', os.path.join(package_prefix, 'tests', 'api_tests.txt') ]) util.run_2to3(outfiles_2to3) # arrange setup to use the copy sys.path.insert(0, package_prefix) src_root = package_prefix print('done.') else: from distutils.core import setup from distutils.core import Extension class build(_build): user_options = _build.user_options + \
#X/usr/bin/env python import sys from lib2to3.main import main sys.exit(main("lib2to3.fixes"))
#!/usr/bin/env python # This works exactly like 2to3, except that it uses Django's fixers rather # than 2to3's built-in fixers. import sys from lib2to3.main import main sys.exit(main("django.utils.2to3_fixes"))
""" Wrapper for lib2to3 main to use custom fixes folder """ import sys from lib2to3.main import main sys.exit(main('fixes'))
#!/usr/bin/env python import sys from lib2to3.main import main sys.exit(main("custom_fixers"))
'Environment :: Console', 'Intended Audience :: Developers', 'License :: OSI Approved', 'Operating System :: OS Independent', 'Programming Language :: C++', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Software Development :: Localization', 'Topic :: Software Development :: Internationalization'], ext_modules=[Extension('_icu', [filename for filename in os.listdir(os.curdir) if filename.endswith('.cpp')], include_dirs=_includes, extra_compile_args=_cflags, extra_link_args=_lflags, libraries=_libraries)], py_modules=['icu', 'PyICU', 'docs']) if sys.version_info >= (3,): path = os.path.join('test', '2to3.note') if not os.path.exists(path): from lib2to3.main import main main("lib2to3.fixes", ['-w', '-n', '--no-diffs', 'test']) output = open(path, 'w') output.write('tests auto-converted by 2to3 during setup\n') output.close()
def main(): from lib2to3.main import main return main("lib2to3.fixes")
for line in open("MANIFEST.in"): fl.process_template_line(line) dir_util.create_tree(tmp_src, fl.files) outfiles_2to3 = [] dist_script = os.path.join("build", "src", "distribute_setup.py") for f in fl.files: outf, copied = file_util.copy_file(f, os.path.join(tmp_src, f), update=1) if copied and outf.endswith(".py") and outf != dist_script: outfiles_2to3.append(outf) if copied and outf.endswith('api_tests.txt'): # XXX support this in distutils as well from lib2to3.main import main main( 'lib2to3.fixes', ['-wd', os.path.join(tmp_src, 'tests', 'api_tests.txt')]) util.run_2to3(outfiles_2to3) # arrange setup to use the copy sys.path.insert(0, tmp_src) src_root = tmp_src from distutils.util import convert_path d = {} init_path = convert_path('setuptools/command/__init__.py') exec(open(init_path).read(), d) SETUP_COMMANDS = d['__all__']
import os import sys from lib2to3 import main sys.path.append( os.path.dirname( os.path.dirname( __file__ ) ) ) sys.exit( main.main( "9to10" ) )
def run_test(self, code, *params, **interface): """ Test if a function call return value is equal for Pythran and Pythran. Args: code (str): python (pythran valid) module to test. params (tuple): arguments to pass to the function to test. prelude (fct): function to call between 'code' and the c++ generated code interface (dict): pythran interface for the module to test. Each key is the name of a function to call, the value is a list of the arguments' type. Special keys are 'prelude' and 'check_exception'. Returns: nothing. Raises: AssertionError by 'unittest' if return value differ. SyntaxError if code is not python valid. pythran.CompileError if generated code can't be compiled. ...possibly others... """ # Extract special keys from interface. prelude = interface.pop('prelude', None) check_exception = interface.pop('check_exception', False) assert len(interface) == 1 name = next(iter(interface.keys())) modname = "test_" + name code = dedent(code) if sys.version_info.major == 3: from tempfile import NamedTemporaryFile from lib2to3 import main as lib2to3 tmp_py = NamedTemporaryFile(suffix='.py', delete=False) tmp_py.write(code.encode('ascii')) tmp_py.close() lib2to3.main('lib2to3.fixes', [tmp_py.name, '-w', '-n']) code = open(tmp_py.name).read() os.remove(tmp_py.name) cxx_compiled = compile_pythrancode( modname, code, interface, extra_compile_args=self.PYTHRAN_CXX_FLAGS) # FIXME Check should be done on input parameters after function call python_ref = self.run_python(code, (name, copy.deepcopy(params)), prelude, check_exception) pythran_res = self.run_pythran(modname, cxx_compiled, (name, params), prelude, check_exception) if check_exception: if pythran_res != python_ref: raise AssertionError( "expected exception was %s, but received %s" % (python_ref, pythran_res)) print("Python result: ", python_ref) print("Pythran result: ", pythran_res) self.assertAlmostEqual(python_ref, pythran_res)
break yield "# end Py3K" def consume_py2k(): yield "# start Py2K" while lines: line = lines.pop(0) if not end_py2k_pattern.match(line): yield "#%s" % line else: break yield "# end Py2K" return "\n".join(consume_normal()) old_refactor_string = refactor.RefactoringTool.refactor_string def refactor_string(self, data, name): newdata = preprocess(data) tree = old_refactor_string(self, newdata, name) if tree: if newdata != data: tree.was_changed = True return tree if __name__ == '__main__': refactor.RefactoringTool.refactor_string = refactor_string main.main("lib2to3.fixes")