Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Changelog
=========

* 2022-11-15: Add experimental operations to the upgrade_pythoncapi script:
``Py_INCREF_return``, ``Py_INCREF_assign``, ``Py_CLEAR`` and ``Py_SETREF``.
``Py_NewRef``, ``Py_CLEAR`` and ``Py_SETREF``.
* 2022-11-09: Fix ``Py_SETREF()`` and ``Py_XSETREF()`` macros
for `gh-98724 <https://github.com/python/cpython/issues/98724>`_.
* 2022-11-04: Add ``PyFrame_GetVar()`` and ``PyFrame_GetVarString()``
Expand Down
5 changes: 1 addition & 4 deletions docs/upgrade.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,9 @@ Example: ``-o all,Py_SETREF``.

Experimental operations:

* ``Py_INCREF_return``:
* ``Py_NewRef``:

* Replace ``Py_INCREF(res); return res;`` with ``return Py_NewRef(res);``

* ``Py_INCREF_assign``:

* Replace ``x = y; Py_INCREF(x);`` with ``x = Py_NewRef(y);``
* Replace ``x = y; Py_INCREF(y);`` with ``x = Py_NewRef(y);``
* Replace ``Py_INCREF(y); x = y;`` with ``x = Py_NewRef(y);``
Expand Down
4 changes: 2 additions & 2 deletions tests/test_upgrade_pythoncapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def test_pythreadstate_getframe(self):
{ return _PyThreadState_GetFrameBorrow(tstate); }
""")

def test_py_incref_return(self):
def test_py_newref_return(self):
self.check_replace("""
PyObject* new_ref(PyObject *obj) {
Py_INCREF(obj);
Expand Down Expand Up @@ -448,7 +448,7 @@ def test_py_incref_return(self):
}
""")

def test_py_incref_assign(self):
def test_py_newref(self):
# INCREF, assign
self.check_replace("""
void set_attr(MyStruct *obj, PyObject *value, int test)
Expand Down
22 changes: 7 additions & 15 deletions upgrade_pythoncapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ class PyThreadState_GetFrame(Operation):
NEED_PYTHONCAPI_COMPAT = (MIN_PYTHON < (3, 9))


class Py_INCREF_return(Operation):
NAME = "Py_INCREF_return"
class Py_NewRef(Operation):
NAME = "Py_NewRef"
REPLACE = (
# "Py_INCREF(x); return x;" => "return Py_NewRef(x);"
# "Py_XINCREF(x); return x;" => "return Py_XNewRef(x);"
Expand All @@ -266,17 +266,11 @@ class Py_INCREF_return(Operation):
+ fr'return {OPT_CAST_REGEX}\2;',
re.MULTILINE),
r'return Py_\1NewRef(\2);'),
)
# Need Py_NewRef(): new in Python 3.10
NEED_PYTHONCAPI_COMPAT = (MIN_PYTHON < (3, 10))

# "Py_INCREF(x); y = x;" must be replaced before
# "y = x; Py_INCREF(y);", to not miss consecutive
# "Py_INCREF; assign; Py_INCREF; assign; ..." (see unit tests).

class Py_INCREF_assign(Operation):
NAME = "Py_INCREF_assign"
# "Py_INCREF(x); y = x;" must be replaced before "y = x; Py_INCREF(y);",
# to not miss consecutive "Py_INCREF; assign; Py_INCREF; assign; ..."
# (see unit tests)
REPLACE = (
# "Py_INCREF(x); y = x;" => "y = Py_NewRef(x)"
# "Py_XINCREF(x); y = x;" => "y = Py_XNewRef(x)"
# The two statements must have the same indentation, otherwise the
Expand Down Expand Up @@ -488,15 +482,13 @@ def replace2(regs):
PyThreadState_GetFrame,

# Code style: excluded from "all"
Py_INCREF_return,
Py_INCREF_assign,
Py_NewRef,
Py_CLEAR,
Py_SETREF,
)

EXCLUDE_FROM_ALL = (
Py_INCREF_return,
Py_INCREF_assign,
Py_NewRef,
Py_CLEAR,
Py_SETREF,
)
Expand Down