Skip to content

Commit e1b3212

Browse files
committed
Fix array.fromstring on strings
1 parent 9ec1b60 commit e1b3212

File tree

2 files changed

+23
-8
lines changed
  • graalpython

2 files changed

+23
-8
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_array.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
*graalpython.lib-python.3.test.test_array.DoubleTest.test_tofrombytes
109109
*graalpython.lib-python.3.test.test_array.DoubleTest.test_tofromfile
110110
*graalpython.lib-python.3.test.test_array.DoubleTest.test_tofromlist
111+
*graalpython.lib-python.3.test.test_array.DoubleTest.test_tofromstring
111112
*graalpython.lib-python.3.test.test_array.DoubleTest.test_weakref
112113
*graalpython.lib-python.3.test.test_array.FloatTest.test_add
113114
*graalpython.lib-python.3.test.test_array.FloatTest.test_assignment
@@ -162,6 +163,7 @@
162163
*graalpython.lib-python.3.test.test_array.FloatTest.test_tofrombytes
163164
*graalpython.lib-python.3.test.test_array.FloatTest.test_tofromfile
164165
*graalpython.lib-python.3.test.test_array.FloatTest.test_tofromlist
166+
*graalpython.lib-python.3.test.test_array.FloatTest.test_tofromstring
165167
*graalpython.lib-python.3.test.test_array.FloatTest.test_weakref
166168
*graalpython.lib-python.3.test.test_array.IntTest.test_add
167169
*graalpython.lib-python.3.test.test_array.IntTest.test_assignment
@@ -217,6 +219,7 @@
217219
*graalpython.lib-python.3.test.test_array.IntTest.test_tofrombytes
218220
*graalpython.lib-python.3.test.test_array.IntTest.test_tofromfile
219221
*graalpython.lib-python.3.test.test_array.IntTest.test_tofromlist
222+
*graalpython.lib-python.3.test.test_array.IntTest.test_tofromstring
220223
*graalpython.lib-python.3.test.test_array.IntTest.test_type_error
221224
*graalpython.lib-python.3.test.test_array.IntTest.test_weakref
222225
*graalpython.lib-python.3.test.test_array.LargeArrayTest.test_access
@@ -286,6 +289,7 @@
286289
*graalpython.lib-python.3.test.test_array.LongLongTest.test_tofrombytes
287290
*graalpython.lib-python.3.test.test_array.LongLongTest.test_tofromfile
288291
*graalpython.lib-python.3.test.test_array.LongLongTest.test_tofromlist
292+
*graalpython.lib-python.3.test.test_array.LongLongTest.test_tofromstring
289293
*graalpython.lib-python.3.test.test_array.LongLongTest.test_type_error
290294
*graalpython.lib-python.3.test.test_array.LongLongTest.test_weakref
291295
*graalpython.lib-python.3.test.test_array.LongTest.test_add
@@ -341,6 +345,7 @@
341345
*graalpython.lib-python.3.test.test_array.LongTest.test_tofrombytes
342346
*graalpython.lib-python.3.test.test_array.LongTest.test_tofromfile
343347
*graalpython.lib-python.3.test.test_array.LongTest.test_tofromlist
348+
*graalpython.lib-python.3.test.test_array.LongTest.test_tofromstring
344349
*graalpython.lib-python.3.test.test_array.LongTest.test_type_error
345350
*graalpython.lib-python.3.test.test_array.LongTest.test_weakref
346351
*graalpython.lib-python.3.test.test_array.MiscTest.test_bad_constructor
@@ -399,6 +404,7 @@
399404
*graalpython.lib-python.3.test.test_array.ShortTest.test_tofrombytes
400405
*graalpython.lib-python.3.test.test_array.ShortTest.test_tofromfile
401406
*graalpython.lib-python.3.test.test_array.ShortTest.test_tofromlist
407+
*graalpython.lib-python.3.test.test_array.ShortTest.test_tofromstring
402408
*graalpython.lib-python.3.test.test_array.ShortTest.test_type_error
403409
*graalpython.lib-python.3.test.test_array.ShortTest.test_weakref
404410
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_add
@@ -448,6 +454,7 @@
448454
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_tofrombytes
449455
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_tofromfile
450456
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_tofromlist
457+
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_tofromstring
451458
*graalpython.lib-python.3.test.test_array.UnicodeTest.test_weakref
452459
*graalpython.lib-python.3.test.test_array.UnsignedByteTest.test_add
453460
*graalpython.lib-python.3.test.test_array.UnsignedByteTest.test_assignment
@@ -562,6 +569,7 @@
562569
*graalpython.lib-python.3.test.test_array.UnsignedIntTest.test_tofrombytes
563570
*graalpython.lib-python.3.test.test_array.UnsignedIntTest.test_tofromfile
564571
*graalpython.lib-python.3.test.test_array.UnsignedIntTest.test_tofromlist
572+
*graalpython.lib-python.3.test.test_array.UnsignedIntTest.test_tofromstring
565573
*graalpython.lib-python.3.test.test_array.UnsignedIntTest.test_type_error
566574
*graalpython.lib-python.3.test.test_array.UnsignedIntTest.test_weakref
567575
*graalpython.lib-python.3.test.test_array.UnsignedLongLongTest.test_add
@@ -619,6 +627,7 @@
619627
*graalpython.lib-python.3.test.test_array.UnsignedLongLongTest.test_tofrombytes
620628
*graalpython.lib-python.3.test.test_array.UnsignedLongLongTest.test_tofromfile
621629
*graalpython.lib-python.3.test.test_array.UnsignedLongLongTest.test_tofromlist
630+
*graalpython.lib-python.3.test.test_array.UnsignedLongLongTest.test_tofromstring
622631
*graalpython.lib-python.3.test.test_array.UnsignedLongLongTest.test_type_error
623632
*graalpython.lib-python.3.test.test_array.UnsignedLongLongTest.test_weakref
624633
*graalpython.lib-python.3.test.test_array.UnsignedLongTest.test_add
@@ -676,6 +685,7 @@
676685
*graalpython.lib-python.3.test.test_array.UnsignedLongTest.test_tofrombytes
677686
*graalpython.lib-python.3.test.test_array.UnsignedLongTest.test_tofromfile
678687
*graalpython.lib-python.3.test.test_array.UnsignedLongTest.test_tofromlist
688+
*graalpython.lib-python.3.test.test_array.UnsignedLongTest.test_tofromstring
679689
*graalpython.lib-python.3.test.test_array.UnsignedLongTest.test_type_error
680690
*graalpython.lib-python.3.test.test_array.UnsignedLongTest.test_weakref
681691
*graalpython.lib-python.3.test.test_array.UnsignedShortTest.test_add
@@ -733,5 +743,6 @@
733743
*graalpython.lib-python.3.test.test_array.UnsignedShortTest.test_tofrombytes
734744
*graalpython.lib-python.3.test.test_array.UnsignedShortTest.test_tofromfile
735745
*graalpython.lib-python.3.test.test_array.UnsignedShortTest.test_tofromlist
746+
*graalpython.lib-python.3.test.test_array.UnsignedShortTest.test_tofromstring
736747
*graalpython.lib-python.3.test.test_array.UnsignedShortTest.test_type_error
737748
*graalpython.lib-python.3.test.test_array.UnsignedShortTest.test_weakref

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/array/ArrayBuiltins.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -854,19 +854,23 @@ Object error(Object self, Object arg) {
854854
@Builtin(name = "fromstring", minNumOfPositionalArgs = 2)
855855
@GenerateNodeFactory
856856
abstract static class FromStringNode extends PythonBinaryBuiltinNode {
857-
@Specialization(limit = "2")
857+
858+
@Specialization(guards = "isString(str)", limit = "2")
858859
static Object fromstring(VirtualFrame frame, PArray self, Object str,
859860
@CachedLibrary("str") PythonObjectLibrary lib,
860-
@Cached ConditionProfile bufferProfile,
861861
@Cached WarningsModuleBuiltins.WarnNode warnNode,
862-
@Cached FromUnicodeNode fromUnicodeNode,
863862
@Cached FromBytesNode fromBytesNode) {
864863
warnNode.warnEx(frame, DeprecationWarning, "fromstring() is deprecated. Use frombytes() instead.", 1);
865-
if (bufferProfile.profile(lib.isBuffer(str))) {
866-
return fromBytesNode.execute(frame, self, str);
867-
} else {
868-
return fromUnicodeNode.execute(frame, self, str);
869-
}
864+
Object bytes = lib.lookupAndCallRegularMethod(str, frame, "encode", "utf-8");
865+
return fromBytesNode.execute(frame, self, bytes);
866+
}
867+
868+
@Specialization(guards = "!isString(str)")
869+
static Object fromother(VirtualFrame frame, PArray self, Object str,
870+
@Cached WarningsModuleBuiltins.WarnNode warnNode,
871+
@Cached FromBytesNode fromBytesNode) {
872+
warnNode.warnEx(frame, DeprecationWarning, "fromstring() is deprecated. Use frombytes() instead.", 1);
873+
return fromBytesNode.execute(frame, self, str);
870874
}
871875
}
872876

0 commit comments

Comments
 (0)