Commit c596ab9c authored by Kirill Smelkov's avatar Kirill Smelkov

golang_str: tests: Fix test_strings_refcount wrt upcoming str=bstr and unicode=ustr

When gpython will start patching builtin str and unicode types with bstr
and ustr it might be the case that b('abc') return the same 'abc' object
and so the logic in this test will become broken.

-> Avoid that by keeping the original data in bytearray which for sure
   won't overlap with bytes/str nor unicode irregardless whether those
   builtin types are patched or not.
parent 674b74e1
...@@ -288,20 +288,26 @@ def test_strings_basic(): ...@@ -288,20 +288,26 @@ def test_strings_basic():
# verify that bstr/ustr are created with correct refcount. # verify that bstr/ustr are created with correct refcount.
def test_strings_refcount(): def test_strings_refcount():
# buffer with string data - not bytes nor unicode so that when builtin
# string types are patched there is no case where bytes is created from the
# same bytes, or unicode is created from the same unicode - only increasing
# refcount of original object.
data = bytearray([ord('a'), ord('b'), ord('c'), ord('4')])
# first verify our logic on std type # first verify our logic on std type
obj = xbytes(u'abc'); assert type(obj) is bytes obj = bytes(data); assert type(obj) is bytes
gc.collect(); assert sys.getrefcount(obj) == 1+1 # +1 due to obj passed to getrefcount call gc.collect(); assert sys.getrefcount(obj) == 1+1 # +1 due to obj passed to getrefcount call
# bstr # bstr
obj = b('abc'); assert type(obj) is bstr obj = b(data); assert type(obj) is bstr
gc.collect(); assert sys.getrefcount(obj) == 1+1 gc.collect(); assert sys.getrefcount(obj) == 1+1
obj = bstr('abc'); assert type(obj) is bstr obj = bstr(data); assert type(obj) is bstr
gc.collect(); assert sys.getrefcount(obj) == 1+1 gc.collect(); assert sys.getrefcount(obj) == 1+1
# ustr # ustr
obj = u('abc'); assert type(obj) is ustr obj = u(data); assert type(obj) is ustr
gc.collect(); assert sys.getrefcount(obj) == 1+1 gc.collect(); assert sys.getrefcount(obj) == 1+1
obj = ustr('abc'); assert type(obj) is ustr obj = ustr(data); assert type(obj) is ustr
gc.collect(); assert sys.getrefcount(obj) == 1+1 gc.collect(); assert sys.getrefcount(obj) == 1+1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment