Skip to content

Negative number raised to even power gives negative result if exponent is a variable #1720

@bart-nouveau

Description

@bart-nouveau

Unexpected behavior of the power operator with a variable of even value as exponent. It returns the negative value of the expected result.

mcve

from jinja2 import Environment

env = Environment()
env.globals = {"x": 2}
env.from_string("{{ -1 ** x }}").render()

From which I get '-1'

Expected behavior

I expected the positive value '1' as a result

It instead works as expected in a Sandboxed environment when intercepting the ** operator:

from jinja2.sandbox import SandboxedEnvironment

sandbox_env = SandboxedEnvironment()
sandbox_env.globals = {"x": 2}
sandbox_env.intercepted_binops = frozenset(["**"])
sandbox_env.from_string("{{ -1 ** x }}").render()

From which I get '1'

Environment:

  • Python version: 3.8.10
  • Jinja version: 3.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions