New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Porting to Python 3, part 4 ("final") #558
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
adbc076
doc/src/gotchas: Make type() doctest Python 3 compatible
vperic 56c3969
doc/../evalf.txt: Skip some doctests related to float()
vperic d12ea8f
utilities/runtest: Wrap a str.encode with a version check
vperic 07f343b
ntheory/factor_: Don't use reduce in doctest
vperic 8b4bb4c
utilities/runtests: Fix doctest runner for Python 3
vperic b239fc2
Fix 2to3 TokenError parsing errors
vperic 7f7b86c
physics/wigner: Explicitly cast range() arguments to int
vperic 55e0bf1
ntheory/factor_: Fix namespace leak from list comprehension
vperic 0319a91
doc/src/*.txt: Use init_printing() instead of setting the displayhook
vperic 1dec4c5
printing/printer: Raise AttributeError in a property
vperic f732c4e
matrices.py: Raise AttributeError in a property
vperic 49f0686
combinatorics/permutations: Use floor division
vperic 6121523
Add bin/use2to3 script
vperic b0edd91
utilities/runtest: Make findout_terminal_width Python 3 compatible
vperic 055a166
quantum/state: Use floor division in doctest
vperic 300e926
quantum/__init__: Change imports syntax for Python 3 compatibility
vperic b448dbe
doc/src/tutorial: Misc improvements to the documentation
smichr File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ MANIFEST | |
my/ | ||
dist/ | ||
build/ | ||
sympy-py3k/ | ||
|
||
tox.ini | ||
.tox/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
This script converts SymPy code to a Python 3-compatible version. | ||
|
||
The script copies all files except the ones related to mpmath to a sympy-py3k | ||
directory, runs 2to3 on them and then copies the vanilla mpmath files over. We | ||
need this because running 2to3 on mpmath (which is already Python 3 compatible) | ||
produces errors. You can then use SymPy normally from the sympy-py3k directory | ||
(installing it or importing it directly). | ||
|
||
Because copying and running 2to3 can take a lot of time, we try to do it only on | ||
files that have been modified since the last run. | ||
|
||
Note that the 2to3 shipped with Python 2.6 crashes when converting doctests. It | ||
is recommended to use the Python 3.2 version (or newer) as it is much faster. | ||
|
||
TODO: Add --destination argument (others?) | ||
--destination # copy over the source to a user-specified destination | ||
""" | ||
import os | ||
import fnmatch | ||
import shutil | ||
|
||
destination = "sympy-py3k" # directory to copy to | ||
|
||
# TODO: build this from .gitignore | ||
skip_dirs = ( | ||
'.*', # skip hidden dirs; .git and .tox in particular can be quite big | ||
'mpmath', # everything related to mpmath, both in doc/ and sympy/ | ||
'_build', # files built by Sphinx | ||
'__pycache__', | ||
'covhtml', # files produced by bin/test_coverage | ||
'my', # the user can have stuff here we don't want to copy | ||
destination # this prevents infinite recursion if the dir already exists | ||
) | ||
|
||
skip_files = ( | ||
'*.pyc', | ||
'.*', | ||
'ast_parser_python25.py', # this files produces doctest errors under py3k | ||
# as we need it only for 2.5, just skip copying it | ||
) | ||
|
||
modified_files = [] | ||
modified_txt_files = [] | ||
|
||
# we need to run 2to3 on .txt files; however, not all .txt files are doctests, | ||
# so we need a list of files we care about | ||
relevant_txt_files = [] | ||
|
||
# generate the relevant txt files | ||
# most of them should be in this directory: | ||
for root, dirs, files in os.walk('./doc/src/modules'): | ||
# NOTE: this will consider mpmath-related files relevant, but it doesn't matter | ||
for filename in fnmatch.filter(files, '*.txt'): | ||
relevant_txt_files.append(os.path.join(root,filename)) | ||
|
||
# some files aren't in /doc/src/modules, add them explicitly | ||
relevant_txt_files.append('./doc/src/tutorial.txt') | ||
relevant_txt_files.append('./doc/src/gotchas.txt') | ||
relevant_txt_files.append('./doc/src/guide.txt') | ||
relevant_txt_files.append('./doc/src/python-comparisons.txt') | ||
|
||
# walk the tree and copy over files as necessary | ||
for root, dirs, files in os.walk('.'): | ||
for pattern in skip_dirs: | ||
for directory in fnmatch.filter(dirs, pattern): | ||
dirs.remove(directory) | ||
for pattern in skip_files: | ||
for filename in fnmatch.filter(files, pattern): | ||
files.remove(filename) | ||
for directory in dirs: | ||
dstdir = os.path.join(destination, root, directory) | ||
if not os.path.exists(dstdir): | ||
os.makedirs(dstdir) | ||
for filename in files: | ||
src = os.path.join(root, filename) | ||
dst = os.path.join(destination, root, filename) | ||
if os.path.isfile(dst): | ||
if os.path.getmtime(src) - os.path.getmtime(dst) < 1: | ||
# the file hasn't been modified since the last run, so skip it | ||
# we check for one second of difference because Python can be | ||
# imprecise (when copying) with smaller time periods | ||
continue | ||
shutil.copy2(src, dst) | ||
# add to the list of files to pass to 2to3 if needed | ||
if filename.endswith(".py"): | ||
modified_files.append(dst) | ||
elif filename.endswith(".txt"): | ||
# we need to check the exact path here, not just the filename | ||
# as there are eg. multiple index.txt files and not all are relevant | ||
if src in relevant_txt_files: | ||
modified_txt_files.append(dst) | ||
|
||
|
||
# arguments to call 2to3 with | ||
args_2to3 = [ | ||
"-w", # writes back the changes | ||
"-n", # doesn't write a backup file | ||
"--no-diffs", # don't show the diffs for individual files | ||
] | ||
|
||
args_2to3_doctests = args_2to3 + ["-d"] # convert doctests too | ||
|
||
# extend the argument list with the list of files that need it | ||
args_2to3.extend(modified_files) | ||
args_2to3_doctests.extend(modified_files) | ||
args_2to3_doctests.extend(modified_txt_files) | ||
|
||
# call 2to3, once for regular files and once for doctests | ||
from lib2to3.main import main as main2to3 | ||
main2to3("lib2to3.fixes", args_2to3) | ||
main2to3("lib2to3.fixes", args_2to3_doctests) | ||
|
||
# once we are finished with everything, we should finally copy over the files | ||
# provided by mpmath; these should all be in the following two directories | ||
|
||
# to skip checking if something has been updated, just copy everything always | ||
# the main bottleneck is running 2to3, not copying files | ||
# TODO: only copy updated files; this would need a heavy modification to the | ||
# above code, or copy-pasting the relevant part over | ||
try: | ||
shutil.rmtree(os.path.join(destination, "./sympy/mpmath")) | ||
shutil.rmtree(os.path.join(destination, "./doc/src/modules/mpmath")) | ||
except OSError: # directories don't exist | ||
pass | ||
|
||
shutil.copytree("sympy/mpmath", os.path.join(destination, "./sympy/mpmath")) | ||
shutil.copytree("doc/src/modules/mpmath", os.path.join(destination, "./doc/src/modules/mpmath")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to avoid the long line can this be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the line is just one character over 80 so I think it's fine. I can change it though, it's not a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never mind. I can go either way.