77import os
88import weakref
99
10+ from contextlib import contextmanager
11+
1012from roundup .anypy .html import html_escape
1113
1214import roundup .instance
@@ -89,28 +91,29 @@ def __call__(self, environ, start_response):
8991
9092 tracker = roundup .instance .open (self .home , not self .debug )
9193
92- # need to strip the leading '/'
93- environ ["PATH_INFO" ] = environ ["PATH_INFO" ][1 :]
94- if request .timing :
95- environ ["CGI_SHOW_TIMING" ] = request .timing
96-
97- form = BinaryFieldStorage (fp = environ ['wsgi.input' ], environ = environ )
98-
99- if environ ['REQUEST_METHOD' ] in ("OPTIONS" , "DELETE" ):
100- # these methods have no data. When we init tracker.Client
101- # set form to None and request.rfile to None to get a
102- # properly initialized empty form.
103- form = None
104- request .rfile = None
105-
106- client = tracker .Client (tracker , request , environ , form ,
107- request .translator )
108- try :
109- client .main ()
110- except roundup .cgi .client .NotFound :
111- request .start_response ([('Content-Type' , 'text/html' )], 404 )
112- request .wfile .write (s2b ('Not found: %s' %
113- html_escape (client .path )))
94+ with self .get_tracker () as tracker :
95+ # need to strip the leading '/'
96+ environ ["PATH_INFO" ] = environ ["PATH_INFO" ][1 :]
97+ if request .timing :
98+ environ ["CGI_SHOW_TIMING" ] = request .timing
99+
100+ form = BinaryFieldStorage (fp = environ ['wsgi.input' ], environ = environ )
101+
102+ if environ ['REQUEST_METHOD' ] in ("OPTIONS" , "DELETE" ):
103+ # these methods have no data. When we init tracker.Client
104+ # set form to None and request.rfile to None to get a
105+ # properly initialized empty form.
106+ form = None
107+ request .rfile = None
108+
109+ client = tracker .Client (tracker , request , environ , form ,
110+ request .translator )
111+ try :
112+ client .main ()
113+ except roundup .cgi .client .NotFound :
114+ request .start_response ([('Content-Type' , 'text/html' )], 404 )
115+ request .wfile .write (s2b ('Not found: %s' %
116+ html_escape (client .path )))
114117
115118 # all body data has been written using wfile
116119 return []
@@ -125,3 +128,8 @@ def get_wfile(self):
125128 if self .__wfile is None :
126129 raise ValueError ('start_response() not called' )
127130 return self .__wfile
131+
132+ @contextmanager
133+ def get_tracker (self ):
134+ # get a new instance for each request
135+ yield roundup .instance .open (self .home , not self .debug )
0 commit comments