8000 Rename Py_INCREF_assign to Py_NewRef (#58) · python/pythoncapi-compat@ee57e99 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee57e99

Browse files
authored
Rename Py_INCREF_assign to Py_NewRef (#58)
Rename Py_INCREF_assign and Py_INCREF_return to Py_NewRef.
1 parent 8ebf997 commit ee57e99

File tree

4 files changed

+11
-22
lines changed

4 files changed

+11
-22
lines changed

docs/changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Changelog
22
=========
33

44
* 2022-11-15: Add experimental operations to the upgrade_pythoncapi script:
5-
``Py_INCREF_return``, ``Py_INCREF_assign``, ``Py_CLEAR`` and ``Py_SETREF``.
5+
``Py_NewRef``, ``Py_CLEAR`` and ``Py_SETREF``.
66
* 2022-11-09: Fix ``Py_SETREF()`` and ``Py_XSETREF()`` macros
77
for `gh-98724 <https://github.com/python/cpython/issues/98724>`_.
88
* 2022-11-04: Add ``PyFrame_GetVar()`` and ``PyFrame_GetVarString()``

docs/upgrade.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,9 @@ Example: ``-o all,Py_SETREF``.
151151

152152
Experimental operations:
153153

154-
* ``Py_INCREF_return``:
154+
* ``Py_NewRef``:
155155

156156
* Replace ``Py_INCREF(res); return res;`` with ``return Py_NewRef(res);``
157-
158-
* ``Py_INCREF_assign``:
159-
160157
* Replace ``x = y; Py_INCREF(x);`` with ``x = Py_NewRef(y);``
161158
* Replace ``x = y; Py_INCREF(y);`` with ``x = Py_NewRef(y);``
162159
* Replace ``Py_INCREF(y); x = y;`` with ``x = Py_NewRef(y);``

tests/test_upgrade_pythoncapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def test_pythreadstate_getframe(self):
408408
{ return _PyThreadState_GetFrameBorrow(tstate); }
409409
""")
410410

411-
def test_py_incref_return(self):
411+
def test_py_newref_return(self):
412412
self.check_replace("""
413413
PyObject* new_ref(PyObject *obj) {
414414
Py_INCREF(obj);
@@ -448,7 +448,7 @@ def test_py_incref_return(self):
448448
}
449449
""")
450450

451-
def test_py_incref_assign(self):
451+
def test_py_newref(self):
452452
# INCREF, assign
453453
self.check_replace("""
454454
void set_attr(MyStruct *obj, PyObject *value, int test)

upgrade_pythoncapi.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ class PyThreadState_GetFrame(Operation):
245245
NEED_PYTHONCAPI_COMPAT = (MIN_PYTHON < (3, 9))
246246

247247

248-
class Py_INCREF_return(Operation):
249-
NAME = "Py_INCREF_return"
248+
class Py_NewRef(Operation):
249+
NAME = "Py_NewRef"
250250
REPLACE = (
251251
# "Py_INCREF(x); return x;" => "return Py_NewRef(x);"
252252
# "Py_XINCREF(x); return x;" => "return Py_XNewRef(x);"
@@ -266,17 +266,11 @@ class Py_INCREF_return(Operation):
266266
+ fr'return {OPT_CAST_REGEX}\2;',
267267
re.MULTILINE),
268268
r'return Py_\1NewRef(\2);'),
269-
)
270-
# Need Py_NewRef(): new in Python 3.10
271-
NEED_PYTHONCAPI_COMPAT = (MIN_PYTHON < (3, 10))
272269

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

274-
class Py_INCREF_assign(Operation):
275-
NAME = "Py_INCREF_assign"
276-
# "Py_INCREF(x); y = x;" must be replaced before "y = x; Py_INCREF(y);",
277-
# to not miss consecutive "Py_INCREF; assign; Py_INCREF; assign; ..."
278-
# (see unit tests)
279-
REPLACE = (
280274
# "Py_INCREF(x); y = x;" => "y = Py_NewRef(x)"
281275
# "Py_XINCREF(x); y = x;" => "y = Py_XNewRef(x)"
282276
# The two statements must have the same indentation, otherwise the
@@ -488,15 +482,13 @@ def replace2(regs):
488482
PyThreadState_GetFrame,
489483

490484
# Code style: excluded from "all"
491-
Py_INCREF_return,
492-
Py_INCREF_assign,
485+
Py_NewRef,
493486
Py_CLEAR,
494487
Py_SETREF,
495488
)
496489

497490
EXCLUDE_FROM_ALL = (
498-
Py_INCREF_return,
499-
Py_INCREF_assign,
491+
Py_NewRef,
500492
Py_CLEAR,
501493
Py_SETREF,
502494
)

0 commit comments

Comments
 (0)
0