Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@anthrotype
Copy link
Member

This round thing is killing me..

There's a problem with the built-in round() function on Python 3.5.

If one passes None as a second argument, and the first argument is a float, everything is ok

>>> round(1.0, None)
1

But if the first argument is an integer and you pass None as second argument, it raises:

>>> round(1, None)
TypeError: 'NoneType' object cannot be interpreted as an integer

Now, this happens to be a known issue (https://bugs.python.org/issue27936) which has been fixed in Python 3.6.

Here, on Python3 below 3.6, I have to wrap the built-in round() so that when it gets None as second argument, it behaves as if one did not provide the second argument at all.

See also robotools/fontMath#37 (comment)

@adrientetar
Copy link
Member

Is it common to call round with None?

@anthrotype
Copy link
Member Author

well, fontMath does.
And with our own pure-python round3 (used on Python 2 only) we do allow to pass ndigits=None, so it would be an inconsistency if on Python2 we allow round3 having a second None argument, whereas on Python3 we don't (or we do, but only if the first arg is a float..)

if sys.version_info[:2] >= (3, 6):
# in Python 3.6, 'round3' is an alias to the built-in 'round'
round = round3 = round
else:
Copy link
Member

Choose a reason for hiding this comment

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

Looking at https://bugs.python.org/issue27936 it seems the issue only affects 3.5 (right?) so you could do if sys.version_info[:2] == (3, 5)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Python 3.3 and 3.4 give the same error.

@anthrotype anthrotype merged commit f59c3c5 into fonttools:master Dec 4, 2016
@anthrotype anthrotype deleted the fix-round3-None branch December 4, 2016 10:56
anthrotype pushed a commit to robotools/fontMath that referenced this pull request Dec 4, 2016
…as the pure-python backport already accepts a second None argument

It is only the built-in round() in Python 3.x (< 3.6) that has that inconsistent behavior (fonttols round3 is an alias of the built-in round in Python 3), not the
pure-python round3 backport used on Python 2.7.

Note that the current fonttools HEAD contains the same workaround:
fonttools/fonttools#757

We don't want to duplicate it here, so we shall drop this and update fontMath's
minimum fonttools requirement as soon as a new version of fonttools is out, which
is very soon.
anthrotype pushed a commit to robotools/fontMath that referenced this pull request Dec 13, 2016
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.

3 participants