Skip to content

Commit 4723085

Browse files
committed
Hide internal methods implemented in python from python stack
1 parent dc0ad6d commit 4723085

File tree

8 files changed

+143
-17
lines changed

8 files changed

+143
-17
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_traceback.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*graalpython.lib-python.3.test.test_traceback.TestStack.test_extract_stack_limit
2828
*graalpython.lib-python.3.test.test_traceback.TestStack.test_extract_stack_lookup_lines
2929
*graalpython.lib-python.3.test.test_traceback.TestStack.test_extract_stackup_deferred_lookup_lines
30+
*graalpython.lib-python.3.test.test_traceback.TestStack.test_format_locals
3031
*graalpython.lib-python.3.test.test_traceback.TestStack.test_format_smoke
3132
*graalpython.lib-python.3.test.test_traceback.TestStack.test_from_list
3233
*graalpython.lib-python.3.test.test_traceback.TestStack.test_from_list_edited_stack

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ public synchronized PFunction convertToBuiltin(PFunction func) {
389389
*/
390390
Signature signature = func.getSignature();
391391
PFunction builtinFunc;
392+
FunctionRootNode functionRootNode = (FunctionRootNode) func.getFunctionRootNode();
393+
assert !functionRootNode.isPythonInternal() : "a function cannot be annotated as builtin twice";
394+
functionRootNode.setPythonInternal(true);
392395
if (signature.getParameterIds().length > 0 && signature.getParameterIds()[0].equals("self")) {
393396
/*
394397
* If the first parameter is called self, we assume the function does explicitly
@@ -402,9 +405,6 @@ public synchronized PFunction convertToBuiltin(PFunction func) {
402405
* PFunction cannot be used anymore (its signature won't agree with it's indexed
403406
* parameter reads).
404407
*/
405-
FunctionRootNode functionRootNode = (FunctionRootNode) func.getFunctionRootNode();
406-
assert !functionRootNode.isRewritten() : "a function cannot be annotated as builtin twice";
407-
408408
functionRootNode = functionRootNode.rewriteWithNewSignature(signature.createWithSelf(), new NodeVisitor() {
409409

410410
public boolean visit(Node node) {
@@ -428,6 +428,17 @@ public boolean visit(Node node) {
428428
}
429429
}
430430

431+
@Builtin(name = "builtin_method", minNumOfPositionalArgs = 1)
432+
@GenerateNodeFactory
433+
public abstract static class BuiltinMethodNode extends PythonUnaryBuiltinNode {
434+
@Specialization
435+
public Object doIt(PFunction func) {
436+
FunctionRootNode functionRootNode = (FunctionRootNode) func.getFunctionRootNode();
437+
functionRootNode.setPythonInternal(true);
438+
return func;
439+
}
440+
}
441+
431442
@Builtin(name = "get_toolchain_path", minNumOfPositionalArgs = 1)
432443
@TypeSystemReference(PythonArithmeticTypes.class)
433444
@GenerateNodeFactory

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/FunctionRootNode.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class FunctionRootNode extends PClosureFunctionRootNode {
7070
@Child private CalleeContext calleeContext = CalleeContext.create();
7171

7272
private final ExpressionNode uninitializedBody;
73-
private final boolean isRewritten;
73+
private boolean isPythonInternal;
7474

7575
public FunctionRootNode(PythonLanguage language, SourceSection sourceSection, String functionName, boolean isGenerator, boolean isRewritten, FrameDescriptor frameDescriptor,
7676
ExpressionNode uninitializedBody, ExecutionCellSlots executionCellSlots, Signature signature) {
@@ -85,7 +85,7 @@ public FunctionRootNode(PythonLanguage language, SourceSection sourceSection, St
8585
// "uninitializedBody" is never modified or executed
8686
this.uninitializedBody = uninitializedBody;
8787
this.generatorFrameProfile = isGenerator ? ValueProfile.createClassProfile() : null;
88-
this.isRewritten = isRewritten;
88+
this.isPythonInternal = isRewritten;
8989
}
9090

9191
/**
@@ -99,7 +99,7 @@ private FunctionRootNode(FunctionRootNode other) {
9999
this.functionName = other.functionName;
100100
this.isGenerator = other.isGenerator;
101101
this.generatorFrameProfile = other.isGenerator ? ValueProfile.createClassProfile() : null;
102-
this.isRewritten = other.isRewritten;
102+
this.isPythonInternal = other.isPythonInternal;
103103
this.uninitializedBody = other.uninitializedBody;
104104
}
105105

@@ -110,7 +110,7 @@ protected boolean isCloneUninitializedSupported() {
110110

111111
@Override
112112
protected RootNode cloneUninitialized() {
113-
return new FunctionRootNode(PythonLanguage.getCurrent(), getSourceSection(), functionName, isGenerator, isRewritten, getFrameDescriptor(), uninitializedBody, executionCellSlots,
113+
return new FunctionRootNode(PythonLanguage.getCurrent(), getSourceSection(), functionName, isGenerator, isPythonInternal, getFrameDescriptor(), uninitializedBody, executionCellSlots,
114114
getSignature());
115115
}
116116

@@ -206,18 +206,18 @@ public boolean isInternal() {
206206
return sourceSection != null && sourceSection.getSource().isInternal();
207207
}
208208

209-
public boolean isRewritten() {
210-
return isRewritten;
211-
}
212-
213209
@Override
214210
public void initializeFrame(VirtualFrame frame) {
215211
initClosureAndCellVars(frame);
216212
}
217213

218214
@Override
219215
public boolean isPythonInternal() {
220-
return isRewritten;
216+
return isPythonInternal;
217+
}
218+
219+
public void setPythonInternal(boolean pythonInternal) {
220+
isPythonInternal = pythonInternal;
221221
}
222222

223223
public ExecutionCellSlots getExecutionCellSlots() {

graalpython/lib-graalpython/_collections.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
class Block(object):
3434
__slots__ = ('leftlink', 'rightlink', 'data')
3535

36+
@__graalpython__.builtin_method
3637
def __init__(self, leftlink, rightlink):
3738
self.leftlink = leftlink
3839
self.rightlink = rightlink
@@ -68,6 +69,7 @@ def _next_or_none(_iter):
6869

6970

7071
class deque(object):
72+
@__graalpython__.builtin_method
7173
def __init__(self, iterable=None, maxlen=None):
7274
if maxlen is None:
7375
self._maxlen = sys.maxsize
@@ -90,6 +92,7 @@ def __init__(self, iterable=None, maxlen=None):
9092
self.extend(iterable)
9193

9294
def synchronized(fun):
95+
@__graalpython__.builtin_method
9396
def fun_synchronized(self, *args):
9497
with self._mutex:
9598
return fun(self, *args)
@@ -121,14 +124,17 @@ def pop(self):
121124
self._modified()
122125
return obj
123126

127+
@__graalpython__.builtin_method
124128
def _modified(self):
125129
self._lock = None
126130

131+
@__graalpython__.builtin_method
127132
def _getlock(self):
128133
if self._lock is None:
129134
self._lock = Lock()
130135
return self._lock
131136

137+
@__graalpython__.builtin_method
132138
def _checklock(self, lock):
133139
if lock is not self._lock:
134140
raise RuntimeError("deque mutated during iteration")
@@ -225,23 +231,29 @@ def extend(self, iterable):
225231
break
226232
self.append(obj)
227233

234+
@__graalpython__.builtin_method
228235
def __iadd__(self, other):
229236
return _add(self, other)
230237

238+
@__graalpython__.builtin_method
231239
def __add__(self, other):
232240
if not isinstance(other, deque):
233241
raise TypeError("can only concatenate deque (not '%s') to deque" % (type(other)))
234242
return _add(deque(self, maxlen=self.maxlen), other)
235243

244+
@__graalpython__.builtin_method
236245
def __imul__(self, times):
237246
return _mul(self, times)
238247

248+
@__graalpython__.builtin_method
239249
def __mul__(self, times):
240250
return _mul(deque(self, maxlen=self.maxlen), times)
241251

252+
@__graalpython__.builtin_method
242253
def __rmul__(self, times):
243254
return _mul(deque(self, maxlen=self.maxlen), times)
244255

256+
@__graalpython__.builtin_method
245257
def __hash__(self):
246258
raise TypeError("unhashable type: '{}'".format(type(self).__name__))
247259

@@ -343,16 +355,20 @@ def rotate(self, n=1):
343355
self.append(self.popleft())
344356
i -= 1
345357

358+
@__graalpython__.builtin_method
346359
def __iter__(self):
347360
return _DequeIter(self)
348361

362+
@__graalpython__.builtin_method
349363
def __reversed__(self):
350364
"""Return a reverse iterator over the deque."""
351365
return _DequeRevIter(self)
352366

367+
@__graalpython__.builtin_method
353368
def __len__(self):
354369
return self.len
355370

371+
@__graalpython__.builtin_method
356372
def __repr__(self):
357373
# TODO: this does not handle infinite repr recursive calls ... (GR-10763)
358374
try:
@@ -405,6 +421,7 @@ def __compare__(self, other, op):
405421
return x1 >= x2
406422
assert False, "bad value for op"
407423

424+
@__graalpython__.builtin_method
408425
def __contains__(self, v):
409426
lock = self._getlock()
410427
n = self.len
@@ -427,13 +444,15 @@ def __contains__(self, v):
427444

428445
return False
429446

447+
@__graalpython__.builtin_method
430448
def _norm_index(self, idx, force_index_to_zero=True):
431449
if idx < 0:
432450
idx += self.len
433451
if idx < 0 and force_index_to_zero:
434452
idx = 0
435453
return idx
436454

455+
@__graalpython__.builtin_method
437456
def _check_index(self, idx):
438457
if idx < 0 or idx >= self.len:
439458
raise IndexError("deque index out of range")
@@ -482,24 +501,31 @@ def index(self, v, start=0, stop=None):
482501

483502
raise ValueError("%s is not in deque" % v)
484503

504+
@__graalpython__.builtin_method
485505
def __lt__(self, other):
486506
return self.__compare__(other, 'lt')
487507

508+
@__graalpython__.builtin_method
488509
def __le__(self, other):
489510
return self.__compare__(other, 'le')
490511

512+
@__graalpython__.builtin_method
491513
def __eq__(self, other):
492514
return self.__compare__(other, 'eq')
493515

516+
@__graalpython__.builtin_method
494517
def __ne__(self, other):
495518
return self.__compare__(other, 'ne')
496519

520+
@__graalpython__.builtin_method
497521
def __gt__(self, other):
498522
return self.__compare__(other, 'gt')
499523

524+
@__graalpython__.builtin_method
500525
def __ge__(self, other):
501526
return self.__compare__(other, 'ge')
502527

528+
@__graalpython__.builtin_method
503529
def _locate(self, i):
504530
if i < (self.len >> 1):
505531
i += self.leftindex
@@ -517,25 +543,29 @@ def _locate(self, i):
517543
assert i >= 0
518544
return b, i
519545

546+
@__graalpython__.builtin_method
520547
def delitem(self, i):
521548
# delitem() implemented in terms of rotate for simplicity and
522549
# reasonable performance near the end points.
523550
self.rotate(-i)
524551
self.popleft()
525552
self.rotate(i)
526553

554+
@__graalpython__.builtin_method
527555
def __getitem__(self, idx):
528556
idx = self._norm_index(idx)
529557
self._check_index(idx)
530558
b, i = self._locate(idx)
531559
return b.data[i]
532560

561+
@__graalpython__.builtin_method
533562
def __setitem__(self, idx, value):
534563
idx = self._norm_index(idx, force_index_to_zero=False)
535564
self._check_index(idx)
536565
b, i = self._locate(idx)
537566
b.data[i] = value
538567

568+
@__graalpython__.builtin_method
539569
def __delitem__(self, idx):
540570
idx = self._norm_index(idx, force_index_to_zero=False)
541571
self._check_index(idx)
@@ -549,9 +579,11 @@ def copy(self):
549579
else:
550580
return deque(self, self.maxlen)
551581

582+
@__graalpython__.builtin_method
552583
def __copy__(self):
553584
return self.copy()
554585

586+
@__graalpython__.builtin_method
555587
def __reduce__(self):
556588
"""Return state information for pickling."""
557589
_type = type(self)
@@ -581,6 +613,7 @@ def maxlen(self):
581613

582614

583615
class _DequeIter(object):
616+
@__graalpython__.builtin_method
584617
def __init__(self, dq):
585618
self._deque = dq
586619
self.block = dq.leftblock
@@ -589,12 +622,15 @@ def __init__(self, dq):
589622
self.lock = dq._getlock()
590623
assert self.index >= 0
591624

625+
@__graalpython__.builtin_method
592626
def __iter__(self):
593627
return self
594628

629+
@__graalpython__.builtin_method
595630
def __len__(self):
596631
return self.counter
597632

633+
@__graalpython__.builtin_method
598634
def __next__(self):
599635
if self.lock is not self._deque._lock:
600636
self.counter = 0
@@ -613,6 +649,7 @@ def __next__(self):
613649

614650

615651
class _DequeRevIter(object):
652+
@__graalpython__.builtin_method
616653
def __init__(self, dq):
617654
self._deque = dq
618655
self.block = dq.rightblock
@@ -621,12 +658,15 @@ def __init__(self, dq):
621658
self.lock = dq._getlock()
622659
assert self.index >= 0
623660

661+
@__graalpython__.builtin_method
624662
def __iter__(self):
625663
return self
626664

665+
@__graalpython__.builtin_method
627666
def __len__(self):
628667
return self.counter
629668

669+
@__graalpython__.builtin_method
630670
def __next__(self):
631671
if self.lock is not self._deque._lock:
632672
self.counter = 0
@@ -645,22 +685,26 @@ def __next__(self):
645685

646686

647687
class defaultdict(dict):
688+
@__graalpython__.builtin_method
648689
def __init__(self, default_factory=None, *args, **kwds):
649690
dict.__init__(self, *args, **kwds)
650691
if (default_factory is None or callable(default_factory)):
651692
self.default_factory = default_factory
652693
else:
653694
raise TypeError("first argument must be callable or None")
654695

696+
@__graalpython__.builtin_method
655697
def __missing__(self, key):
656698
if self.default_factory is None:
657699
raise KeyError(key)
658700
self[key] = value = self.default_factory()
659701
return value
660702

703+
@__graalpython__.builtin_method
661704
def __repr__(self):
662705
return "%s(%r, %s)" % (type(self).__name__, self.default_factory, dict.__repr__(self))
663706

707+
@__graalpython__.builtin_method
664708
def copy(self):
665709
cp = defaultdict(default_factory=self.default_factory)
666710
for k,v in self.items():

graalpython/lib-graalpython/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ def frombytes(self, bytesLike):
4545
def fromfile(self, f, n):
4646
self.frombytes(f.read(n))
4747

48-
array.frombytes = frombytes
49-
array.fromfile = fromfile
48+
array.frombytes = __graalpython__.builtin_method(frombytes)
49+
array.fromfile = __graalpython__.builtin_method(fromfile)

graalpython/lib-graalpython/bytearray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ def strip(self, what=None):
4646
return self.lstrip(what).rstrip(what)
4747

4848

49-
bytearray.strip = strip
49+
bytearray.strip = __graalpython__.builtin_method(strip)
5050
bytearray.rfind = bytes.rfind

0 commit comments

Comments
 (0)