From 6e2cc369593a33f65f8db51876f38424637c7ef5 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 19 Jul 2022 19:22:53 +0300 Subject: [PATCH 01/15] Migrated syslog module to Argument Clinic --- ...2-07-19-16-32-52.gh-issue-95011.P5t6l9.rst | 1 + Modules/clinic/syslogmodule.c.h | 228 ++++++++++++++++++ Modules/syslogmodule.c | 162 ++++++++----- 3 files changed, 327 insertions(+), 64 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst create mode 100644 Modules/clinic/syslogmodule.c.h diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst new file mode 100644 index 00000000000000..83a2fadf48eee1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst @@ -0,0 +1 @@ +Migrated :mod:`syslog` to Argument Clinic. Patch by Noam Cohen. diff --git a/Modules/clinic/syslogmodule.c.h b/Modules/clinic/syslogmodule.c.h new file mode 100644 index 00000000000000..e4da3bb67e49e9 --- /dev/null +++ b/Modules/clinic/syslogmodule.c.h @@ -0,0 +1,228 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(syslog_openlog__doc__, +"openlog($module, /, ident=, logoption=0,\n" +" facility=LOG_USER)\n" +"--\n" +"\n" +"Set logging options of subsequent syslog() calls."); + +#define SYSLOG_OPENLOG_METHODDEF \ + {"openlog", _PyCFunction_CAST(syslog_openlog), METH_FASTCALL|METH_KEYWORDS, syslog_openlog__doc__}, + +static PyObject * +syslog_openlog_impl(PyObject *module, PyObject *new_S_ident_o, long logopt, + long facility); + +static PyObject * +syslog_openlog(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"ident", "logoption", "facility", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "openlog", 0}; + PyObject *argsbuf[3]; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; + PyObject *new_S_ident_o = NULL; + long logopt = 0; + long facility = LOG_USER; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 3, 0, argsbuf); + if (!args) { + goto exit; + } + if (!noptargs) { + goto skip_optional_pos; + } + if (args[0]) { + if (!PyUnicode_Check(args[0])) { + _PyArg_BadArgument("openlog", "argument 'ident'", "str", args[0]); + goto exit; + } + if (PyUnicode_READY(args[0]) == -1) { + goto exit; + } + new_S_ident_o = args[0]; + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (args[1]) { + logopt = PyLong_AsLong(args[1]); + if (logopt == -1 && PyErr_Occurred()) { + goto exit; + } + if (!--noptargs) { + goto skip_optional_pos; + } + } + facility = PyLong_AsLong(args[2]); + if (facility == -1 && PyErr_Occurred()) { + goto exit; + } +skip_optional_pos: + return_value = syslog_openlog_impl(module, new_S_ident_o, logopt, facility); + +exit: + return return_value; +} + +PyDoc_STRVAR(syslog_syslog__doc__, +"syslog([priority=LOG_INFO,] message)\n" +"Send the string message to the system logger."); + +#define SYSLOG_SYSLOG_METHODDEF \ + {"syslog", (PyCFunction)syslog_syslog, METH_VARARGS, syslog_syslog__doc__}, + +static PyObject * +syslog_syslog_impl(PyObject *module, int group_left_1, int priority, + PyObject *message_object); + +static PyObject * +syslog_syslog(PyObject *module, PyObject *args) +{ + PyObject *return_value = NULL; + int group_left_1 = 0; + int priority = LOG_INFO; + PyObject *message_object; + + switch (PyTuple_GET_SIZE(args)) { + case 1: + if (!PyArg_ParseTuple(args, "U:syslog", &message_object)) { + goto exit; + } + break; + case 2: + if (!PyArg_ParseTuple(args, "iU:syslog", &priority, &message_object)) { + goto exit; + } + group_left_1 = 1; + break; + default: + PyErr_SetString(PyExc_TypeError, "syslog.syslog requires 1 to 2 arguments"); + goto exit; + } + return_value = syslog_syslog_impl(module, group_left_1, priority, message_object); + +exit: + return return_value; +} + +PyDoc_STRVAR(syslog_closelog__doc__, +"closelog($module, /)\n" +"--\n" +"\n" +"Reset the syslog module values and call the system library closelog()."); + +#define SYSLOG_CLOSELOG_METHODDEF \ + {"closelog", (PyCFunction)syslog_closelog, METH_NOARGS, syslog_closelog__doc__}, + +static PyObject * +syslog_closelog_impl(PyObject *module); + +static PyObject * +syslog_closelog(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return syslog_closelog_impl(module); +} + +PyDoc_STRVAR(syslog_setlogmask__doc__, +"setlogmask($module, maskpri, /)\n" +"--\n" +"\n" +"Set the priority mask to maskpri and return the previous mask value."); + +#define SYSLOG_SETLOGMASK_METHODDEF \ + {"setlogmask", (PyCFunction)syslog_setlogmask, METH_O, syslog_setlogmask__doc__}, + +static long +syslog_setlogmask_impl(PyObject *module, long maskpri); + +static PyObject * +syslog_setlogmask(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + long maskpri; + long _return_value; + + maskpri = PyLong_AsLong(arg); + if (maskpri == -1 && PyErr_Occurred()) { + goto exit; + } + _return_value = syslog_setlogmask_impl(module, maskpri); + if ((_return_value == -1) && PyErr_Occurred()) { + goto exit; + } + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; +} + +PyDoc_STRVAR(syslog_LOG_MASK__doc__, +"LOG_MASK($module, pri, /)\n" +"--\n" +"\n" +"calculates the mask for the individual priority pri."); + +#define SYSLOG_LOG_MASK_METHODDEF \ + {"LOG_MASK", (PyCFunction)syslog_LOG_MASK, METH_O, syslog_LOG_MASK__doc__}, + +static long +syslog_LOG_MASK_impl(PyObject *module, long pri); + +static PyObject * +syslog_LOG_MASK(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + long pri; + long _return_value; + + pri = PyLong_AsLong(arg); + if (pri == -1 && PyErr_Occurred()) { + goto exit; + } + _return_value = syslog_LOG_MASK_impl(module, pri); + if ((_return_value == -1) && PyErr_Occurred()) { + goto exit; + } + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; +} + +PyDoc_STRVAR(syslog_LOG_UPTO__doc__, +"LOG_UPTO($module, pri, /)\n" +"--\n" +"\n" +"calculates the mask for all priorities up to and including pri."); + +#define SYSLOG_LOG_UPTO_METHODDEF \ + {"LOG_UPTO", (PyCFunction)syslog_LOG_UPTO, METH_O, syslog_LOG_UPTO__doc__}, + +static long +syslog_LOG_UPTO_impl(PyObject *module, long pri); + +static PyObject * +syslog_LOG_UPTO(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + long pri; + long _return_value; + + pri = PyLong_AsLong(arg); + if (pri == -1 && PyErr_Occurred()) { + goto exit; + } + _return_value = syslog_LOG_UPTO_impl(module, pri); + if ((_return_value == -1) && PyErr_Occurred()) { + goto exit; + } + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; +} +/*[clinic end generated code: output=f294eb609a4087c8 input=a9049054013a1b77]*/ diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 2655a0c94bbd95..09f9709f9050e6 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -54,6 +54,13 @@ Revision history: #include +/*[clinic input] +module syslog +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=478f4ac94a1d4cae]*/ + +#include "clinic/syslogmodule.c.h" + /* only one instance, only one syslog, so globals should be ok */ static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */ static char S_log_open = 0; @@ -109,19 +116,23 @@ syslog_get_argv(void) } +/*[clinic input] +syslog.openlog + + ident as new_S_ident_o: unicode = NULL + logoption as logopt: long = 0 + facility: long(c_default="LOG_USER") = LOG_USER + +Set logging options of subsequent syslog() calls. +[clinic start generated code]*/ + static PyObject * -syslog_openlog(PyObject * self, PyObject * args, PyObject *kwds) +syslog_openlog_impl(PyObject *module, PyObject *new_S_ident_o, long logopt, + long facility) +/*[clinic end generated code: output=f1539844405f613e input=c36e977098346f67]*/ { - long logopt = 0; - long facility = LOG_USER; - PyObject *new_S_ident_o = NULL; - static char *keywords[] = {"ident", "logoption", "facility", 0}; const char *ident = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, - "|Ull:openlog", keywords, &new_S_ident_o, &logopt, &facility)) - return NULL; - if (new_S_ident_o) { Py_INCREF(new_S_ident_o); } @@ -155,20 +166,27 @@ syslog_openlog(PyObject * self, PyObject * args, PyObject *kwds) } + +/*[clinic input] +syslog.syslog + + [ + priority: int(c_default="LOG_INFO") = LOG_INFO + ] + + message as message_object: unicode + + / + +Send the string message to the system logger. +[clinic start generated code]*/ + static PyObject * -syslog_syslog(PyObject * self, PyObject * args) +syslog_syslog_impl(PyObject *module, int group_left_1, int priority, + PyObject *message_object) +/*[clinic end generated code: output=3361dc7f2e863377 input=02500dddce0095df]*/ { - PyObject *message_object; const char *message; - int priority = LOG_INFO; - - if (!PyArg_ParseTuple(args, "iU;[priority,] message string", - &priority, &message_object)) { - PyErr_Clear(); - if (!PyArg_ParseTuple(args, "U;[priority,] message string", - &message_object)) - return NULL; - } message = PyUnicode_AsUTF8(message_object); if (message == NULL) @@ -180,27 +198,26 @@ syslog_syslog(PyObject * self, PyObject * args) /* if log is not opened, open it now */ if (!S_log_open) { - PyObject *openargs; - - /* Continue even if PyTuple_New fails, because openlog(3) is optional. - * So, we can still do logging in the unlikely event things are so hosed - * that we can't do this tuple. - */ - if ((openargs = PyTuple_New(0))) { - PyObject *openlog_ret = syslog_openlog(self, openargs, NULL); - Py_XDECREF(openlog_ret); - Py_DECREF(openargs); - } + syslog_openlog_impl(module, NULL, 0, LOG_USER); } Py_BEGIN_ALLOW_THREADS; syslog(priority, "%s", message); Py_END_ALLOW_THREADS; + Py_RETURN_NONE; } + +/*[clinic input] +syslog.closelog + +Reset the syslog module values and call the system library closelog(). +[clinic start generated code]*/ + static PyObject * -syslog_closelog(PyObject *self, PyObject *unused) +syslog_closelog_impl(PyObject *module) +/*[clinic end generated code: output=97890a80a24b1b84 input=fb77a54d447acf07]*/ { if (PySys_Audit("syslog.closelog", NULL) < 0) { return NULL; @@ -210,54 +227,71 @@ syslog_closelog(PyObject *self, PyObject *unused) Py_CLEAR(S_ident_o); S_log_open = 0; } + Py_RETURN_NONE; } -static PyObject * -syslog_setlogmask(PyObject *self, PyObject *args) -{ - long maskpri, omaskpri; +/*[clinic input] +syslog.setlogmask -> long - if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri)) - return NULL; - if (PySys_Audit("syslog.setlogmask", "(O)", args ? args : Py_None) < 0) { - return NULL; + maskpri: long + / + +Set the priority mask to maskpri and return the previous mask value. +[clinic start generated code]*/ + +static long +syslog_setlogmask_impl(PyObject *module, long maskpri) +/*[clinic end generated code: output=d6ed163917b434bf input=adff2c2b76c7629c]*/ +{ + if (PySys_Audit("syslog.setlogmask", "(l)", maskpri) < 0) { + return -1; } - omaskpri = setlogmask(maskpri); - return PyLong_FromLong(omaskpri); + + return setlogmask(maskpri); } -static PyObject * -syslog_log_mask(PyObject *self, PyObject *args) +/*[clinic input] +syslog.LOG_MASK -> long + + pri: long + / + +calculates the mask for the individual priority pri. +[clinic start generated code]*/ + +static long +syslog_LOG_MASK_impl(PyObject *module, long pri) +/*[clinic end generated code: output=c4a5bbfcc74c7c94 input=25d2453cfd964871]*/ { - long mask; - long pri; - if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri)) - return NULL; - mask = LOG_MASK(pri); - return PyLong_FromLong(mask); + return LOG_MASK(pri); } -static PyObject * -syslog_log_upto(PyObject *self, PyObject *args) +/*[clinic input] +syslog.LOG_UPTO -> long + + pri: long + / + +calculates the mask for all priorities up to and including pri. +[clinic start generated code]*/ + +static long +syslog_LOG_UPTO_impl(PyObject *module, long pri) +/*[clinic end generated code: output=9eab083c90601d7e input=6e2cb9d73da6e397]*/ { - long mask; - long pri; - if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri)) - return NULL; - mask = LOG_UPTO(pri); - return PyLong_FromLong(mask); + return LOG_UPTO(pri); } /* List of functions defined in the module */ static PyMethodDef syslog_methods[] = { - {"openlog", _PyCFunction_CAST(syslog_openlog), METH_VARARGS | METH_KEYWORDS}, - {"closelog", syslog_closelog, METH_NOARGS}, - {"syslog", syslog_syslog, METH_VARARGS}, - {"setlogmask", syslog_setlogmask, METH_VARARGS}, - {"LOG_MASK", syslog_log_mask, METH_VARARGS}, - {"LOG_UPTO", syslog_log_upto, METH_VARARGS}, + SYSLOG_OPENLOG_METHODDEF + SYSLOG_CLOSELOG_METHODDEF + SYSLOG_SYSLOG_METHODDEF + SYSLOG_SETLOGMASK_METHODDEF + SYSLOG_LOG_MASK_METHODDEF + SYSLOG_LOG_UPTO_METHODDEF {NULL, NULL, 0} }; From 565d4b94df0de3451dd903c332c13d8747658137 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Wed, 20 Jul 2022 10:24:27 +0300 Subject: [PATCH 02/15] reformat syslog functions doc --- Modules/clinic/syslogmodule.c.h | 6 +++--- Modules/syslogmodule.c | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Modules/clinic/syslogmodule.c.h b/Modules/clinic/syslogmodule.c.h index e4da3bb67e49e9..c838c0183bdcbb 100644 --- a/Modules/clinic/syslogmodule.c.h +++ b/Modules/clinic/syslogmodule.c.h @@ -164,7 +164,7 @@ PyDoc_STRVAR(syslog_LOG_MASK__doc__, "LOG_MASK($module, pri, /)\n" "--\n" "\n" -"calculates the mask for the individual priority pri."); +"Calculates the mask for the individual priority pri."); #define SYSLOG_LOG_MASK_METHODDEF \ {"LOG_MASK", (PyCFunction)syslog_LOG_MASK, METH_O, syslog_LOG_MASK__doc__}, @@ -197,7 +197,7 @@ PyDoc_STRVAR(syslog_LOG_UPTO__doc__, "LOG_UPTO($module, pri, /)\n" "--\n" "\n" -"calculates the mask for all priorities up to and including pri."); +"Calculates the mask for all priorities up to and including pri."); #define SYSLOG_LOG_UPTO_METHODDEF \ {"LOG_UPTO", (PyCFunction)syslog_LOG_UPTO, METH_O, syslog_LOG_UPTO__doc__}, @@ -225,4 +225,4 @@ syslog_LOG_UPTO(PyObject *module, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=f294eb609a4087c8 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=5aac7edfd632f512 input=a9049054013a1b77]*/ diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 09f9709f9050e6..9f751139b2313d 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -257,12 +257,12 @@ syslog.LOG_MASK -> long pri: long / -calculates the mask for the individual priority pri. +Calculates the mask for the individual priority pri. [clinic start generated code]*/ static long syslog_LOG_MASK_impl(PyObject *module, long pri) -/*[clinic end generated code: output=c4a5bbfcc74c7c94 input=25d2453cfd964871]*/ +/*[clinic end generated code: output=c4a5bbfcc74c7c94 input=534829cb7fb5f7d2]*/ { return LOG_MASK(pri); } @@ -273,12 +273,12 @@ syslog.LOG_UPTO -> long pri: long / -calculates the mask for all priorities up to and including pri. +Calculates the mask for all priorities up to and including pri. [clinic start generated code]*/ static long syslog_LOG_UPTO_impl(PyObject *module, long pri) -/*[clinic end generated code: output=9eab083c90601d7e input=6e2cb9d73da6e397]*/ +/*[clinic end generated code: output=9eab083c90601d7e input=5e906d6c406b7458]*/ { return LOG_UPTO(pri); } From 8626c65b6e73ca12ae8eeed08f4f29d8e0e31824 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Wed, 20 Jul 2022 10:25:09 +0300 Subject: [PATCH 03/15] change syslog audit args formatting --- Modules/syslogmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 9f751139b2313d..c719f4432e4cf2 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -244,7 +244,7 @@ static long syslog_setlogmask_impl(PyObject *module, long maskpri) /*[clinic end generated code: output=d6ed163917b434bf input=adff2c2b76c7629c]*/ { - if (PySys_Audit("syslog.setlogmask", "(l)", maskpri) < 0) { + if (PySys_Audit("syslog.setlogmask", "l", maskpri) < 0) { return -1; } From a2844f05df53b734e8e5905b1cfbd80ebcdfa6f9 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 07:49:40 +0300 Subject: [PATCH 04/15] Update Modules/syslogmodule.c Co-authored-by: Erlend Egeberg Aasland --- Modules/syslogmodule.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index c719f4432e4cf2..6f34c5d02168e4 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -204,7 +204,6 @@ syslog_syslog_impl(PyObject *module, int group_left_1, int priority, Py_BEGIN_ALLOW_THREADS; syslog(priority, "%s", message); Py_END_ALLOW_THREADS; - Py_RETURN_NONE; } From 9b853db597f69cff988dc2fde212db4e2d67f199 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 07:50:50 +0300 Subject: [PATCH 05/15] Update Modules/syslogmodule.c Co-authored-by: Erlend Egeberg Aasland --- Modules/syslogmodule.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 6f34c5d02168e4..f66ee2a5c7a18d 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -226,7 +226,6 @@ syslog_closelog_impl(PyObject *module) Py_CLEAR(S_ident_o); S_log_open = 0; } - Py_RETURN_NONE; } From 30db6590c2e75cf8a141cda37a8f363b71d2e1f1 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 08:30:48 +0300 Subject: [PATCH 06/15] syslog: better test coverage --- Lib/test/audit-tests.py | 14 ++++++++++++++ Lib/test/test_audit.py | 21 +++++++++++++++++++++ Lib/test/test_syslog.py | 5 +++++ 3 files changed, 40 insertions(+) diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 00333cc9036a3c..4efe1497445422 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -419,6 +419,20 @@ def hook(event, args): sys._getframe() +def test_syslog(): + import syslog + + def hook(event, args): + if event.startswith("syslog."): + print(event, *args) + + sys.addaudithook(hook) + syslog.openlog('python') + syslog.syslog('test') + syslog.setlogmask(syslog.LOG_DEBUG) + syslog.closelog() + + if __name__ == "__main__": from test.support import suppress_msvcrt_asserts diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index 6a025f39912e67..5cca3fe8815aae 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -188,5 +188,26 @@ def test_sys_getframe(self): self.assertEqual(actual, expected) + def test_syslog(self): + try: + import syslog + except ImportError: + return + + returncode, events, stderr = self.run_python("test_syslog") + if returncode: + self.fail(stderr) + + if support.verbose: + print(*events, sep='\n') + + self.assertSequenceEqual( + [('syslog.openlog', ' ', f'python 0 {syslog.LOG_USER}'), + ('syslog.syslog', ' ', f'{syslog.LOG_INFO} test'), + ('syslog.setlogmask', ' ', f'{syslog.LOG_DEBUG}'), + ('syslog.closelog', '', '')], + events + ) + if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_syslog.py b/Lib/test/test_syslog.py index fe09bd39f8b7fd..d82e4bb688c46d 100644 --- a/Lib/test/test_syslog.py +++ b/Lib/test/test_syslog.py @@ -18,6 +18,11 @@ def test_syslog(self): syslog.syslog('test message from python test_syslog') syslog.syslog(syslog.LOG_ERR, 'test error from python test_syslog') + def test_syslog_implicit_open(self): + syslog.closelog() # Make sure log is closed + syslog.syslog('test message from python test_syslog') + syslog.syslog(syslog.LOG_ERR, 'test error from python test_syslog') + def test_closelog(self): syslog.openlog('python') syslog.closelog() From dde4e7cdffba6aabeb5582b01ba9b1d5240197d3 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 08:50:51 +0300 Subject: [PATCH 07/15] syslog: fix ref leak --- Modules/syslogmodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index f66ee2a5c7a18d..2f93ced09fc1d4 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -198,7 +198,8 @@ syslog_syslog_impl(PyObject *module, int group_left_1, int priority, /* if log is not opened, open it now */ if (!S_log_open) { - syslog_openlog_impl(module, NULL, 0, LOG_USER); + PyObject *openlog_ret = syslog_openlog_impl(module, NULL, 0, LOG_USER); + Py_XDECREF(openlog_ret); } Py_BEGIN_ALLOW_THREADS; From a90f93df0ddbed7fb3dce823c23eb9ee7a515a7b Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Tue, 26 Jul 2022 09:11:53 +0300 Subject: [PATCH 08/15] fix whitespace issues --- Lib/test/test_audit.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index 5cca3fe8815aae..d9f01ed5f9b600 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -193,7 +193,7 @@ def test_syslog(self): import syslog except ImportError: return - + returncode, events, stderr = self.run_python("test_syslog") if returncode: self.fail(stderr) @@ -202,10 +202,10 @@ def test_syslog(self): print(*events, sep='\n') self.assertSequenceEqual( - [('syslog.openlog', ' ', f'python 0 {syslog.LOG_USER}'), - ('syslog.syslog', ' ', f'{syslog.LOG_INFO} test'), - ('syslog.setlogmask', ' ', f'{syslog.LOG_DEBUG}'), - ('syslog.closelog', '', '')], + [('syslog.openlog', ' ', f'python 0 {syslog.LOG_USER}'), + ('syslog.syslog', ' ', f'{syslog.LOG_INFO} test'), + ('syslog.setlogmask', ' ', f'{syslog.LOG_DEBUG}'), + ('syslog.closelog', '', '')], events ) From 895a492c77aaf5cb6f006b41a14547e200bf9e5a Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Wed, 27 Jul 2022 02:53:11 +0300 Subject: [PATCH 09/15] use `support.import_helper.import_module` --- Lib/test/test_audit.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index d9f01ed5f9b600..45055b91d810ea 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -189,10 +189,7 @@ def test_sys_getframe(self): self.assertEqual(actual, expected) def test_syslog(self): - try: - import syslog - except ImportError: - return + syslog = import_helper.import_module("syslog") returncode, events, stderr = self.run_python("test_syslog") if returncode: From 3d2957708c1f509abc1bb79ac9ee609d3e875a6c Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Wed, 27 Jul 2022 02:55:46 +0300 Subject: [PATCH 10/15] syslog: pep 7 --- Modules/syslogmodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 38b806b105de0e..6224e819a2591e 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -139,7 +139,8 @@ syslog_openlog_impl(PyObject *module, PyObject *ident, long logopt, if (ident) { Py_INCREF(ident); - } else { + } + else { /* get sys.argv[0] or NULL if we can't for some reason */ ident = syslog_get_argv(); } From 29680c9dc6a0b034ef642a86d8c0ddfe0db93c68 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Wed, 27 Jul 2022 03:01:16 +0300 Subject: [PATCH 11/15] use the str converter --- Lib/test/test_audit.py | 2 +- Modules/clinic/syslogmodule.c.h | 12 ++++++------ Modules/syslogmodule.c | 12 +++--------- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index 45055b91d810ea..06301c6efc30a1 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -189,7 +189,7 @@ def test_sys_getframe(self): self.assertEqual(actual, expected) def test_syslog(self): - syslog = import_helper.import_module("syslog") + syslog = import_helper.import_module("syslog") returncode, events, stderr = self.run_python("test_syslog") if returncode: diff --git a/Modules/clinic/syslogmodule.c.h b/Modules/clinic/syslogmodule.c.h index c63103e08cbfee..bc75cf82deadc0 100644 --- a/Modules/clinic/syslogmodule.c.h +++ b/Modules/clinic/syslogmodule.c.h @@ -77,7 +77,7 @@ PyDoc_STRVAR(syslog_syslog__doc__, static PyObject * syslog_syslog_impl(PyObject *module, int group_left_1, int priority, - PyObject *message_object); + const char *message); static PyObject * syslog_syslog(PyObject *module, PyObject *args) @@ -85,16 +85,16 @@ syslog_syslog(PyObject *module, PyObject *args) PyObject *return_value = NULL; int group_left_1 = 0; int priority = LOG_INFO; - PyObject *message_object; + const char *message; switch (PyTuple_GET_SIZE(args)) { case 1: - if (!PyArg_ParseTuple(args, "U:syslog", &message_object)) { + if (!PyArg_ParseTuple(args, "s:syslog", &message)) { goto exit; } break; case 2: - if (!PyArg_ParseTuple(args, "iU:syslog", &priority, &message_object)) { + if (!PyArg_ParseTuple(args, "is:syslog", &priority, &message)) { goto exit; } group_left_1 = 1; @@ -103,7 +103,7 @@ syslog_syslog(PyObject *module, PyObject *args) PyErr_SetString(PyExc_TypeError, "syslog.syslog requires 1 to 2 arguments"); goto exit; } - return_value = syslog_syslog_impl(module, group_left_1, priority, message_object); + return_value = syslog_syslog_impl(module, group_left_1, priority, message); exit: return return_value; @@ -225,4 +225,4 @@ syslog_LOG_UPTO(PyObject *module, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=ac4ea57479c292c9 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=23b0c39420c43124 input=a9049054013a1b77]*/ diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 6224e819a2591e..b6296ed0a69aa5 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -177,7 +177,7 @@ syslog.syslog priority: int(c_default="LOG_INFO") = LOG_INFO ] - message as message_object: unicode + message: str / @@ -186,15 +186,9 @@ Send the string message to the system logger. static PyObject * syslog_syslog_impl(PyObject *module, int group_left_1, int priority, - PyObject *message_object) -/*[clinic end generated code: output=3361dc7f2e863377 input=02500dddce0095df]*/ + const char *message) +/*[clinic end generated code: output=c3dbc73445a0e078 input=ac83d92b12ea3d4e]*/ { - const char *message; - - message = PyUnicode_AsUTF8(message_object); - if (message == NULL) - return NULL; - if (PySys_Audit("syslog.syslog", "is", priority, message) < 0) { return NULL; } From 430d12c2375494f69a2e7380e1b4499a23bef028 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Wed, 17 Aug 2022 07:51:15 +0300 Subject: [PATCH 12/15] regen clinic --- Modules/clinic/syslogmodule.c.h | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/Modules/clinic/syslogmodule.c.h b/Modules/clinic/syslogmodule.c.h index bc75cf82deadc0..0ce66ad4e1a490 100644 --- a/Modules/clinic/syslogmodule.c.h +++ b/Modules/clinic/syslogmodule.c.h @@ -2,6 +2,12 @@ preserve [clinic start generated code]*/ +#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) +# include "pycore_gc.h" // PyGC_Head +# include "pycore_runtime.h" // _Py_ID() +#endif + + PyDoc_STRVAR(syslog_openlog__doc__, "openlog($module, /, ident=, logoption=0,\n" " facility=LOG_USER)\n" @@ -20,8 +26,31 @@ static PyObject * syslog_openlog(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { PyObject *return_value = NULL; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + + #define NUM_KEYWORDS 3 + static struct { + PyGC_Head _this_is_not_used; + PyObject_VAR_HEAD + PyObject *ob_item[NUM_KEYWORDS]; + } _kwtuple = { + .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) + .ob_item = { &_Py_ID(ident), &_Py_ID(logoption), &_Py_ID(facility), }, + }; + #undef NUM_KEYWORDS + #define KWTUPLE (&_kwtuple.ob_base.ob_base) + + #else // !Py_BUILD_CORE + # define KWTUPLE NULL + #endif // !Py_BUILD_CORE + static const char * const _keywords[] = {"ident", "logoption", "facility", NULL}; - static _PyArg_Parser _parser = {NULL, _keywords, "openlog", 0}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "openlog", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE PyObject *argsbuf[3]; Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; PyObject *ident = NULL; @@ -225,4 +254,4 @@ syslog_LOG_UPTO(PyObject *module, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=23b0c39420c43124 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=3b1bdb16565b8fda input=a9049054013a1b77]*/ From fd6e5890ad1bc154ea14dc8301bd5669cbfccc41 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Wed, 17 Aug 2022 08:21:35 +0300 Subject: [PATCH 13/15] regen global strings --- Include/internal/pycore_global_strings.h | 3 +++ .../internal/pycore_runtime_init_generated.h | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Include/internal/pycore_global_strings.h b/Include/internal/pycore_global_strings.h index aada220395023d..418c6b017f5496 100644 --- a/Include/internal/pycore_global_strings.h +++ b/Include/internal/pycore_global_strings.h @@ -349,6 +349,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(exception) STRUCT_FOR_ID(exp) STRUCT_FOR_ID(extend) + STRUCT_FOR_ID(facility) STRUCT_FOR_ID(factory) STRUCT_FOR_ID(family) STRUCT_FOR_ID(fanout) @@ -391,6 +392,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(hi) STRUCT_FOR_ID(hook) STRUCT_FOR_ID(id) + STRUCT_FOR_ID(ident) STRUCT_FOR_ID(ignore) STRUCT_FOR_ID(imag) STRUCT_FOR_ID(importlib) @@ -445,6 +447,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(lo) STRUCT_FOR_ID(locale) STRUCT_FOR_ID(locals) + STRUCT_FOR_ID(logoption) STRUCT_FOR_ID(loop) STRUCT_FOR_ID(mapping) STRUCT_FOR_ID(match) diff --git a/Include/internal/pycore_runtime_init_generated.h b/Include/internal/pycore_runtime_init_generated.h index 09890cd812015b..0951131f94dddc 100644 --- a/Include/internal/pycore_runtime_init_generated.h +++ b/Include/internal/pycore_runtime_init_generated.h @@ -858,6 +858,7 @@ extern "C" { INIT_ID(exception), \ INIT_ID(exp), \ INIT_ID(extend), \ + INIT_ID(facility), \ INIT_ID(factory), \ INIT_ID(family), \ INIT_ID(fanout), \ @@ -900,6 +901,7 @@ extern "C" { INIT_ID(hi), \ INIT_ID(hook), \ INIT_ID(id), \ + INIT_ID(ident), \ INIT_ID(ignore), \ INIT_ID(imag), \ INIT_ID(importlib), \ @@ -954,6 +956,7 @@ extern "C" { INIT_ID(lo), \ INIT_ID(locale), \ INIT_ID(locals), \ + INIT_ID(logoption), \ INIT_ID(loop), \ INIT_ID(mapping), \ INIT_ID(match), \ @@ -2018,6 +2021,8 @@ _PyUnicode_InitStaticStrings(void) { PyUnicode_InternInPlace(&string); string = &_Py_ID(extend); PyUnicode_InternInPlace(&string); + string = &_Py_ID(facility); + PyUnicode_InternInPlace(&string); string = &_Py_ID(factory); PyUnicode_InternInPlace(&string); string = &_Py_ID(family); @@ -2102,6 +2107,8 @@ _PyUnicode_InitStaticStrings(void) { PyUnicode_InternInPlace(&string); string = &_Py_ID(id); PyUnicode_InternInPlace(&string); + string = &_Py_ID(ident); + PyUnicode_InternInPlace(&string); string = &_Py_ID(ignore); PyUnicode_InternInPlace(&string); string = &_Py_ID(imag); @@ -2210,6 +2217,8 @@ _PyUnicode_InitStaticStrings(void) { PyUnicode_InternInPlace(&string); string = &_Py_ID(locals); PyUnicode_InternInPlace(&string); + string = &_Py_ID(logoption); + PyUnicode_InternInPlace(&string); string = &_Py_ID(loop); PyUnicode_InternInPlace(&string); string = &_Py_ID(mapping); @@ -5959,6 +5968,10 @@ _PyStaticObjects_CheckRefcnt(void) { _PyObject_Dump((PyObject *)&_Py_ID(extend)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); }; + if (Py_REFCNT((PyObject *)&_Py_ID(facility)) < _PyObject_IMMORTAL_REFCNT) { + _PyObject_Dump((PyObject *)&_Py_ID(facility)); + Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); + }; if (Py_REFCNT((PyObject *)&_Py_ID(factory)) < _PyObject_IMMORTAL_REFCNT) { _PyObject_Dump((PyObject *)&_Py_ID(factory)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); @@ -6127,6 +6140,10 @@ _PyStaticObjects_CheckRefcnt(void) { _PyObject_Dump((PyObject *)&_Py_ID(id)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); }; + if (Py_REFCNT((PyObject *)&_Py_ID(ident)) < _PyObject_IMMORTAL_REFCNT) { + _PyObject_Dump((PyObject *)&_Py_ID(ident)); + Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); + }; if (Py_REFCNT((PyObject *)&_Py_ID(ignore)) < _PyObject_IMMORTAL_REFCNT) { _PyObject_Dump((PyObject *)&_Py_ID(ignore)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); @@ -6343,6 +6360,10 @@ _PyStaticObjects_CheckRefcnt(void) { _PyObject_Dump((PyObject *)&_Py_ID(locals)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); }; + if (Py_REFCNT((PyObject *)&_Py_ID(logoption)) < _PyObject_IMMORTAL_REFCNT) { + _PyObject_Dump((PyObject *)&_Py_ID(logoption)); + Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); + }; if (Py_REFCNT((PyObject *)&_Py_ID(loop)) < _PyObject_IMMORTAL_REFCNT) { _PyObject_Dump((PyObject *)&_Py_ID(loop)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); From 8903e8bc61f42547d7664506649c0df583bc1338 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Sat, 8 Oct 2022 09:30:28 +0300 Subject: [PATCH 14/15] remove backported tests --- Lib/test/audit-tests.py | 14 -------------- Lib/test/test_audit.py | 20 -------------------- 2 files changed, 34 deletions(-) diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 4d2fdb57dc9132..4abf33d7cad1b6 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -450,20 +450,6 @@ def hook(event, args): syslog.closelog() -def test_syslog(): - import syslog - - def hook(event, args): - if event.startswith("syslog."): - print(event, *args) - - sys.addaudithook(hook) - syslog.openlog('python') - syslog.syslog('test') - syslog.setlogmask(syslog.LOG_DEBUG) - syslog.closelog() - - if __name__ == "__main__": from test.support import suppress_msvcrt_asserts diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index dfc5931343e8e5..50f78f2a6b8a46 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -223,25 +223,5 @@ def test_syslog(self): ) - def test_syslog(self): - syslog = import_helper.import_module("syslog") - - returncode, events, stderr = self.run_python("test_syslog") - - if returncode: - self.fail(stderr) - - if support.verbose: - print(*events, sep='\n') - - self.assertSequenceEqual( - [('syslog.openlog', ' ', f'python 0 {syslog.LOG_USER}'), - ('syslog.syslog', ' ', f'{syslog.LOG_INFO} test'), - ('syslog.setlogmask', ' ', f'{syslog.LOG_DEBUG}'), - ('syslog.closelog', '', '')], - events - ) - - if __name__ == "__main__": unittest.main() From e09825c4f2a5655dd3321995f1d3a67bc23e7cc6 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 8 Oct 2022 12:20:22 +0300 Subject: [PATCH 15/15] Delete 2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst --- .../2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst deleted file mode 100644 index 83a2fadf48eee1..00000000000000 --- a/Misc/NEWS.d/next/Core and Builtins/2022-07-19-16-32-52.gh-issue-95011.P5t6l9.rst +++ /dev/null @@ -1 +0,0 @@ -Migrated :mod:`syslog` to Argument Clinic. Patch by Noam Cohen.