PNG  IHDR* pHYs+ IDATx]n#; cdLb Ǚ[at¤_:uP}>!Usă cag޿ ֵNu`ݼTâabO7uL&y^wFٝA"l[|ŲHLN밪4*sG3|Dv}?+y߉{OuOAt4Jj.u]Gz*҉sP'VQKbA1u\`& Af;HWj hsO;ogTu uj7S3/QzUr&wS`M$X_L7r2;aE+ώ%vikDA:dR+%KzƉo>eOth$z%: :{WwaQ:wz%4foɹE[9<]#ERINƻv溂E%P1i01 |Jvҗ&{b?9g=^wζXn/lK::90KwrюO\!ջ3uzuGv^;騢wq<Iatv09:tt~hEG`v;3@MNZD.1]L:{ծI3`L(÷ba")Y.iljCɄae#I"1 `3*Bdz>j<fU40⨬%O$3cGt]j%Fߠ_twJ;ABU8vP3uEԑwQ V:h%))LfraqX-ۿX]v-\9I gl8tzX ]ecm)-cgʒ#Uw=Wlێn(0hPP/ӨtQ“&J35 $=]r1{tLuǮ*i0_;NƝ8;-vݏr8+U-kruȕYr0RnC]*ެ(M:]gE;{]tg(#ZJ9y>utRDRMdr9㪩̞zֹb<ģ&wzJM"iI( .ꮅX)Qw:9,i좜\Ԛi7&N0:asϓc];=ΗOӣ APqz93 y $)A*kVHZwBƺnWNaby>XMN*45~ղM6Nvm;A=jֲ.~1}(9`KJ/V F9[=`~[;sRuk]rєT!)iQO)Y$V ی ۤmzWz5IM Zb )ˆC`6 rRa}qNmUfDsWuˤV{ Pݝ'=Kֳbg,UҘVz2ﴻnjNgBb{? ߮tcsͻQuxVCIY۠:(V뺕 ٥2;t`@Fo{Z9`;]wMzU~%UA蛚dI vGq\r82iu +St`cR.6U/M9IENDB` REDROOM
PHP 5.6.40
Preview: fix_idioms.py Size: 4.76 KB
//lib64/python3.6/lib2to3/fixes/fix_idioms.py

"""Adjust some old Python 2 idioms to their modern counterparts.

* Change some type comparisons to isinstance() calls:
    type(x) == T -> isinstance(x, T)
    type(x) is T -> isinstance(x, T)
    type(x) != T -> not isinstance(x, T)
    type(x) is not T -> not isinstance(x, T)

* Change "while 1:" into "while True:".

* Change both

    v = list(EXPR)
    v.sort()
    foo(v)

and the more general

    v = EXPR
    v.sort()
    foo(v)

into

    v = sorted(EXPR)
    foo(v)
"""
# Author: Jacques Frechet, Collin Winter

# Local imports
from .. import fixer_base
from ..fixer_util import Call, Comma, Name, Node, BlankLine, syms

CMP = "(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)"
TYPE = "power< 'type' trailer< '(' x=any ')' > >"

class FixIdioms(fixer_base.BaseFix):
    explicit = True # The user must ask for this fixer

    PATTERN = r"""
        isinstance=comparison< %s %s T=any >
        |
        isinstance=comparison< T=any %s %s >
        |
        while_stmt< 'while' while='1' ':' any+ >
        |
        sorted=any<
            any*
            simple_stmt<
              expr_stmt< id1=any '='
                         power< list='list' trailer< '(' (not arglist<any+>) any ')' > >
              >
              '\n'
            >
            sort=
            simple_stmt<
              power< id2=any
                     trailer< '.' 'sort' > trailer< '(' ')' >
              >
              '\n'
            >
            next=any*
        >
        |
        sorted=any<
            any*
            simple_stmt< expr_stmt< id1=any '=' expr=any > '\n' >
            sort=
            simple_stmt<
              power< id2=any
                     trailer< '.' 'sort' > trailer< '(' ')' >
              >
              '\n'
            >
            next=any*
        >
    """ % (TYPE, CMP, CMP, TYPE)

    def match(self, node):
        r = super(FixIdioms, self).match(node)
        # If we've matched one of the sort/sorted subpatterns above, we
        # want to reject matches where the initial assignment and the
        # subsequent .sort() call involve different identifiers.
        if r and "sorted" in r:
            if r["id1"] == r["id2"]:
                return r
            return None
        return r

    def transform(self, node, results):
        if "isinstance" in results:
            return self.transform_isinstance(node, results)
        elif "while" in results:
            return self.transform_while(node, results)
        elif "sorted" in results:
            return self.transform_sort(node, results)
        else:
            raise RuntimeError("Invalid match")

    def transform_isinstance(self, node, results):
        x = results["x"].clone() # The thing inside of type()
        T = results["T"].clone() # The type being compared against
        x.prefix = ""
        T.prefix = " "
        test = Call(Name("isinstance"), [x, Comma(), T])
        if "n" in results:
            test.prefix = " "
            test = Node(syms.not_test, [Name("not"), test])
        test.prefix = node.prefix
        return test

    def transform_while(self, node, results):
        one = results["while"]
        one.replace(Name("True", prefix=one.prefix))

    def transform_sort(self, node, results):
        sort_stmt = results["sort"]
        next_stmt = results["next"]
        list_call = results.get("list")
        simple_expr = results.get("expr")

        if list_call:
            list_call.replace(Name("sorted", prefix=list_call.prefix))
        elif simple_expr:
            new = simple_expr.clone()
            new.prefix = ""
            simple_expr.replace(Call(Name("sorted"), [new],
                                     prefix=simple_expr.prefix))
        else:
            raise RuntimeError("should not have reached here")
        sort_stmt.remove()

        btwn = sort_stmt.prefix
        # Keep any prefix lines between the sort_stmt and the list_call and
        # shove them right after the sorted() call.
        if "\n" in btwn:
            if next_stmt:
                # The new prefix should be everything from the sort_stmt's
                # prefix up to the last newline, then the old prefix after a new
                # line.
                prefix_lines = (btwn.rpartition("\n")[0], next_stmt[0].prefix)
                next_stmt[0].prefix = "\n".join(prefix_lines)
            else:
                assert list_call.parent
                assert list_call.next_sibling is None
                # Put a blank line after list_call and set its prefix.
                end_line = BlankLine()
                list_call.parent.append_child(end_line)
                assert list_call.next_sibling is end_line
                # The new prefix should be everything up to the first new line
                # of sort_stmt's prefix.
                end_line.prefix = btwn.rpartition("\n")[0]

Directory Contents

Dirs: 1 × Files: 53

Name Size Perms Modified Actions
- drwxr-xr-x 2026-02-06 23:01:23
Edit Download
2.37 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
984 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
320 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
590 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.67 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.27 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
979 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.00 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.44 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.59 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
644 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
547 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
451 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.12 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
4.76 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.18 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.55 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
289 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
708 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.21 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.57 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.51 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.04 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
476 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.55 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.00 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
606 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
571 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.10 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
591 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
768 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
3.39 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.20 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.78 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.86 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
454 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
837 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.13 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.17 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
613 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.66 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
449 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.01 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.54 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
5.43 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.73 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.23 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
8.16 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.06 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
2.63 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
689 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download
1.26 KB lrw-r--r-- 2018-12-23 21:37:14
Edit Download
47 B lrw-r--r-- 2018-12-23 21:37:14
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).
© 2026 REDROOM — Secure File Manager. All rights reserved. Built with ❤️ & Red Dark UI