New submission from Karthikeyan Singaravelan <tir.kar...@gmail.com>:

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)

----------
components: Interpreter Core
messages: 339988
nosy: benjamin.peterson, eric.snow, michael.foord, ncoghlan, nedbat, xtreak
priority: normal
severity: normal
status: open
title: calling super() causes __class__ to be not defined when 
sys.settrace(trace) is set
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36606>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to