gemini-code-assist[bot] commented on code in PR #38425:
URL: https://github.com/apache/beam/pull/38425#discussion_r3212296562


##########
sdks/python/apache_beam/dataframe/io.py:
##########
@@ -584,7 +584,19 @@ def _read(self, size=-1):
     return res
 
   def flush(self):
-    self._underlying.flush()
+    if not self.closed:
+      try:
+        self._underlying.flush()
+      except ValueError:
+        pass
+
+  def close(self):
+    if not self.closed:
+      try:
+        if hasattr(self._underlying, 'close'):
+          self._underlying.close()
+      except Exception:
+        pass

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The `close` method uses a broad `except Exception: pass` block, which can 
suppress unexpected bugs such as `NameError` or `TypeError`. Following PEP 8, 
it is recommended to catch specific exceptions whenever possible. Since 
`hasattr` is already used to check for the existence of the `close` method, 
catching `(OSError, ValueError)` should be sufficient to handle common I/O 
issues during cleanup while allowing other errors to surface.
   
   ```suggestion
     def close(self):
       if not self.closed and hasattr(self._underlying, 'close'):
         try:
           self._underlying.close()
         except (OSError, ValueError):
           pass
   ```



##########
sdks/python/apache_beam/dataframe/io_test.py:
##########
@@ -296,6 +296,27 @@ def test_truncating_filehandle_iter(self):
     self._run_truncating_file_handle_iter_test('aaa b cccccccccccccccccccc')
     self._run_truncating_file_handle_iter_test('aaa b ccccccccccccccccc ')
 
+  def test_truncating_filehandle_flush_on_closed_stream(self):
+    class ClosedFlushingStream(StringIO):
+      def flush(self):
+        if self.closed:
+          raise ValueError("I/O operation on closed file.")
+        super().flush()
+
+    s = 'a b c'
+    tracker = restriction_trackers.OffsetRestrictionTracker(
+        restriction_trackers.OffsetRange(0, len(s)))
+    underlying = ClosedFlushingStream(s)
+    handle = io._TruncatingFileHandle(
+        underlying, tracker, splitter=io._DelimSplitter(' ', 10))
+
+    # Verify that calling flush() when the underlying stream is closed
+    # succeeds without raising ValueError.
+    underlying.close()
+    handle.flush()

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The new test case verifies that `flush()` handles closed streams correctly, 
but it doesn't exercise the newly added `close()` method. Adding a call to 
`handle.close()` would ensure that the method works as expected and provides 
coverage for the fix that addresses the `AttributeError` mentioned in the PR 
description.
   
   ```suggestion
       underlying.close()
       handle.flush()
       handle.close()
   ```



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to