|
59 | 59 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
60 | 60 | import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
|
61 | 61 | import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
|
| 62 | +import com.oracle.graal.python.nodes.util.CannotCastException; |
62 | 63 | import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
|
63 | 64 | import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
|
64 | 65 | import com.oracle.graal.python.runtime.PythonContext;
|
@@ -123,30 +124,27 @@ protected Source doString(String pattern, String flags) {
|
123 | 124 |
|
124 | 125 | @Specialization(limit = "3")
|
125 | 126 | protected Source doGeneric(VirtualFrame frame, Object pattern, String flags,
|
126 |
| - @CachedLibrary("pattern") InteropLibrary interopLib, |
| 127 | + @Cached CastToJavaStringNode cast, |
127 | 128 | @CachedLibrary("pattern") PythonBufferAcquireLibrary bufferAcquireLib,
|
128 | 129 | @CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib) {
|
129 |
| - if (interopLib.isString(pattern)) { |
| 130 | + try { |
| 131 | + return doString(cast.execute(pattern), flags); |
| 132 | + } catch (CannotCastException ce) { |
| 133 | + Object buffer; |
130 | 134 | try {
|
131 |
| - return doString(interopLib.asString(pattern), flags); |
132 |
| - } catch (UnsupportedMessageException e) { |
133 |
| - throw CompilerDirectives.shouldNotReachHere(); |
| 135 | + buffer = bufferAcquireLib.acquireReadonly(pattern, frame, this); |
| 136 | + } catch (PException e) { |
| 137 | + throw raise(TypeError, "expected string or bytes-like object"); |
| 138 | + } |
| 139 | + try { |
| 140 | + String options = "Flavor=PythonBytes,Encoding=BYTES"; |
| 141 | + byte[] bytes = bufferLib.getInternalOrCopiedByteArray(buffer); |
| 142 | + int bytesLen = bufferLib.getBufferLength(buffer); |
| 143 | + String patternStr = decodeLatin1(bytes, bytesLen); |
| 144 | + return constructRegexSource(options, patternStr, flags); |
| 145 | + } finally { |
| 146 | + bufferLib.release(buffer, frame, this); |
134 | 147 | }
|
135 |
| - } |
136 |
| - Object buffer; |
137 |
| - try { |
138 |
| - buffer = bufferAcquireLib.acquireReadonly(pattern, frame, this); |
139 |
| - } catch (PException e) { |
140 |
| - throw raise(TypeError, "expected string or bytes-like object"); |
141 |
| - } |
142 |
| - try { |
143 |
| - String options = "Flavor=PythonBytes,Encoding=BYTES"; |
144 |
| - byte[] bytes = bufferLib.getInternalOrCopiedByteArray(buffer); |
145 |
| - int bytesLen = bufferLib.getBufferLength(buffer); |
146 |
| - String patternStr = decodeLatin1(bytes, bytesLen); |
147 |
| - return constructRegexSource(options, patternStr, flags); |
148 |
| - } finally { |
149 |
| - bufferLib.release(buffer, frame, this); |
150 | 148 | }
|
151 | 149 | }
|
152 | 150 | }
|
@@ -212,13 +210,21 @@ abstract static class TRegexCallExec extends PythonTernaryBuiltinNode {
|
212 | 210 |
|
213 | 211 | @Specialization(limit = "1")
|
214 | 212 | Object call(VirtualFrame frame, Object callable, Object inputStringOrBytes, Number fromIndex,
|
| 213 | + @Cached CastToJavaStringNode cast, |
215 | 214 | @Cached BranchProfile typeError,
|
216 | 215 | @CachedLibrary("callable") InteropLibrary interop) {
|
217 | 216 | PythonContext context = getContext();
|
218 | 217 | PythonLanguage language = getLanguage();
|
| 218 | + Object input = inputStringOrBytes; |
| 219 | + try { |
| 220 | + // This would materialize the string if it was native |
| 221 | + input = cast.execute(inputStringOrBytes); |
| 222 | + } catch (CannotCastException e) { |
| 223 | + // It's bytes or other buffer object |
| 224 | + } |
219 | 225 | Object state = IndirectCallContext.enter(frame, language, context, this);
|
220 | 226 | try {
|
221 |
| - return interop.execute(callable, inputStringOrBytes, fromIndex); |
| 227 | + return interop.execute(callable, input, fromIndex); |
222 | 228 | } catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
|
223 | 229 | typeError.enter();
|
224 | 230 | throw raise(TypeError, "%m", e);
|
|
0 commit comments