|
32 | 32 | # ======= |
33 | 33 |
|
34 | 34 | from io import StringIO, BytesIO, TextIOWrapper |
| 35 | +from collections import Mapping |
35 | 36 | import sys |
36 | 37 | import os |
37 | 38 | import urllib.parse |
38 | 39 | from email.parser import FeedParser |
| 40 | +from email.message import Message |
39 | 41 | from warnings import warn |
40 | 42 | import html |
41 | 43 | import locale |
@@ -472,18 +474,24 @@ def __init__(self, fp=None, headers=None, outerboundary=b'', |
472 | 474 | self.qs_on_post = environ['QUERY_STRING'] |
473 | 475 | if 'CONTENT_LENGTH' in environ: |
474 | 476 | headers['content-length'] = environ['CONTENT_LENGTH'] |
| 477 | + else: |
| 478 | + if not (isinstance(headers, (Mapping, Message))): |
| 479 | + raise TypeError("headers must be mapping or an instance of " |
| 480 | + "email.message.Message") |
| 481 | + self.headers = headers |
475 | 482 | if fp is None: |
476 | 483 | self.fp = sys.stdin.buffer |
477 | 484 | # self.fp.read() must return bytes |
478 | 485 | elif isinstance(fp, TextIOWrapper): |
479 | 486 | self.fp = fp.buffer |
480 | 487 | else: |
| 488 | + if not (hasattr(fp, 'read') and hasattr(fp, 'readline')): |
| 489 | + raise TypeError("fp must be file pointer") |
481 | 490 | self.fp = fp |
482 | 491 |
|
483 | 492 | self.encoding = encoding |
484 | 493 | self.errors = errors |
485 | 494 |
|
486 | | - self.headers = headers |
487 | 495 | if not isinstance(outerboundary, bytes): |
488 | 496 | raise TypeError('outerboundary must be bytes, not %s' |
489 | 497 | % type(outerboundary).__name__) |
@@ -642,7 +650,9 @@ def __len__(self): |
642 | 650 | """Dictionary style len(x) support.""" |
643 | 651 | return len(self.keys()) |
644 | 652 |
|
645 | | - def __nonzero__(self): |
| 653 | + def __bool__(self): |
| 654 | + if self.list is None: |
| 655 | + raise TypeError("Cannot be converted to bool.") |
646 | 656 | return bool(self.list) |
647 | 657 |
|
648 | 658 | def read_urlencoded(self): |
|
0 commit comments