xxxnnorwitz this test sucks i don t know of a platform independent way to verify that the messages were really logged the only purpose of this test is to verify the code doesn t crash or leak issue 6697 syslog syslog is not allowed in subinterpreters but only if syslog openlog hasn t been called in the main interpreter yet res support runinsubinterpcode self assertequalres 0 syslog openlog try with self subtest after openlog code dedent import syslog syslog syslog foo res support runinsubinterpcode self assertequalres 0 finally syslog closelog def testsubinterpretercloselogself syslog openlog python try code dedent import syslog caughterror false try syslog closelog except runtimeerror caughterror true assertcaughterror skip if not supported xxx nnorwitz this test sucks i don t know of a platform independent way to verify that the messages were really logged the only purpose of this test is to verify the code doesn t crash or leak issue 6697 make sure log is closed idempotent operation new string object syslog syslog is not allowed in subinterpreters but only if syslog openlog hasn t been called in the main interpreter yet import syslog caught_error false try syslog syslog foo except runtimeerror caught_error true assert caught_error import syslog syslog syslog foo import syslog caught_error false try syslog openlog except runtimeerror caught_error true assert caught_error import syslog caught_error false try syslog closelog except runtimeerror caught_error true assert caught_error
from test.support import import_helper, threading_helper
syslog = import_helper.import_module("syslog") 
from test import support
import sys
import threading
import time
import unittest
from textwrap import dedent





class Test(unittest.TestCase):

    def tearDown(self):
        syslog.closelog()

    def test_openlog(self):
        syslog.openlog('python')
        
        self.assertRaises(UnicodeEncodeError, syslog.openlog, '\uD800')

    def test_syslog(self):
        syslog.openlog('python')
        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() 
        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()
        syslog.closelog()  

    def test_setlogmask(self):
        mask = syslog.LOG_UPTO(syslog.LOG_WARNING)
        oldmask = syslog.setlogmask(mask)
        self.assertEqual(syslog.setlogmask(0), mask)
        self.assertEqual(syslog.setlogmask(oldmask), mask)

    def test_log_mask(self):
        mask = syslog.LOG_UPTO(syslog.LOG_WARNING)
        self.assertTrue(mask & syslog.LOG_MASK(syslog.LOG_WARNING))
        self.assertTrue(mask & syslog.LOG_MASK(syslog.LOG_ERR))
        self.assertFalse(mask & syslog.LOG_MASK(syslog.LOG_INFO))

    def test_openlog_noargs(self):
        syslog.openlog()
        syslog.syslog('test message from python test_syslog')

    @threading_helper.requires_working_threading()
    def test_syslog_threaded(self):
        start = threading.Event()
        stop = False
        def opener():
            start.wait(10)
            i = 1
            while not stop:
                syslog.openlog(f'python-test-{i}')  
                i += 1
        def logger():
            start.wait(10)
            while not stop:
                syslog.syslog('test message from python test_syslog')

        orig_si = sys.getswitchinterval()
        support.setswitchinterval(1e-9)
        try:
            threads = [threading.Thread(target=opener)]
            threads += [threading.Thread(target=logger) for k in range(10)]
            with threading_helper.start_threads(threads):
                start.set()
                time.sleep(0.1)
                stop = True
        finally:
            sys.setswitchinterval(orig_si)

    def test_subinterpreter_syslog(self):
        
        
        with self.subTest('before openlog()'):
            code = dedent()
            res = support.run_in_subinterp(code)
            self.assertEqual(res, 0)

        syslog.openlog()
        try:
            with self.subTest('after openlog()'):
                code = dedent()
                res = support.run_in_subinterp(code)
                self.assertEqual(res, 0)
        finally:
            syslog.closelog()

    def test_subinterpreter_openlog(self):
        try:
            code = dedent()
            res = support.run_in_subinterp(code)
            self.assertEqual(res, 0)
        finally:
            syslog.closelog()

    def test_subinterpreter_closelog(self):
        syslog.openlog('python')
        try:
            code = dedent()
            res = support.run_in_subinterp(code)
            self.assertEqual(res, 0)
        finally:
            syslog.closelog()


if __name__ == "__main__":
    unittest.main()