| 1 |
|
| 2 |
import friendly_traceback |
| 3 |
|
| 4 |
spam_missing_global = 1 |
| 5 |
spam_missing_both = 1 |
| 6 |
|
| 7 |
def outer_missing_global(): |
| 8 |
def inner(): |
| 9 |
spam_missing_global += 1 |
| 10 |
inner() |
| 11 |
|
| 12 |
def outer_missing_nonlocal(): |
| 13 |
spam_missing_nonlocal = 1 |
| 14 |
def inner(): |
| 15 |
spam_missing_nonlocal += 1 |
| 16 |
inner() |
| 17 |
|
| 18 |
def outer_missing_both(): |
| 19 |
spam_missing_both = 2 |
| 20 |
def inner(): |
| 21 |
spam_missing_both += 1 |
| 22 |
inner() |
| 23 |
|
| 24 |
|
| 25 |
def test_Missing_global(): |
| 26 |
try: |
| 27 |
outer_missing_global() |
| 28 |
except UnboundLocalError as e: |
| 29 |
message = str(e) |
| 30 |
friendly_traceback.explain_traceback(redirect="capture") |
| 31 |
result = friendly_traceback.get_output() |
| 32 |
|
| 33 |
assert ("local variable 'spam_missing_global' referenced" in result or |
| 34 |
"cannot access local variable 'spam_missing_global'" in result) |
| 35 |
if friendly_traceback.get_lang() == "en": |
| 36 |
assert ( |
| 37 |
"Did you forget to add `global spam_missing_global`?\n" |
| 38 |
in result |
| 39 |
) |
| 40 |
if friendly_traceback._writing_docs: |
| 41 |
return result, message |
| 42 |
|
| 43 |
|
| 44 |
def test_Missing_nonlocal(): |
| 45 |
try: |
| 46 |
outer_missing_nonlocal() |
| 47 |
except UnboundLocalError as e: |
| 48 |
message = str(e) |
| 49 |
friendly_traceback.explain_traceback(redirect="capture") |
| 50 |
result = friendly_traceback.get_output() |
| 51 |
|
| 52 |
assert ("local variable 'spam_missing_nonlocal' referenced" in result |
| 53 |
or "cannot access local variable 'spam_missing_nonlocal'" in result) |
| 54 |
if friendly_traceback.get_lang() == "en": |
| 55 |
assert ( |
| 56 |
"Did you forget to add `nonlocal spam_missing_nonlocal`?\n" |
| 57 |
in result |
| 58 |
) |
| 59 |
if friendly_traceback._writing_docs: |
| 60 |
return result, message |
| 61 |
|
| 62 |
|
| 63 |
def test_Missing_both(): |
| 64 |
try: |
| 65 |
outer_missing_both() |
| 66 |
except UnboundLocalError as e: |
| 67 |
message = str(e) |
| 68 |
friendly_traceback.explain_traceback(redirect="capture") |
| 69 |
result = friendly_traceback.get_output() |
| 70 |
|
| 71 |
assert ("local variable 'spam_missing_both' referenced" in result |
| 72 |
or "cannot access local variable 'spam_missing_both'" in result) |
| 73 |
if friendly_traceback.get_lang() == "en": |
| 74 |
assert "either `global spam_missing_both`" in result |
| 75 |
assert "`nonlocal spam_missing_both`" in result |
| 76 |
|
| 77 |
if friendly_traceback._writing_docs: |
| 78 |
return result, message |
| 79 |
|
| 80 |
|
| 81 |
def test_Typo_in_local(): |
| 82 |
|
| 83 |
def test1(): |
| 84 |
alpha1 = 1 |
| 85 |
alpha2 += 1 |
| 86 |
|
| 87 |
try: |
| 88 |
test1() |
| 89 |
except UnboundLocalError: |
| 90 |
friendly_traceback.explain_traceback(redirect="capture") |
| 91 |
result = friendly_traceback.get_output() |
| 92 |
|
| 93 |
assert ("local variable 'alpha2' referenced before assignment" in result |
| 94 |
or "cannot access local variable 'alpha2'" in result) |
| 95 |
if friendly_traceback.get_lang() == "en": |
| 96 |
assert "similar name `alpha1` was found" in result |
| 97 |
|
| 98 |
def test2(): |
| 99 |
alpha1 = 1 |
| 100 |
alpha2 = 1 |
| 101 |
alpha3 += 1 |
| 102 |
|
| 103 |
try: |
| 104 |
test2() |
| 105 |
except UnboundLocalError as e: |
| 106 |
message = str(e) |
| 107 |
friendly_traceback.explain_traceback(redirect="capture") |
| 108 |
result = friendly_traceback.get_output() |
| 109 |
|
| 110 |
assert ("local variable 'alpha3' referenced before assignment" in result |
| 111 |
or "cannot access local variable 'alpha3'" in result) |
| 112 |
if friendly_traceback.get_lang() == "en": |
| 113 |
assert "perhaps you meant one of the following" in result |
| 114 |
|
| 115 |
if friendly_traceback._writing_docs: |
| 116 |
return result, message |
| 117 |
|
| 118 |
|
| 119 |
def test_Using_name_of_builtin(): |
| 120 |
def dist(points): |
| 121 |
max = max(points) |
| 122 |
min = min(points) |
| 123 |
return max - min |
| 124 |
try: |
| 125 |
dist([]) |
| 126 |
except UnboundLocalError as e: |
| 127 |
message = str(e) |
| 128 |
friendly_traceback.explain_traceback(redirect="capture") |
| 129 |
result = friendly_traceback.get_output() |
| 130 |
|
| 131 |
assert ("local variable 'max' referenced" in result |
| 132 |
or "cannot access local variable 'max'" in result) |
| 133 |
if friendly_traceback.get_lang() == "en": |
| 134 |
assert "`max` is a Python builtin function." in result |
| 135 |
if friendly_traceback._writing_docs: |
| 136 |
return result, message |
| 137 |
|
| 138 |
if __name__ == "__main__": |
| 139 |
print(test_Missing_global()[0]) |