Skip to content

Commit c992e4e

Browse files
committed
Remove AST rewriting of @Builtin functions
1 parent 7919a3d commit c992e4e

File tree

5 files changed

+24
-78
lines changed

5 files changed

+24
-78
lines changed

graalpython/com.oracle.graal.python.test/testData/testFiles/RuntimeFileTests/functions.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,29 @@
3838
# SOFTWARE.
3939

4040
@__graalpython__.builtin
41-
def hasattr(obj, key):
41+
def hasattr(module, obj, key):
4242
default = object()
4343
return getattr(obj, key, default) is not default
4444

4545

4646
@__graalpython__.builtin
47-
def any(iterable):
47+
def any(module, iterable):
4848
for i in iterable:
4949
if i:
5050
return True
5151
return False
5252

5353

5454
@__graalpython__.builtin
55-
def all(iterable):
55+
def all(module, iterable):
5656
for i in iterable:
5757
if not i:
5858
return False
5959
return True
6060

6161

6262
@__graalpython__.builtin
63-
def filter(func, iterable):
63+
def filter(module, func, iterable):
6464
result = []
6565
predicate = func if func is not None else lambda a: a
6666
for i in iterable:
@@ -112,7 +112,7 @@ def __contains__(self, x):
112112

113113

114114
@__graalpython__.builtin
115-
def vars(*obj):
115+
def vars(module, *obj):
116116
"""Return a dictionary of all the attributes currently bound in obj. If
117117
called with no argument, return the variables bound in local scope."""
118118
if len(obj) == 0:
@@ -127,7 +127,7 @@ def vars(*obj):
127127

128128

129129
@__graalpython__.builtin
130-
def format(value, format_spec=''):
130+
def format(module, value, format_spec=''):
131131
"""Return value.__format__(format_spec)
132132
133133
format_spec defaults to the empty string.
@@ -137,7 +137,7 @@ def format(value, format_spec=''):
137137

138138

139139
@__graalpython__.builtin
140-
def sorted(iterable, key=None, reverse=False):
140+
def sorted(module, iterable, key=None, reverse=False):
141141
"""Return a new list containing all items from the iterable in ascending order.
142142
143143
A custom key function can be supplied to customize the sort order, and the

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
7979
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum.ErrorAndMessagePair;
8080
import com.oracle.graal.python.builtins.objects.function.PFunction;
81-
import com.oracle.graal.python.builtins.objects.function.Signature;
8281
import com.oracle.graal.python.builtins.objects.generator.PGenerator;
8382
import com.oracle.graal.python.builtins.objects.list.PList;
8483
import com.oracle.graal.python.builtins.objects.method.PMethod;
@@ -88,10 +87,7 @@
8887
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
8988
import com.oracle.graal.python.lib.PyObjectTypeCheck;
9089
import com.oracle.graal.python.nodes.ErrorMessages;
91-
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
92-
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
9390
import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetCallTargetNode;
94-
import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetSignatureNode;
9591
import com.oracle.graal.python.nodes.call.CallNode;
9692
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
9793
import com.oracle.graal.python.nodes.function.FunctionRootNode;
@@ -115,7 +111,6 @@
115111
import com.oracle.truffle.api.CallTarget;
116112
import com.oracle.truffle.api.CompilerDirectives;
117113
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
118-
import com.oracle.truffle.api.RootCallTarget;
119114
import com.oracle.truffle.api.TruffleFile;
120115
import com.oracle.truffle.api.TruffleLanguage.Env;
121116
import com.oracle.truffle.api.TruffleLogger;
@@ -131,9 +126,7 @@
131126
import com.oracle.truffle.api.interop.UnsupportedMessageException;
132127
import com.oracle.truffle.api.library.CachedLibrary;
133128
import com.oracle.truffle.api.nodes.LanguageInfo;
134-
import com.oracle.truffle.api.nodes.Node;
135129
import com.oracle.truffle.api.nodes.NodeUtil;
136-
import com.oracle.truffle.api.nodes.NodeVisitor;
137130
import com.oracle.truffle.api.source.Source;
138131
import com.oracle.truffle.llvm.api.Toolchain;
139132

@@ -448,56 +441,9 @@ public Object doIt(VirtualFrame frame, PFunction func) {
448441

449442
@TruffleBoundary
450443
public synchronized PFunction convertToBuiltin(PFunction func) {
451-
/*
452-
* (tfel): To be compatible with CPython, builtin module functions must be bound to
453-
* their respective builtin module. We ignore that builtin functions should really be
454-
* builtin methods here - it does not hurt if they are normal methods. What does hurt,
455-
* however, is if they are not bound, because then using these functions in class field
456-
* won't work when they are called from an instance of that class due to the implicit
457-
* currying with "self".
458-
*/
459-
Signature signature = GetSignatureNode.getUncached().execute(func);
460-
PFunction builtinFunc;
461-
FunctionRootNode functionRootNode = (FunctionRootNode) CodeNodes.GetCodeRootNode.getUncached().execute(func.getCode());
462-
if (signature.getParameterIds().length > 0 && signature.getParameterIds()[0].equals("self")) {
463-
/*
464-
* If the first parameter is called self, we assume the function does explicitly
465-
* declare the module argument
466-
*/
467-
builtinFunc = func;
468-
functionRootNode.setPythonInternal(true);
469-
} else {
470-
RootCallTarget callTarget = PythonLanguage.get(functionRootNode).createCachedCallTarget(
471-
r -> {
472-
/*
473-
* Otherwise, we create a new function with a signature that
474-
* requires one extra argument in front. We actually modify the
475-
* function's AST here, so the original PFunction cannot be used
476-
* anymore (its signature won't agree with it's indexed
477-
* parameter reads).
478-
*/
479-
assert !functionRootNode.isPythonInternal() : "a function cannot be rewritten as builtin twice";
480-
return functionRootNode.rewriteWithNewSignature(signature.createWithSelf(), new NodeVisitor() {
481-
482-
@Override
483-
public boolean visit(Node node) {
484-
if (node instanceof ReadVarArgsNode) {
485-
node.replace(ReadVarArgsNode.create(((ReadVarArgsNode) node).isBuiltin()));
486-
} else if (node instanceof ReadIndexedArgumentNode) {
487-
node.replace(ReadIndexedArgumentNode.create(((ReadIndexedArgumentNode) node).getIndex() + 1));
488-
}
489-
return true;
490-
}
491-
}, x -> x);
492-
}, functionRootNode);
493-
494-
String name = func.getName();
495-
builtinFunc = factory().createFunction(name, func.getEnclosingClassName(),
496-
factory().createCode(callTarget),
497-
func.getGlobals(), func.getDefaults(), func.getKwDefaults(), func.getClosure());
498-
}
499-
500-
return builtinFunc;
444+
FunctionRootNode rootNode = (FunctionRootNode) CodeNodes.GetCodeRootNode.getUncached().execute(func.getCode());
445+
rootNode.setPythonInternal(true);
446+
return func;
501447
}
502448
}
503449

graalpython/lib-graalpython/__graalpython__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import sys
4141

4242
@builtin
43-
def import_current_as_named_module(name, owner_globals=None):
43+
def import_current_as_named_module(module, name, owner_globals=None):
4444
"""
4545
load a builtin anonymous module which does not have a Truffle land builtin module counter part
4646
@@ -62,7 +62,7 @@ def import_current_as_named_module(name, owner_globals=None):
6262

6363

6464
@builtin
65-
def lazy_attributes_from_delegate(delegate_name, attributes, owner_module, on_import_error):
65+
def lazy_attributes_from_delegate(module, delegate_name, attributes, owner_module, on_import_error):
6666
"""
6767
used to lazily load attributes defined in another module via the __getattr__ mechanism.
6868
This will only cache the attributes in the caller module.
@@ -99,7 +99,7 @@ def __getattr__(attr):
9999

100100

101101
@builtin
102-
def auto_wrap_methods(delegate_name, delegate_attributes, owner_globals):
102+
def auto_wrap_methods(module, delegate_name, delegate_attributes, owner_globals):
103103
func_type = type(import_current_as_named_module)
104104

105105
new_globals = dict(**owner_globals)
@@ -117,7 +117,7 @@ def auto_wrap_methods(delegate_name, delegate_attributes, owner_globals):
117117
if attr in delegate_attributes:
118118
def make_wrapper(attribute, method):
119119
@__graalpython__.builtin
120-
def wrapper(*args, **kwargs):
120+
def wrapper(module, *args, **kwargs):
121121
try:
122122
return method(*args, **kwargs)
123123
except NotImplementedError:
@@ -131,7 +131,7 @@ def wrapper(*args, **kwargs):
131131

132132

133133
@builtin
134-
def import_current_as_named_module_with_delegate(module_name, delegate_name, delegate_attributes=None,
134+
def import_current_as_named_module_with_delegate(module, module_name, delegate_name, delegate_attributes=None,
135135
owner_globals=None, wrap_methods=True, on_import_error=None):
136136
owner_module = import_current_as_named_module(module_name, owner_globals=owner_globals)
137137
if wrap_methods and owner_globals:
@@ -142,7 +142,7 @@ def import_current_as_named_module_with_delegate(module_name, delegate_name, del
142142

143143

144144
@builtin
145-
def build_java_class(ns, name, base):
145+
def build_java_class(module, ns, name, base):
146146
ns['__super__'] = None # place where store the original java class when instance is created
147147
ExtenderClass = type("PythonJavaExtenderClass", (object, ), ns)
148148
HostAdapter = __graalpython__.extend(base)

graalpython/lib-graalpython/_sre.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,32 +587,32 @@ def compile(pattern, flags, code, groups, groupindex, indexgroup):
587587

588588

589589
@__graalpython__.builtin
590-
def getcodesize(*args, **kwargs):
590+
def getcodesize(module, *args, **kwargs):
591591
raise NotImplementedError("_sre.getcodesize is not yet implemented")
592592

593593

594594
@__graalpython__.builtin
595-
def getlower(char_ord, flags):
595+
def getlower(module, char_ord, flags):
596596
import _cpython_sre
597597
return _cpython_sre.getlower(char_ord, flags)
598598

599599

600600
@__graalpython__.builtin
601-
def unicode_iscased(codepoint):
601+
def unicode_iscased(module, codepoint):
602602
ch = chr(codepoint)
603603
return ch != ch.lower() or ch != ch.upper()
604604

605605

606606
@__graalpython__.builtin
607-
def unicode_tolower(codepoint):
607+
def unicode_tolower(module, codepoint):
608608
return ord(chr(codepoint).lower())
609609

610610

611611
@__graalpython__.builtin
612-
def ascii_iscased(codepoint):
612+
def ascii_iscased(module, codepoint):
613613
return codepoint < 128 and chr(codepoint).isalpha()
614614

615615

616616
@__graalpython__.builtin
617-
def ascii_tolower(codepoint):
617+
def ascii_tolower(module, codepoint):
618618
return ord(chr(codepoint).lower()) if codepoint < 128 else codepoint

graalpython/lib-graalpython/builtins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _f(): pass
5656

5757

5858
@__graalpython__.builtin
59-
def vars(*obj):
59+
def vars(module, *obj):
6060
"""Return a dictionary of all the attributes currently bound in obj. If
6161
called with no argument, return the variables bound in local scope."""
6262
if len(obj) == 0:
@@ -71,7 +71,7 @@ def vars(*obj):
7171

7272

7373
@__graalpython__.builtin
74-
def input(prompt=None):
74+
def input(module, prompt=None):
7575
import sys
7676
if(not hasattr(sys, "stdin")):
7777
raise RuntimeError('input(): lost sys.stdin')

0 commit comments

Comments
 (0)