| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  | from test.support import import_helper, threading_helper | 
					
						
							| 
									
										
										
										
											2020-08-04 00:47:42 +08:00
										 |  |  | syslog = import_helper.import_module("syslog") #skip if not supported | 
					
						
							| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  | from test import support | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import threading | 
					
						
							|  |  |  | import time | 
					
						
							| 
									
										
										
										
											2009-04-01 11:28:47 +00:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2022-11-30 07:58:20 +09:00
										 |  |  | from textwrap import dedent | 
					
						
							| 
									
										
										
										
											2007-08-25 00:21:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # 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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Test(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         syslog.closelog() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-25 00:21:36 +00:00
										 |  |  |     def test_openlog(self): | 
					
						
							|  |  |  |         syslog.openlog('python') | 
					
						
							| 
									
										
										
										
											2010-12-08 23:31:48 +00:00
										 |  |  |         # Issue #6697. | 
					
						
							|  |  |  |         self.assertRaises(UnicodeEncodeError, syslog.openlog, '\uD800') | 
					
						
							| 
									
										
										
										
											2007-08-25 00:21:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  |     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') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-25 00:21:36 +00:00
										 |  |  |     def test_closelog(self): | 
					
						
							|  |  |  |         syslog.openlog('python') | 
					
						
							|  |  |  |         syslog.closelog() | 
					
						
							| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  |         syslog.closelog()  # idempotent operation | 
					
						
							| 
									
										
										
										
											2007-08-25 00:21:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_setlogmask(self): | 
					
						
							| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  |         mask = syslog.LOG_UPTO(syslog.LOG_WARNING) | 
					
						
							|  |  |  |         oldmask = syslog.setlogmask(mask) | 
					
						
							|  |  |  |         self.assertEqual(syslog.setlogmask(0), mask) | 
					
						
							|  |  |  |         self.assertEqual(syslog.setlogmask(oldmask), mask) | 
					
						
							| 
									
										
										
										
											2007-08-25 00:21:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_log_mask(self): | 
					
						
							| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  |         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)) | 
					
						
							| 
									
										
										
										
											2007-08-25 00:21:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:56:56 +01:00
										 |  |  |     def test_openlog_noargs(self): | 
					
						
							|  |  |  |         syslog.openlog() | 
					
						
							|  |  |  |         syslog.syslog('test message from python test_syslog') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  |     @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}')  # new string object | 
					
						
							|  |  |  |                 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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-30 07:58:20 +09:00
										 |  |  |     def test_subinterpreter_syslog(self): | 
					
						
							|  |  |  |         # syslog.syslog() is not allowed in subinterpreters, but only if | 
					
						
							|  |  |  |         # syslog.openlog() hasn't been called in the main interpreter yet. | 
					
						
							|  |  |  |         with self.subTest('before openlog()'): | 
					
						
							|  |  |  |             code = dedent('''
 | 
					
						
							|  |  |  |                 import syslog | 
					
						
							|  |  |  |                 caught_error = False | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     syslog.syslog('foo') | 
					
						
							|  |  |  |                 except RuntimeError: | 
					
						
							|  |  |  |                     caught_error = True | 
					
						
							|  |  |  |                 assert(caught_error) | 
					
						
							|  |  |  |             ''')
 | 
					
						
							|  |  |  |             res = support.run_in_subinterp(code) | 
					
						
							|  |  |  |             self.assertEqual(res, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         syslog.openlog() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with self.subTest('after openlog()'): | 
					
						
							|  |  |  |                 code = dedent('''
 | 
					
						
							|  |  |  |                     import syslog | 
					
						
							|  |  |  |                     syslog.syslog('foo') | 
					
						
							|  |  |  |                 ''')
 | 
					
						
							|  |  |  |                 res = support.run_in_subinterp(code) | 
					
						
							|  |  |  |                 self.assertEqual(res, 0) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             syslog.closelog() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_subinterpreter_openlog(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             code = dedent('''
 | 
					
						
							|  |  |  |                 import syslog | 
					
						
							|  |  |  |                 caught_error = False | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     syslog.openlog() | 
					
						
							|  |  |  |                 except RuntimeError: | 
					
						
							|  |  |  |                     caught_error = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 assert(caught_error) | 
					
						
							|  |  |  |             ''')
 | 
					
						
							|  |  |  |             res = support.run_in_subinterp(code) | 
					
						
							|  |  |  |             self.assertEqual(res, 0) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             syslog.closelog() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_subinterpreter_closelog(self): | 
					
						
							|  |  |  |         syslog.openlog('python') | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             code = dedent('''
 | 
					
						
							|  |  |  |                 import syslog | 
					
						
							|  |  |  |                 caught_error = False | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     syslog.closelog() | 
					
						
							|  |  |  |                 except RuntimeError: | 
					
						
							|  |  |  |                     caught_error = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 assert(caught_error) | 
					
						
							|  |  |  |             ''')
 | 
					
						
							|  |  |  |             res = support.run_in_subinterp(code) | 
					
						
							|  |  |  |             self.assertEqual(res, 0) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             syslog.closelog() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 20:17:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-25 00:21:36 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2015-04-13 15:00:43 -05:00
										 |  |  |     unittest.main() |