Skip to content

Commit a3898cd

Browse files
committed
Fix DirEntry.stat
The attribute was not writable, it was missing follow_symlinks and its return value was tuple instead of stat_result.
1 parent 03eedd0 commit a3898cd

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/posix/DirEntryBuiltins.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
import com.oracle.graal.python.builtins.objects.function.PArguments;
5353
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5454
import com.oracle.graal.python.nodes.SpecialMethodNames;
55-
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
56-
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
5755
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5856
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
5957
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
@@ -150,22 +148,19 @@ boolean test(PDirEntry self) {
150148
}
151149
}
152150

153-
@Builtin(name = "stat", minNumOfPositionalArgs = 1, doc = "return stat_result object for the entry; cached per entry")
151+
@Builtin(name = "stat", minNumOfPositionalArgs = 1, parameterNames = {"$self", "follow_symlinks"}, doc = "return stat_result object for the entry; cached per entry")
154152
@GenerateNodeFactory
155-
abstract static class StatNode extends PythonUnaryBuiltinNode {
156-
private static final String STAT_RESULT = "__stat_result__";
153+
abstract static class StatNode extends PythonBinaryBuiltinNode {
157154

158155
@Specialization
159-
Object test(VirtualFrame frame, PDirEntry self,
160-
@Cached("create()") ReadAttributeFromObjectNode readNode,
161-
@Cached("create()") WriteAttributeToObjectNode writeNode,
156+
Object test(VirtualFrame frame, PDirEntry self, Object followSymlinks,
162157
@Cached("create()") PosixModuleBuiltins.StatNode statNode) {
163-
Object stat_result = readNode.execute(self, STAT_RESULT);
164-
if (stat_result == PNone.NO_VALUE) {
165-
stat_result = statNode.execute(frame, self.getFile().getAbsoluteFile().getPath(), PNone.NO_VALUE);
166-
writeNode.execute(self, STAT_RESULT, stat_result);
158+
Object statResult = self.getCachedStatResult();
159+
if (statResult == null) {
160+
statResult = statNode.execute(frame, self.getFile().getAbsoluteFile().getPath(), followSymlinks);
161+
self.setCachedStatResult(statResult);
167162
}
168-
return stat_result;
163+
return statResult;
169164
}
170165
}
171166

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/posix/PDirEntry.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -47,6 +47,7 @@
4747
public class PDirEntry extends PythonBuiltinObject {
4848
private final TruffleFile file;
4949
private final String name;
50+
private Object cachedStatResult;
5051

5152
public PDirEntry(LazyPythonClass cls, String name, TruffleFile file) {
5253
super(cls);
@@ -61,4 +62,12 @@ public TruffleFile getFile() {
6162
public String getName() {
6263
return name;
6364
}
65+
66+
public Object getCachedStatResult() {
67+
return cachedStatResult;
68+
}
69+
70+
public void setCachedStatResult(Object cachedStatResult) {
71+
this.cachedStatResult = cachedStatResult;
72+
}
6473
}

graalpython/lib-graalpython/posix.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -56,6 +56,12 @@ def stat(filename, follow_symlinks=True):
5656
return stat_result(old_stat(filename, follow_symlinks=follow_symlinks))
5757

5858

59+
__dir_entry_old_stat = DirEntry.stat
60+
def __dir_entry_stat(self, follow_symlinks=True):
61+
return stat_result(__dir_entry_old_stat(self, follow_symlinks=follow_symlinks))
62+
DirEntry.stat = __dir_entry_stat
63+
64+
5965
@__builtin__
6066
def lstat(filename):
6167
if not graal_python_is_native:

0 commit comments

Comments
 (0)