Bug report
def test_load_attr_class_default_not_set_on_instance(self):
class C:
timeout = 30
def set_timeout(self, value):
self.timeout = value # puts "timeout" in C's shared keys
c = C() # never sets timeout, so c.timeout is the class default
@reset_code
def get_timeout(n):
for _ in range(n):
value = c.timeout
return value
self.assertEqual(get_timeout(_testinternalcapi.SPECIALIZATION_THRESHOLD), 30)
# Must load the class attribute, not specialize as an instance
# attribute load that misses on every execution.
self.assert_specialized(get_timeout, "LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES")
self.assert_no_opcode(get_timeout, "LOAD_ATTR_INSTANCE_VALUE")
# Setting the attribute on the instance must be seen.
c.set_timeout(5)
self.assertEqual(get_timeout(1), 5)
This is due to instance_has_key returning True even if setting an instance field is not actually set. Json dumps shows this issue with many instance level attributes not set by default relying on class level defaults
|
if separators is not None: |
For json.dumps this can add about a 5% tax due to missed optimisations although it depends on the payload size. threading.Condition is another example from the stdlib.
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
Bug report
This is due to
instance_has_keyreturningTrueeven if setting an instance field is not actually set. Json dumps shows this issue with many instance level attributes not set by default relying on class level defaultscpython/Lib/json/encoder.py
Line 156 in 978a8be
For json.dumps this can add about a 5% tax due to missed optimisations although it depends on the payload size.
threading.Conditionis another example from the stdlib.CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs