# 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 |
98 | 98 | |
99 | 99 | import os |
100 | 100 | import re |
101 | | from jinja import Environment |
102 | | from jinja.datastructure import ComplainingUndefined |
| 101 | from jinja2 import Environment |
| 102 | from jinja2.runtime import StrictUndefined |
103 | 103 | from collections import defaultdict |
104 | 104 | from distutils.extension import Extension |
105 | 105 | |
… |
… |
from distutils.extension import Extensio |
114 | 114 | ############################## |
115 | 115 | |
116 | 116 | |
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. |
| 121 | jinja_env = Environment(trim_blocks=True, undefined=StrictUndefined) |
| 122 | |
123 | 123 | # Allow 'i' as a shorter alias for the built-in 'indent' filter. |
124 | 124 | jinja_env.filters['i'] = jinja_env.filters['indent'] |
125 | 125 | |
… |
… |
def je(template, **kwargs): |
169 | 169 | if len(template) > 0 and template[0] == '\n': |
170 | 170 | template = template[1:] |
171 | 171 | |
172 | | # It looks like Jinja automatically removes one trailing newline? |
| 172 | # It looks like Jinja2 automatically removes one trailing newline? |
173 | 173 | if len(template) > 0 and template[-1] == '\n': |
174 | 174 | template = template + '\n' |
175 | 175 | |
… |
… |
class StorageType(object): |
424 | 424 | return je(""" |
425 | 425 | {# XXX Variables here (and everywhere, really) should actually be Py_ssize_t #} |
426 | 426 | 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) |
429 | 429 | |
430 | 430 | def alloc_chunk_data(self, name, len): |
431 | 431 | r""" |
… |
… |
class StorageType(object): |
444 | 444 | """ |
445 | 445 | return je(""" |
446 | 446 | 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 }}) |
448 | 448 | if self._{{ name }} == NULL: raise MemoryError |
449 | | {% if self.needs_cython_init_clear() %} |
| 449 | {% if myself.needs_cython_init_clear() %} |
450 | 450 | for i in range({{ len }}): |
451 | | {{ self.cython_init('self._%s[i]' % name) }} |
| 451 | {{ myself.cython_init('self._%s[i]' % name) }} |
452 | 452 | {% endif %} |
453 | | """, self=self, name=name, len=len) |
| 453 | """, myself=self, name=name, len=len) |
454 | 454 | |
455 | 455 | def dealloc_chunk_data(self, name): |
456 | 456 | r""" |
… |
… |
class StorageType(object): |
473 | 473 | """ |
474 | 474 | return je(""" |
475 | 475 | if self._{{ name }}: |
476 | | {% if self.needs_cython_init_clear() %} |
| 476 | {% if myself.needs_cython_init_clear() %} |
477 | 477 | for i in range(self._n_{{ name }}): |
478 | | {{ self.cython_clear('self._%s[i]' % name) }} |
| 478 | {{ myself.cython_clear('self._%s[i]' % name) }} |
479 | 479 | {% endif %} |
480 | 480 | sage_free(self._{{ name }}) |
481 | | """, self=self, name=name) |
| 481 | """, myself=self, name=name) |
482 | 482 | |
483 | 483 | class StorageTypeAssignable(StorageType): |
484 | 484 | r""" |
… |
… |
class StorageTypePython(StorageTypeAssig |
671 | 671 | return je(""" |
672 | 672 | cdef object _list_{{ name }} |
673 | 673 | 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) |
676 | 676 | |
677 | 677 | def alloc_chunk_data(self, name, len): |
678 | 678 | r""" |
… |
… |
class StorageTypePython(StorageTypeAssig |
691 | 691 | self._n_{{ name }} = {{ len }} |
692 | 692 | self._list_{{ name }} = PyList_New(self._n_{{ name }}) |
693 | 693 | self._{{ name }} = (<PyListObject *>self._list_{{ name }}).ob_item |
694 | | """, self=self, name=name, len=len) |
| 694 | """, myself=self, name=name, len=len) |
695 | 695 | |
696 | 696 | def dealloc_chunk_data(self, name): |
697 | 697 | r""" |
… |
… |
class StorageTypeMPFR(StorageTypeAutoRef |
901 | 901 | sage: ty_mpfr.cython_init('foo[i]') |
902 | 902 | u'mpfr_init2(foo[i], self.domain.prec())' |
903 | 903 | """ |
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) |
906 | 906 | |
907 | 907 | def cython_clear(self, loc): |
908 | 908 | r""" |
… |
… |
class StorageTypeMPFR(StorageTypeAutoRef |
928 | 928 | u'rn = self.domain(bar[j])\nmpfr_set(foo[i], rn.value, GMP_RNDN)' |
929 | 929 | """ |
930 | 930 | return je(""" |
931 | | rn{{ self.id }} = self.domain({{ py }}) |
932 | | mpfr_set({{ c }}, rn.value, GMP_RNDN)""", self=self, c=c, py=py) |
| 931 | rn{{ myself.id }} = self.domain({{ py }}) |
| 932 | mpfr_set({{ c }}, rn.value, GMP_RNDN)""", myself=self, c=c, py=py) |
933 | 933 | |
934 | 934 | ty_mpfr = StorageTypeMPFR() |
935 | 935 | |
… |
… |
class MemoryChunkLonglivedArray(MemoryCh |
1184 | 1184 | <BLANKLINE> |
1185 | 1185 | """ |
1186 | 1186 | 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) |
1190 | 1190 | |
1191 | 1191 | def dealloc_class_members(self): |
1192 | 1192 | r""" |
… |
… |
class MemoryChunkConstants(MemoryChunkLo |
1249 | 1249 | <BLANKLINE> |
1250 | 1250 | """ |
1251 | 1251 | 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)') %} |
1254 | 1254 | 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) |
1257 | 1257 | |
1258 | 1258 | class MemoryChunkArguments(MemoryChunkLonglivedArray): |
1259 | 1259 | r""" |
… |
… |
class MemoryChunkArguments(MemoryChunkLo |
1282 | 1282 | <BLANKLINE> |
1283 | 1283 | """ |
1284 | 1284 | return je(""" |
1285 | | cdef {{ self.storage_type.c_ptr_type() }} c_args = self._args |
| 1285 | cdef {{ myself.storage_type.c_ptr_type() }} c_args = self._args |
1286 | 1286 | cdef int i |
1287 | 1287 | for 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) |
1290 | 1290 | |
1291 | 1291 | def pass_argument(self): |
1292 | 1292 | r""" |
… |
… |
class MemoryChunkScratch(MemoryChunkLong |
1383 | 1383 | # XXX This is a lot slower than it needs to be, because |
1384 | 1384 | # we don't have a "cdef int i" in scope here. |
1385 | 1385 | return je(""" |
1386 | | for i in range(self._n_{{ self.name }}): |
1387 | | Py_CLEAR(self._{{ self.name }}[i]) |
1388 | | """, self=self) |
| 1386 | for i in range(self._n_{{ myself.name }}): |
| 1387 | Py_CLEAR(self._{{ myself.name }}[i]) |
| 1388 | """, myself=self) |
1389 | 1389 | |
1390 | 1390 | class MemoryChunkRRRetval(MemoryChunk): |
1391 | 1391 | r""" |
… |
… |
class MemoryChunkRRRetval(MemoryChunk): |
1418 | 1418 | u' cdef RealNumber retval = (self.domain)()\n' |
1419 | 1419 | """ |
1420 | 1420 | return je(""" |
1421 | | cdef RealNumber {{ self.name }} = (self.domain)() |
1422 | | """, self=self) |
| 1421 | cdef RealNumber {{ myself.name }} = (self.domain)() |
| 1422 | """, myself=self) |
1423 | 1423 | |
1424 | 1424 | def pass_argument(self): |
1425 | 1425 | r""" |
… |
… |
class MemoryChunkRRRetval(MemoryChunk): |
1432 | 1432 | sage: mc.pass_argument() |
1433 | 1433 | u'&retval.value' |
1434 | 1434 | """ |
1435 | | return je("""&{{ self.name }}.value""", self=self) |
| 1435 | return je("""&{{ myself.name }}.value""", myself=self) |
1436 | 1436 | |
1437 | 1437 | def pass_call_c_argument(self): |
1438 | 1438 | r""" |
… |
… |
class MemoryChunkPythonArguments(MemoryC |
1479 | 1479 | u" count = args['args']\n self._n_args = count\n" |
1480 | 1480 | """ |
1481 | 1481 | return je(""" |
1482 | | count = args['{{ self.name }}'] |
| 1482 | count = args['{{ myself.name }}'] |
1483 | 1483 | self._n_args = count |
1484 | | """, self=self) |
| 1484 | """, myself=self) |
1485 | 1485 | |
1486 | 1486 | def setup_args(self): |
1487 | 1487 | r""" |
… |
… |
class MemoryChunkPyConstant(MemoryChunk) |
1579 | 1579 | u' cdef object _domain\n' |
1580 | 1580 | """ |
1581 | 1581 | return je(""" |
1582 | | cdef object _{{ self.name }} |
1583 | | """, self=self) |
| 1582 | cdef object _{{ myself.name }} |
| 1583 | """, myself=self) |
1584 | 1584 | |
1585 | 1585 | def init_class_members(self): |
1586 | 1586 | r""" |
… |
… |
class MemoryChunkPyConstant(MemoryChunk) |
1595 | 1595 | u" self._domain = args['domain']\n" |
1596 | 1596 | """ |
1597 | 1597 | return je(""" |
1598 | | self._{{ self.name }} = args['{{ self.name }}'] |
1599 | | """, self=self) |
| 1598 | self._{{ myself.name }} = args['{{ myself.name }}'] |
| 1599 | """, myself=self) |
1600 | 1600 | |
1601 | 1601 | def declare_parameter(self): |
1602 | 1602 | r""" |
… |
… |
class InterpreterGenerator(object): |
2967 | 2967 | /* Automatically generated by ext/gen_interpreters.py. Do not edit! */ |
2968 | 2968 | #include <Python.h> |
2969 | 2969 | {% print s.header %} |
2970 | | {{ self.func_header() }} { |
| 2970 | {{ myself.func_header() }} { |
2971 | 2971 | while (1) { |
2972 | 2972 | switch (*code++) { |
2973 | | """, s=s, self=self, i=indent_lines)) |
| 2973 | """, s=s, myself=self, i=indent_lines)) |
2974 | 2974 | for instr_desc in s.instr_descs: |
2975 | 2975 | self.gen_code(instr_desc, w) |
2976 | 2976 | w(je(""" |
2977 | 2977 | } |
2978 | 2978 | } |
2979 | | {% if self.uses_error_handler %} |
| 2979 | {% if myself.uses_error_handler %} |
2980 | 2980 | error: |
2981 | 2981 | return {{ s.err_return }}; |
2982 | 2982 | {% endif %} |
2983 | 2983 | } |
2984 | 2984 | |
2985 | | """, s=s, i=indent_lines, self=self)) |
| 2985 | """, s=s, i=indent_lines, myself=self)) |
2986 | 2986 | |
2987 | 2987 | def write_wrapper(self, write): |
2988 | 2988 | r""" |
… |
… |
cdef extern from "tupleobject.h": |
3059 | 3059 | from sage.ext.fast_callable cimport Wrapper |
3060 | 3060 | {% print s.pyx_header %} |
3061 | 3061 | |
3062 | | cdef extern {{ self.func_header(cython=true) -}} |
| 3062 | cdef extern {{ myself.func_header(cython=true) -}} |
3063 | 3063 | {% if s.err_return != 'NULL' %} |
3064 | 3064 | except? {{ s.err_return -}} |
3065 | 3065 | {% endif %} |
… |
… |
metadata = InterpreterMetadata(by_opname |
3146 | 3146 | {% endfor %} |
3147 | 3147 | ], |
3148 | 3148 | 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)) |
3150 | 3150 | |
3151 | 3151 | def write_pxd(self, write): |
3152 | 3152 | r""" |
… |
… |
cdef class Wrapper_{{ s.name }}(Wrapper) |
3199 | 3199 | {{ arg_ch.storage_type.c_ptr_type() }} args, |
3200 | 3200 | {{ arg_ch.storage_type.c_ptr_type() }} result) except 0 |
3201 | 3201 | {% 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)) |
3203 | 3203 | |
3204 | 3204 | def get_interpreter(self): |
3205 | 3205 | r""" |
diff --git a/sage/server/notebook/template.py b/sage/server/notebook/template.py
a
|
b
|
AUTHORS: |
28 | 28 | # The full text of the GPL is available at: |
29 | 29 | # http://www.gnu.org/licenses/ |
30 | 30 | ############################################################################# |
31 | | import jinja |
| 31 | import jinja2 |
32 | 32 | import sage.misc.misc |
33 | 33 | from sage.version import version |
34 | 34 | |
35 | 35 | TEMPLATE_PATH = sage.misc.misc.SAGE_ROOT + '/devel/sage/sage/server/notebook/templates' |
36 | | env = jinja.Environment(loader=jinja.FileSystemLoader(TEMPLATE_PATH)) |
| 36 | env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_PATH)) |
37 | 37 | |
38 | 38 | def contained_in(container): |
39 | 39 | """ |
… |
… |
def template(filename, **user_context): |
100 | 100 | """ |
101 | 101 | try: |
102 | 102 | tmpl = env.get_template(filename) |
103 | | except jinja.exceptions.TemplateNotFound: |
| 103 | except jinja2.exceptions.TemplateNotFound: |
104 | 104 | return template('template_error.html', template=filename) |
105 | 105 | context = dict(default_context) |
106 | 106 | context.update(user_context) |