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-99574: Add Py_SETREF() to the limited C API by vstinner · Pull Request #99575 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Nov 18, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Add Py_SETREF() and Py_XSETREF() macros to the limited C API.

  • Issue: Add Py_SETREF() and Py_XSETREF() macros to the limited C API 3.12 #99574

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot mentioned this pull request Nov 18, 2022
Add Py_SETREF() and Py_XSETREF() macros to the limited C API 3.12 #99574
Closed
@bedevere-bot bedevere-bot added the awaiting core review label Nov 18, 2022
@vstinner

vstinner commented Nov 18, 2022

Copy link
Copy Markdown
Member Author

cc @encukou @serhiy-storchaka @erlend-aasland

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner
vstinner marked this pull request as ready for review November 18, 2022 12:23
@encukou

encukou commented Nov 21, 2022

Copy link
Copy Markdown
Member

This adds much more than Py_SETREF() and Py_XSETREF().
Could you add comments about all the added functions, especially those added for earlier versions? Preferably in a different issue/PR, so it's not misleading.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Nov 21, 2022

Copy link
Copy Markdown
Member Author

This adds much more than Py_SETREF() and Py_XSETREF(). Could you add comments about all the added functions, especially those added for earlier versions? Preferably in a different issue/PR, so it's not misleading.

In terms of API, only Py_SETREF() and Py_XSETREF() are added to the limited C API. But while I was updating Misc/stable_abi.toml, I noticed that many macros were already part of the limited C API but not documented as being part of it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Nov 21, 2022

Copy link
Copy Markdown
Member Author

I updated the commit message (and PR description) to mention that the change also adds macros missing in Misc/stable_abi.toml but already part of the limited C API.

All reactions

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Nov 21, 2022

Copy link
Copy Markdown
Member

Py_LIMITED_API does not define what the limited API is.
Please allow more discussion on the retroactive additions. And add comments in the source.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Nov 21, 2022

Copy link
Copy Markdown
Member Author

Py_LIMITED_API does not define what the limited API is.

I don't understand that. Is there a definition of what is the limited C API in this case? What is Py_LIMITED_API if it doesn't define the limited C API?

All reactions

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Nov 21, 2022

Copy link
Copy Markdown
Member

Misc/stable_abi.toml defines it. Py_LIMITED_API should follow Misc/stable_abi.toml, but that's not always possible (and there might be bugs).
We can decide the definition is wrong and change it retroactively, but that should be a deliberate change.

All reactions

Sorry, something went wrong.

Uh oh!

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

encukou
encukou reviewed Nov 21, 2022
View reviewed changes
Comment thread Misc/stable_abi.toml Outdated
Comment on lines +380 to +383
[macro.Py_REFCNT]
added = '3.2'
[macro.Py_SET_REFCNT]
added = '3.9'

@encukou encukou Nov 21, 2022

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.

I really don't think we want to add these.

Sorry, something went wrong.

Uh oh!

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

All reactions
@vstinner

vstinner commented Nov 21, 2022

Copy link
Copy Markdown
Member Author

Misc/stable_abi.toml defines it. Py_LIMITED_API should follow Misc/stable_abi.toml, but that's not always possible (and there might be bugs).

Ok, now I'm confused. Py_LIMITED_API is documented as:

Define this macro before including Python.h to opt in to only use the Limited API

When I define this macro, the Py_TYPE() macro is available. But Py_TYPE() is not part of Misc/stable_abi.toml. So something is wrong: either Py_TYPE() should be added to Misc/stable_abi.toml, to Py_TYPE() must not defined if Py_LIMITED_API is defined.

For example module Modules/xxlimited.c uses Py_TYPE():

static int
Xxo_traverse(PyObject *self_obj, visitproc visit, void *arg)
{
    // Visit the type
    Py_VISIT(Py_TYPE(self_obj));
    ...
}

This code is even required to define heap types:

Heap-allocated types are expected to visit Py_TYPE(self) in tp_traverse.

This is just one example. I'm trying to understand what is the "limited C API" "according to you" :-) (Sadly, it seems like different people have a different definition.)

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Nov 21, 2022

Copy link
Copy Markdown
Member Author

[macro.Py_REFCNT]
[macro.Py_SET_REFCNT]
I really don't think we want to add these.

PEP 384 explicitly gives access to object type and reference count, by giving access to PyObject.ob_refcnt and PyObject.ob_type. But accessing directly structure members is bad for future Python evolutions. The "nogil" project is one concrete example: see the nogil section of PEP 674.

IMO using an abstraction, Py_REFCNT() and Py_SET_REFCNT(), is better than giving access to the structure members. IMO these macros are already part of the limited C API, since they are accessible with the Py_LIMITED_API macro is defined, but it seems like we have a different view on what is the limited C API.

All reactions

Sorry, something went wrong.

Uh oh!

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

serhiy-storchaka
serhiy-storchaka reviewed Nov 21, 2022
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

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.

Please add

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000

Sorry, something went wrong.

Uh oh!

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

All reactions
@encukou

encukou commented Nov 21, 2022

Copy link
Copy Markdown
Member

Ok, now I'm confused. Py_LIMITED_API is documented as:

Define this macro before including Python.h to opt in to only use the Limited API

That's the intention. But, there are always bugs -- wrong Py_LIMITED_API guards, or wrong Misc/stable_abi.toml. And there are technical issues, like exposing underscored names.

What makes Py_LIMITED_API work badly as the definition is that you can't easily check what it exposes on all the different builds/platforms.

When I define this macro, the Py_TYPE() macro is available. But Py_TYPE() is not part of Misc/stable_abi.toml. So something is wrong: either Py_TYPE() should be added to Misc/stable_abi.toml, to Py_TYPE() must not defined if Py_LIMITED_API is defined.

In this case, it looks like Misc/stable_abi.toml is wrong. But in that case, shouldn't the fix be backported to all the supported versions?
It would work better as a separate PR.

This is just one example. I'm trying to understand what is the "limited C API" "according to you" :-) (Sadly, it seems like different people have a different definition.)

See Misc/stable_abi.toml. The file exists to give a simple answer :)
Unfortunately, you can find bugs in Misc/stable_abi.toml, as in any other source.

IMO using an abstraction, Py_REFCNT() and Py_SET_REFCNT(), is better than giving access to the structure members. IMO these macros are already part of the limited C API, since they are accessible with the Py_LIMITED_API macro is defined, but it seems like we have a different view on what is the limited C API.

I see as Misc/stable_abi.toml is the definition. If it's buggy, we should fix it. (We need to be careful that the fix can't break existing code, but for macros that Py_LIMITED_API always allowed, and that work with stable ABI, that shouldn't be an issue.)
A bugfix should be backported.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Nov 21, 2022

Copy link
Copy Markdown
Member Author

I rewrote this PR to restrict it to Py_SETREF() and Py_XSETREF(). I added #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000 as asked by @serhiy-storchaka.

I now agree that adding other macros to Misc/stable_abi.toml should be discussed in separated issues/PRs.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Nov 21, 2022

Copy link
Copy Markdown
Member Author

Once this PR will be merged, I will open separated issues/PRs for other discussed macros:

  • Py_CLEAR()
  • Py_DECREF(), Py_XDECREF()
  • Py_INCREF(), Py_XINCREF()
  • Py_REFCNT(), Py_SET_REFCNT()
All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Nov 22, 2022

Copy link
Copy Markdown
Member Author

PR rebased on main to fix a conflict on Misc/stable_abi.toml.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner
gh-99574: Add Py_SETREF() to the limited C API
bf7dc14
Add Py_SETREF() and Py_XSETREF() macros to the limited C API.
@netlify

netlify Bot commented Dec 7, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

✅ Deploy Preview for python-cpython-preview ready!

Name Link
🔨 Latest commit bf7dc14
🔍 Latest deploy log https://app.netlify.com/sites/python-cpython-preview/deploys/6390a2a93409ff0008adde75
😎 Deploy Preview https://deploy-preview-99575--python-cpython-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Dec 7, 2022

Copy link
Copy Markdown
Member Author

I fixed a type punning bug which caused Python to be miscompiled: commit b11a384.

I rebased this PR on top of it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Dec 8, 2022
gh-98724: Fix the Py_CLEAR() macro in the limited C API #100121
Closed
@vstinner

vstinner commented Dec 8, 2022

Copy link
Copy Markdown
Member Author

I fixed a type punning bug which caused Python to be miscompiled: commit b11a384.

Oh. The new Py_CLEAR() implementation uses memcpy() of <string.h>, whereas the limited C API <Python.h> no longer includes <string.h> (since Python 3.11). I proposed PR #100121 to fix Py_CLEAR().

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner closed this Dec 13, 2022
@vstinner
vstinner deleted the limited_setref branch December 13, 2022 15:22
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
@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments

Uh oh!

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

Assignees

No one assigned

Labels

awaiting core review

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 @serhiy-storchaka @bedevere-bot
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.