Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialization is being done in PyType_GenericAlloc #68057

Closed
hvenev mannequin opened this issue Apr 4, 2015 · 3 comments
Closed

Initialization is being done in PyType_GenericAlloc #68057

hvenev mannequin opened this issue Apr 4, 2015 · 3 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@hvenev
Copy link
Mannequin

hvenev mannequin commented Apr 4, 2015

BPO 23869
Nosy @ericsnowcurrently, @hvenev

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-06-13.16:21:39.907>
created_at = <Date 2015-04-04.15:03:22.175>
labels = ['interpreter-core', '3.7', '3.8']
title = 'Initialization is being done in PyType_GenericAlloc'
updated_at = <Date 2018-06-13.16:22:49.236>
user = 'https://github.com/hvenev'

bugs.python.org fields:

activity = <Date 2018-06-13.16:22:49.236>
actor = 'eric.snow'
assignee = 'none'
closed = True
closed_date = <Date 2018-06-13.16:21:39.907>
closer = 'eric.snow'
components = ['Interpreter Core']
creation = <Date 2015-04-04.15:03:22.175>
creator = 'h.venev'
dependencies = []
files = []
hgrepos = []
issue_num = 23869
keywords = []
message_count = 3.0
messages = ['240080', '319476', '319477']
nosy_count = 2.0
nosy_names = ['eric.snow', 'h.venev']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue23869'
versions = ['Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8']

@hvenev
Copy link
Mannequin Author

hvenev mannequin commented Apr 4, 2015

In PyType_GenericAlloc, the initialization is being done. Namely, the PyObject part of the object is initialized and it is tracked by the garbage collector.

In the documentation it is stated that tp_alloc should do no initialization.

@hvenev hvenev mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Apr 4, 2015
@ericsnowcurrently
Copy link
Member

Thanks for bringing this up, Hristo!

"Initialization" (in this context, and hopefully everywhere in the C-API docs) is referring specifically to setting fields on a custom instance, much as you would expect in __init__ (or sometimes even __new__). In that regard PyType_GenericAlloc does not do any initialization. Instead, what it does is at a low level which is consistent with allocating a basic object for use in the CPython object machinery:

  • allocate the memory
  • NULL out the allocated memory
  • set initial refcount to 1
  • fill in the fundamental PyObject fields (e.g. type)
  • register with GC

Most of that relates directly to memory management and definitely belongs in PyType_GenericAlloc. I suppose you could argue that filling in the fundamental PyObject fields could go into PyType_GenericNew. However, doing so would leave the "allocated" object in an inconsistent state for the object machinery, to be resolved by the type's tp_new implementation. This could lead to problems for types that have a custom tp_new that does not use PyType_GenericNew. In that case moving anything from PyType_GenericAlloc to PyType_GenericNew would be a backward-incompatible change (and add complexity to the object creation workflow). Even though the chance for breakage is small, there would have to be a really strong reason to make such a change.

Consequently, there isn't a whole lot to be done here and I'm closing this issue. If I've misunderstood then please let us know. We can re-open the issue then.

FTR, PyType_GenericAlloc is implemented here:

https://github.com/python/cpython/blob/master/Objects/typeobject.c#L963

@ericsnowcurrently ericsnowcurrently added 3.7 (EOL) end of life 3.8 only security fixes labels Jun 13, 2018
@ericsnowcurrently
Copy link
Member

As to the docs, the entry for tp_alloc in Doc/c-api/typeobj.rst does not mention initialization. The tp_new entry does say that it should call tp_alloc and then do the minimum initialization possible. That implies (weakly) that tp_alloc should do the minimum initialization possible. Could you point me to the place where the docs talk about tp_alloc and initialization? That would be useful to see.

Regardless, having the tp_alloc entry explicitly say it shouldn't do any initialization does make sense. Feel free to open a separate issue on that.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

1 participant