1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""A request-local environment and logging stream."""
import collections
import os
import sys
import threading
_sys_stderr = sys.stderr
class RequestEnvironment(threading.local):
"""A thread local request environment.
A thread local request environment that provides an error stream errors and a
dict of the request environment environ as would be found in os.environ. A
single error stream is shared between threads of a single request, but each
thread accesses an independent copy of environ created when
CloneRequestEnvironment is called. A request environment of one thread can be
installed in another thread as follows:
1. Call CloneRequestEnvironment in the first thread.
2. Call the returned callable from the other thread.
"""
def __init__(self):
super(RequestEnvironment, self).__init__()
self.Reset()
def Reset(self):
"""Resets the error stream and environment for this request."""
self.errors = _sys_stderr
self.environ = {}
def Init(self, errors, environ):
self.errors = errors
self.environ = environ
def CloneRequestEnvironment(self):
"""Returns a callable that will install the environment in another thread.
Returns:
A callable that will duplicate the request environment of this thread in
another thread that calls it.
"""
errors = self.errors
environ = dict(self.environ)
return lambda: self.Init(errors, environ)
def Clear(self):
"""Clears the thread locals."""
self.__dict__.clear()
self.Reset()
class RequestLocalStream(object):
"""A stream that delegates to a RequestEnvironment stream."""
def __init__(self, request):
self._request = request
def close(self):
pass
def flush(self):
self._request.errors.flush()
def write(self, data):
self._request.errors.write(data)
def writelines(self, data):
self._request.errors.writelines(data)
class RequestLocalEnviron(collections.MutableMapping):
"""A MutableMapping that delegates to a RequestEnvironment environ."""
def __init__(self, request):
self._request = request
def __len__(self):
return len(self._request.environ)
def __iter__(self):
return iter(self._request.environ)
def __getitem__(self, key):
return self._request.environ[key]
def __setitem__(self, key, value):
self._request.environ[key] = value
def __delitem__(self, key):
del self._request.environ[key]
def __repr__(self):
return repr(self._request.environ)
def has_key(self, key):
return key in self._request.environ
def copy(self):
return dict(self._request.environ)
def viewitems(self):
return collections.ItemsView(self)
def viewkeys(self):
return collections.KeysView(self)
def viewvalues(self):
return collections.ValuesView(self)
current_request = RequestEnvironment()
def PatchOsEnviron():
"""Replace os.environ by a RequestLocalEnviron instance.
This is called from init.py when it modifies the execution
environment (in the wider sense of the word).
"""
os.environ = RequestLocalEnviron(current_request)
|