Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Helpers for getting references to singletons #91927

Copy link
Copy link
@hvenev

Description

@hvenev
Issue body actions

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

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.