Skip to content

Commit bc2f304

Browse files
committed
[GR-28698] Various smaller fixes and cleanups.
PullRequest: graalpython/1630
2 parents f05db68 + 923c682 commit bc2f304

36 files changed

+511
-808
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,9 @@ public abstract static class IntNode extends PythonTernaryBuiltinNode {
11331133
private static Object stringToIntInternal(String num, int base) {
11341134
try {
11351135
BigInteger bi = asciiToBigInteger(num, base);
1136+
if (bi == null) {
1137+
return null;
1138+
}
11361139
if (bi.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0 || bi.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) {
11371140
return bi;
11381141
} else {
@@ -1267,18 +1270,22 @@ private static BigInteger asciiToBigInteger(String str, int possibleBase) throws
12671270
base = 10;
12681271
}
12691272

1270-
int i = b;
1271-
while (i < e) {
1272-
if (str.charAt(i) == '_') {
1273+
// reject invalid characters without going to BigInteger
1274+
for (int i = b; i < e; i++) {
1275+
char c = str.charAt(i);
1276+
if (c == '_') {
12731277
if (!acceptUnderscore || i == e - 1) {
12741278
throw new NumberFormatException("Illegal underscore in int literal");
12751279
} else {
12761280
acceptUnderscore = false;
12771281
}
12781282
} else {
12791283
acceptUnderscore = true;
1284+
if (Character.digit(c, base) == -1) {
1285+
// invalid char
1286+
return null;
1287+
}
12801288
}
1281-
++i;
12821289
}
12831290

12841291
String s = str;
@@ -2273,7 +2280,6 @@ Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict
22732280
@Cached WriteAttributeToObjectNode writeItemSize,
22742281
@Cached GetBestBaseClassNode getBestBaseNode,
22752282
@Cached IsIdentifierNode isIdentifier,
2276-
@Cached HashingCollectionNodes.SetDictStorageNode setStorage,
22772283
@Cached HashingStorage.InitNode initNode) {
22782284
// Determine the proper metatype to deal with this
22792285
String name = castStr.execute(wName);
@@ -2290,7 +2296,7 @@ Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict
22902296

22912297
try {
22922298
PDict namespace = factory().createDict();
2293-
setStorage.execute(namespace, initNode.execute(frame, namespaceOrig, PKeyword.EMPTY_KEYWORDS));
2299+
namespace.setDictStorage(initNode.execute(frame, namespaceOrig, PKeyword.EMPTY_KEYWORDS));
22942300
PythonClass newType = typeMetaclass(frame, name, bases, namespace, metaclass, lib, hashingStoragelib, getDictAttrNode, getWeakRefAttrNode, getBestBaseNode, getItemSize, writeItemSize,
22952301
isIdentifier);
22962302

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

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import com.oracle.graal.python.builtins.objects.code.CodeNodes.CreateCodeNode;
5252
import com.oracle.graal.python.builtins.objects.code.PCode;
5353
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage;
54-
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes.GetDictStorageNode;
5554
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
5655
import com.oracle.graal.python.builtins.objects.common.HashingStorage.DictEntry;
5756
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
@@ -60,7 +59,6 @@
6059
import com.oracle.graal.python.builtins.objects.complex.PComplex;
6160
import com.oracle.graal.python.builtins.objects.dict.PDict;
6261
import com.oracle.graal.python.builtins.objects.floats.PFloat;
63-
import com.oracle.graal.python.builtins.objects.function.PArguments;
6462
import com.oracle.graal.python.builtins.objects.ints.PInt;
6563
import com.oracle.graal.python.builtins.objects.list.PList;
6664
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
@@ -93,7 +91,6 @@
9391
import com.oracle.truffle.api.frame.VirtualFrame;
9492
import com.oracle.truffle.api.interop.UnsupportedMessageException;
9593
import com.oracle.truffle.api.library.CachedLibrary;
96-
import com.oracle.truffle.api.profiles.ConditionProfile;
9794
import com.oracle.truffle.api.source.SourceSection;
9895

9996
@CoreFunctions(defineModule = "marshal")
@@ -401,12 +398,10 @@ void handlePList(VirtualFrame frame, PList l, int version, DataOutputStream buff
401398

402399
@Specialization(limit = "1")
403400
void handlePDict(VirtualFrame frame, PDict d, int version, DataOutputStream buffer,
404-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
405-
@Cached GetDictStorageNode getStore,
406-
@CachedLibrary("getStore.execute(d)") HashingStorageLibrary lib) {
401+
@CachedLibrary("d.getDictStorage()") HashingStorageLibrary lib) {
407402
writeByte(TYPE_DICT, version, buffer);
408-
HashingStorage dictStorage = getStore.execute(d);
409-
int len = lib.lengthWithFrame(dictStorage, hasFrame, frame);
403+
HashingStorage dictStorage = d.getDictStorage();
404+
int len = lib.length(dictStorage);
410405
writeInt(len, version, buffer);
411406
for (DictEntry entry : lib.entries(dictStorage)) {
412407
getRecursiveNode().execute(frame, entry.key, version, buffer);
@@ -415,7 +410,7 @@ void handlePDict(VirtualFrame frame, PDict d, int version, DataOutputStream buff
415410
}
416411

417412
@Specialization
418-
void handlePCode(@SuppressWarnings("unused") VirtualFrame frame, PCode c, int version, DataOutputStream buffer) {
413+
void handlePCode(PCode c, int version, DataOutputStream buffer) {
419414
writeByte(TYPE_CODE, version, buffer);
420415
writeString(getSourceCode(c), version, buffer);
421416
writeString(c.getFilename(), version, buffer);
@@ -433,17 +428,10 @@ private static String getSourceCode(PCode c) {
433428

434429
@Specialization(limit = "1")
435430
void handlePSet(VirtualFrame frame, PSet s, int version, DataOutputStream buffer,
436-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
437-
@Cached GetDictStorageNode getStore,
438-
@CachedLibrary("getStore.execute(s)") HashingStorageLibrary lib) {
431+
@CachedLibrary("s.getDictStorage()") HashingStorageLibrary lib) {
439432
writeByte(TYPE_SET, version, buffer);
440-
int len;
441-
HashingStorage dictStorage = getStore.execute(s);
442-
if (hasFrame.profile(frame != null)) {
443-
len = lib.lengthWithState(dictStorage, PArguments.getThreadState(frame));
444-
} else {
445-
len = lib.length(dictStorage);
446-
}
433+
HashingStorage dictStorage = s.getDictStorage();
434+
int len = lib.length(dictStorage);
447435
writeInt(len, version, buffer);
448436
for (DictEntry entry : lib.entries(dictStorage)) {
449437
getRecursiveNode().execute(frame, entry.key, version, buffer);
@@ -452,17 +440,10 @@ void handlePSet(VirtualFrame frame, PSet s, int version, DataOutputStream buffer
452440

453441
@Specialization(limit = "1")
454442
void handlePForzenSet(VirtualFrame frame, PFrozenSet s, int version, DataOutputStream buffer,
455-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
456-
@Cached GetDictStorageNode getStore,
457-
@CachedLibrary("getStore.execute(s)") HashingStorageLibrary lib) {
443+
@CachedLibrary("s.getDictStorage()") HashingStorageLibrary lib) {
458444
writeByte(TYPE_FROZENSET, version, buffer);
459-
int len;
460-
HashingStorage dictStorage = getStore.execute(s);
461-
if (hasFrame.profile(frame != null)) {
462-
len = lib.lengthWithState(dictStorage, PArguments.getThreadState(frame));
463-
} else {
464-
len = lib.length(dictStorage);
465-
}
445+
HashingStorage dictStorage = s.getDictStorage();
446+
int len = lib.length(dictStorage);
466447
writeInt(len, version, buffer);
467448
for (DictEntry entry : lib.entries(dictStorage)) {
468449
getRecursiveNode().execute(frame, entry.key, version, buffer);

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
import com.oracle.graal.python.builtins.objects.cext.capi.PyDateTimeMRNode.DateTimeMode;
103103
import com.oracle.graal.python.builtins.objects.cext.capi.UnicodeObjectNodes.UnicodeAsWideCharNode;
104104
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage;
105-
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
106105
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
107106
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
108107
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
@@ -976,9 +975,8 @@ static Object doDQualname(PythonObject object, @SuppressWarnings("unused") Pytho
976975

977976
@Specialization(guards = "eq(SET_USED, key)", limit = "1")
978977
static long doSetUsed(PSet object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
979-
@Cached HashingCollectionNodes.GetDictStorageNode getStorageNode,
980-
@CachedLibrary("getStorageNode.execute(object)") HashingStorageLibrary lib) {
981-
return lib.length(getStorageNode.execute(object));
978+
@CachedLibrary("object.getDictStorage()") HashingStorageLibrary lib) {
979+
return lib.length(object.getDictStorage());
982980
}
983981

984982
@Specialization(guards = "eq(MMAP_DATA, key)")
@@ -1208,11 +1206,10 @@ static EachSubclassAdd getUncached() {
12081206
static void doTpSubclasses(PythonClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key, PythonObjectNativeWrapper value,
12091207
@Cached GetSubclassesNode getSubclassesNode,
12101208
@Cached EachSubclassAdd eachNode,
1211-
@Cached HashingCollectionNodes.GetDictStorageNode getStorage,
12121209
@CachedLibrary("value") PythonNativeWrapperLibrary lib,
12131210
@CachedLibrary(limit = "1") HashingStorageLibrary hashLib) {
12141211
PDict dict = (PDict) lib.getDelegate(value);
1215-
HashingStorage storage = getStorage.execute(dict);
1212+
HashingStorage storage = dict.getDictStorage();
12161213
Set<PythonAbstractClass> subclasses = getSubclassesNode.execute(object);
12171214
hashLib.forEach(storage, eachNode, new SubclassAddState(storage, hashLib, subclasses));
12181215
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/common/CExtParseArgumentsNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,6 @@ static Object doGeneric(ParserState state, Object kwds, Object kwdnames, boolean
10441044
@Shared("lenNode") @Cached SequenceNodes.LenNode lenNode,
10451045
@Shared("getSequenceStorageNode") @Cached GetSequenceStorageNode getSequenceStorageNode,
10461046
@Shared("getItemNode") @Cached SequenceStorageNodes.GetItemDynamicNode getItemNode,
1047-
@Cached HashingCollectionNodes.GetDictStorageNode getDictStorageNode,
10481047
@CachedLibrary(limit = "1") InteropLibrary kwdnamesLib,
10491048
@CachedLibrary(limit = "1") HashingStorageLibrary lib,
10501049
@Cached PCallCExtFunction callCStringToString,
@@ -1066,7 +1065,7 @@ static Object doGeneric(ParserState state, Object kwds, Object kwdnames, boolean
10661065
if (kwdname instanceof String) {
10671066
// the cast to PDict is safe because either it is null or a PDict (ensured by
10681067
// the guards)
1069-
out = lib.getItem(getDictStorageNode.execute((PDict) kwds), kwdname);
1068+
out = lib.getItem(((PDict) kwds).getDictStorage(), kwdname);
10701069
}
10711070
}
10721071
if (out == null && !state.restOptional) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/DynamicObjectStorage.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,7 @@ static HashingStorage generalize(DynamicObjectStorage self, Object key, Object v
326326
// To avoid calling the costly length message we use SIZE_THRESHOLD
327327
newStore = new HashMapStorage(SIZE_THRESHOLD);
328328
} else {
329-
int len;
330-
if (gotState.profile(state != null)) {
331-
len = lib.lengthWithState(self, state);
332-
} else {
333-
len = lib.length(self);
334-
}
335-
newStore = EconomicMapStorage.create(len);
329+
newStore = EconomicMapStorage.create(lib.length(self));
336330
}
337331

338332
newStore = lib.addAllToOther(self, newStore);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/EconomicMapStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ static boolean equalGeneric(EconomicMapStorage self, HashingStorage other, Threa
445445
@CachedLibrary(limit = "2") HashingStorageLibrary otherlib,
446446
@CachedLibrary(limit = "2") PythonObjectLibrary compareLib1,
447447
@CachedLibrary(limit = "2") PythonObjectLibrary compareLib2) {
448-
if (self.map.size() != otherlib.lengthWithState(other, state)) {
448+
if (self.map.size() != otherlib.length(other)) {
449449
return false;
450450
}
451451
MapCursor<DictKey, Object> cursor = self.map.getEntries();

0 commit comments

Comments
 (0)