Skip to content

Commit

Permalink
Python 3 super
Browse files Browse the repository at this point in the history
  • Loading branch information
anivegesana committed May 7, 2022
1 parent 363180d commit 77355a5
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions tests/test_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,36 @@ def test_slots():
assert dill.copy(y).y == value

def test_metaclass():
class metaclass_with_new(type):
def __new__(mcls, name, bases, ns, **kwds):
cls = super().__new__(mcls, name, bases, ns, **kwds)
assert mcls is not None
assert cls.method(mcls)
return cls
def method(cls, mcls):
return isinstance(cls, mcls)

if dill._dill.PY3:
class metaclass_with_new(type):
def __new__(mcls, name, bases, ns, **kwds):
cls = super().__new__(mcls, name, bases, ns, **kwds)
assert mcls is not None
assert cls.method(mcls)
return cls
def method(cls, mcls):
return isinstance(cls, mcls)

l = locals()
exec("""class subclass_with_new(metaclass=metaclass_with_new):
def __new__(cls):
self = super().__new__(cls)
return self""", None, l)
subclass_with_new = l['subclass_with_new']
else:
class metaclass_with_new(type):
def __new__(mcls, name, bases, ns, **kwds):
cls = super(mcls, metaclass_with_new).__new__(mcls, name, bases, ns, **kwds)
assert mcls is not None
assert cls.method(mcls)
return cls
def method(cls, mcls):
return isinstance(cls, mcls)

class subclass_with_new:
__metaclass__ = metaclass_with_new
def __new__(cls):
self = super().__new__(cls)
self = super(subclass_with_new, cls).__new__(cls)
return self

assert dill.copy(subclass_with_new())
Expand Down

0 comments on commit 77355a5

Please sign in to comment.