Kaydet (Commit) bf16623c authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Merge branch 'master' of github.com:abstractequalsmagic/pepallow

...@@ -4,16 +4,16 @@ from pepallow.peps.p231 import AssetBean as Bean ...@@ -4,16 +4,16 @@ from pepallow.peps.p231 import AssetBean as Bean
with Allow(211): with Allow(211):
s = [1, 2, 3] s = [1, 2, 3]
t = "abc" t = "abc"
product = [] product = []
counter = 0 counter = 0
for i, j in s @ t: for i, j in s @ t:
product.append(f"{i}{j}") product.append(f"{i}{j}")
counter += 1 counter += 1
assert counter == 9 assert counter == 9
assert product == ["1a", "1b", "1c", "2a", "2b", "2c", "3a", "3b", "3c"] assert product == ['1a', '1b', '1c', '2a', '2b', '2c', '3a', '3b', '3c']
with Allow(231): with Allow(231):
b = Bean(3) b = Bean(3)
assert b.foo == 3 assert b.foo == 3
...@@ -26,11 +26,12 @@ with Allow(276): ...@@ -26,11 +26,12 @@ with Allow(276):
for number in 10: for number in 10:
items.append(number) items.append(number)
counter += 1 counter += 1
assert counter == 10 assert counter == 10
assert sum(items) == 45 assert sum(items) == 45
with Allow(313): with Allow(313):
assert IV + I == V assert IV + I == V
assert VI + M == 1006 assert VI + M == 1006
assert (M - D) + VI - X == (500) + 6 - 10 assert (M - D) + VI - X == (500) + 6 - 10
import ast import ast
class HandledTransformer(ast.NodeTransformer): class HandledTransformer(ast.NodeTransformer):
def __init__(self, handler, *args, **kwargs): def __init__(self, handler, *args, **kwargs):
self.handler = handler self.handler = handler
......
...@@ -21,30 +21,24 @@ class TreeHandler: ...@@ -21,30 +21,24 @@ class TreeHandler:
def __init__(self, tree): def __init__(self, tree):
self.tree = tree self.tree = tree
self._inserteds = set() self._inserteds = set()
def set_global(self, itemid, item): def set_global(self, itemid, item):
self._inserteds.add(itemid) self._inserteds.add(itemid)
self.tree.body = [item] + self.tree.body # self.tree.body.insert(0, item) self.tree.body = [item] + self.tree.body # self.tree.body.insert(0, item)
class PEPTransformer(ast.NodeTransformer): class PEPTransformer(ast.NodeTransformer):
def visit(self, tree): def visit(self, tree):
if isinstance(tree, ast.Module): if isinstance(tree, ast.Module):
self.handler = TreeHandler(tree) self.handler = TreeHandler(tree)
return super().visit(tree) return super().visit(tree)
def visit_With(self, node): def visit_With(self, node):
if ( if isinstance(node.items[0].context_expr, ast.Call) and isinstance(node.items[0].context_expr.func, ast.Name) and node.items[0].context_expr.func.id == "Allow":
isinstance(node.items[0].context_expr, ast.Call)
and isinstance(node.items[0].context_expr.func, ast.Name)
and node.items[0].context_expr.func.id == "Allow"
):
pep = node.items[0].context_expr.args[0].n pep = node.items[0].context_expr.args[0].n
new_node = PEPS[pep]["transformer"](self.handler).visit(node) new_node = PEPS[pep]["transformer"](self.handler).visit(node)
ast.copy_location(new_node, node) ast.copy_location(new_node, node)
ast.fix_missing_locations(new_node) ast.fix_missing_locations(new_node)
return node return node
......
...@@ -23,12 +23,10 @@ class PEP211Transformer(HandledTransformer): ...@@ -23,12 +23,10 @@ class PEP211Transformer(HandledTransformer):
and isinstance(node.iter.op, ast.MatMult) and isinstance(node.iter.op, ast.MatMult)
): ):
if len(node.target.elts) < 2: if len(node.target.elts) < 2:
raise ValueError( raise ValueError(f"Not enough values to unpack (expected 2, got {len(node.target.elts)})")
f"Not enough values to unpack (expected 2, got {len(node.target.elts)})"
)
elif len(node.target.elts) > 2: elif len(node.target.elts) > 2:
raise ValueError("Too many values to unpack (expected 2)") raise ValueError("Too many values to unpack (expected 2)")
a, b = node.target.elts a, b = node.target.elts
return ast.For( return ast.For(
target=a, target=a,
......
...@@ -7,18 +7,14 @@ class PEP231Transformer(HandledTransformer): ...@@ -7,18 +7,14 @@ class PEP231Transformer(HandledTransformer):
name = node.value name = node.value
attr = node.attr attr = node.attr
ctx = node.ctx ctx = node.ctx
if isinstance(ctx, ast.Load): if isinstance(ctx, ast.Load):
node = ast.IfExp( node = ast.IfExp(
ast.Call( ast.Call(ast.Name("hasattr", ast.Load()), [name, ast.Str("__findattr__")], []),
ast.Name("hasattr", ast.Load()), [name, ast.Str("__findattr__")], [] ast.Call(ast.Attribute(name, "__findattr__", ast.Load()), [ast.Str(attr)], []),
),
ast.Call(
ast.Attribute(name, "__findattr__", ast.Load()), [ast.Str(attr)], []
),
orelse=ast.Attribute(name, attr, ctx), orelse=ast.Attribute(name, attr, ctx),
) )
return node return node
def visit_Assign(self, node): def visit_Assign(self, node):
...@@ -27,27 +23,13 @@ class PEP231Transformer(HandledTransformer): ...@@ -27,27 +23,13 @@ class PEP231Transformer(HandledTransformer):
name = target.value name = target.value
attr = target.attr attr = target.attr
ctx = target.ctx ctx = target.ctx
node = ast.Expr( node = ast.Expr(ast.IfExp(
ast.IfExp( ast.Call(ast.Name("hasattr", ast.Load()), [name, ast.Str("__findattr__")], []),
ast.Call( ast.Call(ast.Attribute(name, "__findattr__", ast.Load()), [ast.Str(attr), node.value], []),
ast.Name("hasattr", ast.Load()), orelse=ast.Call(ast.Name("setattr", ast.Load()), [name, ast.Str(attr), node.value], []),
[name, ast.Str("__findattr__")], ))
[],
),
ast.Call(
ast.Attribute(name, "__findattr__", ast.Load()),
[ast.Str(attr), node.value],
[],
),
orelse=ast.Call(
ast.Name("setattr", ast.Load()),
[name, ast.Str(attr), node.value],
[],
),
)
)
return node return node
...@@ -55,7 +37,6 @@ class AssetBean: ...@@ -55,7 +37,6 @@ class AssetBean:
""" """
Directly copied from https://www.python.org/dev/peps/pep-0231/ Directly copied from https://www.python.org/dev/peps/pep-0231/
""" """
def __init__(self, x): def __init__(self, x):
self.__myfoo = x self.__myfoo = x
......
...@@ -7,7 +7,6 @@ try: ...@@ -7,7 +7,6 @@ try:
except ImportError: except ImportError:
from pepallow.romana import roman from pepallow.romana import roman
class PEP313Transformer(HandledTransformer): class PEP313Transformer(HandledTransformer):
""" """
PEP313 => Adding Roman Numeral Literals to Python PEP313 => Adding Roman Numeral Literals to Python
......
...@@ -18,9 +18,9 @@ class PEP377Transformer(HandledTransformer): ...@@ -18,9 +18,9 @@ class PEP377Transformer(HandledTransformer):
], ],
[], [],
), ),
), )
) )
return node return node
def visit_With(self, node): def visit_With(self, node):
...@@ -36,10 +36,7 @@ class PEP377Transformer(HandledTransformer): ...@@ -36,10 +36,7 @@ class PEP377Transformer(HandledTransformer):
assign = ( assign = (
ast.Pass() ast.Pass()
if node.items[0] is None if node.items[0] is None
else ast.Assign( else ast.Assign([node.items[0].optional_vars], ast.Name("StatementSkipped", ast.Load()))
[node.items[0].optional_vars],
ast.Name("StatementSkipped", ast.Load()),
)
) )
node = ast.Try( node = ast.Try(
[node], [node],
......
...@@ -8,7 +8,7 @@ with open(current_dir / "README.md", encoding="utf-8") as f: ...@@ -8,7 +8,7 @@ with open(current_dir / "README.md", encoding="utf-8") as f:
setup( setup(
name="pepallow", name="pepallow",
version="0.3.1", version="0.3.2",
packages=find_packages(), packages=find_packages(),
url="https://github.com/abstractequalsmagic/pepallow", url="https://github.com/abstractequalsmagic/pepallow",
description = "Hack the interpreter for running rejected pep's changes.", description = "Hack the interpreter for running rejected pep's changes.",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment