Skip to content

Commit 2884d42

Browse files
committed
Clean up naming in clif/python/gen.py (functional no-op).
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
1 parent 7501b3c commit 2884d42

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

clif/python/gen.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -394,20 +394,24 @@ def TypeObject(ht_qualname, tracked_slot_groups,
394394
# NOT identical. tp_group has additional customizations.
395395
if ctor:
396396
yield ''
397-
yield '// %s __init__' % pyname
398-
yield 'static int _ctor(PyObject* self, PyObject* args, PyObject* kw);'
397+
yield '// %s tp_init_impl' % pyname
398+
yield (
399+
'static int tp_init_impl(PyObject* self, PyObject* args, PyObject* kw);'
400+
)
399401
if not iterator:
400402
yield ''
401-
yield '// %s __new__' % pyname
402-
yield 'static PyObject* _new(PyTypeObject* type, Py_ssize_t nitems);'
403-
tp_slots['tp_alloc'] = '_new'
403+
yield '// %s tp_alloc' % pyname
404+
yield (
405+
'static PyObject* tp_alloc_impl(PyTypeObject* type, Py_ssize_t nitems);'
406+
)
407+
tp_slots['tp_alloc'] = 'tp_alloc_impl'
404408
tp_slots['tp_new'] = 'PyType_GenericNew'
405409
yield ''
406-
yield '// %s __del__' % pyname
407410
# Use dtor for dynamic types (derived) to wind down malloc'ed C++ obj, so
408411
# the C++ dtors are run.
409-
tp_slots['tp_dealloc'] = '_dtor'
410-
yield 'static void _dtor(PyObject* self) {'
412+
yield '// %s tp_dealloc_impl' % pyname
413+
yield 'static void tp_dealloc_impl(PyObject* self) {'
414+
tp_slots['tp_dealloc'] = 'tp_dealloc_impl'
411415
if not iterator:
412416
yield I+'if (%s(self)->weakrefs) {' % _Cast(wname)
413417
yield I+I+'PyObject_ClearWeakRefs(self);'
@@ -426,13 +430,15 @@ def TypeObject(ht_qualname, tracked_slot_groups,
426430
yield I+'Py_TYPE(self)->tp_free(self);'
427431
yield '}'
428432
if not iterator:
429-
# Use delete for static types (not derived), allocated with _new.
430-
tp_slots['tp_free'] = '_del'
433+
# Use delete for static types (not derived), allocated with tp_alloc_impl.
434+
tp_slots['tp_free'] = 'tp_free_impl'
431435
yield ''
432-
yield 'static void _del(void* self) {'
436+
yield 'static void tp_free_impl(void* self) {'
433437
yield I+'delete %s(self);' % _Cast(wname)
434438
yield '}'
435-
tp_slots['tp_init'] = '_ctor' if ctor else 'Clif_PyType_Inconstructible'
439+
tp_slots['tp_init'] = (
440+
'tp_init_impl' if ctor else 'Clif_PyType_Inconstructible'
441+
)
436442
tp_slots['tp_basicsize'] = 'sizeof(%s)' % wname
437443
tp_slots['tp_itemsize'] = tp_slots['tp_version_tag'] = '0'
438444
tp_slots['tp_dictoffset'] = tp_slots['tp_weaklistoffset'] = '0'
@@ -474,7 +480,10 @@ def TypeObject(ht_qualname, tracked_slot_groups,
474480
yield '}'
475481
if ctor:
476482
yield ''
477-
yield 'static int _ctor(PyObject* self, PyObject* args, PyObject* kw) {'
483+
yield (
484+
'static int tp_init_impl('
485+
'PyObject* self, PyObject* args, PyObject* kw) {'
486+
)
478487
if abstract:
479488
yield I+'if (Py_TYPE(self) == %s) {' % wtype
480489
yield I+I+'return Clif_PyType_Inconstructible(self, args, kw);'
@@ -491,9 +500,10 @@ def TypeObject(ht_qualname, tracked_slot_groups,
491500
# We have been lucky so far because NULL initialization of clif::Instance
492501
# object is equivalent to constructing it with the default constructor.
493502
# (NULL initialization happens in PyType_GenericAlloc).
494-
# We don't have a place to call placement new. __init__ (and so _ctor) can
495-
# be called many times and we have no way to ensure the previous object is
496-
# destructed properly (it may be NULL or new initialized).
503+
# We don't have a place to call placement new. __init__ (and so
504+
# tp_init_impl) can be called many times and we have no way to ensure the
505+
# previous object is destructed properly (it may be NULL or new
506+
# initialized).
497507
yield I+'%s = ::clif::MakeShared<%s>();' % (cpp,
498508
subst_cpp_ptr or fqclassname)
499509
if subst_cpp_ptr:
@@ -522,7 +532,10 @@ def TypeObject(ht_qualname, tracked_slot_groups,
522532
yield '}'
523533
if not iterator:
524534
yield ''
525-
yield 'static PyObject* _new(PyTypeObject* type, Py_ssize_t nitems) {'
535+
yield (
536+
'static PyObject* tp_alloc_impl('
537+
'PyTypeObject* type, Py_ssize_t nitems) {'
538+
)
526539
yield I+'DCHECK(nitems == 0);'
527540
yield I+'%s* wobj = new %s;' % (wname, wname)
528541
if enable_instance_dict:

0 commit comments

Comments
 (0)