blob: 4181572aed3c8be7a5d6e3a6fc8e35cba326b189 [file] [log] [blame]
from pydevd_file_utils import GetFileNameAndBaseFromFile
import pydev_log
import traceback
from pydevd_constants import DictContains
def read_file(filename):
f = open(filename, "r")
try:
s = f.read()
finally:
f.close()
return s
def offset_to_line_number(text, offset):
curLine = 1
curOffset = 0
while curOffset < offset:
if curOffset == len(text):
return -1
c = text[curOffset]
if c == '\n':
curLine += 1
elif c == '\r':
curLine += 1
if curOffset < len(text) and text[curOffset + 1] == '\n':
curOffset += 1
curOffset += 1
return curLine
def get_source(frame):
try:
node = frame.f_locals['self']
if hasattr(node, 'source'):
return node.source
else:
pydev_log.error_once(
"WARNING: Template path is not available. Please set TEMPLATE_DEBUG=True "
"in your settings.py to make django template breakpoints working")
return None
except:
pydev_log.debug(traceback.format_exc())
return None
def get_template_file_name(frame):
try:
source = get_source(frame)
if source is None:
pydev_log.debug("Source is None\n")
return None
fname = source[0].name
if fname == '<unknown source>':
pydev_log.debug("Source name is %s\n" % fname)
return None
else:
filename, base = GetFileNameAndBaseFromFile(fname)
return filename
except:
pydev_log.debug(traceback.format_exc())
return None
def get_template_line(frame, template_frame_file):
source = get_source(frame)
try:
return offset_to_line_number(read_file(template_frame_file), source[1][0])
except:
return None
class DjangoTemplateFrame:
def __init__(
self,
frame,
template_frame_file=None,
template_frame_line=None):
if template_frame_file is None:
template_frame_file = get_template_file_name(frame)
self.back_context = frame.f_locals['context']
self.f_code = FCode('Django Template', template_frame_file)
if template_frame_line is None:
template_frame_line = get_template_line(frame, template_frame_file)
self.f_lineno = template_frame_line
self.f_back = frame
self.f_globals = {}
self.f_locals = self.collect_context()
self.f_trace = None
def collect_context(self):
res = {}
try:
for d in self.back_context.dicts:
res.update(d)
except AttributeError:
pass
return res
def changeVariable(self, name, value):
for d in self.back_context.dicts:
if DictContains(d, name):
d[name] = value
self.f_locals[name] = value
class FCode:
def __init__(self, name, filename):
self.co_name = name
self.co_filename = filename
def is_django_exception_break_context(frame):
try:
return frame.f_code.co_name in ['_resolve_lookup', 'find_template']
except:
return False
def just_raised(trace):
if trace is None:
return False
return trace.tb_next is None