diff --git a/README.md b/README.md index 14ce56d..7fd58d7 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,11 @@ pip install traceback-with-variables==2.1.1 conda install -c conda-forge traceback-with-variables=2.1.1 ``` +to use shorter `tb` alias in interactive mode call this once: +``` +python3 -c 'from traceback_with_variables.tb_alias import create_tb_alias as c; c()' +``` + ### 🚀 Quick Start Using without code editing, running your script/command/module: @@ -60,17 +65,15 @@ Using without code editing, Simplest usage in regular Python, for the whole program: +Simplest usage, for the whole program: ```python from traceback_with_variables import activate_by_import ``` - -Simplest usage in Jupyter or IPython, for the whole program: +or just (if you added an alias by the above command) ```python - from traceback_with_variables import activate_in_ipython_by_import + import tb.a ``` - Decorator, for a single function: ```python @prints_exc diff --git a/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_1_without_tb.txt b/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_1_without_tb.txt new file mode 100644 index 0000000..9a588fc --- /dev/null +++ b/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_1_without_tb.txt @@ -0,0 +1,4 @@ +Traceback (most recent call last): + File "...omitted for tests only.../code.py", line...omitted for tests only..., in + import tb.a +ModuleNotFoundError: No module named 'tb' diff --git a/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_2_without_tb.txt b/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_2_without_tb.txt new file mode 100644 index 0000000..9a588fc --- /dev/null +++ b/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_2_without_tb.txt @@ -0,0 +1,4 @@ +Traceback (most recent call last): + File "...omitted for tests only.../code.py", line...omitted for tests only..., in + import tb.a +ModuleNotFoundError: No module named 'tb' diff --git a/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_with_tb.txt b/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_with_tb.txt new file mode 100644 index 0000000..1f41027 --- /dev/null +++ b/tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_with_tb.txt @@ -0,0 +1,29 @@ +Traceback with variables (most recent call last): + File "...omitted for tests only.../code.py", line...omitted for tests only..., in + f() + ...skipped... 13 vars + File "...omitted for tests only.../code.py", line...omitted for tests only..., in f + return f( + n = 10001 + s1 = 'short string with n: 10001' + l1 = 'long string with 0..n: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 1...9951, 9952, 9953, 9954, 9955, 9956, 9957, 9958, 9959, 9960, 9961, 9962, 9963, 9964, 9965, 9966, 9967, 9968, 9969, 9970, 9971, 9972, 9973, 9974, 9975, 9976, 9977, 9978, 9979, 9980, 9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000' + us = Traceback (most recent call last): + File "...omitted for tests only.../core.py", line...omitted for tests only..., in _to_cropped_str + raw = print_(obj) + File "...omitted for tests only.../code.py", line...omitted for tests only..., in __repr__ + raise ValueError("please don't print me") + ValueError: please don't print me + + File "...omitted for tests only.../code.py", line...omitted for tests only..., in f + return 1 // (n * 0) + n = 10000 + s1 = 'short string with n: 10000' + l1 = 'long string with 0..n: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 1... 9950, 9951, 9952, 9953, 9954, 9955, 9956, 9957, 9958, 9959, 9960, 9961, 9962, 9963, 9964, 9965, 9966, 9967, 9968, 9969, 9970, 9971, 9972, 9973, 9974, 9975, 9976, 9977, 9978, 9979, 9980, 9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999' + us = Traceback (most recent call last): + File "...omitted for tests only.../core.py", line...omitted for tests only..., in _to_cropped_str + raw = print_(obj) + File "...omitted for tests only.../code.py", line...omitted for tests only..., in __repr__ + raise ValueError("please don't print me") + ValueError: please don't print me + +builtins.ZeroDivisionError: integer division or modulo by zero diff --git a/tests/test_module_alias.py b/tests/test_module_alias.py index 5c1ffd4..5cbc2bb 100644 --- a/tests/test_module_alias.py +++ b/tests/test_module_alias.py @@ -1,9 +1,8 @@ -import importlib import os import pytest -import traceback_with_variabless as twv +import traceback_with_variables as twv from traceback_with_variables.module_alias import ( create_alias, rm_alias, @@ -13,50 +12,50 @@ ) -ROOT_PATH = Path(twv.__file__).parent +ROOT_PATH = Path(twv.__file__).parent.parent TWV = 'traceback_with_variables' def test_module_name_to_path(): assert module_name_to_path('os') == Path(os.__file__) - assert module_name_to_path(TWV) = ROOT_PATH / TWV + assert module_name_to_path(TWV) == ROOT_PATH / TWV with pytest.raises(NoModuleError): module_name_to_path('nonexistant_module') def test_create_and_rm_alias(): - with pytest.raised(ValueError) as e: + with pytest.raises(ValueError) as e: create_alias('', TWV) - assert e.value == 'the alias must be non-empty') + assert str(e.value) == 'the alias must be non-empty' with pytest.raises(ValueError) as e: create_alias('bad name', TWV) - assert e.value == 'the alias must have only ascii lowecases, digits and underscores' + assert str(e.value) == 'the alias must have only ascii lowecases, digits and underscores' with pytest.raises(NoModuleError): create_alias('alias1', 'nonexistant_module') with pytest.raises(ValueError) as e: create_alias('os', TWV) - assert e.value == 'a module with the alias name already exists' + assert str(e.value) == 'a module with the alias name already exists' with pytest.raises(NoModuleError): rm_alias('alias1') with pytest.raises(ValueError) as e: rm_alias(TWV) - assert e.value == 'the module is not an alias' + assert str(e.value) == 'the module is not an alias' create_alias('alias1', TWV) with pytest.raises(ValueError) as e: create_alias('alias1', TWV) - assert e.value == 'a module with the alias name already exists' + assert str(e.value) == 'a module with the alias name already exists' with pytest.raises(ValueError) as e: create_alias('alias1', 'os') - assert e.value == 'a module with the alias name already exists' + assert str(e.value) == 'a module with the alias name already exists' rm_alias('alias1') with pytest.raises(NoModuleError): rm_alias('alias1') - os.symlink(str(ROOT_PATH / TWV), str(ROOT_PATH / 'alias1')) + os.symlink(str(ROOT_PATH / 'nonexistant'), str(ROOT_PATH / 'alias1')) with pytest.raises(ValueError) as e: create_alias('alias1', 'os') - assert e.value == 'the needed file system location already occupied' + assert str(e.value) == 'the needed file system location already occupied' os.remove(str(ROOT_PATH / 'alias1')) diff --git a/tests/test_tb_alias.py b/tests/test_tb_alias.py new file mode 100644 index 0000000..56a8641 --- /dev/null +++ b/tests/test_tb_alias.py @@ -0,0 +1,15 @@ +import pytest + +from traceback_with_variables.tb_alias import create_tb_alias, rm_tb_alias + +from tests.test_utils import run_code, dummies_code, tb_reg + + +def test_create_and_rm_tb_alias(tmp_path, tb_reg): + tb_reg(run_code(tmp_path, [], ['import tb.a', dummies_code, 'f()'], [], True), 'run 1 without tb') + + create_tb_alias() + tb_reg(run_code(tmp_path, [], ['import tb.a', dummies_code, 'f()'], [], True), 'run with tb') + + rm_tb_alias() + tb_reg(run_code(tmp_path, [], ['import tb.a', dummies_code, 'f()'], [], True), 'run 2 without tb') diff --git a/traceback_with_variables/module_alias.py b/traceback_with_variables/module_alias.py index 058c0fd..cecb825 100644 --- a/traceback_with_variables/module_alias.py +++ b/traceback_with_variables/module_alias.py @@ -30,16 +30,16 @@ def module_exists(name: str) -> bool: def create_alias(alias: str, module_name: str) -> None: - if not name: - raise ValueError('alias must be non-empty') - if any(c not in VALID_CHARS for c in name): + if not alias: + raise ValueError('the alias must be non-empty') + if any(c not in VALID_CHARS for c in alias): raise ValueError('the alias must have only ascii lowecases, digits and underscores') if module_exists(alias): raise ValueError('a module with the alias name already exists') module_path = module_name_to_path(module_name) try: - os.symlink(str(module_path), str(module_path.parent / name)) + os.symlink(str(module_path), str(module_path.parent / alias)) except FileExistsError as e: raise ValueError('the needed file system location already occupied')