-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong line number for UndefinedError exceptions #276
Comments
Any ideas? |
I use something like this: class UndefinedVar(jinja2.Undefined):
def __getattribute__(self, name, *args, **kwargs):
try:
return super(UndefinedVar, self).__getattribute__(name, *args, **kwargs)
except AttributeError:
if settings.TEMPLATE_DEBUG:
import inspect
f = inspect.currentframe().f_back.f_back.f_code
file_name = f.co_filename
lineno = f.co_firstlineno
warnings.warn("[{}:{}] Trying to access attribute '{}' on undefined variable '{}'".format(file_name, lineno, name, self._undefined_name))
return UndefinedVar()
return None |
@Naddiseo Can you provide a more complete example? I don't see how this fits into my example. |
Nevermind, after looking at the jinja2 source code it seems like when I instantiate the class I just need to use |
Has there been any progress in resolving this bug? |
The reason this issue is so hard to fix is because jinja compiles the templates to python code, so when there's an exception you get the traceback of the compiled code. One (ugly) approach I can think of is to use the backtrace data to get the python line number, then use the linemapping data that jinja puts in the same file to try and calculate what the template line number would be. |
@terminalmage I'm not sure I understand this issue and actually stumbled over here from this saltstack issue where I seem to get the line number as can be seen in my comment. Is this what you were looking for? |
@rbjorklin I think you misread this issue. The problem isn't that jinja doesn't give a line number (a perusal of my original post will show you that it does), the problem is that it gives the wrong line number, for reasons upon which others have elaborated. |
I think I hit this same issue with Raven where the stack information collection fails if the call stack contains jinja templates. The wrong line numbers cause |
…ineno in the traceback
This comment has been minimized.
This comment has been minimized.
…ineno in the traceback
@davidism Thanks! |
When using
jinja2.StrictUndefined
and rendering a template, the line numbers seem to be off when anUndefinedError
exception is triggered:The
missing
variable is on line 2, not line 3. However, when the variables are properly defined, the value for themissing
variable is on line 2 like you would expect.The same line number mismatch is seen when loading the template from a file, as well:
So,
missing
is undefined, yet the traceback shows theexists
line.The above testing was done using version 2.7.1.
The text was updated successfully, but these errors were encountered: