Skip to content

Sync Fork from Upstream Repo #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 86 additions & 2 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import functools
import pickle
import tempfile
import textwrap
import unittest

import test.support
import test.string_tests
import test.list_tests
from test.support import bigaddrspacetest, MAX_Py_ssize_t
from test.support.script_helper import assert_python_failure


if sys.flags.bytes_warning:
Expand Down Expand Up @@ -319,6 +321,64 @@ def test_decode(self):
# Default encoding is utf-8
self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603')

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_check_encoding_errors(self):
# bpo-37388: bytes(str) and bytes.encode() must check encoding
# and errors arguments in dev mode
invalid = 'Boom, Shaka Laka, Boom!'
encodings = ('ascii', 'utf8', 'latin1')
code = textwrap.dedent(f'''
import sys
type2test = {self.type2test.__name__}
encodings = {encodings!r}

for data in ('', 'short string'):
try:
type2test(data, encoding={invalid!r})
except LookupError:
pass
else:
sys.exit(21)

for encoding in encodings:
try:
type2test(data, encoding=encoding, errors={invalid!r})
except LookupError:
pass
else:
sys.exit(22)

for data in (b'', b'short string'):
data = type2test(data)
print(repr(data))
try:
data.decode(encoding={invalid!r})
except LookupError:
sys.exit(10)
else:
sys.exit(23)

try:
data.decode(errors={invalid!r})
except LookupError:
pass
else:
sys.exit(24)

for encoding in encodings:
try:
data.decode(encoding=encoding, errors={invalid!r})
except LookupError:
pass
else:
sys.exit(25)

sys.exit(10)
''')
proc = assert_python_failure('-X', 'dev', '-c', code)
self.assertEqual(proc.rc, 10, proc)

def test_from_int(self):
b = self.type2test(0)
self.assertEqual(b, self.type2test())
Expand Down Expand Up @@ -494,9 +554,13 @@ def test_join(self):
self.assertEqual(dot_join([bytearray(b"ab"), b"cd"]), b"ab.:cd")
self.assertEqual(dot_join([b"ab", bytearray(b"cd")]), b"ab.:cd")
# Stress it with many items
seq = [b"abc"] * 1000
expected = b"abc" + b".:abc" * 999
seq = [b"abc"] * 100000
expected = b"abc" + b".:abc" * 99999
self.assertEqual(dot_join(seq), expected)
# Stress test with empty separator
seq = [b"abc"] * 100000
expected = b"abc" * 100000
self.assertEqual(self.type2test(b"").join(seq), expected)
self.assertRaises(TypeError, self.type2test(b" ").join, None)
# Error handling and cleanup when some item in the middle of the
# sequence has the wrong type.
Expand Down Expand Up @@ -917,6 +981,15 @@ def test_translate(self):
c = b.translate(None, delete=b'e')
self.assertEqual(c, b'hllo')

def test_sq_item(self):
_testcapi = test.support.import_module('_testcapi')
obj = self.type2test((42,))
with self.assertRaises(IndexError):
_testcapi.sequence_getitem(obj, -2)
with self.assertRaises(IndexError):
_testcapi.sequence_getitem(obj, 1)
self.assertEqual(_testcapi.sequence_getitem(obj, 0), 42)


class BytesTest(BaseBytesTest, unittest.TestCase):
type2test = bytes
Expand Down Expand Up @@ -978,6 +1051,7 @@ def test_from_format(self):
c_char_p)

PyBytes_FromFormat = pythonapi.PyBytes_FromFormat
PyBytes_FromFormat.argtypes = (c_char_p,)
PyBytes_FromFormat.restype = py_object

# basic tests
Expand Down Expand Up @@ -1615,6 +1689,16 @@ def test_iterator_length_hint(self):
# Shouldn't raise an error
self.assertEqual(list(it), [])

def test_repeat_after_setslice(self):
# bpo-42924: * used to copy from the wrong memory location
b = bytearray(b'abc')
b[:2] = b'x'
b1 = b * 1
b3 = b * 3
self.assertEqual(b1, b'xc')
self.assertEqual(b1, b)
self.assertEqual(b3, b'xcxcxc')


class AssortedBytesTest(unittest.TestCase):
#
Expand Down
46 changes: 46 additions & 0 deletions extra_tests/snippets/builtin_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
assert type.__module__ == 'builtins'
assert type.__qualname__ == 'type'
assert type.__name__ == 'type'
assert isinstance(type.__doc__, str)
assert object.__qualname__ == 'object'
assert int.__qualname__ == 'int'


class A(type):
pass


class B(type):
__module__ = 'b'
__qualname__ = 'BB'


class C:
pass


class D:
__module__ = 'd'
__qualname__ = 'DD'


assert A.__module__ == '__main__'
assert A.__qualname__ == 'A'
assert B.__module__ == 'b'
assert B.__qualname__ == 'BB'
assert C.__module__ == '__main__'
assert C.__qualname__ == 'C'
assert D.__module__ == 'd'
assert D.__qualname__ == 'DD'


# Regression to
# https://github.com/RustPython/RustPython/issues/2310
import builtins
assert builtins.iter.__class__.__module__ == 'builtins'
assert builtins.iter.__class__.__qualname__ == 'builtin_function_or_method'

assert iter.__class__.__module__ == 'builtins'
assert iter.__class__.__qualname__ == 'builtin_function_or_method'
assert type(iter).__module__ == 'builtins'
assert type(iter).__qualname__ == 'builtin_function_or_method'
16 changes: 16 additions & 0 deletions vm/src/builtins/pytype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ impl PyType {
.read()
.get("__qualname__")
.cloned()
// We need to exclude this method from going into recursion:
.and_then(|found| {
if found.isinstance(&vm.ctx.types.getset_type) {
None
} else {
Some(found)
}
})
.unwrap_or_else(|| vm.ctx.new_str(self.name.clone()))
}

Expand All @@ -316,6 +324,14 @@ impl PyType {
.read()
.get("__module__")
.cloned()
// We need to exclude this method from going into recursion:
.and_then(|found| {
if found.isinstance(&vm.ctx.types.getset_type) {
None
} else {
Some(found)
}
})
.unwrap_or_else(|| vm.ctx.new_str("builtins"))
}

Expand Down