Skip to content

Commit 5581e5d

Browse files
committed
[GR-12453] remove incorrect int.from_bytes with Strings
PullRequest: graalpython/340
2 parents 21cab58 + 335f61f commit 5581e5d

File tree

3 files changed

+29
-46
lines changed
  • graalpython

3 files changed

+29
-46
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_int.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -60,29 +60,29 @@ def test_int_subclassing():
6060
MAXREPEAT = _NamedIntConstant(1, 'MAXREPEAT')
6161
assert MAXREPEAT == 1
6262
assert str(MAXREPEAT) == "MAXREPEAT"
63-
63+
6464

6565
def test_boolean2int():
6666
assert int(True) == 1
6767
assert int(False) == 0
68-
69-
68+
69+
7070
def test_int_from_custom():
7171
class CustomInt4():
7272
def __int__(self):
7373
return 1
74-
74+
7575
class CustomInt8():
7676
def __int__(self):
7777
return 0xCAFEBABECAFED00D
78-
78+
7979
class SubInt(int):
8080
def __int__(self):
8181
return 0xBADF00D
82-
82+
8383
class NoInt():
8484
pass
85-
85+
8686
assert int(CustomInt4()) == 1
8787
assert int(CustomInt8()) == 0xCAFEBABECAFED00D
8888
assert CustomInt8() != 0xCAFEBABECAFED00D
@@ -124,7 +124,7 @@ def builtinTest(number):
124124
builtinTest(9)
125125
builtinTest(6227020800)
126126
builtinTest(9999992432902008176640000999999)
127-
127+
128128
assert True.__int__() == 1
129129
assert False.__int__() == 0
130130

@@ -161,13 +161,13 @@ def builtinTest(number):
161161
builtinTest(9)
162162
builtinTest(6227020800)
163163
builtinTest(9999992432902008176640000999999)
164-
164+
165165
assert True.real == 1
166166
assert False.real == 0
167167
assert True.imag == 0
168168
assert False.imag == 0
169169

170-
def test_real_imag_subclass():
170+
def test_real_imag_subclass():
171171
def subclassTest(number):
172172
a = MyInt(number)
173173
b = a.real
@@ -202,7 +202,7 @@ def builtinTest(number):
202202
builtinTest(9)
203203
builtinTest(6227020800)
204204
builtinTest(9999992432902008176640000999999)
205-
205+
206206
assert True.numerator == 1
207207
assert False.numerator == 0
208208
assert True.denominator == 1
@@ -240,7 +240,7 @@ def builtinTest(number):
240240
builtinTest(9)
241241
builtinTest(6227020800)
242242
builtinTest(9999992432902008176640000999999)
243-
243+
244244
assert True.conjugate() == 1
245245
assert False.conjugate() == 0
246246

@@ -282,7 +282,7 @@ def builtinTest(number):
282282
builtinTest(9)
283283
builtinTest(6227020800)
284284
builtinTest(9999992432902008176640000999999)
285-
285+
286286
assert True.__trunc__() == 1
287287
assert False.__trunc__() == 0
288288

@@ -325,7 +325,7 @@ def test_create_int_from_string():
325325

326326

327327
class FromBytesTests(unittest.TestCase):
328-
328+
329329
def check(self, tests, byteorder, signed=False):
330330
for test, expected in tests.items():
331331
try:
@@ -447,10 +447,10 @@ def test_from_list(self):
447447
class LyingList(list):
448448
def __iter__(self):
449449
return iter([10, 20, 30, 40])
450-
450+
451451
self.assertEqual(
452452
int.from_bytes(LyingList([255, 1, 1]), 'big'), 169090600)
453-
453+
454454
def test_from_tuple(self):
455455
self.assertEqual(
456456
int.from_bytes((255, 0, 0), 'big', signed=True), -65536)
@@ -464,7 +464,7 @@ def __iter__(self):
464464
return iter((15, 25, 35, 45))
465465
self.assertEqual(
466466
int.from_bytes(LyingTuple((255, 1, 1)), 'big'), 253305645)
467-
467+
468468
def test_from_bytearray(self):
469469
self.assertEqual(int.from_bytes(
470470
bytearray(b'\xff\x00\x00'), 'big', signed=True), -65536)
@@ -487,11 +487,10 @@ def test_wrong_input(self):
487487
self.assertRaises(TypeError, int.from_bytes, 0, 'big')
488488
self.assertRaises(TypeError, int.from_bytes, 0, 'big', True)
489489

490-
#TODO uncoment these tests, when GR-12453 is fixed
491-
#self.assertRaises(TypeError, int.from_bytes, "", 'big')
492-
#self.assertRaises(TypeError, int.from_bytes, "\x00", 'big')
493-
#self.assertRaises(TypeError, MyInt.from_bytes, "", 'big')
494-
#self.assertRaises(TypeError, MyInt.from_bytes, "\x00", 'big')
490+
self.assertRaises(TypeError, int.from_bytes, "", 'big')
491+
self.assertRaises(TypeError, int.from_bytes, "\x00", 'big')
492+
self.assertRaises(TypeError, MyInt.from_bytes, "", 'big')
493+
self.assertRaises(TypeError, MyInt.from_bytes, "\x00", 'big')
495494
self.assertRaises(TypeError, MyInt.from_bytes, 0, 'big')
496495
self.assertRaises(TypeError, int.from_bytes, 0, 'big', True)
497496

@@ -550,7 +549,7 @@ class mybyteslike2():
550549
def __bytes__(self):
551550
return array.array('b', [2, 2, 3])
552551

553-
self.assertRaises(TypeError, int.from_bytes, mybyteslike2(), 'big')
552+
self.assertRaises(TypeError, int.from_bytes, mybyteslike2(), 'big')
554553

555554
def test_from_list_with_byteslike(self):
556555
class StrangeList(list):
@@ -564,7 +563,7 @@ def __iter__(self):
564563
class ToBytesTests(unittest.TestCase):
565564

566565
class MyInt(int):
567-
pass
566+
pass
568567

569568
def check(self, tests, byteorder, signed=False):
570569
for test, expected in tests.items():
@@ -634,7 +633,7 @@ def test_SignedLittleEndian(self):
634633
}
635634
self.check(tests2, 'little', signed=True)
636635
self.checkPIntSpec(tests2, 'little', signed=True)
637-
636+
638637
def test_UnsignedBigEndian(self):
639638
# Convert integers to unsigned big-endian byte arrays.
640639
tests3 = {
@@ -706,7 +705,5 @@ def __int__(self):
706705
return 3
707706
def __index__(self):
708707
return 4
709-
710-
self.assertEqual(MyTest(1).to_bytes(MyTest(10), 'big'), b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01')
711-
712708

709+
self.assertEqual(MyTest(1).to_bytes(MyTest(10), 'big'), b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01')

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -1981,20 +1981,6 @@ private Object compute(PythonClass cl, byte[] bytes, String byteorder, boolean s
19811981
return createIntObject(cl, bi);
19821982
}
19831983

1984-
// from String / PString
1985-
// TODO these specialization shouldn't be there. The CPython ends up with TypeError in such
1986-
// case. See GR-12453
1987-
@Specialization
1988-
@TruffleBoundary
1989-
public Object fromString(PythonClass cl, String str, String byteorder, Object[] args, boolean signed) {
1990-
return compute(cl, str.getBytes(), byteorder, signed);
1991-
}
1992-
1993-
@Specialization
1994-
public Object fromString(PythonClass cl, String str, String byteorder, Object[] args, PNone keywordArg) {
1995-
return fromString(cl, str, byteorder, args, false);
1996-
}
1997-
19981984
// from PBytes
19991985
@Specialization
20001986
public Object fromPBytes(PythonClass cl, PBytes bytes, String byteorder, Object[] args, boolean signed) {

graalpython/lib-graalpython/_imp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -48,7 +48,7 @@ def extension_suffixes():
4848

4949
@__builtin__
5050
def get_magic():
51-
return '\x0c\xaf\xaf\xe1'
51+
return b'\x0c\xaf\xaf\xe1'
5252

5353

5454
@__builtin__

0 commit comments

Comments
 (0)