classification
Title: Don't enable GC for classes that don't add new fields
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, eltoder, njs, pitrou, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2015-03-20 20:25 by eltoder, last changed 2015-04-13 19:30 by eltoder. This issue is now closed.

Files
File name Uploaded Description Edit
class_gc.diff eltoder, 2015-03-20 20:25 review
class_gc2.diff eltoder, 2015-03-21 18:47 review
Messages (11)
msg238718 - (view) Author: Eugene Toder (eltoder) Date: 2015-03-20 20:25
As far as I can tell, if a new class does not add any new fields, and its base class doesn't use GC, there's no reason to enable GC for the new class.
This is useful for creating lightweight wrappers around classes implemented in C.
msg238721 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-20 20:51
>>> class UserIntSlots(int):
...     __slots__ = ()
...     def __repr__(s): return '5'
... 
>>> (4).__class__ = UserIntSlots
>>> 2+2
5
>>> type(2+2)
<class '__main__.UserIntSlots'>

It looks weird.
msg238727 - (view) Author: Eugene Toder (eltoder) Date: 2015-03-20 22:00
Agreed, but this is not new. This works without my change:

>>> class Tuple(tuple):
...   __slots__ = ()
...   def __repr__(self): return 'Imma tuple!'                                                    
... 
>>> ().__class__ = Tuple
>>> ()
Imma tuple!
msg238730 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-03-20 22:10
I wasn't aware of that :-o
msg238732 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-20 22:26
May be we should forbid assigning the __class__ attribute of basic builtin 
types (especially internable, such as int, str, bytes, tuple, bool, NoneType).
msg238733 - (view) Author: Eugene Toder (eltoder) Date: 2015-03-20 22:32
Actually, this is rather new -- new in 3.5. The check was relaxed in #22986: https://hg.python.org/cpython/rev/c0d25de5919e
Previously only heap types allowed re-assigning __class__.
msg240690 - (view) Author: Roundup Robot (python-dev) Date: 2015-04-13 18:10
New changeset a60b7945ef87 by Antoine Pitrou in branch 'default':
Issue #23726: Don't enable GC for user subclasses of non-GC types that don't add any new fields.
https://hg.python.org/cpython/rev/a60b7945ef87
msg240691 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-04-13 18:10
I've pushed the patch, thank you!
msg240699 - (view) Author: Eugene Toder (eltoder) Date: 2015-04-13 18:25
Thank you!

Benjamin, Nathaniel, any opinion if we should restrict reassigning __class__ for types like tuple, int and str, where some/many instances are cached?
msg240710 - (view) Author: Nathaniel Smith (njs) * Date: 2015-04-13 19:26
Yes, it probably would be a good idea to disallow assigning __class__ for immutable types.
msg240711 - (view) Author: Eugene Toder (eltoder) Date: 2015-04-13 19:30
Agreed. There's a small problem with that, as far as I know. Nothing on type declares that it is immutable.
History
Date User Action Args
2015-04-13 19:30:52eltodersetmessages: + msg240711
2015-04-13 19:26:46njssetmessages: + msg240710
2015-04-13 18:25:40eltodersetnosy: + njs
messages: + msg240699
2015-04-13 18:10:31pitrousetstatus: open -> closed
resolution: fixed
messages: + msg240691

stage: patch review -> resolved
2015-04-13 18:10:14python-devsetnosy: + python-dev
messages: + msg240690
2015-03-21 18:47:31eltodersetfiles: + class_gc2.diff
2015-03-20 22:36:11serhiy.storchakasetnosy: + benjamin.peterson
2015-03-20 22:32:53eltodersetmessages: + msg238733
2015-03-20 22:26:34serhiy.storchakasetmessages: + msg238732
2015-03-20 22:10:10pitrousetmessages: + msg238730
2015-03-20 22:00:14eltodersetmessages: + msg238727
2015-03-20 20:51:00serhiy.storchakasetmessages: + msg238721
2015-03-20 20:35:47serhiy.storchakasetnosy: + serhiy.storchaka
stage: patch review

components: + Interpreter Core
versions: + Python 3.5, - Python 3.6
2015-03-20 20:25:57eltodercreate