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 cb0a959
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions tests/test_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,32 @@ 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):
Expand Down

0 comments on commit cb0a959

Please sign in to comment.