|
5 | 5 | from base64 import b64decode as decode64 |
6 | 6 | from base64 import b64encode as encode64 |
7 | 7 |
|
8 | | -try: |
9 | | - from urllib.parse import quote, unquote |
10 | | - BYTES = True |
11 | | -except ImportError: |
12 | | - from urllib import quote, unquote |
13 | | - BYTES = False |
14 | | - |
| 8 | +from urllib.parse import quote, unquote |
15 | 9 |
|
16 | 10 | from .exceptions import InvalidCharset, InvalidDataURI, InvalidMimeType |
17 | 11 |
|
18 | | - |
19 | 12 | MIMETYPE_REGEX = r'[\w]+\/[\w\-\+\.]+' |
20 | 13 | _MIMETYPE_RE = re.compile('^{}$'.format(MIMETYPE_REGEX)) |
21 | 14 |
|
@@ -47,15 +40,12 @@ def make(cls, mimetype, charset, base64, data): |
47 | 40 | parts.extend([';charset=', charset]) |
48 | 41 | if base64: |
49 | 42 | parts.append(';base64') |
50 | | - if BYTES: |
51 | | - _charset = charset or 'utf-8' |
52 | | - if isinstance(data, bytes): |
53 | | - _data = data |
54 | | - else: |
55 | | - _data = bytes(data, _charset) |
56 | | - encoded_data = encode64(_data).decode(_charset).strip() |
| 43 | + _charset = charset or 'utf-8' |
| 44 | + if isinstance(data, bytes): |
| 45 | + _data = data |
57 | 46 | else: |
58 | | - encoded_data = encode64(data).strip() |
| 47 | + _data = bytes(data, _charset) |
| 48 | + encoded_data = encode64(_data).decode(_charset).strip() |
59 | 49 | else: |
60 | 50 | encoded_data = quote(data) |
61 | 51 | parts.extend([',', encoded_data]) |
@@ -120,12 +110,9 @@ def _parse(self): |
120 | 110 | charset = match.group('charset') or None |
121 | 111 |
|
122 | 112 | if match.group('base64'): |
123 | | - if BYTES: |
124 | | - _charset = charset or 'utf-8' |
125 | | - _data = bytes(match.group('data'), _charset) |
126 | | - data = decode64(_data) |
127 | | - else: |
128 | | - data = decode64(match.group('data')) |
| 113 | + _charset = charset or 'utf-8' |
| 114 | + _data = bytes(match.group('data'), _charset) |
| 115 | + data = decode64(_data) |
129 | 116 | else: |
130 | 117 | data = unquote(match.group('data')) |
131 | 118 |
|
|
0 commit comments