|
55 | 55 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.__STDOUT__;
|
56 | 56 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__SIZEOF__;
|
57 | 57 |
|
| 58 | +import java.io.BufferedReader; |
58 | 59 | import java.io.IOException;
|
59 | 60 | import java.nio.ByteOrder;
|
60 | 61 | import java.nio.charset.Charset;
|
| 62 | +import java.nio.charset.StandardCharsets; |
61 | 63 | import java.util.Arrays;
|
62 | 64 | import java.util.Date;
|
63 | 65 | import java.util.HashSet;
|
|
67 | 69 |
|
68 | 70 | import org.graalvm.nativeimage.ImageInfo;
|
69 | 71 |
|
| 72 | +import com.oracle.graal.python.PythonFileDetector; |
70 | 73 | import com.oracle.graal.python.PythonLanguage;
|
71 | 74 | import com.oracle.graal.python.annotations.ArgumentClinic;
|
72 | 75 | import com.oracle.graal.python.annotations.ArgumentClinic.ClinicConversion;
|
|
153 | 156 | import com.oracle.truffle.api.frame.VirtualFrame;
|
154 | 157 | import com.oracle.truffle.api.nodes.Node;
|
155 | 158 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
156 |
| -import com.oracle.truffle.api.source.Source; |
157 | 159 |
|
158 | 160 | @CoreFunctions(defineModule = "sys", isEager = true)
|
159 | 161 | public class SysModuleBuiltins extends PythonBuiltins {
|
@@ -1046,13 +1048,28 @@ private static String getIndent(int indent) {
|
1046 | 1048 | CharSequence getSourceLine(String fileName, int lineNo) {
|
1047 | 1049 | final PythonContext context = getContext();
|
1048 | 1050 | TruffleFile file = context.getEnv().getInternalTruffleFile(fileName);
|
1049 |
| - final Source source; |
| 1051 | + String line = null; |
1050 | 1052 | try {
|
1051 |
| - source = PythonLanguage.newSource(context, file, fileName); |
1052 |
| - return source.getCharacters(lineNo); |
| 1053 | + Charset encoding; |
| 1054 | + try { |
| 1055 | + encoding = PythonFileDetector.findEncodingStrict(file); |
| 1056 | + } catch (PythonFileDetector.InvalidEncodingException e) { |
| 1057 | + encoding = StandardCharsets.UTF_8; |
| 1058 | + } |
| 1059 | + BufferedReader reader = file.newBufferedReader(encoding); |
| 1060 | + int i = 1; |
| 1061 | + while (i <= lineNo) { |
| 1062 | + if (i == lineNo) { |
| 1063 | + line = reader.readLine(); |
| 1064 | + } else { |
| 1065 | + reader.readLine(); |
| 1066 | + } |
| 1067 | + i++; |
| 1068 | + } |
1053 | 1069 | } catch (IOException ioe) {
|
1054 |
| - return null; |
| 1070 | + line = null; |
1055 | 1071 | }
|
| 1072 | + return line; |
1056 | 1073 | }
|
1057 | 1074 |
|
1058 | 1075 | void displaySourceLine(VirtualFrame frame, Object out, String fileName, int lineNo, int indent) {
|
|
0 commit comments