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