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
Provenance
- Commit
9adf937- Tree
aa2056f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a919538
2 files changed+51 added−2 removed
modified:docs/sandbox.rst+49 added-1 removed
12 unmodified lines13 Traceback (most recent call last):14 ...15 SecurityError: access to attribute 'func_code' of 'function' object is unsafe.16 17+API18+---19 20 .. module:: jinja2.sandbox21 22 .. 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_unop26 27 .. autoclass:: ImmutableSandboxedEnvironment([options])28 29 .. autoexception:: SecurityError17 unmodified lines47 some sort of whitelisting for attributes.48 49 Also keep in mind that templates may raise runtime or compile time errors,50 so make sure to catch them.51+52+Operator Intercepting53+---------------------54+55+.. versionadded:: 2.656+57+For maximum performace Jinja2 will let operators call directly the type58+specific callback methods. This means that it's not possible to have this59+intercepted by overriding :meth:`Environment.call`. Furthermore a60+conversion from operator to special method is not always directly possible61+due to how operators work. For instance for divisions more than one62+special method exist.63+64+With Jinja 2.6 there is now support for explicit operator intercepting.65+This can be used to customize specific operators as necessary. In order66+to intercept an operator one has to override the67+:attr:`SandboxedEnvironment.intercepted_binops` attribute. Once the68+operator that needs to be intercepted is added to that set Jinja2 will69+generate bytecode that calls the :meth:`SandboxedEnvironment.call_binop`70+function. For unary operators the `unary` attributes and methods have to71+be used instead.72+73+The default implementation of :attr:`SandboxedEnvironment.call_binop`74+will use the :attr:`SandboxedEnvironment.binop_table` to translate75+operator symbols into callbacks performing the default operator behavior.76+77+This example shows how the power (``**``) operator can be disabled in78+Jinja2::79+80+ from jinja2.sandbox import SandboxedEnvironment81+82+83+ class MyEnvironment(SandboxedEnvironment):84+ intercepted_binops = frozenset(['**'])85+86+ 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)91+92+Make sure to always call into the super method, even if you are not93+intercepting the call. Jinja2 might internally call the method to94+evaluate expressions.modified:jinja2/environment.py+2 added-1 removed
194 unmodified lines195 """196 197 #: 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 = False202 203 #: True if the environment is just an overlay204 overlayed = False915 unmodified lines