python: Fix NameError if asyncio is not available

This commit is contained in:
Tatsuhiro Tsujikawa 2014-02-26 00:54:16 +09:00
parent 13cc3f2fe9
commit 52cec35906
1 changed files with 235 additions and 229 deletions

View File

@ -250,7 +250,7 @@ try:
import datetime
import time
except ImportError:
pass
asyncio = None
cdef _get_stream_user_data(cnghttp2.nghttp2_session *session,
int32_t stream_id):
@ -659,7 +659,9 @@ cdef class _HTTP2SessionCore:
datestr, method, path, handler.status,
'P' if handler.pushed else '-'))
class BaseRequestHandler:
if asyncio:
class BaseRequestHandler:
"""HTTP/2 request (stream) handler base class.
@ -737,8 +739,8 @@ class BaseRequestHandler:
def on_data(self, data):
'''Called when a chunk of request body is arrived. This method will be
called multiple times until all data are received.
'''Called when a chunk of request body is arrived. This method
will be called multiple times until all data are received.
'''
pass
@ -786,12 +788,14 @@ class BaseRequestHandler:
def push(self, path, method='GET', request_headers=None,
status=200, headers=None, body=None):
'''Push a resource. The path is a path portion of request URI for this
resource. The method is a method to access this resource. The
request_headers is additional request headers to access this
resource. The :scheme, :method, :authority and :path are
appended by the library. The :scheme and :authority are
inherited from the request (not request_headers parameter).
'''Push a resource. The path is a path portion of request URI
for this
resource. The method is a method to access this
resource. The request_headers is additional request
headers to access this resource. The :scheme, :method,
:authority and :path are appended by the library. The
:scheme and :authority are inherited from the request (not
request_headers parameter).
The status is HTTP status code. The headers is additional
response headers. The :status header field is appended by the
@ -845,7 +849,8 @@ class BaseRequestHandler:
headers = []
self.response_headers = _encode_headers(headers)
self.response_headers.append((b':status', str(status).encode('utf-8')))
self.response_headers.append((b':status', str(status)\
.encode('utf-8')))
self.response_body = body
@ -859,15 +864,15 @@ class BaseRequestHandler:
elif isinstance(body, io.IOBase):
return body
else:
raise Exception(('body must be None or instance of str or bytes '
'or io.IOBase'))
raise Exception(('body must be None or instance of str or '
'bytes or io.IOBase'))
def _encode_headers(headers):
def _encode_headers(headers):
return [(k if isinstance(k, bytes) else k.encode('utf-8'),
v if isinstance(v, bytes) else v.encode('utf-8')) \
for k, v in headers]
class _HTTP2Session(asyncio.Protocol):
class _HTTP2Session(asyncio.Protocol):
def __init__(self, RequestHandlerClass):
asyncio.Protocol.__init__(self)
@ -897,7 +902,8 @@ class _HTTP2Session(asyncio.Protocol):
self.connection_header = self.connection_header[nread:]
if len(self.connection_header) == 0:
try:
self.http2 = _HTTP2SessionCore(self.transport,
self.http2 = _HTTP2SessionCore\
(self.transport,
self.RequestHandlerClass)
except Exception as err:
sys.stderr.write(traceback.format_exc())
@ -926,7 +932,7 @@ class _HTTP2Session(asyncio.Protocol):
self.transport.close()
return
class HTTP2Server:
class HTTP2Server:
'''HTTP/2 server.