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//normalize_ifelse.py
""" NormalizeIfElse transform early exit in if into if-else. """

from pythran.analyses import Ancestors
from pythran.passmanager import Transformation

import gast as ast


class NormalizeIfElse(Transformation):
    '''

    >>> import gast as ast
    >>> from pythran import passmanager, backend
    >>> node = ast.parse("""
    ... def foo(y):
    ...  if y: return 1
    ...  return 2""")
    >>> pm = passmanager.PassManager("test")
    >>> _, node = pm.apply(NormalizeIfElse, node)
    >>> print(pm.dump(backend.Python, node))
    def foo(y):
        if y:
            return 1
        else:
            return 2

    >>> node = ast.parse("""
    ... def foo(y):
    ...  if y:
    ...    z = y + 1
    ...    if z:
    ...      return 1
    ...    else:
    ...      return 3
    ...  return 2""")
    >>> pm = passmanager.PassManager("test")
    >>> _, node = pm.apply(NormalizeIfElse, node)
    >>> print(pm.dump(backend.Python, node))
    def foo(y):
        if y:
            z = (y + 1)
            if z:
                return 1
            else:
                return 3
        else:
            return 2
    '''

    def __init__(self):
        super(NormalizeIfElse, self).__init__(Ancestors)

    def check_lasts(self, node):
        if isinstance(node, (ast.Return, ast.Break, ast.Return)):
            return True
        if isinstance(node, ast.If):
            if not self.check_lasts(node.body[-1]):
                return False
            return node.orelse and self.check_lasts(node.orelse[-1])

    def visit_If(self, node):
        self.generic_visit(node)
        if not self.check_lasts(node.body[-1]):
            return node
        parent = self.ancestors[node][-1]
        for attr in ('body', 'orelse', 'finalbody'):
            try:
                body = getattr(parent, attr)
                index = body.index(node)
                if index == len(body) - 1:
                    return node
                if not node.orelse:
                    node.orelse = []
                node.orelse.extend(body[index + 1:])
                body[index + 1:] = []
                self.update = True
                return node
            except ValueError:
                continue
            except AttributeError:
                continue
        return node

Youez - 2016 - github.com/yon3zu
LinuXploit