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-101291: Refactor the `PyLongObject` struct by markshannon · Pull Request #101292 · python/cpython

Conversation

@markshannon

@markshannon markshannon commented Jan 24, 2023 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

There is no change to the implementation, apart from the refactoring of the struct. Even the binary layout remains the same.

This is the first step in improving int performance. The real changes will come later, but this PR will help keep later PR small and manageable.

  • Issue: Restore (or beat) Python 2 performance for arithmetic operations on ints that fit into a single word #101291

Sorry, something went wrong.

Uh oh!

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

avloss reacted with eyes emoji
All reactions
  • 👀 1 reaction
markshannon added 3 commits January 24, 2023 17:11
@markshannon
Refactor PyLongObject into object header and PyLongValue struct.
0ea6bff
@markshannon
Add news
c53a674
@markshannon
Restore lost comment
e668f2b
@markshannon
markshannon requested a review from mdickinson January 24, 2023 17:23
@markshannon
markshannon requested review from rhettinger and tiran as code owners January 24, 2023 17:23
@bedevere-bot bedevere-bot added the awaiting core review label Jan 24, 2023
@bedevere-bot bedevere-bot mentioned this pull request Jan 24, 2023
Restore (or beat) Python 2 performance for arithmetic operations on ints that fit into a single word #101291
Closed
mdickinson
mdickinson approved these changes Jan 29, 2023
View reviewed changes

@mdickinson mdickinson 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.

Changes seem fine to me, though it's a little bit disturbing to see how much the longobject internals have leaked into the rest of the codebase.

Have we looked into the possible impact of these changes on 3rd party libraries? Most shouldn't care, but I'd imagine it'll affect Cython at least, and possibly also things like NumPy and gmpy2.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Jan 29, 2023
@markshannon

markshannon commented Jan 29, 2023

Copy link
Copy Markdown
Member Author

Changes seem fine to me, though it's a little bit disturbing to see how much the longobject internals have leaked into the rest of the codebase.

It is, but at least it gives us a guide for what API functions we need to add.

Have we looked into the possible impact of these changes on 3rd party libraries? Most shouldn't care, but I'd imagine it'll affect Cython at least, and possibly also things like NumPy and gmpy2.

I guess we need to clearly document the changes and recommended API functions.
I will do that in the next PR.

mdickinson reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@markshannon
markshannon merged commit c1b1f51 into python:main Jan 30, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label Jan 30, 2023
mdboom pushed a commit to mdboom/cpython that referenced this pull request Jan 31, 2023
@markshannon @mdboom
pythonGH-101291: Refactor the PyLongObject struct into object heade…
4874c33
…r and PyLongValue struct. (pythonGH-101292)
@tacaswell tacaswell mentioned this pull request Feb 3, 2023
incompatibility with cpython main branch cython/cython#5238
Closed
@tacaswell

tacaswell commented Feb 3, 2023

Copy link
Copy Markdown
Contributor

but I'd imagine it'll affect Cython at least,

Does indeed hit cython! Can not tell if it hits numpy independent of cython because the cythonized files fail to build.

All reactions

Sorry, something went wrong.

Uh oh!

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

This was referenced Feb 7, 2023
gh-90370: Avoid temporary varargs tuple creation in argument passing #30312
Closed
doctest GitHub Actions job exits with 2 failing all recent PRs #101639
Closed
scoder added a commit to scoder/cython that referenced this pull request Feb 13, 2023
@scoder
Fix PyLong digits access in CPython 3.12.
b4c7d46
Needed after python/cpython#101292

See cython#5238
scoder added a commit to scoder/cython that referenced this pull request Feb 13, 2023
@scoder
Fix PyLong digits access in CPython 3.12.
9b7c357
Needed after python/cpython#101292

See cython#5238
scoder added a commit to cython/cython that referenced this pull request Feb 13, 2023
@scoder
Fix PyLong digits access in CPython 3.12.
1c3a212
Needed after python/cpython#101292

See #5238
This was referenced Mar 1, 2023
Python 3.12: ‘PyLongObject’ {aka ‘struct _longobject’} has no member named ‘ob_digit’ aio-libs/yarl#829
Closed
Bump coverage from 7.1.0 to 7.2.1 python/blurb_it#330
Merged
Python 3.12: ‘PyLongObject’ {aka ‘struct _longobject’} has no member named ‘ob_digit’ aio-libs/aiohttp#7229
Closed
Can't install frozenlist on Python 3.12 aio-libs/frozenlist#433
Closed
@vkrizan vkrizan mentioned this pull request May 3, 2023
Python 3.12: error: PyLongObject {aka struct _longobject} has no member named ob_digit coleifer/peewee#2720
Closed
@encukou

encukou commented Jul 3, 2023

Copy link
Copy Markdown
Member

I guess we need to clearly document the changes and recommended API functions.
I will do that in the next PR.

Did you get around to this? I can't see it in What's New.

All reactions

Sorry, something went wrong.

Uh oh!

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

This was referenced Jul 3, 2023
Incompatibility with Python 3.12 fplll/fpylll#246
Closed
Incompatibility with Python 3.12 sagemath/cypari2#137
Closed
@markshannon
markshannon deleted the split-long branch September 26, 2023 12:55
@sevmonster sevmonster mentioned this pull request May 30, 2024
Tight version constraints on dependencies HLFH/automua#15
Open
MartinNowak added a commit to 7Learnings/ssh-python that referenced this pull request Jun 25, 2024
@MartinNowak
require cython to compile extension
ec9653a
- generated c files are outdated due to [GH-101291](python/cpython#101292)
EliusSolis added a commit to EliusSolis/charm that referenced this pull request Jul 28, 2024
@EliusSolis
Fixed PEP 674 and python/cpython#101292
32edcc3
@EliusSolis EliusSolis mentioned this pull request Jul 31, 2024
Update for python 3.12 JHUISI/charm#310
Merged
MartinNowak added a commit to 7Learnings/ssh-python that referenced this pull request Dec 26, 2024
@MartinNowak
require cython to compile extension
9fb9c41
- generated c files are outdated due to [GH-101291](python/cpython#101292)
MartinNowak added a commit to 7Learnings/ssh-python that referenced this pull request Dec 27, 2024
@MartinNowak
require cython to compile extension
8eb4bf5
- generated c files are outdated due to [GH-101291](python/cpython#101292)
MartinNowak added a commit to 7Learnings/ssh2-python that referenced this pull request Dec 27, 2024
@MartinNowak
require cython to compile extension
423947a
- generated c files are outdated due to [GH-101291](python/cpython#101292)
jakinyele pushed a commit to JHUISI/charm that referenced this pull request Jan 23, 2025
@EliusSolis @jakinyele
Fixed PEP 674 and python/cpython#101292
5fbd092
@Amodio Amodio mentioned this pull request Dec 8, 2025
Unable to install gensim with Python 3.12 (on Alpine) piskvorky/gensim#3643
Open
hubot pushed a commit to gcc-mirror/gcc that referenced this pull request Mar 7, 2026
@davidmalcolm
testsuite: fix ICEs in analyzer plugin with CPython >= 3.11 [PR107646…
c2c64cf
…,PR112520]

In GCC 14 the testsuite gained a plugin that "teaches" the analyzer
about the CPython API, trying for find common mistakes:
  https://gcc.gnu.org/wiki/StaticAnalyzer/CPython

Unfortunately, this has been crashing for more recent versions of
CPython.

Specifically, in Python 3.11,  PyObject's ob_refcnt was moved to an
anonymous union (as part of PEP 683 "Immortal Objects, Using a Fixed
Refcount").  The plugin attempts to find the field but fails, but has
no error-handling, leading to a null pointer dereference.

Also, python/cpython#101292 moved the "ob_digit"
from struct _longobject to a new field long_value of a new
struct _PyLongValue, leading to similar analyzer crashes when not
finding the field.

The following patch fixes this by
* looking within the anonymous union for the ob_refcnt field if it can't
find it directly
* gracefully handling the case of not finding "ob_digit" in PyLongObject
* doing more lookups once at plugin startup, rather than continuously on
analyzing API calls
* adding diagnostics and more error-handling to the plugin startup, so that
if it can't find something in the Python headers it emits a useful note
when disabling itself, e.g.
  cc1: note: could not find field 'ob_digit' of CPython type 'PyLongObject' {aka 'struct _longobject'}
* replacing some copy-and-pasted code with member functions of a new
"class api" (though various other cleanups could be done)

Tested with:
* CPython 3.8: all tests continue to PASS
* CPython 3.13: fixes the ICEs, 2 FAILs remain (reference counting false
negatives)

Given that this is already a large patch, I'm opting to only fix the
crashes and defer the 2 remainings FAILs and other cleanups to followup
work.

gcc/analyzer/ChangeLog:
	PR testsuite/112520
	* region-model-manager.cc
	(region_model_manager::get_field_region): Assert that the args are non-null.

gcc/testsuite/ChangeLog:
	PR analyzer/107646
	PR testsuite/112520
	* gcc.dg/plugin/analyzer_cpython_plugin.cc: Move everything from
	namespace ana:: into ana::cpython_plugin.  Move global tree values
	into a new "class api".
	(pyobj_record): Replace with api.m_type_PyObject.
	(pyobj_ptr_tree): Replace with api.m_type_PyObject_ptr.
	(pyobj_ptr_ptr): Replace with  api.m_type_PyObject_ptr_ptr.
	(varobj_record): Replace with api.m_type_PyVarObject.
	(pylistobj_record): Replace with api.m_type_PyListObject.
	(pylongobj_record): Replace with api.m_type_PyLongObject.
	(pylongtype_vardecl): Replace with api.m_vardecl_PyLong_Type.
	(pylisttype_vardecl): Replace with api.m_vardecl_PyList_Type.
	(get_field_by_name): Add "complain" param and use it to issue a
	note on failure.  Assert that type and	name are non-null.  Don't
	crash on fields that are anonymous unions, and special-case
	looking within them for "ob_refcnt" to	work around the
	Python 3.11 change for PEP 683 (immortal objects).
	(get_sizeof_pyobjptr): Convert to...
	(api::get_sval_sizeof_PyObject_ptr): ...this
	(init_ob_refcnt_field): Convert to...
	(api::init_ob_refcnt_field): ...this.
	(set_ob_type_field): Convert to...
	(api::set_ob_type_field): ..this.
	(api::init_PyObject_HEAD): New.
	(api::get_region_PyObject_ob_refcnt): New.
	(api::do_Py_INCREF): New.
	(api::get_region_PyVarObject_ob_size): New.
	(api::get_region_PyLongObject_ob_digit): New.
	(inc_field_val): Convert to...
	(api::inc_field_val): ...this.
	(refcnt_mismatch::refcnt_mismatch): Add tree params for refcounts
	and initialize corresponding fields.  Fix whitespace.
	(refcnt_mismatch::emit): Use stored tree values, rather than
	assuming we have constants, and crashing non-constants.  Delete
	commented-out dead code.
	(refcnt_mismatch::foo): Delete.
	(refcnt_mismatch::m_expected_refcnt_tree): New field.
	(refcnt_mismatch::m_actual_refcnt_tree): New field.
	(retrieve_ob_refcnt_sval): Simplify using class api.
	(count_pyobj_references): Likewise.
	(check_refcnt): Likewise.  Don't warn on UNKNOWN values.  Use
	get_representative_tree for the expected and actual values and
	skip the warning if it fails, rather than assuming we have
	constants and crashing on non-constants.
	(count_all_references): Update comment.
	(kf_PyList_Append::impl_call_pre): Simplify using class api.
	(kf_PyList_Append::impl_call_post): Likewise.
	(kf_PyList_New::impl_call_post): Likewise.
	(kf_PyLong_FromLong::impl_call_post): Likewise.
	(get_stashed_type_by_name): Emit note if the type couldn't be
	found.
	(get_stashed_global_var_by_name): Likewise for globals.
	(init_py_structs): Convert to...
	(api::init_from_stashed_types): ...this.  Bail out with an error
	code if anything fails.  Look up more things at startup, rather
	than during analysis of calls.
	(ana::cpython_analyzer_events_subscriber): Rename to...
	(ana::cpython_plugin::analyzer_events_subscriber): ...this.
	(analyzer_events_subscriber::analyzer_events_subscriber):
	Initialize m_init_failed.
	(analyzer_events_subscriber::on_message<on_tu_finished>):
	Update for conversion of init_py_structs to
	api::init_from_stashed_types and bail if it fails.
	(analyzer_events_subscriber::on_message<on_frame_popped): Don't
	run if plugin initialization failed.
	(analyzer_events_subscriber::m_init_failed): New field.

Signed-off-by: David Malcolm <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rhettinger rhettinger Awaiting requested review from rhettinger
@tiran tiran Awaiting requested review from tiran
1 more reviewer
@mdickinson mdickinson mdickinson approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

5 participants

@markshannon @tacaswell @encukou @mdickinson @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.