|
| 1 | +#!/usr/bin/env PYTHONHASHSEED=1234 python3 |
| 2 | + |
| 3 | +# Copyright 2014-2019 Brett Slatkin, Pearson Education Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Reproduce book environment |
| 18 | +import random |
| 19 | +random.seed(1234) |
| 20 | + |
| 21 | +import logging |
| 22 | +from pprint import pprint |
| 23 | +from sys import stdout as STDOUT |
| 24 | + |
| 25 | +# Write all output to a temporary directory |
| 26 | +import atexit |
| 27 | +import gc |
| 28 | +import io |
| 29 | +import os |
| 30 | +import tempfile |
| 31 | + |
| 32 | +TEST_DIR = tempfile.TemporaryDirectory() |
| 33 | +atexit.register(TEST_DIR.cleanup) |
| 34 | + |
| 35 | +# Make sure Windows processes exit cleanly |
| 36 | +OLD_CWD = os.getcwd() |
| 37 | +atexit.register(lambda: os.chdir(OLD_CWD)) |
| 38 | +os.chdir(TEST_DIR.name) |
| 39 | + |
| 40 | +def close_open_files(): |
| 41 | + everything = gc.get_objects() |
| 42 | + for obj in everything: |
| 43 | + if isinstance(obj, io.IOBase): |
| 44 | + obj.close() |
| 45 | + |
| 46 | +atexit.register(close_open_files) |
| 47 | + |
| 48 | + |
| 49 | +# Example 1 |
| 50 | +a = b'h\x65llo' |
| 51 | +print(list(a)) |
| 52 | +print(a) |
| 53 | + |
| 54 | + |
| 55 | +# Example 2 |
| 56 | +a = 'a\u0300 propos' |
| 57 | +print(list(a)) |
| 58 | +print(a) |
| 59 | + |
| 60 | + |
| 61 | +# Example 3 |
| 62 | +def to_str(bytes_or_str): |
| 63 | + if isinstance(bytes_or_str, bytes): |
| 64 | + value = bytes_or_str.decode('utf-8') |
| 65 | + else: |
| 66 | + value = bytes_or_str |
| 67 | + return value # Instance of str |
| 68 | + |
| 69 | +print(repr(to_str(b'foo'))) |
| 70 | +print(repr(to_str('bar'))) |
| 71 | + |
| 72 | + |
| 73 | +# Example 4 |
| 74 | +def to_bytes(bytes_or_str): |
| 75 | + if isinstance(bytes_or_str, str): |
| 76 | + value = bytes_or_str.encode('utf-8') |
| 77 | + else: |
| 78 | + value = bytes_or_str |
| 79 | + return value # Instance of bytes |
| 80 | + |
| 81 | +print(repr(to_bytes(b'foo'))) |
| 82 | +print(repr(to_bytes('bar'))) |
| 83 | + |
| 84 | + |
| 85 | +# Example 5 |
| 86 | +print(b'one' + b'two') |
| 87 | +print('one' + 'two') |
| 88 | + |
| 89 | + |
| 90 | +# Example 6 |
| 91 | +try: |
| 92 | + b'one' + 'two' |
| 93 | +except: |
| 94 | + logging.exception('Expected') |
| 95 | +else: |
| 96 | + assert False |
| 97 | + |
| 98 | + |
| 99 | +# Example 7 |
| 100 | +try: |
| 101 | + 'one' + b'two' |
| 102 | +except: |
| 103 | + logging.exception('Expected') |
| 104 | +else: |
| 105 | + assert False |
| 106 | + |
| 107 | + |
| 108 | +# Example 8 |
| 109 | +assert b'red' > b'blue' |
| 110 | +assert 'red' > 'blue' |
| 111 | + |
| 112 | + |
| 113 | +# Example 9 |
| 114 | +try: |
| 115 | + assert 'red' > b'blue' |
| 116 | +except: |
| 117 | + logging.exception('Expected') |
| 118 | +else: |
| 119 | + assert False |
| 120 | + |
| 121 | + |
| 122 | +# Example 10 |
| 123 | +try: |
| 124 | + assert b'blue' < 'red' |
| 125 | +except: |
| 126 | + logging.exception('Expected') |
| 127 | +else: |
| 128 | + assert False |
| 129 | + |
| 130 | + |
| 131 | +# Example 11 |
| 132 | +print(b'foo' == 'foo') |
| 133 | + |
| 134 | + |
| 135 | +# Example 12 |
| 136 | +print(b'red %s' % b'blue') |
| 137 | +print('red %s' % 'blue') |
| 138 | + |
| 139 | + |
| 140 | +# Example 13 |
| 141 | +try: |
| 142 | + print(b'red %s' % 'blue') |
| 143 | +except: |
| 144 | + logging.exception('Expected') |
| 145 | +else: |
| 146 | + assert False |
| 147 | + |
| 148 | + |
| 149 | +# Example 14 |
| 150 | +print('red %s' % b'blue') |
| 151 | + |
| 152 | + |
| 153 | +# Example 15 |
| 154 | +try: |
| 155 | + with open('data.bin', 'w') as f: |
| 156 | + f.write(b'\xf1\xf2\xf3\xf4\xf5') |
| 157 | +except: |
| 158 | + logging.exception('Expected') |
| 159 | +else: |
| 160 | + assert False |
| 161 | + |
| 162 | + |
| 163 | +# Example 16 |
| 164 | +with open('data.bin', 'wb') as f: |
| 165 | + f.write(b'\xf1\xf2\xf3\xf4\xf5') |
| 166 | + |
| 167 | + |
| 168 | +# Example 17 |
| 169 | +try: |
| 170 | + # Silently force UTF-8 here to make sure this test fails on |
| 171 | + # all platforms. cp1252 considers these bytes valid on Windows. |
| 172 | + real_open = open |
| 173 | + def open(*args, **kwargs): |
| 174 | + kwargs['encoding'] = 'utf-8' |
| 175 | + return real_open(*args, **kwargs) |
| 176 | + |
| 177 | + with open('data.bin', 'r') as f: |
| 178 | + data = f.read() |
| 179 | +except: |
| 180 | + logging.exception('Expected') |
| 181 | +else: |
| 182 | + assert False |
| 183 | + |
| 184 | + |
| 185 | +# Example 18 |
| 186 | +# Restore the overloaded open above. |
| 187 | +open = real_open |
| 188 | + |
| 189 | +with open('data.bin', 'rb') as f: |
| 190 | + data = f.read() |
| 191 | + |
| 192 | +assert data == b'\xf1\xf2\xf3\xf4\xf5' |
| 193 | + |
| 194 | + |
| 195 | +# Example 19 |
| 196 | +with open('data.bin', 'r', encoding='cp1252') as f: |
| 197 | + data = f.read() |
| 198 | + |
| 199 | +assert data == 'ñòóôõ' |
0 commit comments