Message339988
I came across this issue in issue36593 where MagicMock had a custom __class__ attribute set and one of the methods used super() which caused __class__ not to be set. This seems to have been fixed in the past with issue12370 and a workaround to alias super at module level and use it was suggested in msg161704. Usage of the alias seems to solve the issue for Mock but the fix for __class__ breaks when sys.settrace is set. Example code as below with custom __class__ defined and with running the code under sys.settrace() super() doesn't set __class__ but using _safe_super alias works. Another aspect in the mock related issue is that the call to super() is under a codepath that is not executed during normal run but executed when sys.settrace during import itself.
import sys
_safe_super = super
def trace(frame, event, arg):
return trace
if len(sys.argv) > 1:
sys.settrace(trace)
class SuperClass(object):
def __init__(self):
super().__init__()
@property
def __class__(self):
return int
class SafeSuperClass(object):
def __init__(self):
_safe_super(SafeSuperClass, self).__init__()
@property
def __class__(self):
return int
print(isinstance(SuperClass(), int))
print(isinstance(SafeSuperClass(), int))
Running above code with trace and without trace
➜ cpython git:(master) ✗ ./python.exe /tmp/buz.py
True
True
➜ cpython git:(master) ✗ ./python.exe /tmp/buz.py 1
False
True
There is a test for the above in Lib/test/test_super.py at https://github.com/python/cpython/blob/4c409beb4c360a73d054f37807d3daad58d1b567/Lib/test/test_super.py#L87
Add a trace as below in test_super.py at the top and the test case fails
import sys
def trace(frame, event, arg):
return trace
sys.settrace(trace)
➜ cpython git:(master) ✗ ./python.exe Lib/test/test_super.py
....................F
======================================================================
FAIL: test_various___class___pathologies (__main__.TestSuper)
----------------------------------------------------------------------
Traceback (most recent call last):
File "Lib/test/test_super.py", line 100, in test_various___class___pathologies
self.assertEqual(x.__class__, 413)
AssertionError: <class '__main__.TestSuper.test_various___class___pathologies.<locals>.X'> != 413
----------------------------------------------------------------------
Ran 21 tests in 0.058s
FAILED (failures=1) |
|
Date |
User |
Action |
Args |
2019-04-11 17:18:57 | xtreak | set | recipients:
+ xtreak, ncoghlan, nedbat, benjamin.peterson, michael.foord, eric.snow |
2019-04-11 17:18:57 | xtreak | set | messageid: <1555003137.23.0.80747971032.issue36606@roundup.psfhosted.org> |
2019-04-11 17:18:57 | xtreak | link | issue36606 messages |
2019-04-11 17:18:56 | xtreak | create | |
|