zackcquic commented on a change in pull request #7952:
URL: https://github.com/apache/tvm/pull/7952#discussion_r639396559



##########
File path: tests/python/relay/test_pass_instrument.py
##########
@@ -168,3 +169,329 @@ def run_after_pass(self, mod, info):
     # Out of pass context scope, should be reset
     assert passes_counter.run_before_count == 0
     assert passes_counter.run_after_count == 0
+
+
+def test_enter_pass_ctx_exception(capsys):
+    @pass_instrument
+    class PI:
+        def __init__(self, id):
+            self.id = id
+
+        def enter_pass_ctx(self):
+            print(self.id + " enter ctx")
+
+        def exit_pass_ctx(self):
+            print(self.id + " exit ctx")
+
+    @pass_instrument
+    class PIBroken(PI):
+        def __init__(self, id):
+            super().__init__(id)
+
+        def enter_pass_ctx(self):
+            print(self.id + " enter ctx")
+            raise RuntimeError("Just a dummy error")
+
+    pass_ctx = tvm.transform.PassContext(instruments=[PI("%1"), 
PIBroken("%2"), PI("%3")])
+    with pytest.raises(tvm.error.TVMError):
+        with pass_ctx:
+            pass
+
+    assert "%1 enter ctx\n" "%2 enter ctx\n" == capsys.readouterr().out

Review comment:
       I am not quite understanding the sentence - i don't think you can 
implement `__enter__` with knowledge of any following `__enter___`.
   
   I think ```__enter__``` and ```__exit__``` throw exceptions is a common 
problem (like following example) beyond the scope of this PR. Maybe we can use 
another RFC/PR to address this?
   
   ```python
   class OpenFile:
   
     def __init__(self, file_name):
       self.file_name = file_name
   
     def __enter__(self):
       print("Try opening " + self.file_name)
       self.file = open(self.file_name)
       print("Open " + self.file_name + "sccussed")
       return self.file
   
     def __exit__(self, exec_type, exc_eval, traceback):
       print("Try colsing " + self.file_name)
       self.file.close();
       print(self.file_name + " closed")
   
   
   with OpenFile("zz"):
     pass
   
   # output
   Try opening zz
   Traceback (most recent call last):
     File "test.py", line 18, in <module>
       with OpenFile("zz"):
     File "test.py", line 8, in __enter__
       self.file = open(self.file_name)
   FileNotFoundError: [Errno 2] No such file or directory: 'zz'
   ```
   
   
   
   
   
   
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to