Skip to content

Commit 110e8ee

Browse files
committed
Refactoring raiseOsError
1 parent ad51cc6 commit 110e8ee

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PNodeWithContext.java

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4949
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
5050
import com.oracle.graal.python.builtins.objects.type.PythonClass;
51+
import com.oracle.graal.python.nodes.argument.positional.PositionalArgumentsNode;
5152
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
5253
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
5354
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
@@ -66,8 +67,6 @@
6667
import com.oracle.truffle.api.nodes.Node;
6768
import com.oracle.truffle.api.nodes.RootNode;
6869
import com.oracle.truffle.api.profiles.ConditionProfile;
69-
import java.util.ArrayList;
70-
import java.util.List;
7170

7271
public abstract class PNodeWithContext extends Node {
7372
@Child private PythonObjectFactory factory;
@@ -130,54 +129,31 @@ public final PException raiseIndexError() {
130129
}
131130

132131
public final PException raiseOSError(VirtualFrame frame, int errno) {
133-
return raiseOSError(frame, errno, null, null, null);
132+
return raiseOSError(frame, new Object[]{errno});
134133
}
135134

136135
public final PException raiseOSError(VirtualFrame frame, OSErrorEnum oserror) {
137-
return raiseOSError(frame, oserror.getNumber(), oserror.getMessage(), null, null);
136+
return raiseOSError(frame, new Object[]{oserror.getNumber(), oserror.getMessage()});
138137
}
139138

140139
public final PException raiseOSError(VirtualFrame frame, OSErrorEnum oserror, String filename) {
141-
return raiseOSError(frame, oserror.getNumber(), oserror.getMessage(), filename, null);
140+
Object[] args = new Object[]{oserror.getNumber(), oserror.getMessage(), filename};
141+
return raiseOSError(frame, args);
142142
}
143143

144144
public final PException raiseOSError(VirtualFrame frame, OSErrorEnum oserror, String filename, String filename2) {
145-
return raiseOSError(frame, oserror.getNumber(), oserror.getMessage(), filename, filename2);
145+
Object[] args = new Object[]{oserror.getNumber(), oserror.getMessage(), filename, PNone.NONE, filename2};
146+
return raiseOSError(frame, args);
146147
}
147148

148-
public final PException raiseOSError(VirtualFrame frame, int errno, String errorstr, String filename, String filename2) {
149-
if (getNewFuncNode == null) {
149+
public final PException raiseOSError(VirtualFrame frame, Object[] arg) {
150+
if (callNode == null) {
150151
CompilerDirectives.transferToInterpreterAndInvalidate();
151-
getNewFuncNode = insert(LookupAttributeInMRONode.create("__new__"));
152+
callNode = insert(CallVarargsMethodNode.create());
152153
}
153-
Object newFunc = getNewFuncNode.execute(PythonBuiltinClassType.OSError);
154-
if (newFunc != PNone.NO_VALUE) {
155-
if (callNode == null) {
156-
CompilerDirectives.transferToInterpreterAndInvalidate();
157-
callNode = insert(CallVarargsMethodNode.create());
158-
}
159-
Object[] args = createArgumentsForOSError(errno, errorstr, filename, filename2);
160-
PBaseException error = (PBaseException) callNode.execute(frame, newFunc, args, new PKeyword[]{});
161-
return raise(error);
162-
}
163-
return raise(factory().createBaseException(PythonBuiltinClassType.OSError));
164-
}
165-
166-
private Object[] createArgumentsForOSError(int errno, String errorstr, String filename, String filename2) {
167-
List<Object> result = new ArrayList<>();
168-
result.add(getBuiltinPythonClass(PythonBuiltinClassType.OSError));
169-
result.add(errno);
170-
if (errorstr != null && !errorstr.isEmpty()) {
171-
result.add(errorstr);
172-
}
173-
if (filename != null && !filename.isEmpty()) {
174-
result.add(filename);
175-
if (filename2 != null && !filename2.isEmpty()) {
176-
result.add(PNone.NONE); // instead winerror
177-
result.add(filename2);
178-
}
179-
}
180-
return result.toArray();
154+
Object[] args = PositionalArgumentsNode.prependArgument(getBuiltinPythonClass(PythonBuiltinClassType.OSError), arg, arg.length + 1);
155+
PBaseException error = (PBaseException) callNode.execute(frame, getBuiltinPythonClass(PythonBuiltinClassType.OSError), args, new PKeyword[]{});
156+
return raise(error);
181157
}
182158

183159
public final PythonClass getPythonClass(LazyPythonClass lazyClass, ConditionProfile profile) {

0 commit comments

Comments
 (0)