Skip to content

Commit aaf95f1

Browse files
committed
[GR-7971] complete _pyio (and possibly io)
PullRequest: graalpython/56
2 parents 1db361b + ca7dd49 commit aaf95f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2125
-545
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_unicode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
# SOFTWARE.
3737

3838
import sys
39-
import warnings
40-
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionVoid, unhandled_error_compare, GRAALPYTHON
39+
40+
from . import CPyExtTestCase, CPyExtFunction, unhandled_error_compare, GRAALPYTHON
41+
4142
__dir__ = __file__.rpartition("/")[0]
4243

4344

@@ -326,7 +327,6 @@ def compile_module(self, name):
326327
cmpfunc=unhandled_error_compare
327328
)
328329

329-
# TODO enable once supported
330330
test_PyUnicode_GET_SIZE = CPyExtFunction(
331331
lambda args: len(args[0]),
332332
lambda: (
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates.
2+
#
3+
# The Universal Permissive License (UPL), Version 1.0
4+
#
5+
# Subject to the condition set forth below, permission is hereby granted to any
6+
# person obtaining a copy of this software, associated documentation and/or data
7+
# (collectively the "Software"), free of charge and under any and all copyright
8+
# rights in the Software, and any and all patent rights owned or freely
9+
# licensable by each licensor hereunder covering either (i) the unmodified
10+
# Software as contributed to or provided by such licensor, or (ii) the Larger
11+
# Works (as defined below), to deal in both
12+
#
13+
# (a) the Software, and
14+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
# one is included with the Software (each a "Larger Work" to which the
16+
# Software is contributed by such licensors),
17+
#
18+
# without restriction, including without limitation the rights to copy, create
19+
# derivative works of, display, perform, and distribute the Software and make,
20+
# use, sell, offer for sale, import, export, have made, and have sold the
21+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
# either these or other terms.
23+
#
24+
# This license is subject to the following condition:
25+
#
26+
# The above copyright notice and either this complete permission notice or at a
27+
# minimum a reference to the UPL must be included in all copies or substantial
28+
# portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
38+
39+
def assert_raises(err, fn, *args, **kwargs):
40+
raised = False
41+
try:
42+
fn(*args, **kwargs)
43+
except err:
44+
raised = True
45+
assert raised
46+
47+
48+
def test_import():
49+
imported = True
50+
try:
51+
import array
52+
except ImportError:
53+
imported = False
54+
assert imported
55+
56+
57+
def test_create():
58+
from array import array
59+
a = array('b', b'x'*10)
60+
assert str(a) == "array('b', [120, 120, 120, 120, 120, 120, 120, 120, 120, 120])"

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3636
# SOFTWARE.
3737

38+
3839
def assert_raises(err, fn, *args, **kwargs):
3940
raised = False
4041
try:
@@ -327,3 +328,91 @@ def test_contains():
327328
assert b"/" in b"/", "b'/' was not in b'/'"
328329
assert bytearray([32]) in b" ", "bytearray([32]) was not in b' '"
329330
assert_raises(TypeError, lambda: "/" in b"/")
331+
332+
333+
def test_extend():
334+
orig = b'hello'
335+
a = bytearray(orig)
336+
a.extend(a)
337+
338+
assert a == orig + orig
339+
assert a[5:] == orig
340+
341+
a = bytearray(b'')
342+
# Test iterators that don't have a __length_hint__
343+
a.extend(map(int, orig * 25))
344+
a.extend(int(x) for x in orig * 25)
345+
assert a == orig * 50
346+
assert a[-5:] == orig
347+
348+
a = bytearray(b'')
349+
a.extend(iter(map(int, orig * 50)))
350+
assert a == orig * 50
351+
assert a[-5:] == orig
352+
353+
a = bytearray(b'')
354+
a.extend(list(map(int, orig * 50)))
355+
assert a == orig * 50
356+
assert a[-5:] == orig
357+
358+
a = bytearray(b'')
359+
assert_raises(ValueError, a.extend, [0, 1, 2, 256])
360+
assert_raises(ValueError, a.extend, [0, 1, 2, -1])
361+
assert len(a) == 0
362+
363+
364+
def test_startswith():
365+
b = b'hello'
366+
assert not b.startswith(b"anything")
367+
assert b.startswith(b"hello")
368+
assert b.startswith(b"hel")
369+
assert b.startswith(b"h")
370+
assert not b.startswith(b"hellow")
371+
assert not b.startswith(b"ha")
372+
373+
b = bytearray(b'hello')
374+
assert not b.startswith(b"anything")
375+
assert b.startswith(b"hello")
376+
assert b.startswith(b"hel")
377+
assert b.startswith(b"h")
378+
assert not b.startswith(b"hellow")
379+
assert not b.startswith(b"ha")
380+
381+
382+
def test_endswith():
383+
b = b'hello'
384+
assert not b.endswith(b"anything")
385+
assert b.endswith(b"hello")
386+
assert b.endswith(b"llo")
387+
assert b.endswith(b"o")
388+
assert not b.endswith(b"whello")
389+
assert not b.endswith(b"no")
390+
391+
b = bytearray(b'hello')
392+
assert not b.endswith(b"anything")
393+
assert b.endswith(b"hello")
394+
assert b.endswith(b"llo")
395+
assert b.endswith(b"o")
396+
assert not b.endswith(b"whello")
397+
assert not b.endswith(b"no")
398+
399+
400+
def test_find():
401+
b = b'mississippi'
402+
i = 105
403+
w = 119
404+
405+
assert b.find(b'ss') == 2
406+
assert b.find(b'w') == -1
407+
assert b.find(b'mississippian') == -1
408+
409+
assert b.find(i) == 1
410+
assert b.find(w) == -1
411+
412+
assert b.find(b'ss', 3) == 5
413+
assert b.find(b'ss', 1, 7) == 2
414+
assert b.find(b'ss', 1, 3) == -1
415+
416+
assert b.find(i, 6) == 7
417+
assert b.find(i, 1, 3) == 1
418+
assert b.find(w, 1, 3) == -1

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Copyright (C) 1996-2017 Python Software Foundation
33
#
44
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
5+
6+
57
def assert_raises(err, fn, *args, **kwargs):
68
raised = False
79
try:
@@ -12,7 +14,12 @@ def assert_raises(err, fn, *args, **kwargs):
1214

1315

1416
def test_import():
15-
import codecs
17+
imported = True
18+
try:
19+
import codecs
20+
except ImportError:
21+
imported = False
22+
assert imported
1623

1724

1825
def test_decode():
@@ -21,9 +28,9 @@ def test_decode():
2128
# TODO: this does not work yet due to the fact that we do not handle all strings literal types yet
2229
# assert codecs.decode(b'\xe4\xf6\xfc', 'latin-1') == '\xe4\xf6\xfc'
2330
# assert_raises(TypeError, codecs.decode)
24-
# assert codecs.decode(b'abc') == 'abc'
31+
assert codecs.decode(b'abc') == 'abc'
2532
# assert_raises(UnicodeDecodeError, codecs.decode, b'\xff', 'ascii')
26-
#
33+
2734
# test keywords
2835
# assert codecs.decode(obj=b'\xe4\xf6\xfc', encoding='latin-1') == '\xe4\xf6\xfc'
2936
# assert codecs.decode(b'[\xff]', 'ascii', errors='ignore') == '[]'
@@ -36,7 +43,7 @@ def test_encode():
3643
# TODO: this does not work yet due to the fact that we do not handle all strings literal types yet
3744
# assert codecs.encode('\xe4\xf6\xfc', 'latin-1') == b'\xe4\xf6\xfc'
3845
# assert_raises(TypeError, codecs.encode)
39-
# assert_raises(LookupError, codecs.encode, "foo", "__spam__")
46+
assert_raises(LookupError, codecs.encode, "foo", "__spam__")
4047
# assert codecs.encode('abc') == b'abc'
4148
# assert_raises(UnicodeEncodeError, codecs.encode, '\xffff', 'ascii')
4249

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ def assert_raises(err, fn, *args, **kwargs):
4545

4646

4747
def test_import():
48-
import collections
49-
from collections import namedtuple, Counter, OrderedDict, _count_elements
50-
from collections import UserDict, UserString, UserList
51-
from collections import ChainMap
52-
# from collections import deque
48+
imported = True
49+
try:
50+
import collections
51+
from collections import namedtuple, Counter, OrderedDict, _count_elements
52+
from collections import UserDict, UserString, UserList
53+
from collections import ChainMap
54+
# from collections import deque
55+
except ImportError:
56+
imported = False
57+
assert imported
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates.
2+
#
3+
# The Universal Permissive License (UPL), Version 1.0
4+
#
5+
# Subject to the condition set forth below, permission is hereby granted to any
6+
# person obtaining a copy of this software, associated documentation and/or data
7+
# (collectively the "Software"), free of charge and under any and all copyright
8+
# rights in the Software, and any and all patent rights owned or freely
9+
# licensable by each licensor hereunder covering either (i) the unmodified
10+
# Software as contributed to or provided by such licensor, or (ii) the Larger
11+
# Works (as defined below), to deal in both
12+
#
13+
# (a) the Software, and
14+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
# one is included with the Software (each a "Larger Work" to which the
16+
# Software is contributed by such licensors),
17+
#
18+
# without restriction, including without limitation the rights to copy, create
19+
# derivative works of, display, perform, and distribute the Software and make,
20+
# use, sell, offer for sale, import, export, have made, and have sold the
21+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
# either these or other terms.
23+
#
24+
# This license is subject to the following condition:
25+
#
26+
# The above copyright notice and either this complete permission notice or at a
27+
# minimum a reference to the UPL must be included in all copies or substantial
28+
# portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
38+
39+
def assert_raises(err, fn, *args, **kwargs):
40+
raised = False
41+
try:
42+
fn(*args, **kwargs)
43+
except err:
44+
raised = True
45+
assert raised
46+
47+
48+
def test_import():
49+
imported = True
50+
try:
51+
from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, localeconv, Error)
52+
except ImportError:
53+
imported = False
54+
assert imported
55+
56+
57+
def test_get_setlocale():
58+
import locale
59+
current_locale = locale.getlocale(0)
60+
try:
61+
new_locale = ('en_GB', 'UTF-8')
62+
assert str(locale.setlocale(0, new_locale)) == '.'.join(new_locale)
63+
assert locale.getlocale(0) == new_locale
64+
finally:
65+
assert str(locale.setlocale(0, current_locale)) == '.'.join(current_locale)

0 commit comments

Comments
 (0)