Created
October 9, 2012 08:50
-
-
Save bmispelon/3857460 to your computer and use it in GitHub Desktop.
Python offset bug
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| #-*- coding:utf-8 -*- | |
| SYNTAXERROR_TEMPLATE = "%s = ())" # Unbalanced parentheses | |
| def get_offset(varname): | |
| """Trigger a syntax error that uses the given variable name and return the | |
| exception's offset, e.g. the position of the syntax error. | |
| """ | |
| try: | |
| exec(SYNTAXERROR_TEMPLATE % varname) | |
| except SyntaxError as e: | |
| return e.offset | |
| def get_expected_offset(varname): | |
| return len(SYNTAXERROR_TEMPLATE % varname) # The error is on the last paren | |
| # Three identifiers of length 1 that use 1, 2 and 3 bytes respectively | |
| VARNAMES = ['a', 'é', '蟒'] | |
| for v in VARNAMES: | |
| print("Expected: %s, Got: %s" % (get_expected_offset(v), get_offset(v))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment