Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-120642: Move private PyCode APIs to the internal C API #120643

Merged
merged 8 commits into from
Jun 26, 2024

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jun 17, 2024

  • Move _Py_BackoffCounter to pycore_backoff.h.
  • Move _Py_CODEUNIT and related functions to pycore_code.h.
  • Move Include/cpython/optimizer.h content to pycore_optimizer.h.
  • Remove Include/cpython/optimizer.h.

* Move _Py_BackoffCounter to pycore_backoff.h.
* Move _Py_CODEUNIT and related functions to pycore_code.h.
* Move Include/cpython/optimizer.h content to pycore_optimizer.h.
* Remove Include/cpython/optimizer.h.
@vstinner
Copy link
Member Author

@markshannon @iritkatriel @encukou: What do you think of this change?

The main effect is that 6 PyUnstable optimizer functions are moved to the internal C API (pycore_optimizer.h):

  • PyUnstable_Replace_Executor()
  • PyUnstable_SetOptimizer()
  • PyUnstable_GetOptimizer()
  • PyUnstable_GetExecutor()
  • PyUnstable_Optimizer_NewCounter()
  • PyUnstable_Optimizer_NewUOpOptimizer()

An alternative is to modify structures to add names to unions and structures, and then modify functions using it.

@zooba
Copy link
Member

zooba commented Jun 17, 2024

We already went through and added union names to some of these, but I guess whichever developer keeps adding them didn't notice.

Looking at the number of CI failures, possibly adding the names is going to be less disruptive! 😆

@vstinner
Copy link
Member Author

Looking at the number of CI failures, possibly adding the names is going to be less disruptive! 😆

I just forgot to push a local change, I'm ashamed :-(

@vstinner
Copy link
Member Author

We already went through and added union names to some of these, but I guess whichever developer keeps adding them didn't notice.

test_cext and test_cppext are supposed to catch such issue, but we don't suppot "strict ISO C99" for now.

We can update these tests once a decision will be taken: capi-workgroup/decisions#30

Remove PyUnstable_Replace_Executor().

Rename functions:

* PyUnstable_GetExecutor() => _Py_GetExecutor()
* PyUnstable_GetOptimizer() => _Py_GetOptimizer()
* PyUnstable_SetOptimizer() => _Py_SetOptimizerAPI()
* PyUnstable_Optimizer_NewCounter() => _PyOptimizer_NewCounter()
* PyUnstable_Optimizer_NewUOpOptimizer() => _PyOptimizer_NewUOpOptimizer()
@vstinner
Copy link
Member Author

If possible, I would like to backport this change to 3.13 to solve a C99 compatibility issue in the Python C API.

@vstinner vstinner added the needs backport to 3.13 bugs and security fixes label Jun 20, 2024
@Yhg1s
Copy link
Member

Yhg1s commented Jun 26, 2024

Since this is removing (unstable) C API functions added in 3.13 that everyone agrees we should remove, we should backport this to 3.13, yes.

@@ -195,7 +182,7 @@ _Py_SetOptimizer(PyInterpreterState *interp, _PyOptimizerObject *optimizer)
}

int
PyUnstable_SetOptimizer(_PyOptimizerObject *optimizer)
_Py_SetOptimizerAPI(_PyOptimizerObject *optimizer)
Copy link
Member

Choose a reason for hiding this comment

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

Why the API suffix?
Maybe Py_SetTier2Optimizer (to distinguish it from the AST, bytecode or tier 1 optimizers)

Copy link
Member Author

Choose a reason for hiding this comment

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

There is another _Py_SetOptimizer() function with a different API. This one adds more boilerplate code and calls the first one.

Copy link
Member Author

Choose a reason for hiding this comment

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

I renamed the function to _Py_SetTier2Optimizer() (I added an underscore prefix).

static inline _Py_CODEUNIT
_py_make_codeunit(uint8_t opcode, uint8_t oparg)
{
// No designated initialisers because of C++ compat
Copy link
Member

Choose a reason for hiding this comment

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

This is an internal header. Why does it need to be C++ compatible?

Copy link
Member Author

Choose a reason for hiding this comment

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

No idea, I just moved code. For now, I prefer to leave it as it is. I can be reworked later.

@vstinner vstinner merged commit 9e4a81f into python:main Jun 26, 2024
56 checks passed
@vstinner vstinner deleted the internal_code branch June 26, 2024 11:54
@miss-islington-app
Copy link

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app
Copy link

Sorry, @vstinner, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 9e4a81f00fef689c6e18a64245aa064eaadc7ac7 3.13

vstinner added a commit to vstinner/cpython that referenced this pull request Jun 26, 2024
…on#120643)

* Move _Py_CODEUNIT and related functions to pycore_code.h.
* Move _Py_BackoffCounter to pycore_backoff.h.
* Move Include/cpython/optimizer.h content to pycore_optimizer.h.
* Remove Include/cpython/optimizer.h.
* Remove PyUnstable_Replace_Executor().

Rename functions:

* PyUnstable_GetExecutor() => _Py_GetExecutor()
* PyUnstable_GetOptimizer() => _Py_GetOptimizer()
* PyUnstable_SetOptimizer() => _Py_SetTier2Optimizer()
* PyUnstable_Optimizer_NewCounter() => _PyOptimizer_NewCounter()
* PyUnstable_Optimizer_NewUOpOptimizer() => _PyOptimizer_NewUOpOptimizer()

(cherry picked from commit 9e4a81f)
vstinner added a commit to vstinner/cpython that referenced this pull request Jun 26, 2024
…on#120643)

* Move _Py_CODEUNIT and related functions to pycore_code.h.
* Move _Py_BackoffCounter to pycore_backoff.h.
* Move Include/cpython/optimizer.h content to pycore_optimizer.h.
* Remove Include/cpython/optimizer.h.
* Remove PyUnstable_Replace_Executor().

Rename functions:

* PyUnstable_GetExecutor() => _Py_GetExecutor()
* PyUnstable_GetOptimizer() => _Py_GetOptimizer()
* PyUnstable_SetOptimizer() => _Py_SetTier2Optimizer()
* PyUnstable_Optimizer_NewCounter() => _PyOptimizer_NewCounter()
* PyUnstable_Optimizer_NewUOpOptimizer() => _PyOptimizer_NewUOpOptimizer()

(cherry picked from commit 9e4a81f)
@bedevere-app
Copy link

bedevere-app bot commented Jun 26, 2024

GH-121043 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 26, 2024
vstinner added a commit that referenced this pull request Jun 26, 2024
…0643) (#121043)

gh-120642: Move private PyCode APIs to the internal C API (#120643)

* Move _Py_CODEUNIT and related functions to pycore_code.h.
* Move _Py_BackoffCounter to pycore_backoff.h.
* Move Include/cpython/optimizer.h content to pycore_optimizer.h.
* Remove Include/cpython/optimizer.h.
* Remove PyUnstable_Replace_Executor().

Rename functions:

* PyUnstable_GetExecutor() => _Py_GetExecutor()
* PyUnstable_GetOptimizer() => _Py_GetOptimizer()
* PyUnstable_SetOptimizer() => _Py_SetTier2Optimizer()
* PyUnstable_Optimizer_NewCounter() => _PyOptimizer_NewCounter()
* PyUnstable_Optimizer_NewUOpOptimizer() => _PyOptimizer_NewUOpOptimizer()

(cherry picked from commit 9e4a81f)
mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
…on#120643)

* Move _Py_CODEUNIT and related functions to pycore_code.h.
* Move _Py_BackoffCounter to pycore_backoff.h.
* Move Include/cpython/optimizer.h content to pycore_optimizer.h.
* Remove Include/cpython/optimizer.h.
* Remove PyUnstable_Replace_Executor().

Rename functions:

* PyUnstable_GetExecutor() => _Py_GetExecutor()
* PyUnstable_GetOptimizer() => _Py_GetOptimizer()
* PyUnstable_SetOptimizer() => _Py_SetTier2Optimizer()
* PyUnstable_Optimizer_NewCounter() => _PyOptimizer_NewCounter()
* PyUnstable_Optimizer_NewUOpOptimizer() => _PyOptimizer_NewUOpOptimizer()
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
…on#120643)

* Move _Py_CODEUNIT and related functions to pycore_code.h.
* Move _Py_BackoffCounter to pycore_backoff.h.
* Move Include/cpython/optimizer.h content to pycore_optimizer.h.
* Remove Include/cpython/optimizer.h.
* Remove PyUnstable_Replace_Executor().

Rename functions:

* PyUnstable_GetExecutor() => _Py_GetExecutor()
* PyUnstable_GetOptimizer() => _Py_GetOptimizer()
* PyUnstable_SetOptimizer() => _Py_SetTier2Optimizer()
* PyUnstable_Optimizer_NewCounter() => _PyOptimizer_NewCounter()
* PyUnstable_Optimizer_NewUOpOptimizer() => _PyOptimizer_NewUOpOptimizer()
@gvanrossum
Copy link
Member

FYI there are two mention of _PyCode_CODE left in Include/cpython/code.h:

#define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
#define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT))

This seems out of place, since those macros cannot work unless one also includes pycore_code.h.

@vstinner
Copy link
Member Author

FYI there are two mention of _PyCode_CODE left in Include/cpython/code.h

Oh correct, I wrote PR gh-121644 to move these 2 macros to internal C API.

estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
…on#120643)

* Move _Py_CODEUNIT and related functions to pycore_code.h.
* Move _Py_BackoffCounter to pycore_backoff.h.
* Move Include/cpython/optimizer.h content to pycore_optimizer.h.
* Remove Include/cpython/optimizer.h.
* Remove PyUnstable_Replace_Executor().

Rename functions:

* PyUnstable_GetExecutor() => _Py_GetExecutor()
* PyUnstable_GetOptimizer() => _Py_GetOptimizer()
* PyUnstable_SetOptimizer() => _Py_SetTier2Optimizer()
* PyUnstable_Optimizer_NewCounter() => _PyOptimizer_NewCounter()
* PyUnstable_Optimizer_NewUOpOptimizer() => _PyOptimizer_NewUOpOptimizer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants