Alexandre CONRAD
2007-05-24 13:49:25 UTC
Hello,
exceptions.NameError: Undefined
Okay, this error has happend to me more than once (way more). I know
that this means something similar to "you're trying to access a variable
that has not yet been declared".
Luckly, I usually knew where it came from because it was obvious enough
to find the problem after some code change. But I've wasted so much time
trying to figure out where this came from.
Would it be possible to make it more verbose somehow ? Like showing the
original name of the variable and it's line number in the template
itself ? How am I supposed to know where this comes from with such a
traceback ?
So I digged into the code and modified it as followed to fit my needs.
-- mako/runtime.py --
class Undefined(object):
"""represents an undefined value in a template."""
def __init__(self, name, filename):
self.name = name
self.filename = filename
def __str__(self):
# raise NameError("Undefined")
raise NameError("Undefined name '%s' in file '%s'" %
(self.name, self.filename))
def __nonzero__(self):
return False
UNDEFINED = Undefined # Not initialized !
--------------------
-- mako/codegen.py around line 292 --
if getattr(self.compiler, 'has_ns_imports', False):
# self.printer.writeline("%s = _import_ns.get(%s,
context.get(%s, UNDEFINED))" % (ident, repr(ident), repr(ident)))
self.printer.writeline("%s = _import_ns.get(%s,
context.get(%s, UNDEFINED(%s, %s)))" % (ident, repr(ident), repr(ident),
repr(ident), repr(self.compiler.filename)))
else:
# self.printer.writeline("%s = context.get(%s,
UNDEFINED)" % (ident, repr(ident)))
self.printer.writeline("%s = context.get(%s,
UNDEFINED(%s, %s))" % (ident, repr(ident), repr(ident),
repr(self.compiler.filename)))
---------------
Same here for readability: http://rafb.net/p/0VKRMH87.html
${i_dont_exist} raises:
exceptions.NameError: Undefined name 'i_dont_exist' in file
'/path/to/erroneous/file.mako'
I couldn't figure out how to put the line number in here, but at least
this helps me _much_ more.
Anyway, my application seems to run fine like this but I suppose this
won't work as it will break the following (from mako documentation):
"""Since UNDEFINED is a singleton object just like Python's True or
False, you can use the is operator to check for it"""
Regards,
exceptions.NameError: Undefined
Okay, this error has happend to me more than once (way more). I know
that this means something similar to "you're trying to access a variable
that has not yet been declared".
Luckly, I usually knew where it came from because it was obvious enough
to find the problem after some code change. But I've wasted so much time
trying to figure out where this came from.
Would it be possible to make it more verbose somehow ? Like showing the
original name of the variable and it's line number in the template
itself ? How am I supposed to know where this comes from with such a
traceback ?
So I digged into the code and modified it as followed to fit my needs.
-- mako/runtime.py --
class Undefined(object):
"""represents an undefined value in a template."""
def __init__(self, name, filename):
self.name = name
self.filename = filename
def __str__(self):
# raise NameError("Undefined")
raise NameError("Undefined name '%s' in file '%s'" %
(self.name, self.filename))
def __nonzero__(self):
return False
UNDEFINED = Undefined # Not initialized !
--------------------
-- mako/codegen.py around line 292 --
if getattr(self.compiler, 'has_ns_imports', False):
# self.printer.writeline("%s = _import_ns.get(%s,
context.get(%s, UNDEFINED))" % (ident, repr(ident), repr(ident)))
self.printer.writeline("%s = _import_ns.get(%s,
context.get(%s, UNDEFINED(%s, %s)))" % (ident, repr(ident), repr(ident),
repr(ident), repr(self.compiler.filename)))
else:
# self.printer.writeline("%s = context.get(%s,
UNDEFINED)" % (ident, repr(ident)))
self.printer.writeline("%s = context.get(%s,
UNDEFINED(%s, %s))" % (ident, repr(ident), repr(ident),
repr(self.compiler.filename)))
---------------
Same here for readability: http://rafb.net/p/0VKRMH87.html
${i_dont_exist} raises:
exceptions.NameError: Undefined name 'i_dont_exist' in file
'/path/to/erroneous/file.mako'
I couldn't figure out how to put the line number in here, but at least
this helps me _much_ more.
Anyway, my application seems to run fine like this but I suppose this
won't work as it will break the following (from mako documentation):
"""Since UNDEFINED is a singleton object just like Python's True or
False, you can use the is operator to check for it"""
Regards,
--
Alexandre CONRAD
Alexandre CONRAD