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

Skip to content

Commit 8d3654d

Browse files
committed
Use unicode and add a "test" for syslog
1 parent a401bbe commit 8d3654d

2 files changed

Lines changed: 52 additions & 7 deletions

File tree

Lib/test/test_syslog.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import syslog
3+
import unittest
4+
from test import test_support
5+
6+
# XXX(nnorwitz): This test sucks. I don't know of a platform independent way
7+
# to verify that the messages were really logged.
8+
# The only purpose of this test is to verify the code doesn't crash or leak.
9+
10+
class Test(unittest.TestCase):
11+
12+
def test_openlog(self):
13+
syslog.openlog('python')
14+
15+
def test_syslog(self):
16+
syslog.openlog('python')
17+
syslog.syslog('test message from python test_syslog')
18+
syslog.syslog(syslog.LOG_ERR, 'test error from python test_syslog')
19+
20+
def test_closelog(self):
21+
syslog.openlog('python')
22+
syslog.closelog()
23+
24+
def test_setlogmask(self):
25+
syslog.setlogmask(syslog.LOG_DEBUG)
26+
27+
def test_log_mask(self):
28+
syslog.LOG_MASK(syslog.LOG_INFO)
29+
30+
def test_log_upto(self):
31+
syslog.LOG_UPTO(syslog.LOG_INFO)
32+
33+
def test_main():
34+
test_support.run_unittest(__name__)
35+
36+
if __name__ == "__main__":
37+
test_main()

Modules/syslogmodule.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ syslog_openlog(PyObject * self, PyObject * args)
5858
long logopt = 0;
5959
long facility = LOG_USER;
6060
PyObject *new_S_ident_o;
61+
const char *ident;
6162

6263
if (!PyArg_ParseTuple(args,
63-
"S|ll;ident string [, logoption [, facility]]",
64+
"U|ll;ident string [, logoption [, facility]]",
6465
&new_S_ident_o, &logopt, &facility))
6566
return NULL;
6667

@@ -71,7 +72,10 @@ syslog_openlog(PyObject * self, PyObject * args)
7172
S_ident_o = new_S_ident_o;
7273
Py_INCREF(S_ident_o);
7374

74-
openlog(PyString_AsString(S_ident_o), logopt, facility);
75+
ident = PyUnicode_AsString(S_ident_o);
76+
if (ident == NULL)
77+
return NULL;
78+
openlog(ident, logopt, facility);
7579

7680
Py_INCREF(Py_None);
7781
return Py_None;
@@ -81,17 +85,21 @@ syslog_openlog(PyObject * self, PyObject * args)
8185
static PyObject *
8286
syslog_syslog(PyObject * self, PyObject * args)
8387
{
84-
char *message;
88+
PyObject *message_object;
89+
const char *message;
8590
int priority = LOG_INFO;
8691

87-
if (!PyArg_ParseTuple(args, "is;[priority,] message string",
88-
&priority, &message)) {
92+
if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
93+
&priority, &message_objecct)) {
8994
PyErr_Clear();
90-
if (!PyArg_ParseTuple(args, "s;[priority,] message string",
91-
&message))
95+
if (!PyArg_ParseTuple(args, "U;[priority,] message string",
96+
&message_objecct))
9297
return NULL;
9398
}
9499

100+
message = PyUnicode_AsString(message_object);
101+
if (message == NULL)
102+
return NULL;
95103
syslog(priority, "%s", message);
96104
Py_INCREF(Py_None);
97105
return Py_None;

0 commit comments

Comments
 (0)