<commit_before>from django.conf import settings
from django.core.template import Template, Context
from django.utils.html import escape
from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound
import inspect, os, re, sys
from itertools import count, izip
from os.path import dirname, join as pathjoin

HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD')

def linebreak_iter(template_source):
    newline_re = re.compile("^", re.M)
    for match in newline_re.finditer(template_source):
        yield match.start()
    yield len(template_source) + 1

def get_template_exception_info(exc_type, exc_value, tb):
    origin, (start, end) = exc_value.source
    template_source = origin.reload()
    context_lines = 10
    line = 0
    upto = 0
    source_lines = []
    linebreaks = izip(count(0), linebreak_iter(template_source))
    linebreaks.next() # skip the nothing before initial line start
    for num, next in linebreaks:
        if start >= upto and end <= next:
            line = num
            before = escape(template_source[upto:start])
            during = escape(template_source[start:end])
            after = escape(template_source[end:next - 1])
        source_lines.append( (num, escape(template_source[upto:next - 1])) )
        upto = next
    total = len(source_lines)

    top = max(0, line - context_lines)
    bottom = min(total, line + 1 + context_lines)

    template_info = {
        'message': exc_value.args[0],
        'source_lines': source_lines[top:bottom],
        'before': before,
        'during': during,
        'after': after,
        'top': top ,
        'bottom': bottom ,
        'total': total,
        'line': line,
        'name': origin.name,
    }
    exc_info = hasattr(exc_value, 'exc_info') and exc_value.exc_info or (exc_type, exc_value, tb)
    return exc_info + (template_info,)

def technical_500_response(request, exc_type, exc_value, tb):
    """
    Create a technical server error response.  The last three arguments are
    the values returned from sys.exc_info() and friends.
    """
    template_info = None
    if settings.TEMPLATE_DEBUG and hasattr(exc_value, 'source'):
        exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type, exc_value, tb)
    frames = []
    while tb is not None:
        filename = tb.tb_frame.f_code.co_filename
        function = tb.tb_frame.f_code.co_name
        lineno = tb.tb_lineno - 1
        pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7)
        frames.append({
            'tb': tb,
            'filename': filename,
            'function': function,
            'lineno': lineno,
            'vars': tb.tb_frame.f_locals.items(),
            'id': id(tb),
            'pre_context': pre_context,
            'context_line': context_line,
            'post_context': post_context,
            'pre_context_lineno': pre_context_lineno,
        })
        tb = tb.tb_next

    # Turn the settings module into a dict, filtering out anything that
    # matches HIDDEN_SETTINGS along the way.
    settings_dict = {}
    for k in dir(settings):
        if k.isupper():
            if HIDDEN_SETTINGS.search(k):
                settings_dict[k] = '********************'
            else:
                settings_dict[k] = getattr(settings, k)

    t = Template(TECHNICAL_500_TEMPLATE)
    c = Context({
        'exception_type': exc_type.__name__,
        'exception_value': exc_value,
        'frames': frames,
        'lastframe': frames[-1],
        'request': request,
        'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http",
        'settings': settings_dict,
        'template_info': template_info,
    })
    return HttpResponseServerError(t.render(c), mimetype='text/html')

def technical_404_response(request, exception):
    """
    Create a technical 404 error response.  The exception should be the Http404
    exception.
    """
    try:
        tried = exception.args[0]['tried']
    except (IndexError, TypeError):
        tried = []

    t = Template(TECHNICAL_404_TEMPLATE)
    c = Context({
        'root_urlconf': settings.ROOT_URLCONF,
        'urlpatterns': tried,
        'reason': str(exception),
        'request': request,
        'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http",
        'settings': dict([(k, getattr(settings, k)) for k in dir(settings) if k.isupper()]),
    })
    return HttpResponseNotFound(t.render(c), mimetype='text/html')

def _get_lines_from_file(filename, lineno, context_lines):
    """
    Returns context_lines before and after lineno from file.
    Returns (pre_context_lineno, pre_context, context_line, post_context).
    """
    try:
        source = open(filename).readlines()
        lower_bound = max(0, lineno - context_lines)
        upper_bound = lineno + context_lines

        pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
        context_line = source[lineno].strip('\n')
        post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]

        return lower_bound, pre_context, context_line, post_context
    except (OSError, IOError):
        return None, [], None, []

#
# Templates are embedded in the file so that we know the error handler will
# always work even if the template loader is broken.
#

TECHNICAL_500_TEMPLATE = """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta name="robots" content="NONE,NOARCHIVE" />
  <title>{{ exception_type }} at {{ request.path }}</title>
  <style type="text/css">
    html * { padding:0; margin:0; }
    body * { padding:10px 20px; }
    body * * { padding:0; }
    body { font:small sans-serif; }
    body>div { border-bottom:1px solid #ddd; }
    h1 { font-weight:normal; }
    h2 { margin-bottom:.8em; }
    h2 span { font-size:80%; color:#666; font-weight:normal; }
    h3 { margin:1em 0 .5em 0; }
    h4 { margin:0 0 .5em 0; font-weight: normal; }
    table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
    tbody td, tbody th { vertical-align:top; padding:2px 3px; }
    thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
    tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
    table.vars { margin:5px 0 2px 40px; }
    table.vars td, table.req td { font-family:monospace; }
    table td.code { width:100%; }
    table td.code div { overflow:hidden; }
    ul.traceback { list-style-type:none; }
    ul.traceback li.frame { margin-bottom:1em; }
    div.context { margin: 10px 0; }
    div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
    div.context ol li { font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
    div.context ol.context-line li { color:black; background-color:#ccc; }
    div.context ol.context-line li span { float: right; }
    div.commands { margin-left: 40px; }
    div.commands a { color:black; text-decoration:none; }
    #summary { background: #ffc; }
    #summary h2 { font-weight: normal; color: #666; }
    #explanation { background:#eee; }
    #traceback { background:#eee; }
    #requestinfo { background:#f6f6f6; padding-left:120px; }
    #summary table { border:none; background:transparent; }
    #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
    #requestinfo h3 { margin-bottom:-1em; }
    table.source td { font-family: monospace; white-space: pre; }
    span.specific { background:#ffcab7; }
    .error { background: #ffc; }
  </style>
  <script type="text/javascript">
  //<!--
    function getElementsByClassName(oElm, strTagName, strClassName){
        // Written by <NAME>, http://www.snook.ca/jon; Add-ons by <NAME>, http://www.robertnyman.com<commit_msg>Added Wilson's CSS tweaks to the template error screen<commit_after>from django.conf import settings
from django.core.template import Template, Context
from django.utils.html import escape
from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound
import inspect, os, re, sys
from itertools import count, izip
from os.path import dirname, join as pathjoin

HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD')

def linebreak_iter(template_source):
    newline_re = re.compile("^", re.M)
    for match in newline_re.finditer(template_source):
        yield match.start()
    yield len(template_source) + 1

def get_template_exception_info(exc_type, exc_value, tb):
    origin, (start, end) = exc_value.source
    template_source = origin.reload()
    context_lines = 10
    line = 0
    upto = 0
    source_lines = []
    linebreaks = izip(count(0), linebreak_iter(template_source))
    linebreaks.next() # skip the nothing before initial line start
    for num, next in linebreaks:
        if start >= upto and end <= next:
            line = num
            before = escape(template_source[upto:start])
            during = escape(template_source[start:end])
            after = escape(template_source[end:next - 1])
        source_lines.append( (num, escape(template_source[upto:next - 1])) )
        upto = next
    total = len(source_lines)

    top = max(0, line - context_lines)
    bottom = min(total, line + 1 + context_lines)

    template_info = {
        'message': exc_value.args[0],
        'source_lines': source_lines[top:bottom],
        'before': before,
        'during': during,
        'after': after,
        'top': top ,
        'bottom': bottom ,
        'total': total,
        'line': line,
        'name': origin.name,
    }
    exc_info = hasattr(exc_value, 'exc_info') and exc_value.exc_info or (exc_type, exc_value, tb)
    return exc_info + (template_info,)

def technical_500_response(request, exc_type, exc_value, tb):
    """
    Create a technical server error response.  The last three arguments are
    the values returned from sys.exc_info() and friends.
    """
    template_info = None
    if settings.TEMPLATE_DEBUG and hasattr(exc_value, 'source'):
        exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type, exc_value, tb)
    frames = []
    while tb is not None:
        filename = tb.tb_frame.f_code.co_filename
        function = tb.tb_frame.f_code.co_name
        lineno = tb.tb_lineno - 1
        pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7)
        frames.append({
            'tb': tb,
            'filename': filename,
            'function': function,
            'lineno': lineno,
            'vars': tb.tb_frame.f_locals.items(),
            'id': id(tb),
            'pre_context': pre_context,
            'context_line': context_line,
            'post_context': post_context,
            'pre_context_lineno': pre_context_lineno,
        })
        tb = tb.tb_next

    # Turn the settings module into a dict, filtering out anything that
    # matches HIDDEN_SETTINGS along the way.
    settings_dict = {}
    for k in dir(settings):
        if k.isupper():
            if HIDDEN_SETTINGS.search(k):
                settings_dict[k] = '********************'
            else:
                settings_dict[k] = getattr(settings, k)

    t = Template(TECHNICAL_500_TEMPLATE)
    c = Context({
        'exception_type': exc_type.__name__,
        'exception_value': exc_value,
        'frames': frames,
        'lastframe': frames[-1],
        'request': request,
        'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http",
        'settings': settings_dict,
        'template_info': template_info,
    })
    return HttpResponseServerError(t.render(c), mimetype='text/html')

def technical_404_response(request, exception):
    """
    Create a technical 404 error response.  The exception should be the Http404
    exception.
    """
    try:
        tried = exception.args[0]['tried']
    except (IndexError, TypeError):
        tried = []

    t = Template(TECHNICAL_404_TEMPLATE)
    c = Context({
        'root_urlconf': settings.ROOT_URLCONF,
        'urlpatterns': tried,
        'reason': str(exception),
        'request': request,
        'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http",
        'settings': dict([(k, getattr(settings, k)) for k in dir(settings) if k.isupper()]),
    })
    return HttpResponseNotFound(t.render(c), mimetype='text/html')

def _get_lines_from_file(filename, lineno, context_lines):
    """
    Returns context_lines before and after lineno from file.
    Returns (pre_context_lineno, pre_context, context_line, post_context).
    """
    try:
        source = open(filename).readlines()
        lower_bound = max(0, lineno - context_lines)
        upper_bound = lineno + context_lines

        pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
        context_line = source[lineno].strip('\n')
        post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]

        return lower_bound, pre_context, context_line, post_context
    except (OSError, IOError):
        return None, [], None, []

#
# Templates are embedded in the file so that we know the error handler will
# always work even if the template loader is broken.
#

TECHNICAL_500_TEMPLATE = """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta name="robots" content="NONE,NOARCHIVE" />
  <title>{{ exception_type }} at {{ request.path }}</title>
  <style type="text/css">
    html * { padding:0; margin:0; }
    body * { padding:10px 20px; }
    body * * { padding:0; }
    body { font:small sans-serif; }
    body>div { border-bottom:1px solid #ddd; }
    h1 { font-weight:normal; }
    h2 { margin-bottom:.8em; }
    h2 span { font-size:80%; color:#666; font-weight:normal; }
    h3 { margin:1em 0 .5em 0; }
    h4 { margin:0 0 .5em 0; font-weight: normal; }
    table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
    tbody td, tbody th { vertical-align:top; padding:2px 3px; }
    thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
    tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
    table.vars { margin:5px 0 2px 40px; }
    table.vars td, table.req td { font-family:monospace; }
    table td.code { width:100%; }
    table td.code div { overflow:hidden; }
    table.source th { color:#666; }
    table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
    ul.traceback { list-style-type:none; }
    ul.traceback li.frame { margin-bottom:1em; }
    div.context { margin: 10px 0; }
    div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
    div.context ol li { font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
    div.context ol.context-line li { color:black; background-color:#ccc; }
    div.context ol.context-line li span { float: right; }
    div.commands { margin-left: 40px; }
    div.commands a { color:black; text-decoration:none; }
    #summary { background: #ffc; }
    #summary h2 { font-weight: normal; color: #666; }
    #explanation { background:#eee; }
    #template { background:#f6f6f6; }
    #traceback { background:#eee; }
    #requestinfo { background:#f6f6f6; padding-left:120px; }
    #summary table { border:none; background:transparent; }
    #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
    #requestinfo h3 { margin-bottom:-1em; }
    .error { background: #ffc; }
    .specific { color:#cc3300; font-weight:bold; }
  </style>
  <script type="text/javascript">
  //<!--
    function getElementsByClassName(oElm, strTagName, strClassName){
        // Written by <NAME>, http://www.snook.ca/jon; Add-ons by <NAME>, http://www.robertnyman.com
{'fraction_non_alphanumeric': 0.12978328173374612, 'fraction_numerical': 0.016842105263157894, 'mean_word_length': 3.0713385429795816, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '314221', 'n_tokens_mistral': 5374, 'n_tokens_neox': 5134, 'n_words': 1266}