Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81f68a7

Browse files
Issue #22453: Warn against the use of leaking macro PyObject_REPR().
1 parent 60fe569 commit 81f68a7

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

Include/object.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,12 @@ PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
572572
PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
573573
PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
574574

575-
/* Helper for passing objects to printf and the like */
576-
#define PyObject_REPR(obj) _PyUnicode_AsString(PyObject_Repr(obj))
575+
#ifndef Py_LIMITED_API
576+
/* Helper for passing objects to printf and the like.
577+
Leaks refcounts. Don't use it!
578+
*/
579+
#define PyObject_REPR(obj) PyUnicode_AsUTF8(PyObject_Repr(obj))
580+
#endif
577581

578582
/* Flag bits for printing: */
579583
#define Py_PRINT_RAW 1 /* No string quotes etc. */

Python/compile.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,12 +1412,12 @@ get_ref_type(struct compiler *c, PyObject *name)
14121412
PyOS_snprintf(buf, sizeof(buf),
14131413
"unknown scope for %.100s in %.100s(%s)\n"
14141414
"symbols: %s\nlocals: %s\nglobals: %s",
1415-
PyBytes_AS_STRING(name),
1416-
PyBytes_AS_STRING(c->u->u_name),
1417-
PyObject_REPR(c->u->u_ste->ste_id),
1418-
PyObject_REPR(c->u->u_ste->ste_symbols),
1419-
PyObject_REPR(c->u->u_varnames),
1420-
PyObject_REPR(c->u->u_names)
1415+
PyUnicode_AsUTF8(name),
1416+
PyUnicode_AsUTF8(c->u->u_name),
1417+
PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1418+
PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1419+
PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1420+
PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
14211421
);
14221422
Py_FatalError(buf);
14231423
}
@@ -1474,11 +1474,11 @@ compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyO
14741474
fprintf(stderr,
14751475
"lookup %s in %s %d %d\n"
14761476
"freevars of %s: %s\n",
1477-
PyObject_REPR(name),
1478-
PyBytes_AS_STRING(c->u->u_name),
1477+
PyUnicode_AsUTF8(PyObject_Repr(name)),
1478+
PyUnicode_AsUTF8(c->u->u_name),
14791479
reftype, arg,
1480-
_PyUnicode_AsString(co->co_name),
1481-
PyObject_REPR(co->co_freevars));
1480+
PyUnicode_AsUTF8(co->co_name),
1481+
PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
14821482
Py_FatalError("compiler_make_closure()");
14831483
}
14841484
ADDOP_I(c, LOAD_CLOSURE, arg);

0 commit comments

Comments
 (0)