Skip to content

Instantly share code, notes, and snippets.

@stephanh42
Last active November 22, 2017 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephanh42/61eceadc2890cf1b53ada5e48ef98ad1 to your computer and use it in GitHub Desktop.
Save stephanh42/61eceadc2890cf1b53ada5e48ef98ad1 to your computer and use it in GitHub Desktop.
Check a Python source code if it contains identifiers which are confusable.
"""Check a Python source code if it contains
identifiers which are confusable.
Usage:
python confusable_check.py < my_source_code.py
Needs the confusables package:
python -m pip install confusables
"""
import tokenize
import confusables
import sys
identifiers = {}
status = 0
for token in tokenize.tokenize(sys.stdin.buffer.readline):
skel = confusables.skeleton(token.string)
token2 = identifiers.get(skel)
if token2 is not None and token.string != token2.string:
sys.stderr.write("Confusable: {} and {}\n".format(token2.string, token.string))
sys.stderr.write("{}: {}".format(token2.start[0], token2.line))
sys.stderr.write("{}: {}".format(token.start[0], token.line))
status = 1
identifiers[skel] = token
sys.exit(status)
MIT License
Copyright (c) 2017 Stephan Houben
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment