Skip to content

Fix compatibility issues with Python >= 3.6 #352

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

Merged
merged 2 commits into from
May 15, 2018

Conversation

egao1980
Copy link
Contributor

@egao1980 egao1980 commented May 14, 2018

  • \$ and \. trigger SyntaxError in Python 3.6
  • % format operator is not compatible with Python 3.6

@ShaneHarvey
Copy link
Member

Hi @egao1980. What do you mean "$ and . trigger SyntaxError in Python 3.6"?

$ python3.6
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> {"$regex": "^(?!.*\$)"}
{'$regex': '^(?!.*\\$)'}
>>> ^D
$ python3.7
Python 3.7.0b4 (v3.7.0b4:eb96c37699, May  2 2018, 04:13:13)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> {"$regex": "^(?!.*\$)"}
{'$regex': '^(?!.*\\$)'}
>>>

@egao1980
Copy link
Contributor Author

Markdown ate my slashes. Python 3.6 do not allow non standard escapes. So we need to use raw strings or escape the backslash

@ShaneHarvey
Copy link
Member

ShaneHarvey commented May 14, 2018

But what problem are you trying to solve? This code already works on all versions of Python (besides benchmark.py).

@egao1980
Copy link
Contributor Author

egao1980 commented May 14, 2018

This breaks strict tests with 'python -We ...'

python3.6 -We
Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> '\s'
  File "<stdin>", line 1
SyntaxError: invalid escape sequence \s
>>> '\$'
  File "<stdin>", line 1
SyntaxError: invalid escape sequence \$
>>> '\.'
  File "<stdin>", line 1
SyntaxError: invalid escape sequence \.

@@ -87,7 +88,7 @@ def timed(name, function, args=[], setup=None):
function(*args)
times.append(time.time() - start)
best_time = min(times)
print "%s%d" % (name + (60 - len(name)) * ".", per_trial / best_time)
print("{:s}{:d}".format(name + (60 - len(name)) * ".", per_trial / best_time))
Copy link
Member

@ShaneHarvey ShaneHarvey May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be {0}{1} to support python 2.6. Or you can just use % formatting as before which is valid on all python versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will make sure that 2.6 version works.

@ShaneHarvey
Copy link
Member

Ah I see. That is a problem. My only comment is on the change to benchmark.py.

@ShaneHarvey ShaneHarvey merged commit 72cab93 into mongodb:master May 15, 2018
@ShaneHarvey
Copy link
Member

@egao1980 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants