403Webshell
Server IP : 217.160.0.212  /  Your IP : 216.73.216.141
Web Server : Apache
System : Linux www 6.18.51-i1-ampere #1196 SMP Fri Sep 11 20:43:55 CEST 2026 aarch64
User : sws1073854427 ( 1073854427)
PHP Version : 8.4.23
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /lib/python3/dist-packages/pythran/transformations/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /lib/python3/dist-packages/pythran/transformations//remove_fstrings.py
"""Turns f-strings to format syntax with modulus"""

import gast as ast

from pythran.passmanager import Transformation
from pythran.syntax import PythranSyntaxError


class RemoveFStrings(Transformation, ast.NodeTransformer):
    """Turns f-strings to format syntax with modulus

    >>> import gast as ast
    >>> from pythran import passmanager, backend
    >>> node = ast.parse("f'a = {1+1:4d}; b = {b:s};'")
    >>> pm = passmanager.PassManager("test")
    >>> _, node = pm.apply(RemoveFStrings, node)
    >>> print(pm.dump(backend.Python, node))
    ('a = %4d; b = %s;' % ((1 + 1), b))
    """

    def visit_JoinedStr(self, node):
        if len(node.values) == 1 and not isinstance(
            node.values[0], ast.FormattedValue
        ):
            # f-strings with no reference to variable (like `f"bar"`, see #1767)
            return node.values[0]

        if not any(
            isinstance(value, ast.FormattedValue) for value in node.values
        ):
            # nothing to do (not a f-string)
            return node

        base_str = ""
        elements = []
        for value in node.values:
            if isinstance(value, ast.Constant):
                base_str += value.value.replace("%", "%%")
            elif isinstance(value, ast.FormattedValue):
                base_str += "%"
                if value.format_spec is None:
                    raise PythranSyntaxError(
                        "f-strings without format specifier not supported", value
                    )
                base_str += value.format_spec.values[0].value
                elements.append(value.value)
            else:
                raise NotImplementedError

        return ast.BinOp(
            left=ast.Constant(value=base_str, kind=None),
            op=ast.Mod(),
            right=ast.Tuple(elts=elements, ctx=ast.Load()),
        )

Youez - 2016 - github.com/yon3zu
LinuXploit