Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd12654a

Browse files
committed
Address Steve's review
1 parent73ac254 commitd12654a

File tree

3 files changed

+20
-34
lines changed

3 files changed

+20
-34
lines changed

‎Doc/c-api/sys.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,12 @@ accessible to C code. They all work with the current interpreter thread's
296296
If any hooks have been added, *format* and other arguments will be used
297297
to construct a tuple to pass. Apart from ``N``, the same format characters
298298
as used in:c:func:`Py_BuildValue` are available. If the built value is not
299-
a tuple, it will be added into a single-element tuple. The ``N`` format
300-
must not be used in *format*.
299+
a tuple, it will be added into a single-element tuple.
300+
301+
:exc:`ValueError` is raised if the ``N`` format is used in *format*. The
302+
``N`` format option consumes a reference, but since there is no way to know
303+
whether arguments to this function will be consumed, using it may cause
304+
reference leaks.
301305
302306
Note that ``#`` format characters should always be treated as
303307
:c:type:`Py_ssize_t`, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined.

‎Programs/_testembed.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,35 +1278,14 @@ static int _test_audit(Py_ssize_t setValue)
12781278
printf("Set event failed");
12791279
return4;
12801280
}
1281+
assert(!PyErr_Occurred());
12811282

12821283
if (sawSet!=42) {
12831284
printf("Failed to see *userData change\n");
12841285
return5;
12851286
}
12861287

1287-
// event argument must not be NULL (format can be NULL)
1288-
assert(!PyErr_Occurred());
1289-
assert(PySys_Audit(NULL,NULL)==-1);
1290-
assert(PyErr_ExceptionMatches(PyExc_ValueError));
1291-
PyErr_Clear();
1292-
1293-
// 'N' must not be used in the format
1294-
PyObject*arg=PyUnicode_FromString("arg");
1295-
if (arg==NULL) {
1296-
gotoerror;
1297-
}
1298-
Py_ssize_trefcnt=Py_REFCNT(arg);
1299-
assert(PySys_Audit("_testembed.raise","N",arg)==-1);
1300-
assert(PyErr_ExceptionMatches(PyExc_ValueError));
1301-
PyErr_Clear();
1302-
assert(Py_REFCNT(arg)==refcnt);
1303-
Py_DECREF(arg);
1304-
13051288
return0;
1306-
1307-
error:
1308-
PyErr_Print();
1309-
return1;
13101289
}
13111290

13121291
staticinttest_audit(void)

‎Python/sysmodule.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ static int
189189
sys_audit_tstate(PyThreadState*ts,constchar*event,
190190
constchar*argFormat,va_listvargs)
191191
{
192+
// In release mode, event and argFormat are only tested if a hook is
193+
// installed to reduce the audit performance overhead.
194+
assert(event!=NULL);
195+
assert(!argFormat|| !strchr(argFormat,'N'));
196+
192197
if (!ts) {
193198
/* Audit hooks cannot be called with a NULL thread state */
194199
return0;
@@ -201,10 +206,8 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
201206
/* Early exit when no hooks are registered */
202207
PyInterpreterState*is=ts->interp;
203208
if (!should_audit(is)) {
204-
// Validate arguments using assertions, only impact peformance
205-
// of Python built in debug mode.
206-
assert(event!=NULL);
207-
assert(!argFormat|| !strchr(argFormat,'N'));
209+
// Don't check event and argFormat if no hook is installed
210+
// to reduce the performance overhead of audit.
208211
return0;
209212
}
210213

@@ -215,8 +218,7 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
215218
}
216219

217220
/* N format is inappropriate, because you do not know
218-
whether the reference is consumed by the call.
219-
Assert rather than exception for perf reasons */
221+
whether the reference is consumed by the call. */
220222
if (argFormat!=NULL&&strchr(argFormat,'N')) {
221223
_PyErr_SetString(ts,PyExc_ValueError,
222224
"N format must not be used in the format argument");
@@ -538,11 +540,12 @@ sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
538540
returnNULL;
539541
}
540542

543+
// In release mode, event and argFormat are only tested if a hook is
544+
// installed to reduce the audit performance overhead.
545+
assert(args[0]!=NULL);
546+
assert(PyUnicode_Check(args[0]));
547+
541548
if (!should_audit(tstate->interp)) {
542-
// Validate first argument using assertions, only impact peformance
543-
// of Python built in debug mode.
544-
assert(args[0]!=NULL);
545-
assert(PyUnicode_Check(args[0]));
546549
Py_RETURN_NONE;
547550
}
548551

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp