diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-03-27 21:49:08 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-03-28 08:43:26 -0600 |
commit | d4b88c1dbd6898fb6fcebc97f36b421999340f71 (patch) | |
tree | 61cc0282cf2509afe364c91e97b59dfb2ebcafd3 /tools/py3tool.py | |
parent | 40742184df68fc01f3392c9865f35d5402e74b01 (diff) | |
download | numpy-d4b88c1dbd6898fb6fcebc97f36b421999340f71.tar.gz |
2to3: Use absolute imports.
The new import `absolute_import` is added the `from __future__ import`
statement and The 2to3 `import` fixer is run to make the imports
compatible. There are several things that need to be dealt with to make
this work.
1) Files meant to be run as scripts run in a different environment than
files imported as part of a package, and so changes to those files need
to be skipped. The affected script files are:
* all setup.py files
* numpy/core/code_generators/generate_umath.py
* numpy/core/code_generators/generate_numpy_api.py
* numpy/core/code_generators/generate_ufunc_api.py
2) Some imported modules are not available as they are created during
the build process and consequently 2to3 is unable to handle them
correctly. Files that import those modules need a bit of extra work.
The affected files are:
* core/__init__.py,
* core/numeric.py,
* core/_internal.py,
* core/arrayprint.py,
* core/fromnumeric.py,
* numpy/__init__.py,
* lib/npyio.py,
* lib/function_base.py,
* fft/fftpack.py,
* random/__init__.py
Closes #3172
Diffstat (limited to 'tools/py3tool.py')
-rwxr-xr-x | tools/py3tool.py | 45 |
1 files changed, 3 insertions, 42 deletions
diff --git a/tools/py3tool.py b/tools/py3tool.py index ef175ebfd..656327450 100755 --- a/tools/py3tool.py +++ b/tools/py3tool.py @@ -18,7 +18,7 @@ When running py3tool again, only changed files are re-processed, which makes the test-bugfix cycle faster. """ -from __future__ import division +from __future__ import division, absolute_import from optparse import OptionParser import shutil @@ -37,10 +37,6 @@ TEMP = os.path.normpath(os.path.join(BASE, '_py3k')) SCRIPT_2TO3 = os.path.join(BASE, 'tools', '2to3.py') EXTRA_2TO3_FLAGS = { - '*/setup.py': '-x import', - 'numpy/core/code_generators/generate_umath.py': '-x import', - 'numpy/core/code_generators/generate_numpy_api.py': '-x import', - 'numpy/core/code_generators/generate_ufunc_api.py': '-x import', 'numpy/core/defchararray.py': '-x unicode', 'numpy/compat/py3k.py': '-x unicode', 'numpy/ma/timer_comparison.py': 'skip', @@ -80,7 +76,8 @@ FIXES_TO_SKIP = [ 'input', 'raw_input', 'xreadlines', - 'xrange' + 'xrange', + 'import' ] skip_fixes= [] @@ -172,38 +169,6 @@ except: # Run nosetests subprocess.call(['nosetests3', '-v', d], cwd=TEMP) -def custom_mangling(filename): - import_mangling = [ - os.path.join('core', '__init__.py'), - os.path.join('core', 'numeric.py'), - os.path.join('core', '_internal.py'), - os.path.join('core', 'arrayprint.py'), - os.path.join('core', 'fromnumeric.py'), - os.path.join('numpy', '__init__.py'), - os.path.join('lib', 'npyio.py'), - os.path.join('lib', 'function_base.py'), - os.path.join('fft', 'fftpack.py'), - os.path.join('random', '__init__.py'), - ] - - if any(filename.endswith(x) for x in import_mangling): - f = open(filename, 'r') - text = f.read() - f.close() - for mod in ['multiarray', 'scalarmath', 'umath', '_sort', - '_compiled_base', 'core', 'lib', 'testing', 'fft', - 'polynomial', 'random', 'ma', 'linalg', 'compat', - 'mtrand', '_dotblas', 'version']: - text = re.sub(r'^(\s*)import %s' % mod, - r'\1from . import %s' % mod, - text, flags=re.M) - text = re.sub(r'^(\s*)from %s import' % mod, - r'\1from .%s import' % mod, - text, flags=re.M) - text = text.replace('from matrixlib', 'from .matrixlib') - f = open(filename, 'w') - f.write(text) - f.close() def walk_sync(dir1, dir2, _seen=None): if _seen is None: @@ -313,10 +278,6 @@ def sync_2to3(src, dst, patchfile=None, clean=False): finally: sys.stdout = _old_stdout - for fn, dst_fn in to_convert: - # perform custom mangling - custom_mangling(dst_fn) - p.close() if __name__ == "__main__": |