Ticket #8316: trac_8316-remove_jinja.2.patch

File trac_8316-remove_jinja.2.patch, 11.9 KB (added by mpatel, 12 years ago)

Use myself instead of self. Replaces previous.

  • sage/ext/gen_interpreters.py

    # HG changeset patch
    # User Mitesh Patel <qed777@gmail.com>
    # Date 1267756016 28800
    # Node ID 229d0893f9923e5fef1ad51183ca0acd70321a17
    # Parent  c49613dc3a07d8be9cd9ae50384e322cd010855c
    #8316: Use Jinja2 instead of Jinja
    
    diff --git a/sage/ext/gen_interpreters.py b/sage/ext/gen_interpreters.py
    a b from __future__ import with_statement 
    9898
    9999import os
    100100import re
    101 from jinja import Environment
    102 from jinja.datastructure import ComplainingUndefined
     101from jinja2 import Environment
     102from jinja2.runtime import StrictUndefined
    103103from collections import defaultdict
    104104from distutils.extension import Extension
    105105
    from distutils.extension import Extensio 
    114114##############################
    115115
    116116
    117 # We share a single jinja environment among all templating in this file.
    118 # We use trim_blocks=True (which means that we ignore white space after
    119 # "%}" jinja command endings), and set undefined_singleton to complain
    120 # if we use an undefined variable.
    121 jinja_env = Environment(trim_blocks=True,
    122                         undefined_singleton=ComplainingUndefined)
     117# We share a single jinja2 environment among all templating in this
     118# file.  We use trim_blocks=True (which means that we ignore white
     119# space after "%}" jinja2 command endings), and set undefined to
     120# complain if we use an undefined variable.
     121jinja_env = Environment(trim_blocks=True, undefined=StrictUndefined)
     122
    123123# Allow 'i' as a shorter alias for the built-in 'indent' filter.
    124124jinja_env.filters['i'] = jinja_env.filters['indent']
    125125
    def je(template, **kwargs): 
    169169    if len(template) > 0 and template[0] == '\n':
    170170        template = template[1:]
    171171
    172     # It looks like Jinja automatically removes one trailing newline?
     172    # It looks like Jinja2 automatically removes one trailing newline?
    173173    if len(template) > 0 and template[-1] == '\n':
    174174        template = template + '\n'
    175175
    class StorageType(object): 
    424424        return je("""
    425425{# XXX Variables here (and everywhere, really) should actually be Py_ssize_t #}
    426426    cdef int _n_{{ name }}
    427     cdef {{ self.cython_array_type() }} _{{ name }}
    428 """, self=self, name=name)
     427    cdef {{ myself.cython_array_type() }} _{{ name }}
     428""", myself=self, name=name)
    429429
    430430    def alloc_chunk_data(self, name, len):
    431431        r"""
    class StorageType(object): 
    444444        """
    445445        return je("""
    446446        self._n_{{ name }} = {{ len }}
    447         self._{{ name }} = <{{ self.c_ptr_type() }}>sage_malloc(sizeof({{ self.c_decl_type() }}) * {{ len }})
     447        self._{{ name }} = <{{ myself.c_ptr_type() }}>sage_malloc(sizeof({{ myself.c_decl_type() }}) * {{ len }})
    448448        if self._{{ name }} == NULL: raise MemoryError
    449 {% if self.needs_cython_init_clear() %}
     449{% if myself.needs_cython_init_clear() %}
    450450        for i in range({{ len }}):
    451             {{ self.cython_init('self._%s[i]' % name) }}
     451            {{ myself.cython_init('self._%s[i]' % name) }}
    452452{% endif %}
    453 """, self=self, name=name, len=len)
     453""", myself=self, name=name, len=len)
    454454
    455455    def dealloc_chunk_data(self, name):
    456456        r"""
    class StorageType(object): 
    473473        """
    474474        return je("""
    475475        if self._{{ name }}:
    476 {%     if self.needs_cython_init_clear() %}
     476{%     if myself.needs_cython_init_clear() %}
    477477            for i in range(self._n_{{ name }}):
    478                 {{ self.cython_clear('self._%s[i]' % name) }}
     478                {{ myself.cython_clear('self._%s[i]' % name) }}
    479479{%     endif %}
    480480            sage_free(self._{{ name }})
    481 """, self=self, name=name)
     481""", myself=self, name=name)
    482482
    483483class StorageTypeAssignable(StorageType):
    484484    r"""
    class StorageTypePython(StorageTypeAssig 
    671671        return je("""
    672672    cdef object _list_{{ name }}
    673673    cdef int _n_{{ name }}
    674     cdef {{ self.cython_array_type() }} _{{ name }}
    675 """, self=self, name=name)
     674    cdef {{ myself.cython_array_type() }} _{{ name }}
     675""", myself=self, name=name)
    676676
    677677    def alloc_chunk_data(self, name, len):
    678678        r"""
    class StorageTypePython(StorageTypeAssig 
    691691        self._n_{{ name }} = {{ len }}
    692692        self._list_{{ name }} = PyList_New(self._n_{{ name }})
    693693        self._{{ name }} = (<PyListObject *>self._list_{{ name }}).ob_item
    694 """, self=self, name=name, len=len)
     694""", myself=self, name=name, len=len)
    695695
    696696    def dealloc_chunk_data(self, name):
    697697        r"""
    class StorageTypeMPFR(StorageTypeAutoRef 
    901901            sage: ty_mpfr.cython_init('foo[i]')
    902902            u'mpfr_init2(foo[i], self.domain.prec())'
    903903        """
    904         return je("mpfr_init2({{ loc }}, self.domain{{ self.id }}.prec())",
    905                   self=self, loc=loc)
     904        return je("mpfr_init2({{ loc }}, self.domain{{ myself.id }}.prec())",
     905                  myself=self, loc=loc)
    906906
    907907    def cython_clear(self, loc):
    908908        r"""
    class StorageTypeMPFR(StorageTypeAutoRef 
    928928            u'rn = self.domain(bar[j])\nmpfr_set(foo[i], rn.value, GMP_RNDN)'
    929929        """
    930930        return je("""
    931 rn{{ self.id }} = self.domain({{ py }})
    932 mpfr_set({{ c }}, rn.value, GMP_RNDN)""", self=self, c=c, py=py)
     931rn{{ myself.id }} = self.domain({{ py }})
     932mpfr_set({{ c }}, rn.value, GMP_RNDN)""", myself=self, c=c, py=py)
    933933
    934934ty_mpfr = StorageTypeMPFR()
    935935
    class MemoryChunkLonglivedArray(MemoryCh 
    11841184            <BLANKLINE>
    11851185        """
    11861186        return je("""
    1187         count = args['{{ self.name }}']
    1188 {% print self.storage_type.alloc_chunk_data(self.name, 'count') %}
    1189 """, self=self)
     1187        count = args['{{ myself.name }}']
     1188{% print myself.storage_type.alloc_chunk_data(myself.name, 'count') %}
     1189""", myself=self)
    11901190
    11911191    def dealloc_class_members(self):
    11921192        r"""
    class MemoryChunkConstants(MemoryChunkLo 
    12491249            <BLANKLINE>
    12501250        """
    12511251        return je("""
    1252         val = args['{{ self.name }}']
    1253 {% print self.storage_type.alloc_chunk_data(self.name, 'len(val)') %}
     1252        val = args['{{ myself.name }}']
     1253{% print myself.storage_type.alloc_chunk_data(myself.name, 'len(val)') %}
    12541254        for i in range(len(val)):
    1255             {{ self.storage_type.assign_c_from_py('self._%s[i]' % self.name, 'val[i]') | i(12) }}
    1256 """, self=self)
     1255            {{ myself.storage_type.assign_c_from_py('self._%s[i]' % myself.name, 'val[i]') | i(12) }}
     1256""", myself=self)
    12571257
    12581258class MemoryChunkArguments(MemoryChunkLonglivedArray):
    12591259    r"""
    class MemoryChunkArguments(MemoryChunkLo 
    12821282            <BLANKLINE>
    12831283        """
    12841284        return je("""
    1285 cdef {{ self.storage_type.c_ptr_type() }} c_args = self._args
     1285cdef {{ myself.storage_type.c_ptr_type() }} c_args = self._args
    12861286cdef int i
    12871287for i from 0 <= i < len(args):
    1288     {{ self.storage_type.assign_c_from_py('self._args[i]', 'args[i]') | i(4) }}
    1289 """, self=self)
     1288    {{ myself.storage_type.assign_c_from_py('self._args[i]', 'args[i]') | i(4) }}
     1289""", myself=self)
    12901290
    12911291    def pass_argument(self):
    12921292        r"""
    class MemoryChunkScratch(MemoryChunkLong 
    13831383        # XXX This is a lot slower than it needs to be, because
    13841384        # we don't have a "cdef int i" in scope here.
    13851385        return je("""
    1386 for i in range(self._n_{{ self.name }}):
    1387     Py_CLEAR(self._{{ self.name }}[i])
    1388 """, self=self)
     1386for i in range(self._n_{{ myself.name }}):
     1387    Py_CLEAR(self._{{ myself.name }}[i])
     1388""", myself=self)
    13891389
    13901390class MemoryChunkRRRetval(MemoryChunk):
    13911391    r"""
    class MemoryChunkRRRetval(MemoryChunk): 
    14181418            u'        cdef RealNumber retval = (self.domain)()\n'
    14191419        """
    14201420        return je("""
    1421         cdef RealNumber {{ self.name }} = (self.domain)()
    1422 """, self=self)
     1421        cdef RealNumber {{ myself.name }} = (self.domain)()
     1422""", myself=self)
    14231423       
    14241424    def pass_argument(self):
    14251425        r"""
    class MemoryChunkRRRetval(MemoryChunk): 
    14321432            sage: mc.pass_argument()
    14331433            u'&retval.value'
    14341434        """
    1435         return je("""&{{ self.name }}.value""", self=self)
     1435        return je("""&{{ myself.name }}.value""", myself=self)
    14361436
    14371437    def pass_call_c_argument(self):
    14381438        r"""
    class MemoryChunkPythonArguments(MemoryC 
    14791479            u"        count = args['args']\n        self._n_args = count\n"
    14801480        """
    14811481        return je("""
    1482         count = args['{{ self.name }}']
     1482        count = args['{{ myself.name }}']
    14831483        self._n_args = count
    1484 """, self=self)
     1484""", myself=self)
    14851485
    14861486    def setup_args(self):
    14871487        r"""
    class MemoryChunkPyConstant(MemoryChunk) 
    15791579            u'    cdef object _domain\n'
    15801580        """
    15811581        return je("""
    1582     cdef object _{{ self.name }}
    1583 """, self=self)
     1582    cdef object _{{ myself.name }}
     1583""", myself=self)
    15841584
    15851585    def init_class_members(self):
    15861586        r"""
    class MemoryChunkPyConstant(MemoryChunk) 
    15951595            u"        self._domain = args['domain']\n"
    15961596        """
    15971597        return je("""
    1598         self._{{ self.name }} = args['{{ self.name }}']
    1599 """, self=self)
     1598        self._{{ myself.name }} = args['{{ myself.name }}']
     1599""", myself=self)
    16001600
    16011601    def declare_parameter(self):
    16021602        r"""
    class InterpreterGenerator(object): 
    29672967/* Automatically generated by ext/gen_interpreters.py.  Do not edit! */
    29682968#include <Python.h>
    29692969{% print s.header %}
    2970 {{ self.func_header() }} {
     2970{{ myself.func_header() }} {
    29712971  while (1) {
    29722972    switch (*code++) {
    2973 """, s=s, self=self, i=indent_lines))
     2973""", s=s, myself=self, i=indent_lines))
    29742974        for instr_desc in s.instr_descs:
    29752975            self.gen_code(instr_desc, w)
    29762976        w(je("""
    29772977    }
    29782978  }
    2979 {% if self.uses_error_handler %}
     2979{% if myself.uses_error_handler %}
    29802980error:
    29812981  return {{ s.err_return }};
    29822982{% endif %}
    29832983}
    29842984
    2985 """, s=s, i=indent_lines, self=self))
     2985""", s=s, i=indent_lines, myself=self))
    29862986
    29872987    def write_wrapper(self, write):
    29882988        r"""
    cdef extern from "tupleobject.h": 
    30593059from sage.ext.fast_callable cimport Wrapper
    30603060{% print s.pyx_header %}
    30613061
    3062 cdef extern {{ self.func_header(cython=true) -}}
     3062cdef extern {{ myself.func_header(cython=true) -}}
    30633063{% if s.err_return != 'NULL' %}
    30643064 except? {{ s.err_return -}}
    30653065{% endif %}
    metadata = InterpreterMetadata(by_opname 
    31463146{% endfor %}
    31473147 ],
    31483148 ipow_range={{ s.ipow_range }})
    3149 """, s=s, self=self, types=types, arg_ch=arg_ch, indent_lines=indent_lines, the_call=the_call, the_call_c=the_call_c, do_cleanup=do_cleanup))
     3149""", s=s, myself=self, types=types, arg_ch=arg_ch, indent_lines=indent_lines, the_call=the_call, the_call_c=the_call_c, do_cleanup=do_cleanup))
    31503150
    31513151    def write_pxd(self, write):
    31523152        r"""
    cdef class Wrapper_{{ s.name }}(Wrapper) 
    31993199                     {{ arg_ch.storage_type.c_ptr_type() }} args,
    32003200                     {{ arg_ch.storage_type.c_ptr_type() }} result) except 0
    32013201{% endif %}
    3202 """, s=s, self=self, types=types, indent_lines=indent_lines, arg_ch=arg_ch))
     3202""", s=s, myself=self, types=types, indent_lines=indent_lines, arg_ch=arg_ch))
    32033203
    32043204    def get_interpreter(self):
    32053205        r"""
  • sage/server/notebook/template.py

    diff --git a/sage/server/notebook/template.py b/sage/server/notebook/template.py
    a b AUTHORS: 
    2828#  The full text of the GPL is available at:
    2929#                  http://www.gnu.org/licenses/
    3030#############################################################################
    31 import jinja
     31import jinja2
    3232import sage.misc.misc
    3333from sage.version import version
    3434
    3535TEMPLATE_PATH = sage.misc.misc.SAGE_ROOT + '/devel/sage/sage/server/notebook/templates'
    36 env = jinja.Environment(loader=jinja.FileSystemLoader(TEMPLATE_PATH))
     36env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_PATH))
    3737
    3838def contained_in(container):
    3939    """
    def template(filename, **user_context): 
    100100    """
    101101    try:
    102102        tmpl = env.get_template(filename)
    103     except jinja.exceptions.TemplateNotFound:
     103    except jinja2.exceptions.TemplateNotFound:
    104104        return template('template_error.html', template=filename)
    105105    context = dict(default_context)
    106106    context.update(user_context)