def configure_curl(self, timeout=DEFAULT_TIMEOUT, context=None, curl_handle=None):
if curl_handle:<EOL><INDENT>curl = curl_handle<EOL>try:  <EOL><INDENT>curl.getinfo(curl.HTTP_CODE)                <EOL>curl.reset()<EOL>curl.setopt(curl.COOKIELIST, "<STR_LIT>")<EOL><DEDENT>except pycurl.error:<EOL><INDENT>curl = pycurl.Curl()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>curl = pycurl.Curl()<EOL><DEDENT>curl.setopt(curl.URL, str(self.url))<EOL>curl.setopt(curl.TIMEOUT, timeout)<EOL>is_unicoded = False<EOL>bod = self.body<EOL>if isinstance(bod, text_type):  <EOL><INDENT>bod = bod.encode('<STR_LIT>')<EOL>is_unicoded = True<EOL><DEDENT>if bod and len(bod) > <NUM_LIT:0>:<EOL><INDENT>curl.setopt(curl.READFUNCTION, MyIO(bod).read)<EOL><DEDENT>if self.auth_username and self.auth_password:<EOL><INDENT>curl.setopt(pycurl.USERPWD, <EOL>parsing.encode_unicode_bytes(self.auth_username) + b'<STR_LIT::>' + <EOL>parsing.encode_unicode_bytes(self.auth_password))<EOL>if self.auth_type:<EOL><INDENT>curl.setopt(pycurl.HTTPAUTH, self.auth_type)<EOL><DEDENT><DEDENT>if self.method == u'<STR_LIT:POST>':<EOL><INDENT>curl.setopt(HTTP_METHODS[u'<STR_LIT:POST>'], <NUM_LIT:1>)<EOL>if bod is not None:<EOL><INDENT>curl.setopt(pycurl.POSTFIELDSIZE, len(bod))<EOL><DEDENT>else:<EOL><INDENT>curl.setopt(pycurl.POSTFIELDSIZE, <NUM_LIT:0>)<EOL><DEDENT><DEDENT>elif self.method == u'<STR_LIT>':<EOL><INDENT>curl.setopt(HTTP_METHODS[u'<STR_LIT>'], <NUM_LIT:1>)<EOL>if bod is not None:<EOL><INDENT>curl.setopt(pycurl.INFILESIZE, len(bod))<EOL><DEDENT>else:<EOL><INDENT>curl.setopt(pycurl.INFILESIZE, <NUM_LIT:0>)<EOL><DEDENT><DEDENT>elif self.method == u'<STR_LIT>':<EOL><INDENT>curl.setopt(curl.POSTFIELDS, bod)<EOL>curl.setopt(curl.CUSTOMREQUEST, '<STR_LIT>')<EOL>if bod is not None:<EOL><INDENT>curl.setopt(pycurl.INFILESIZE, len(bod))<EOL><DEDENT>else:<EOL><INDENT>curl.setopt(pycurl.INFILESIZE, <NUM_LIT:0>)<EOL><DEDENT><DEDENT>elif self.method == u'<STR_LIT>':<EOL><INDENT>curl.setopt(curl.CUSTOMREQUEST, '<STR_LIT>')<EOL>if bod is not None:<EOL><INDENT>curl.setopt(pycurl.POSTFIELDS, bod)<EOL>curl.setopt(pycurl.POSTFIELDSIZE, len(bod))<EOL><DEDENT><DEDENT>elif self.method == u'<STR_LIT>':<EOL><INDENT>curl.setopt(curl.NOBODY, <NUM_LIT:1>)<EOL>curl.setopt(curl.CUSTOMREQUEST, '<STR_LIT>')<EOL><DEDENT>elif self.method and self.method.upper() != '<STR_LIT:GET>':  <EOL><INDENT>curl.setopt(curl.CUSTOMREQUEST, self.method.upper())<EOL>if bod is not None:<EOL><INDENT>curl.setopt(pycurl.POSTFIELDS, bod)<EOL>curl.setopt(pycurl.POSTFIELDSIZE, len(bod))<EOL><DEDENT><DEDENT>head = self.get_headers(context=context)<EOL>head = copy.copy(head)  <EOL>if is_unicoded and u'<STR_LIT>' in head.keys():<EOL><INDENT>content = head[u'<STR_LIT>']<EOL>if u'<STR_LIT>' not in content:<EOL><INDENT>head[u'<STR_LIT>'] = content + u'<STR_LIT>'<EOL><DEDENT><DEDENT>if head:<EOL><INDENT>headers = [str(headername) + '<STR_LIT::>' + str(headervalue)<EOL>for headername, headervalue in head.items()]<EOL><DEDENT>else:<EOL><INDENT>headers = list()<EOL><DEDENT>headers.append("<STR_LIT>")<EOL>headers.append("<STR_LIT>")<EOL>curl.setopt(curl.HTTPHEADER, headers)<EOL>if self.curl_options:<EOL><INDENT>filterfunc = lambda x: x[<NUM_LIT:0>] is not None and x[<NUM_LIT:1>] is not None  <EOL>for (key, value) in ifilter(filterfunc, self.curl_options.items()):<EOL><INDENT>curl.setopt(getattr(curl, key), value)<EOL><DEDENT><DEDENT>return curl<EOL>
Create and mostly configure a curl object for test, reusing existing if possible