🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@methane
Copy link
Member

@methane methane commented Feb 22, 2018

No description provided.

Lib/ipaddress.py Outdated
raise ValueError("Only hex digits permitted in %r" % hextet_str)
# Reject non-ASCII characters, '+' and '-', since int() accepts them.
msg = "Only hex digits permitted in %r"
if not hextet_str.isascii() or not hextet_str.isalnum():
Copy link
Member

Choose a reason for hiding this comment

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

'z' and '_' shouldn't be permitted.

Copy link
Member Author

Choose a reason for hiding this comment

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

They are rejected by int()

Copy link
Member

Choose a reason for hiding this comment

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

'0x12' is accepted by int().

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. I'll revert it.

Lib/ipaddress.py Outdated
# int allows a leading +/- as well as surrounding whitespace,
# so we ensure that isn't the case
if not _BaseV4._DECIMAL_DIGITS.issuperset(prefixlen_str):
if not prefixlen_str.isascii() or not prefixlen_str.isdigit():
Copy link
Member

Choose a reason for hiding this comment

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

Is _DECIMAL_DIGITS still used?

@serhiy-storchaka
Copy link
Member

What is the benefit of rewriting the code?

@methane
Copy link
Member Author

methane commented Feb 23, 2018

What is the benefit of rewriting the code?

I found these code and I felt "Python should have str.isascii()". I proposed it and it's implemented.
That's why I create this pull request.

There are a little performance gain too.

./python -m perf timeit -s 'import ipaddress,importlib' -- 'importlib.reload(ipaddress)'
master:  Mean +- std dev: 1.95 ms +- 0.04 ms
isascii: Mean +- std dev: 1.88 ms +- 0.03 ms

./python -m perf timeit -s 'from ipaddress import ip_address' -- 'ip_address("10.1.2.3")'
master:  Mean +- std dev: 8.27 us +- 0.13 us
isascii: Mean +- std dev: 7.51 us +- 0.13 us

@methane
Copy link
Member Author

methane commented Feb 23, 2018

BTW, which is readable?

-        if not prefixlen_str.isascii() or not prefixlen_str.isdigit():
+        if not (prefixlen_str.isascii() and prefixlen_str.isdigit()):

@serhiy-storchaka
Copy link
Member

I would prefer the second form. Both are compiled to the same bytecode.

@methane methane merged commit 58a1096 into python:master Feb 23, 2018
@methane methane deleted the ipaddress-isascii branch February 23, 2018 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants