GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-139852: Add PyObject_GetDictPtr() function by vstinner · Pull Request #139854 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Oct 9, 2025 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: [C API] Add PyObject_GetDict() function #139852

📚 Documentation preview 📚: https://cpython-previews--139854.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
pythongh-139852: Add PyObject_GetDictPtr() function
ad723c6
@vstinner
vstinner requested review from AA-Turner and ZeroIntensity as code owners October 9, 2025 14:51
@vstinner vstinner added type-feature A feature request or enhancement interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-C-API labels Oct 9, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Oct 9, 2025
[C API] Add PyObject_GetDict() function #139852
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Oct 9, 2025
@vstinner

vstinner commented Oct 9, 2025

Copy link
Copy Markdown
Member Author

Check labels / Unresolved review (pull_request): Failing after 7s

The job fails with:

Error: Label error. Requires exactly 1 of: awaiting merge. Found: type-feature, awaiting core review, interpreter-core, topic-C-API

I don't understand this error. Let me try to close/reopen the PR to try to repair the CI.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner closed this Oct 9, 2025
@vstinner vstinner reopened this Oct 9, 2025
@vstinner vstinner added the awaiting merge label Oct 9, 2025
@vstinner

vstinner commented Oct 9, 2025

Copy link
Copy Markdown
Member Author

I added the awaiting merge label to work around the CI issue.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Oct 10, 2025

Copy link
Copy Markdown
Member

Can those users switch to PyObject_GenericGetDict, or be better served by a variant of PyObject_GenericGetDict that doesn't raise for the “no __dict__” case?
Do they need a PyObject**?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

encukou
encukou reviewed Oct 10, 2025
View reviewed changes
Comment thread Doc/c-api/object.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

vstinner added 3 commits October 10, 2025 10:33
@vstinner
Deprecate _PyObject_GetDictPtr() function
20ac672
@vstinner
Doc: Add seealso PyObject_GenericGetDict()
4f2f2ff
@vstinner
seealso PyObject_GenericSetDict()
17612b4
@vstinner

vstinner commented Oct 10, 2025

Copy link
Copy Markdown
Member Author

Can those users switch to PyObject_GenericGetDict, or be better served by a variant of PyObject_GenericGetDict that doesn't raise for the “no __dict__” case? Do they need a PyObject**?

It seems like PyObject** is needed.

SWIG generates code which writes into the PyObject **dict_ptr:

#if !defined(SWIG_PYTHON_SLOW_GETSET_THIS)
      PyObject **dictptr = _PyObject_GetDictPtr(inst);
      if (dictptr != NULL) {
        PyObject *dict = *dictptr;
        if (dict == NULL) {
          dict = PyDict_New();
          *dictptr = dict;
        }
        if (dict) {
          PyDict_SetItem(dict, SWIG_This(), swig_this);
        } else{
          Py_DECREF(inst);
          inst = 0;
        }
      }
#else
      if (PyObject_SetAttr(inst, SWIG_This(), swig_this) == -1) {
        Py_DECREF(inst);
        inst = 0;
      }
#endif

pybind11 also writes into the PyObject **dict_ptr:

    PyObject **dict_ptr = _PyObject_GetDictPtr(self);
    if (dict_ptr) {
        Py_CLEAR(*dict_ptr);
    }

Obviously, there are other usages which only read the dictionary. Example from pybind11:

    PyObject *&dict = *_PyObject_GetDictPtr(self);
    Py_VISIT(dict);
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 10, 2025

Copy link
Copy Markdown
Member

I added the awaiting merge label to work around the CI issue.

That comes up because of the type-feature label, because of the plan to make core review required for new features, but I don't know what happened to that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Oct 10, 2025
View reviewed changes
Comment thread Objects/object.c
@@ -1550,18 +1571,12 @@ _PyObject_ComputedDictPointer(PyObject *obj)
PyObject **
_PyObject_GetDictPtr(PyObject *obj)

@ZeroIntensity ZeroIntensity Oct 10, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be deprecated?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@colesbury

colesbury commented Oct 10, 2025

Copy link
Copy Markdown
Contributor

SWIG generates code which writes into the PyObject **dict_ptr:

I don't think Swig uses that code path for Python 3:

https://github.com/swig/swig/blob/df3eec20fa56d43bfc0614bc795b39d51964b894/Lib/python/pyrun.swg#L1407-L1412

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Oct 11, 2025

Copy link
Copy Markdown
Member Author

Making this API a public function seems to be a bad idea: #139852 (comment). I prefer to close this PR for now.

I proposed #139968 to add PyObject_GetDict() function instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner closed this Oct 11, 2025
@vstinner
vstinner deleted the object_dictptr branch December 3, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou left review comments
@ZeroIntensity ZeroIntensity ZeroIntensity left review comments
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review awaiting merge interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-C-API type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@vstinner @encukou @ZeroIntensity @colesbury
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.