Added python-eventlet 0.15.2 for Ubuntu 14.04
[packages/trusty/python-eventlet.git] / eventlet / eventlet / green / builtin.py
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
6 The builtins 'file' and 'open' are patched to collaborate with the
7 notify_opened protocol.
8 """
9
10 builtins_orig = __builtins__
11
12 from eventlet import hubs
13 from eventlet.hubs import hub
14 from eventlet.patcher import slurp_properties
15 import sys
16
17 __all__ = dir(builtins_orig)
18 __patched__ = ['file', 'open']
19
20 slurp_properties(builtins_orig, globals(),
21     ignore=__patched__, srckeys=dir(builtins_orig))
22
23 hubs.get_hub()
24
25 __original_file = file
26 class file(__original_file):
27     def __init__(self, *args, **kwargs):
28         super(file, self).__init__(*args, **kwargs)
29         hubs.notify_opened(self.fileno())
30
31 __original_open = open
32 __opening = False
33 def open(*args):
34     global __opening
35     result = __original_open(*args)
36     if not __opening:
37         # This is incredibly ugly. 'open' is used under the hood by
38         # the import process. So, ensure we don't wind up in an
39         # infinite loop.
40         __opening = True
41         hubs.notify_opened(result.fileno())
42         __opening = False
43     return result