Skip to content
Myr

Updated documentation for operator intercepting

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent a919538 · Browse files · 2 files+51 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+51 added−2 removed

  • other:+51 added−2 removed

2 files+51 added−2 removed

modified:docs/sandbox.rst+49 added-1 removed
12 unmodified lines
13Traceback (most recent call last):14 ...15SecurityError: access to attribute 'func_code' of 'function' object is unsafe.1617API18---1920.. module:: jinja2.sandbox2122.. autoclass:: SandboxedEnvironment([options])21 :members: is_safe_attribute, is_safe_callable23 :members: is_safe_attribute, is_safe_callable, default_binop_table,24 default_unop_table, intercepted_binops, intercepted_unops,25 call_binop, call_unop2627.. autoclass:: ImmutableSandboxedEnvironment([options])2829.. autoexception:: SecurityError
17 unmodified lines
47 some sort of whitelisting for attributes.4849 Also keep in mind that templates may raise runtime or compile time errors,50 so make sure to catch them.5152Operator Intercepting53---------------------5455.. versionadded:: 2.65657For maximum performace Jinja2 will let operators call directly the type58specific callback methods. This means that it's not possible to have this59intercepted by overriding :meth:`Environment.call`. Furthermore a60conversion from operator to special method is not always directly possible61due to how operators work. For instance for divisions more than one62special method exist.6364With Jinja 2.6 there is now support for explicit operator intercepting.65This can be used to customize specific operators as necessary. In order66to intercept an operator one has to override the67:attr:`SandboxedEnvironment.intercepted_binops` attribute. Once the68operator that needs to be intercepted is added to that set Jinja2 will69generate bytecode that calls the :meth:`SandboxedEnvironment.call_binop`70function. For unary operators the `unary` attributes and methods have to71be used instead.7273The default implementation of :attr:`SandboxedEnvironment.call_binop`74will use the :attr:`SandboxedEnvironment.binop_table` to translate75operator symbols into callbacks performing the default operator behavior.7677This example shows how the power (``**``) operator can be disabled in78Jinja2::7980 from jinja2.sandbox import SandboxedEnvironment818283 class MyEnvironment(SandboxedEnvironment):84 intercepted_binops = frozenset(['**'])8586 def call_binop(self, context, operator, left, right):87 if operator == '**':88 return self.undefined('the power operator is unavailable')89 return SandboxedEnvironment.call_binop(self, context,90 operator, left, right)9192Make sure to always call into the super method, even if you are not93intercepting the call. Jinja2 might internally call the method to94evaluate expressions.
modified:jinja2/environment.py+2 added-1 removed
194 unmodified lines
195 """196197 #: if this environment is sandboxed. Modifying this variable won't make198 #: the environment sandboxed though. For a real sandboxed environment199 #: have a look at jinja2.sandbox199 #: have a look at jinja2.sandbox. This flag alone controls the code200 #: generation by the compiler.201 sandboxed = False202203 #: True if the environment is just an overlay204 overlayed = False
915 unmodified lines