Skip to content

Commit

Permalink
Clean up naming in clif/python/gen.py (functional no-op).
Browse files Browse the repository at this point in the history
Preparation for cl/558051567. The existing naming is prone to lead to misunderstandings in the review of cl/558051567.

Renaming overview (see https://docs.python.org/3/c-api/typeobj.html):

```
PyTypeObject  PyCLIF C++ function names
“tp slots”    Old    New
tp_init       _ctor  tp_init_impl
tp_alloc      _new   tp_alloc_impl
tp_dealloc    _dtor  tp_dealloc_impl
tp_free       _del   tp_free_impl
```

Notes:

* cl/558051567 introduces `tp_new_impl` for the `tp_new` slot.

* The removal of class_test.py with cl/557905637 made this CL much easier.

* The TGP (link below) for this CL is extremely noisy, despite several fresh attempts and reruns. See https://yaqs.corp.google.com/eng/q/7353860321519337472#a1n3 for how the risk of missing new failures was mitigated.

PiperOrigin-RevId: 558429410
  • Loading branch information
rwgk committed Aug 29, 2023
1 parent 7501b3c commit 2884d42
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions clif/python/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,20 +394,24 @@ def TypeObject(ht_qualname, tracked_slot_groups,
# NOT identical. tp_group has additional customizations.
if ctor:
yield ''
yield '// %s __init__' % pyname
yield 'static int _ctor(PyObject* self, PyObject* args, PyObject* kw);'
yield '// %s tp_init_impl' % pyname
yield (
'static int tp_init_impl(PyObject* self, PyObject* args, PyObject* kw);'
)
if not iterator:
yield ''
yield '// %s __new__' % pyname
yield 'static PyObject* _new(PyTypeObject* type, Py_ssize_t nitems);'
tp_slots['tp_alloc'] = '_new'
yield '// %s tp_alloc' % pyname
yield (
'static PyObject* tp_alloc_impl(PyTypeObject* type, Py_ssize_t nitems);'
)
tp_slots['tp_alloc'] = 'tp_alloc_impl'
tp_slots['tp_new'] = 'PyType_GenericNew'
yield ''
yield '// %s __del__' % pyname
# Use dtor for dynamic types (derived) to wind down malloc'ed C++ obj, so
# the C++ dtors are run.
tp_slots['tp_dealloc'] = '_dtor'
yield 'static void _dtor(PyObject* self) {'
yield '// %s tp_dealloc_impl' % pyname
yield 'static void tp_dealloc_impl(PyObject* self) {'
tp_slots['tp_dealloc'] = 'tp_dealloc_impl'
if not iterator:
yield I+'if (%s(self)->weakrefs) {' % _Cast(wname)
yield I+I+'PyObject_ClearWeakRefs(self);'
Expand All @@ -426,13 +430,15 @@ def TypeObject(ht_qualname, tracked_slot_groups,
yield I+'Py_TYPE(self)->tp_free(self);'
yield '}'
if not iterator:
# Use delete for static types (not derived), allocated with _new.
tp_slots['tp_free'] = '_del'
# Use delete for static types (not derived), allocated with tp_alloc_impl.
tp_slots['tp_free'] = 'tp_free_impl'
yield ''
yield 'static void _del(void* self) {'
yield 'static void tp_free_impl(void* self) {'
yield I+'delete %s(self);' % _Cast(wname)
yield '}'
tp_slots['tp_init'] = '_ctor' if ctor else 'Clif_PyType_Inconstructible'
tp_slots['tp_init'] = (
'tp_init_impl' if ctor else 'Clif_PyType_Inconstructible'
)
tp_slots['tp_basicsize'] = 'sizeof(%s)' % wname
tp_slots['tp_itemsize'] = tp_slots['tp_version_tag'] = '0'
tp_slots['tp_dictoffset'] = tp_slots['tp_weaklistoffset'] = '0'
Expand Down Expand Up @@ -474,7 +480,10 @@ def TypeObject(ht_qualname, tracked_slot_groups,
yield '}'
if ctor:
yield ''
yield 'static int _ctor(PyObject* self, PyObject* args, PyObject* kw) {'
yield (
'static int tp_init_impl('
'PyObject* self, PyObject* args, PyObject* kw) {'
)
if abstract:
yield I+'if (Py_TYPE(self) == %s) {' % wtype
yield I+I+'return Clif_PyType_Inconstructible(self, args, kw);'
Expand All @@ -491,9 +500,10 @@ def TypeObject(ht_qualname, tracked_slot_groups,
# We have been lucky so far because NULL initialization of clif::Instance
# object is equivalent to constructing it with the default constructor.
# (NULL initialization happens in PyType_GenericAlloc).
# We don't have a place to call placement new. __init__ (and so _ctor) can
# be called many times and we have no way to ensure the previous object is
# destructed properly (it may be NULL or new initialized).
# We don't have a place to call placement new. __init__ (and so
# tp_init_impl) can be called many times and we have no way to ensure the
# previous object is destructed properly (it may be NULL or new
# initialized).
yield I+'%s = ::clif::MakeShared<%s>();' % (cpp,
subst_cpp_ptr or fqclassname)
if subst_cpp_ptr:
Expand Down Expand Up @@ -522,7 +532,10 @@ def TypeObject(ht_qualname, tracked_slot_groups,
yield '}'
if not iterator:
yield ''
yield 'static PyObject* _new(PyTypeObject* type, Py_ssize_t nitems) {'
yield (
'static PyObject* tp_alloc_impl('
'PyTypeObject* type, Py_ssize_t nitems) {'
)
yield I+'DCHECK(nitems == 0);'
yield I+'%s* wobj = new %s;' % (wname, wname)
if enable_instance_dict:
Expand Down

0 comments on commit 2884d42

Please sign in to comment.