Skip to content

Commit 396a221

Browse files
committed
Better refactoring
1 parent 782af65 commit 396a221

File tree

7 files changed

+25
-32
lines changed

7 files changed

+25
-32
lines changed

graalpython/com.oracle.graal.python.benchmarks/java/com/oracle/graal/python/benchmarks/parser/Deserializing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private List<Item> createSerializations() {
8989
PythonSSTNodeFactory sstFactory = new PythonSSTNodeFactory(core, source, parser);
9090
PythonParserImpl.CacheItem item = parser.parseWithANTLR(PythonParser.ParserMode.File, 0, core, sstFactory, source, null, null);
9191
SourceSection section = source.createSection(0, source.getLength());
92-
result.add(new Item(PythonParserImpl.serialize(core, section, item.getAntlrResult(), item.getGlobalScope(), true)));
92+
result.add(new Item(PythonParserImpl.serialize(section, item.getAntlrResult(), item.getGlobalScope(), true)));
9393
} catch (RuntimeException e) {
9494
// do nothing
9595
}

graalpython/com.oracle.graal.python.benchmarks/java/com/oracle/graal/python/benchmarks/parser/Serializing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void execute(Blackhole bh) {
6565
for (int n = 0; n < parsingCycles; n++) {
6666
for (PythonParserImpl.CacheItem item : ssts) {
6767
SourceSection section = item.getSource().createSection(0, item.getSource().getLength());
68-
bh.consume(PythonParserImpl.serialize(core, section, item.getAntlrResult(), item.getGlobalScope(), true));
68+
bh.consume(PythonParserImpl.serialize(section, item.getAntlrResult(), item.getGlobalScope(), true));
6969
}
7070
}
7171
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/StringUtilsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
public class StringUtilsTests extends ParserTestBase {
5656
private static final ParserErrorCallback errorCallback = new ParserErrorCallback() {
57-
57+
5858
@Override
5959
public RuntimeException raiseInvalidSyntax(ErrorType type, Source source, SourceSection section, String message, Object... arguments) {
6060
Assert.fail("Unexpected error: " + String.format(message, arguments));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/WarningsModuleBuiltins.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,11 @@ private static void showWarning(Object filename, int lineno, Object text, Object
590590
}
591591

592592
@TruffleBoundary
593-
private static void callShowWarning(Object category, Object text, Object message,
593+
private static void callShowWarning(PythonContext context, Object category, Object text, Object message,
594594
Object filename, int lineno, Object sourceline, Object sourceIn) {
595595
PythonObjectLibrary polib = PythonObjectLibrary.getUncached();
596596
PRaiseNode raise = PRaiseNode.getUncached();
597597

598-
PythonContext context = PythonContext.get(raise);
599598
Object showFn = getWarningsAttr(context, "_showwarnmsg", sourceIn != null);
600599
if (showFn == null) {
601600
showWarning(filename, lineno, text, category, sourceline);
@@ -677,14 +676,15 @@ private void warnExplicit(VirtualFrame frame, PythonModule warnings,
677676
// about performance when warnings are enabled.
678677
Object state = IndirectCallContext.enter(frame, getLanguage(), getContext(), this);
679678
try {
680-
warnExplicitPart2(this, warnings, filename, lineno, registry, globals, source, category, message, text, key, item, action);
679+
warnExplicitPart2(PythonContext.get(this), this, warnings, filename, lineno, registry, globals, source, category, message, text, key, item, action);
681680
} finally {
682681
IndirectCallContext.exit(frame, getLanguage(), getContext(), state);
683682
}
684683
}
685684

686685
@TruffleBoundary
687-
private static void warnExplicitPart2(Node node, PythonModule warnings, Object filename, int lineno, Object registry, PDict globals, Object source, Object category, Object message,
686+
private static void warnExplicitPart2(PythonContext context, Node node, PythonModule warnings, Object filename, int lineno, Object registry, PDict globals, Object source, Object category,
687+
Object message,
688688
Object text,
689689
Object key, Object[] item, String action) {
690690
PythonObjectLibrary polib = PythonObjectLibrary.getUncached();
@@ -706,7 +706,7 @@ private static void warnExplicitPart2(Node node, PythonModule warnings, Object f
706706
boolean alreadyWarned = false;
707707
if (PString.equals("once", action)) {
708708
if (registry == null || registry == PNone.NONE) {
709-
Object currentRegistry = getOnceRegistry(PythonContext.get(node), warnings);
709+
Object currentRegistry = getOnceRegistry(context, warnings);
710710
alreadyWarned = updateRegistry(warnings, currentRegistry, text, category, false);
711711
} else {
712712
alreadyWarned = updateRegistry(warnings, registry, text, category, false);
@@ -732,7 +732,7 @@ private static void warnExplicitPart2(Node node, PythonModule warnings, Object f
732732
sourceline = getSourceLine(globals, lineno);
733733
}
734734

735-
callShowWarning(category, text, message, filename, lineno, sourceline, source);
735+
callShowWarning(context, category, text, message, filename, lineno, sourceline, source);
736736
}
737737

738738
/**

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PyLongDigitsWrapper.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import com.oracle.truffle.api.library.CachedLibrary;
6363
import com.oracle.truffle.api.library.ExportLibrary;
6464
import com.oracle.truffle.api.library.ExportMessage;
65-
import com.oracle.truffle.api.nodes.Node;
6665
import com.oracle.truffle.llvm.spi.NativeTypeLibrary;
6766

6867
/**
@@ -256,10 +255,9 @@ static Object callGetUInt32ArrayTypeIDUncached(@SuppressWarnings("unused") PyLon
256255
return PCallCapiFunction.getUncached().call(FUN_GET_UINT32_ARRAY_TYPE_ID, 0);
257256
}
258257

259-
@Specialization(assumptions = "singleContextAssumption(lib)")
258+
@Specialization(assumptions = "singleContextAssumption()")
260259
static Object doByteArray(@SuppressWarnings("unused") PyLongDigitsWrapper object,
261-
@Exclusive @Cached("callGetUInt32ArrayTypeIDUncached(object)") Object nativeType,
262-
@SuppressWarnings("unused") @CachedLibrary(limit = "1") InteropLibrary lib) {
260+
@Exclusive @Cached("callGetUInt32ArrayTypeIDUncached(object)") Object nativeType) {
263261
return nativeType;
264262
}
265263

@@ -269,8 +267,8 @@ static Object doByteArrayMultiCtx(@SuppressWarnings("unused") PyLongDigitsWrappe
269267
return callGetTypeIDNode.call(FUN_GET_UINT32_ARRAY_TYPE_ID, 0);
270268
}
271269

272-
protected static Assumption singleContextAssumption(Node node) {
273-
return PythonLanguage.get(node).singleContextAssumption;
270+
protected static Assumption singleContextAssumption() {
271+
return PythonLanguage.get(null).singleContextAssumption;
274272
}
275273
}
276274
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public SSTNode parseExpression(ParserErrorCallback errorCallback, String text, P
116116
null).antlrResult;
117117
}
118118

119-
public static byte[] serialize(ParserErrorCallback errorCallback, SourceSection section, SSTNode node, ScopeInfo scope, boolean isModule) {
119+
public static byte[] serialize(SourceSection section, SSTNode node, ScopeInfo scope, boolean isModule) {
120120
Source source = section.getSource();
121121
ByteArrayOutputStream baos = new ByteArrayOutputStream();
122122
DataOutputStream dos = new DataOutputStream(baos);
@@ -167,13 +167,13 @@ public byte[] serialize(ParserErrorCallback errorCallback, RootNode rootNode) {
167167
}
168168
if (rootNode instanceof ModuleRootNode) {
169169
// serialize whole module
170-
return serialize(errorCallback, rootNode.getSourceSection(), lastParserResult.antlrResult, lastParserResult.globalScope, true);
170+
return serialize(rootNode.getSourceSection(), lastParserResult.antlrResult, lastParserResult.globalScope, true);
171171
} else {
172172
// serialize just the part
173173
SSTNodeWithScopeFinder finder = new SSTNodeWithScopeFinder(rootNode.getSourceSection().getCharIndex(), rootNode.getSourceSection().getCharEndIndex());
174174
SSTNodeWithScope rootSST = lastParserResult.antlrResult.accept(finder);
175175
// store with parent scope
176-
return serialize(errorCallback, rootNode.getSourceSection(), rootSST, rootSST.getScope().getParent(), false);
176+
return serialize(rootNode.getSourceSection(), rootSST, rootSST.getScope().getParent(), false);
177177
}
178178
}
179179

@@ -418,10 +418,6 @@ public Node parseN(ParserMode mode, int optimizeLevel, ParserErrorCallback error
418418
// this wrapper tracks the warnings in a list instead of raising them immediately
419419
ParserErrorCallback collectWarnings = new ParserErrorCallback() {
420420

421-
public RuntimeException raise(PythonBuiltinClassType type, String message, Object... args) {
422-
return PRaiseNode.raiseUncached(null, type, message, args);
423-
}
424-
425421
public RuntimeException raiseInvalidSyntax(ErrorType type, Source src, SourceSection section, String message, Object... arguments) {
426422
return errors.raiseInvalidSyntax(type, src, section, message, arguments);
427423
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/StringUtils.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.util.Map;
4747

4848
import com.ibm.icu.lang.UCharacter;
49-
import com.oracle.graal.python.builtins.Python3Core;
5049
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5150
import com.oracle.graal.python.nodes.PRaiseNode;
5251
import com.oracle.graal.python.nodes.control.BaseBlockNode;
@@ -156,13 +155,13 @@ public static String unescapeJavaString(ParserErrorCallback errorCallback, Strin
156155
continue;
157156
// Hex Unicode: u????
158157
case 'u':
159-
int code = getHexValue(errorCallback, st, i + 2, 4);
158+
int code = getHexValue(st, i + 2, 4);
160159
sb.append(Character.toChars(code));
161160
i += 5;
162161
continue;
163162
// Hex Unicode: U????????
164163
case 'U':
165-
code = getHexValue(errorCallback, st, i + 2, 8);
164+
code = getHexValue(st, i + 2, 8);
166165
if (Character.isValidCodePoint(code)) {
167166
sb.append(Character.toChars(code));
168167
} else {
@@ -172,13 +171,13 @@ public static String unescapeJavaString(ParserErrorCallback errorCallback, Strin
172171
continue;
173172
// Hex Unicode: x??
174173
case 'x':
175-
code = getHexValue(errorCallback, st, i + 2, 2);
174+
code = getHexValue(st, i + 2, 2);
176175
sb.append(Character.toChars(code));
177176
i += 3;
178177
continue;
179178
case 'N':
180179
// a character from Unicode Data Database
181-
i = doCharacterName(errorCallback.getContext().getCore(), st, sb, i + 2);
180+
i = doCharacterName(st, sb, i + 2);
182181
continue;
183182
default:
184183
if (!wasDeprecationWarning) {
@@ -220,7 +219,7 @@ public static void warnInvalidEscapeSequence(ParserErrorCallback errorCallback,
220219
private static final String UNKNOWN_UNICODE_ERROR = " unknown Unicode character name";
221220
private static final String ILLEGAl_CHARACTER = "illegal Unicode character";
222221

223-
private static int getHexValue(ParserErrorCallback errors, String text, int start, int len) {
222+
private static int getHexValue(String text, int start, int len) {
224223
int digit;
225224
int result = 0;
226225
for (int index = start; index < (start + len); index++) {
@@ -229,17 +228,17 @@ private static int getHexValue(ParserErrorCallback errors, String text, int star
229228
if (digit == -1) {
230229
// Like cpython, raise error with the wrong character first,
231230
// even if there are not enough characters
232-
throw createTruncatedError(errors, start - 2, index - 1, len);
231+
throw createTruncatedError(start - 2, index - 1, len);
233232
}
234233
result = result * 16 + digit;
235234
} else {
236-
throw createTruncatedError(errors, start - 2, index - 1, len);
235+
throw createTruncatedError(start - 2, index - 1, len);
237236
}
238237
}
239238
return result;
240239
}
241240

242-
private static PException createTruncatedError(ParserErrorCallback errors, int startIndex, int endIndex, int len) {
241+
private static PException createTruncatedError(int startIndex, int endIndex, int len) {
243242
String truncatedMessage = null;
244243
switch (len) {
245244
case 2:
@@ -264,7 +263,7 @@ private static PException createTruncatedError(ParserErrorCallback errors, int s
264263
* @return offset of the close brace
265264
*/
266265
@CompilerDirectives.TruffleBoundary
267-
private static int doCharacterName(Python3Core core, String text, StringBuilder sb, int offset) {
266+
private static int doCharacterName(String text, StringBuilder sb, int offset) {
268267
if (offset >= text.length()) {
269268
throw PRaiseNode.raiseUncached(null, PythonBuiltinClassType.UnicodeDecodeError, UNICODE_ERROR + MALFORMED_ERROR, offset - 2, offset - 1);
270269
}

0 commit comments

Comments
 (0)