, python . AST, , , - .
, Python . :
import parser
from token import tok_name
def checkForNonUnicode(codeString):
return checkForNonUnicodeHelper(parser.suite(codeString).tolist())
def checkForNonUnicodeHelper(lst):
returnValue = True
nodeType = lst[0]
if nodeType in tok_name and tok_name[nodeType] == 'STRING':
stringValue = lst[1]
if stringValue[0] != "u":
print "%s is not unicode!" % stringValue
returnValue = False
else:
for subNode in [lst[n] for n in range(1, len(lst))]:
if isinstance(subNode, list):
returnValue = returnValue and checkForNonUnicodeHelper(subNode)
return returnValue
print checkForNonUnicode("""
def foo():
a = 'This should blow up!'
""")
print checkForNonUnicode("""
def bar():
b = u'although this is ok.'
""")
'This should blow up!' is not unicode!
False
True
doc unicode, , - , from symbol import sym_name, node . -node , .. - . Unicode.
!
Edit
. , parser.suite Python. , Python, . , , myObscureUtilityFile.py,
from ..obscure.relative.path import whatever
checkForNonUnicode(open('/whoah/softlink/myObscureUtilityFile.py').read())