Skip to content

Helpers for getting references to singletons #91927

Closed as not planned
Closed as not planned
@hvenev

Description

@hvenev

Feature or enhancement

There are lots of places where a new reference to a singleton is needed. This is done in a variety of ways:

1.  obj = Py_NewRef(Py_None);

2.  obj = Py_None;
    Py_INCREF(obj);

3.  Py_INCREF(Py_None);
    obj = Py_None;

4.  obj = Py_None;
    Py_INCREF(Py_None);

5.  obj = (Py_INCREF(Py_None), Py_None);

The proposal is to add another way that can hopefully become idiomatic:

6.  obj = Py_RefNone();

The following singletons are considered:

  • None
  • NotImplemented
  • Ellipsis
  • False
  • True

In the internal API, helpers for the following singletons can be added:

  • The strings returned by _Py_ID and _Py_STR
  • The integers 0 and 1

Pitch

One advantage of using the new helpers is that it abstracts away the necessity to increment the reference count if immortal instances are implemented.

Alternatively, if immortal instances aren't implemented and all singletons are made per-interpreter, it will slightly reduce the performance overhead by making sure that the singletons are only looked up once (compared to patterns 3, 4, and 5, where the singletons are obtained twice).

Previous discussion

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions