You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.2KB

  1. """
  2. In order to detect a filehandle that's been closed, our only clue may be
  3. the operating system returning the same filehandle in response to some
  4. other operation.
  5. The builtins 'file' and 'open' are patched to collaborate with the
  6. notify_opened protocol.
  7. """
  8. builtins_orig = __builtins__
  9. from eventlet import hubs
  10. from eventlet.hubs import hub
  11. from eventlet.patcher import slurp_properties
  12. import sys
  13. __all__ = dir(builtins_orig)
  14. __patched__ = ['file', 'open']
  15. slurp_properties(builtins_orig, globals(),
  16. ignore=__patched__, srckeys=dir(builtins_orig))
  17. hubs.get_hub()
  18. __original_file = file
  19. class file(__original_file):
  20. def __init__(self, *args, **kwargs):
  21. super(file, self).__init__(*args, **kwargs)
  22. hubs.notify_opened(self.fileno())
  23. __original_open = open
  24. __opening = False
  25. def open(*args):
  26. global __opening
  27. result = __original_open(*args)
  28. if not __opening:
  29. # This is incredibly ugly. 'open' is used under the hood by
  30. # the import process. So, ensure we don't wind up in an
  31. # infinite loop.
  32. __opening = True
  33. hubs.notify_opened(result.fileno())
  34. __opening = False
  35. return result