Add python-eventlet 0.16.1
[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
27
28 class file(__original_file):
29     def __init__(self, *args, **kwargs):
30         super(file, self).__init__(*args, **kwargs)
31         hubs.notify_opened(self.fileno())
32
33 __original_open = open
34 __opening = False
35
36
37 def open(*args):
38     global __opening
39     result = __original_open(*args)
40     if not __opening:
41         # This is incredibly ugly. 'open' is used under the hood by
42         # the import process. So, ensure we don't wind up in an
43         # infinite loop.
44         __opening = True
45         hubs.notify_opened(result.fileno())
46         __opening = False
47     return result