40
40
*/
41
41
package com .oracle .graal .python .nodes ;
42
42
43
- import java .io .FileNotFoundException ;
44
- import java .io .IOException ;
45
- import java .nio .channels .ClosedChannelException ;
46
- import java .nio .channels .NonReadableChannelException ;
47
- import java .nio .channels .NonWritableChannelException ;
48
- import java .nio .file .AccessDeniedException ;
49
- import java .nio .file .DirectoryNotEmptyException ;
50
- import java .nio .file .FileAlreadyExistsException ;
51
- import java .nio .file .FileSystemException ;
52
- import java .nio .file .FileSystemLoopException ;
53
- import java .nio .file .NoSuchFileException ;
54
- import java .nio .file .NotDirectoryException ;
55
- import java .nio .file .NotLinkException ;
56
- import java .util .regex .Matcher ;
57
- import java .util .regex .Pattern ;
58
-
59
43
import com .oracle .graal .python .PythonLanguage ;
60
44
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
61
45
import com .oracle .graal .python .builtins .objects .PNone ;
62
46
import com .oracle .graal .python .builtins .objects .exception .OSErrorEnum ;
47
+ import com .oracle .graal .python .builtins .objects .exception .OSErrorEnum .ErrorAndMessagePair ;
63
48
import com .oracle .graal .python .builtins .objects .exception .PBaseException ;
64
49
import com .oracle .graal .python .builtins .objects .function .PKeyword ;
65
50
import com .oracle .graal .python .nodes .call .special .CallVarargsMethodNode ;
66
51
import com .oracle .graal .python .runtime .PythonContext ;
67
52
import com .oracle .graal .python .runtime .PythonCore ;
68
53
import com .oracle .graal .python .runtime .exception .PException ;
69
- import com .oracle .truffle .api .CompilerDirectives ;
70
54
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
71
55
import com .oracle .truffle .api .dsl .Cached ;
72
56
import com .oracle .truffle .api .dsl .CachedContext ;
81
65
@ ImportStatic (PGuards .class )
82
66
public abstract class PRaiseOSErrorNode extends Node {
83
67
84
- private static final Pattern ERRNO_PATTERN = Pattern .compile ("error=(\\ d+)" );
85
-
86
68
public abstract PException execute (Frame frame , Object [] arguments );
87
69
88
70
public final PException raiseOSError (Frame frame , OSErrorEnum oserror ) {
@@ -98,20 +80,6 @@ private static String getMessage(Exception e) {
98
80
return e .getMessage ();
99
81
}
100
82
101
- @ TruffleBoundary
102
- private static String getReason (FileSystemException e ) {
103
- return e .getReason ();
104
- }
105
-
106
- @ TruffleBoundary
107
- private static OSErrorEnum tryFindErrnoFromMessage (Exception e ) {
108
- Matcher m = ERRNO_PATTERN .matcher (e .getMessage ());
109
- if (m .find ()) {
110
- return OSErrorEnum .fromNumber (Integer .parseInt (m .group (1 )));
111
- }
112
- return null ;
113
- }
114
-
115
83
public final PException raiseOSError (Frame frame , OSErrorEnum oserror , String filename ) {
116
84
return execute (frame , new Object []{oserror .getNumber (), oserror .getMessage (), filename });
117
85
}
@@ -129,57 +97,8 @@ public final PException raiseOSError(Frame frame, Exception e, String filename)
129
97
}
130
98
131
99
public final PException raiseOSError (Frame frame , Exception e , String filename , String filename2 ) {
132
- OSErrorEnum oserror ;
133
- String message = null ;
134
- if (e instanceof IOException ) {
135
- if (e instanceof NoSuchFileException || e instanceof FileNotFoundException ) {
136
- oserror = OSErrorEnum .ENOENT ;
137
- } else if (e instanceof AccessDeniedException ) {
138
- oserror = OSErrorEnum .EACCES ;
139
- } else if (e instanceof FileAlreadyExistsException ) {
140
- oserror = OSErrorEnum .EEXIST ;
141
- } else if (e instanceof NotDirectoryException ) {
142
- oserror = OSErrorEnum .ENOTDIR ;
143
- } else if (e instanceof DirectoryNotEmptyException ) {
144
- oserror = OSErrorEnum .ENOTEMPTY ;
145
- } else if (e instanceof FileSystemLoopException ) {
146
- oserror = OSErrorEnum .ELOOP ;
147
- } else if (e instanceof NotLinkException ) {
148
- oserror = OSErrorEnum .EINVAL ;
149
- } else if (e instanceof ClosedChannelException ) {
150
- oserror = OSErrorEnum .EPIPE ;
151
- } else if (e instanceof FileSystemException ) {
152
- String reason = getReason ((FileSystemException ) e );
153
- oserror = OSErrorEnum .fromMessage (reason );
154
- if (oserror == null ) {
155
- oserror = OSErrorEnum .EIO ;
156
- message = reason ;
157
- }
158
- } else { // Generic IOException
159
- oserror = tryFindErrnoFromMessage (e );
160
- if (oserror == null ) {
161
- oserror = OSErrorEnum .EIO ;
162
- message = getMessage (e );
163
- }
164
- }
165
- } else if (e instanceof SecurityException ) {
166
- oserror = OSErrorEnum .EPERM ;
167
- } else if (e instanceof IllegalArgumentException ) {
168
- oserror = OSErrorEnum .EINVAL ;
169
- } else if (e instanceof UnsupportedOperationException ) {
170
- oserror = OSErrorEnum .EOPNOTSUPP ;
171
- } else if (e instanceof NonReadableChannelException || e instanceof NonWritableChannelException ) {
172
- oserror = OSErrorEnum .EBADF ;
173
- } else if (e instanceof RuntimeException ) {
174
- throw (RuntimeException ) e ;
175
- } else {
176
- CompilerDirectives .transferToInterpreterAndInvalidate ();
177
- throw new RuntimeException (getMessage (e ), e );
178
- }
179
- if (message == null ) {
180
- message = oserror .getMessage ();
181
- }
182
- return execute (frame , new Object []{oserror .getNumber (), message , (filename != null ) ? filename : PNone .NONE , PNone .NONE , (filename2 != null ) ? filename2 : PNone .NONE });
100
+ ErrorAndMessagePair errorAndMessage = OSErrorEnum .fromException (e );
101
+ return execute (frame , new Object []{errorAndMessage .oserror .getNumber (), errorAndMessage .message , (filename != null ) ? filename : PNone .NONE , PNone .NONE , (filename2 != null ) ? filename2 : PNone .NONE });
183
102
}
184
103
185
104
@ Specialization
0 commit comments