From 3f8d34ca83c75283f9dca398158b58ba4706b58e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 10 May 2014 23:03:30 +0300 Subject: objlist: Support list slice deletion. --- tests/basics/list_slice_assign.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests') diff --git a/tests/basics/list_slice_assign.py b/tests/basics/list_slice_assign.py index f88052046..baa9a0081 100644 --- a/tests/basics/list_slice_assign.py +++ b/tests/basics/list_slice_assign.py @@ -11,6 +11,9 @@ print(l) l = list(x) l[1:3] = [] print(l) +l = list(x) +del l[1:3] +print(l) l = list(x) l[:3] = [10, 20] @@ -18,6 +21,9 @@ print(l) l = list(x) l[:3] = [] print(l) +l = list(x) +del l[:3] +print(l) l = list(x) l[:-3] = [10, 20] @@ -25,3 +31,6 @@ print(l) l = list(x) l[:-3] = [] print(l) +l = list(x) +del l[:-3] +print(l) -- cgit v1.2.3 From 69f3eb2c9688752d674254c3f4158cae688053be Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 11 May 2014 02:44:46 +0300 Subject: objstr: Make .[r]partition() work with bytes. --- tests/basics/string_partition.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests') diff --git a/tests/basics/string_partition.py b/tests/basics/string_partition.py index ad70d0250..fe0070a65 100644 --- a/tests/basics/string_partition.py +++ b/tests/basics/string_partition.py @@ -27,3 +27,14 @@ except ValueError: print("Raised ValueError") else: print("Did not raise ValueError") + +# Bytes +print(b"abba".partition(b'b')) +try: + print(b"abba".partition('b')) +except TypeError: + print("Raised TypeError") +try: + print("abba".partition(b'b')) +except TypeError: + print("Raised TypeError") -- cgit v1.2.3 From 9511f60f016547ab00f634d451c230351bd8b225 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 11 May 2014 03:12:36 +0300 Subject: py: Don't try to "bind" types store as attributes of objects. This was hit when trying to make urlparse.py from stdlib run. Took quite some time to debug. TODO: Reconsile bound method creation process better, maybe callable is to generic type to bind at all? --- tests/basics/class_store_class.py | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/basics/class_store_class.py (limited to 'tests') diff --git a/tests/basics/class_store_class.py b/tests/basics/class_store_class.py new file mode 100644 index 000000000..c765fceaa --- /dev/null +++ b/tests/basics/class_store_class.py @@ -0,0 +1,44 @@ +# Inspired by urlparse.py from CPython 3.3 stdlib +# There was a bug in MicroPython that under some conditions class stored +# in instance attribute later was returned "bound" as if it was a method, +# which caused class constructor to receive extra argument. +from collections import namedtuple + +_DefragResultBase = namedtuple('DefragResult', 'foo bar') + +class _ResultMixinStr(object): + def encode(self): + return self._encoded_counterpart(*(x.encode() for x in self)) + +class _ResultMixinBytes(object): + def decode(self): + return self._decoded_counterpart(*(x.decode() for x in self)) + +class DefragResult(_DefragResultBase, _ResultMixinStr): + pass + +class DefragResultBytes(_DefragResultBase, _ResultMixinBytes): + pass + + +DefragResult._encoded_counterpart = DefragResultBytes +DefragResultBytes._decoded_counterpart = DefragResult + +# Due to differences in type and native subclass printing, +# the best thing we can do here is to just test that no exceptions +# happen + +#print(DefragResult, DefragResult._encoded_counterpart) +#print(DefragResultBytes, DefragResultBytes._decoded_counterpart) + +o1 = DefragResult("a", "b") +#print(o1, type(o1)) +o2 = DefragResultBytes("a", "b") +#print(o2, type(o2)) + +#print(o1._encoded_counterpart) +_o1 = o1.encode() +print(_o1[0], _o1[1]) +#print(_o1, type(_o1)) + +print("All's ok") -- cgit v1.2.3 From ea9708092e8a49377a465ef8c8500943fe9ba772 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 11 May 2014 03:16:04 +0300 Subject: objtuple: Go out of the way to support comparison of subclasses. Two things are handled here: allow to compare native subtypes of tuple, e.g. namedtuple (TODO: should compare type too, currently compared duck-typedly by content). Secondly, allow user sunclasses of tuples (and its subtypes) be compared either. "Magic" I did previously in objtype.c covers only one argument (lhs is many), so we're in trouble when lhs is native type - there's no other option besides handling rhs in special manner. Fortunately, this patch outlines approach with fast path for native types. --- tests/basics/subclass_native_cmp.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/basics/subclass_native_cmp.py (limited to 'tests') diff --git a/tests/basics/subclass_native_cmp.py b/tests/basics/subclass_native_cmp.py new file mode 100644 index 000000000..1a095bfa1 --- /dev/null +++ b/tests/basics/subclass_native_cmp.py @@ -0,0 +1,9 @@ +# Test calling non-special method inherited from native type + +class mytuple(tuple): + pass + +t = mytuple((1, 2, 3)) +print(t) +print(t == (1, 2, 3)) +print((1, 2, 3) == t) -- cgit v1.2.3 From b4acd028b613a721ffbe5a3136700f190635f7c9 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 11 May 2014 03:40:32 +0300 Subject: tests: Fix import. --- tests/basics/class_store_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/basics/class_store_class.py b/tests/basics/class_store_class.py index c765fceaa..cc80fb5b1 100644 --- a/tests/basics/class_store_class.py +++ b/tests/basics/class_store_class.py @@ -2,7 +2,7 @@ # There was a bug in MicroPython that under some conditions class stored # in instance attribute later was returned "bound" as if it was a method, # which caused class constructor to receive extra argument. -from collections import namedtuple +from _collections import namedtuple _DefragResultBase = namedtuple('DefragResult', 'foo bar') -- cgit v1.2.3 From ce6c10172be6d17f76779ece930908f6248a1706 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 11 May 2014 03:45:42 +0300 Subject: tests: Really fix import. --- tests/basics/class_store_class.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/basics/class_store_class.py b/tests/basics/class_store_class.py index cc80fb5b1..60f65220d 100644 --- a/tests/basics/class_store_class.py +++ b/tests/basics/class_store_class.py @@ -2,7 +2,10 @@ # There was a bug in MicroPython that under some conditions class stored # in instance attribute later was returned "bound" as if it was a method, # which caused class constructor to receive extra argument. -from _collections import namedtuple +try: + from collections import namedtuple +except ImportError: + from _collections import namedtuple _DefragResultBase = namedtuple('DefragResult', 'foo bar') -- cgit v1.2.3 From b2d4fc06fc95d8e96eabd6ef470f0e871275fb82 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 11 May 2014 13:17:29 +0300 Subject: objstr: Make *strip() accept bytes. --- tests/basics/string_strip.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests') diff --git a/tests/basics/string_strip.py b/tests/basics/string_strip.py index 8e03eff93..4684c2a24 100644 --- a/tests/basics/string_strip.py +++ b/tests/basics/string_strip.py @@ -10,3 +10,13 @@ print('www.example.com'.lstrip('cmowz.')) print(' spacious '.rstrip()) print('mississippi'.rstrip('ipz')) + +print(b'mississippi'.rstrip(b'ipz')) +try: + print(b'mississippi'.rstrip('ipz')) +except TypeError: + print("TypeError") +try: + print('mississippi'.rstrip(b'ipz')) +except TypeError: + print("TypeError") -- cgit v1.2.3 From eea01186547f0f1568ea1c8f002da4e33b7b0e46 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 11 May 2014 13:51:24 +0300 Subject: py: Give up and make mp_obj_str_get_data() deal with bytes too. This is not fully correct re: error handling, because we should check that that types are used consistently (only str's or only bytes), but magically makes lot of functions support bytes. --- tests/basics/int1.py | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/basics/int1.py b/tests/basics/int1.py index 2daef9bf0..e8a0a0468 100644 --- a/tests/basics/int1.py +++ b/tests/basics/int1.py @@ -46,6 +46,7 @@ print(int('0B100', 2)) print(int('0100', 2)) print(int(' \t 0o12', 8)) print(int('0o12 \t ', 8)) +print(int(b"12", 10)) def test(value, base): -- cgit v1.2.3