Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test infix operators and precedence in IPython #14077

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions IPython/core/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2658,8 +2658,8 @@ def unicode_name_matches(text: str) -> Tuple[str, List[str]]:
try :
unic = unicodedata.lookup(s)
# allow combining chars
if ('a'+unic).isidentifier():
return '\\'+s,[unic]
# if ('a'+unic).isidentifier():
return "\\" + s, [unic]
except KeyError:
pass
return '', []
Expand Down
59 changes: 59 additions & 0 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
from IPython.utils.syspathcontext import prepended_to_syspath
from IPython.utils.text import DollarFormatter, LSString, SList, format_screen
from IPython.core.oinspect import OInfo
from io import StringIO


sphinxify: Optional[Callable]
Expand Down Expand Up @@ -286,6 +287,10 @@ def _modified_open(file, *args, **kwargs):
class InteractiveShell(SingletonConfigurable):
"""An enhanced, interactive shell for Python."""

OPERATOR_NAME = "⊕"
OPERATOR_PREC = "+"
OPERATOR_ASCII = "opop"
OP_FUNC_NAME = "undefined"
_instance = None

ast_transformers = List([], help=
Expand Down Expand Up @@ -3237,7 +3242,22 @@ def error_before_exec(value):

with self.display_trap:
# Compile to bytecode
import tokenize
from io import BytesIO

def tokenize_and_add_stuff(string, word):
result = []
tokens = tokenize.generate_tokens(StringIO(string).readline)
for toknum, tokval, _, _, _ in tokens:
if tokval == word:
result.append((tokenize.OP, self.OPERATOR_PREC))
result.append((toknum, self.OPERATOR_ASCII))
result.append((tokenize.OP, self.OPERATOR_PREC))
else:
result.append((toknum, tokval))
return tokenize.untokenize(result)
try:
cell = tokenize_and_add_stuff(cell, self.OPERATOR_NAME)
code_ast = compiler.ast_parse(cell, filename=cell_name)
except self.custom_exceptions as e:
etype, value, tb = sys.exc_info()
Expand All @@ -3254,6 +3274,45 @@ def error_before_exec(value):
# Apply AST transformations
try:
code_ast = self.transform_ast(code_ast)
import ast
from ast import BinOp, Name
from copy import deepcopy

template = ast.parse(
"""
infix_(exp1, exp2)
"""
).body[0]

class BinOpReplacer(ast.NodeTransformer):
def __init__(self, op, func_name):
self.search = op
self.func_name = func_name

def visit_BinOp(self, node):
if isinstance(node.left, BinOp):
left = node.left
if (
isinstance(left.right, Name)
and left.right.id == self.search
):
expr = deepcopy(template)
expr.value.func.id = self.func_name
expr.value.args[0] = node.left.left
expr.value.args[1] = node.right
return expr.value
return ast.BinOp(
left=self.visit(node.left),
op=node.op,
right=self.visit(node.right),
)

return node

bor = BinOpReplacer(self.OPERATOR_ASCII, self.OP_FUNC_NAME)
res = bor.visit(code_ast)
code_ast = ast.fix_missing_locations(res)

except InputRejected as e:
self.showtraceback()
return error_before_exec(e)
Expand Down