Skip to content

Improve tests of copy module #100871

Closed
Closed
@sobolevn

Description

@sobolevn

After working on #100817 I've noticed that there are multiple things that can be improved in terms of copy module tests:

  1. First of all, I had submitted some broken code in gh-100817: Speed up copy.deepcopy calls on slice objects #100818 but, our test cases were not able to detect it. Solution: add a new test case in test_slice.py with copy and deepcopy calls. I think it should be in test_slice and not in test_copy, because there's nothing special about it: copy does not change its behaviour or special case it.
  2. This test ensures that after modifing copyreg we can now copy an object, but does not assert the result:
    def test_copy_registry(self):
    class C(object):
    def __new__(cls, foo):
    obj = object.__new__(cls)
    obj.foo = foo
    return obj
    def pickle_C(obj):
    return (C, (obj.foo,))
    x = C(42)
    self.assertRaises(TypeError, copy.copy, x)
    copyreg.pickle(C, pickle_C, C)
    y = copy.copy(x)
    def test_copy_reduce_ex(self):
    Right now it can be whatever. The same happens in
    def test_deepcopy_registry(self):
    class C(object):
    def __new__(cls, foo):
    obj = object.__new__(cls)
    obj.foo = foo
    return obj
    def pickle_C(obj):
    return (C, (obj.foo,))
    x = C(42)
    self.assertRaises(TypeError, copy.deepcopy, x)
    copyreg.pickle(C, pickle_C, C)
    y = copy.deepcopy(x)
    def test_deepcopy_reduce_ex(self):
    Solution: add required assertions
  3. test_deepcopy_atomic misses several important types: bytes, types.EllipsisType, NotImplementedType.
    def test_deepcopy_atomic(self):
    class NewStyle:
    pass
    def f():
    pass
    tests = [None, 42, 2**100, 3.14, True, False, 1j,
    "hello", "hello\u1234", f.__code__,
    NewStyle, range(10), max, property()]
    for x in tests:
    self.assertIs(copy.deepcopy(x), x)

PR is incoming.

Linked PRs

Metadata

Metadata

Assignees

Labels

testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions