Skip to content

Instantly share code, notes, and snippets.

@bmispelon
Created October 9, 2012 08:50
Show Gist options
  • Select an option

  • Save bmispelon/3857460 to your computer and use it in GitHub Desktop.

Select an option

Save bmispelon/3857460 to your computer and use it in GitHub Desktop.
Python offset bug
#!/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