@@ -163,32 +163,29 @@ def test_event_source_registration(self):
163163 handle = _winapi .RegisterEventSource (None , source_name )
164164 self .assertNotEqual (handle , _winapi .INVALID_HANDLE_VALUE )
165165
166- # Test ReportEvent with the registered event source
167- try :
168- # Test with strings and raw data
169- test_strings = ["Test message 1" , "Test message 2" ]
170- test_data = b"test raw data"
171- _winapi .ReportEvent (handle , 4 , 1 , 1002 , test_strings , test_data )
172-
173- # Test with empty strings list
174- # _winapi.ReportEvent(handle, 2, 0, 1003, [], None) # TODO
175- finally :
176- _winapi .DeregisterEventSource (handle )
177-
178- with self .assertRaises (OSError ):
166+ with self .assertRaisesRegex (OSError , '[WinError 87]' ):
179167 _winapi .RegisterEventSource (None , "" )
180168
181- with self .assertRaises (OSError ):
169+ with self .assertRaisesRegex (OSError , '[WinError 6]' ):
182170 _winapi .DeregisterEventSource (_winapi .INVALID_HANDLE_VALUE )
183171
184- # Test ReportEvent with invalid handle
185- with self .assertRaises (OSError ):
172+ def test_report_event (self ):
173+ source_name = "PythonTestEventSource"
174+
175+ handle = _winapi .RegisterEventSource (None , source_name )
176+ self .assertNotEqual (handle , _winapi .INVALID_HANDLE_VALUE )
177+ self .addCleanup (_winapi .DeregisterEventSource , handle )
178+
179+ # Test with strings and raw data
180+ test_strings = ["Test message 1" , "Test message 2" ]
181+ test_data = b"test raw data"
182+ _winapi .ReportEvent (handle , 4 , 1 , 1002 , test_strings , test_data )
183+
184+ # Test with empty strings list
185+ _winapi .ReportEvent (handle , 2 , 0 , 1003 , [], b'' )
186+
187+ with self .assertRaisesRegex (OSError , '[WinError 6]' ):
186188 _winapi .ReportEvent (_winapi .INVALID_HANDLE_VALUE , 1 , 0 , 1001 , [], test_data )
187189
188- # Test ReportEvent with invalid string types
189- handle2 = _winapi .RegisterEventSource (None , "PythonTestEventSource2" )
190- try :
191- with self .assertRaises (TypeError ):
192- _winapi .ReportEvent (handle2 , 1 , 0 , 1001 , ["string" , 123 ], test_data )
193- finally :
194- _winapi .DeregisterEventSource (handle2 )
190+ with self .assertRaisesRegex (TypeError , 'All strings must be unicode' ):
191+ _winapi .ReportEvent (handle , 1 , 0 , 1001 , ["string" , 123 ], test_data )
0 commit comments