Skip to content

Commit e5c2eb1

Browse files
committed
Avoid using true/false singletons in marshal module
1 parent 3aae83d commit e5c2eb1

1 file changed

Lines changed: 7 additions & 15 deletions

File tree

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public final Throwable fillInStackTrace() {
393393

394394
@TruffleBoundary
395395
static byte[] dump(PythonContext context, Object value, int version) throws IOException, MarshalError {
396-
Marshal outMarshal = new Marshal(context, version, context.getTrue(), context.getFalse());
396+
Marshal outMarshal = new Marshal(context, version);
397397
outMarshal.writeObject(value);
398398
return outMarshal.outData.toByteArray();
399399
}
@@ -471,8 +471,6 @@ public int read(byte[] b, int off, int len) {
471471
final DataOutput out;
472472
final DataInput in;
473473
final int version;
474-
final PInt pyTrue;
475-
final PInt pyFalse;
476474
int depth = 0;
477475
long cacheKey;
478476
TruffleFile bytecodeFile;
@@ -487,23 +485,19 @@ public int read(byte[] b, int off, int len) {
487485
*/
488486
Source source = null;
489487

490-
Marshal(PythonContext context, int version, PInt pyTrue, PInt pyFalse) {
488+
Marshal(PythonContext context, int version) {
491489
this.context = context;
492490
this.version = version;
493-
this.pyTrue = pyTrue;
494-
this.pyFalse = pyFalse;
495491
this.outData = new ByteArrayOutputStream();
496492
this.out = new DataOutputStream(outData);
497493
this.refMap = new HashMap<>();
498494
this.in = null;
499495
this.refList = null;
500496
}
501497

502-
Marshal(PythonContext context, int version, PInt pyTrue, PInt pyFalse, DataOutput out) {
498+
Marshal(PythonContext context, int version, DataOutput out) {
503499
this.context = context;
504500
this.version = version;
505-
this.pyTrue = pyTrue;
506-
this.pyFalse = pyFalse;
507501
this.outData = null;
508502
this.out = out;
509503
this.refMap = new HashMap<>();
@@ -531,8 +525,6 @@ public int read(byte[] b, int off, int len) {
531525
this.source = source;
532526
this.refList = new ArrayList<>();
533527
this.version = -1;
534-
this.pyTrue = null;
535-
this.pyFalse = null;
536528
this.outData = null;
537529
this.out = null;
538530
this.refMap = null;
@@ -782,9 +774,9 @@ private void writeObject(Object v) throws IOException {
782774
writeByte(TYPE_STOPITER);
783775
} else if (v == PEllipsis.INSTANCE) {
784776
writeByte(TYPE_ELLIPSIS);
785-
} else if (v == Boolean.TRUE || v == pyTrue) {
777+
} else if (v == Boolean.TRUE || v instanceof PInt i && i.getPythonClass() == PythonBuiltinClassType.Boolean && i.isOne()) {
786778
writeByte(TYPE_TRUE);
787-
} else if (v == Boolean.FALSE || v == pyFalse) {
779+
} else if (v == Boolean.FALSE || v instanceof PInt i && i.getPythonClass() == PythonBuiltinClassType.Boolean && i.isZero()) {
788780
writeByte(TYPE_FALSE);
789781
} else if (v instanceof Integer) {
790782
writeByte(TYPE_INT);
@@ -1574,7 +1566,7 @@ private PCode readCode() {
15741566
@TruffleBoundary
15751567
public static byte[] serializeCodeUnit(Node locationForRaise, PythonContext context, CodeUnit code) {
15761568
try {
1577-
Marshal marshal = new Marshal(context, CURRENT_VERSION, null, null);
1569+
Marshal marshal = new Marshal(context, CURRENT_VERSION);
15781570
marshal.writeCodeUnit(code);
15791571
return marshal.outData.toByteArray();
15801572
} catch (IOException e) {
@@ -1694,7 +1686,7 @@ public void serialize(SerializerContext context, DataOutput buffer, Object objec
16941686
* we must also do the same here. Otherwise, the encoding may be different (e.g., a
16951687
* reference for an already-emitted object).
16961688
*/
1697-
new Marshal(pythonContext, CURRENT_VERSION, pythonContext.getTrue(), pythonContext.getFalse(), buffer).writeObject(object);
1689+
new Marshal(pythonContext, CURRENT_VERSION, buffer).writeObject(object);
16981690
}
16991691
}
17001692

0 commit comments

Comments
 (0)