diff --git a/.travis.yml b/.travis.yml index bd2e4c4..3ef1b03 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,11 +3,12 @@ language: python python: - 2.7 -- 3.4 - 3.5 - 3.6 - 3.7 -- 3.8-dev +- 3.8 +- 3.9 +- 3.10-dev - pypy2.7-6.0 - pypy3.5 @@ -26,6 +27,7 @@ matrix: - env: TOXENV=flake8 - env: TOXENV=pylint - env: TOXENV=bandit + - python: 3.10-dev jobs: include: diff --git a/ADVANCED_USAGE.md b/ADVANCED_USAGE.md new file mode 100644 index 0000000..200bbec --- /dev/null +++ b/ADVANCED_USAGE.md @@ -0,0 +1,98 @@ +# Advanced Usage # + +Use `watch_explode` to expand values to see all their attributes or items of lists/dictionaries: + +```python +@pysnooper.snoop(watch_explode=('foo', 'self')) +``` + +`watch_explode` will automatically guess how to expand the expression passed to it based on its class. You can be more specific by using one of the following classes: + +```python +import pysnooper + +@pysnooper.snoop(watch=( + pysnooper.Attrs('x'), # attributes + pysnooper.Keys('y'), # mapping (e.g. dict) items + pysnooper.Indices('z'), # sequence (e.g. list/tuple) items +)) +``` + +Exclude specific keys/attributes/indices with the `exclude` parameter, e.g. `Attrs('x', exclude=('_foo', '_bar'))`. + +Add a slice after `Indices` to only see the values within that slice, e.g. `Indices('z')[-3:]`. + +```console +$ export PYSNOOPER_DISABLED=1 # This makes PySnooper not do any snooping +``` + +This will output lines like: + +``` +Modified var:.. foo[2] = 'whatever' +New var:....... self.baz = 8 +``` + +Start all snoop lines with a prefix, to grep for them easily: + +```python +@pysnooper.snoop(prefix='ZZZ ') +``` + +Remove all machine-related data (paths, timestamps, memory addresses) to compare with other traces easily: + +```python +@pysnooper.snoop(normalize=True) +``` + +On multi-threaded apps identify which thread are snooped in output: + +```python +@pysnooper.snoop(thread_info=True) +``` + +PySnooper supports decorating generators. + +If you decorate a class with `snoop`, it'll automatically apply the decorator to all the methods. (Not including properties and other special cases.) + +You can also customize the repr of an object: + +```python +def large(l): + return isinstance(l, list) and len(l) > 5 + +def print_list_size(l): + return 'list(size={})'.format(len(l)) + +def print_ndarray(a): + return 'ndarray(shape={}, dtype={})'.format(a.shape, a.dtype) + +@pysnooper.snoop(custom_repr=((large, print_list_size), (numpy.ndarray, print_ndarray))) +def sum_to_x(x): + l = list(range(x)) + a = numpy.zeros((10,10)) + return sum(l) + +sum_to_x(10000) +``` + +You will get `l = list(size=10000)` for the list, and `a = ndarray(shape=(10, 10), dtype=float64)` for the ndarray. +The `custom_repr` are matched in order, if one condition matches, no further conditions will be checked. + +Variables and exceptions get truncated to 100 characters by default. You +can customize that: + +```python + @pysnooper.snoop(max_variable_length=200) +``` + +You can also use `max_variable_length=None` to never truncate them. + +Use `relative_time=True` to show timestamps relative to start time rather than +wall time. + +The output is colored for easy viewing by default, except on Windows. Disable colors like so: + +```python + @pysnooper.snoop(color=False) +```` diff --git a/AUTHORS b/AUTHORS index adb2971..6053f6d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -19,3 +19,12 @@ Alexander Bersenev Xiang Gao pikez Jonathan Reichelt Gjertsen +Guoqiang Ding +Itamar.Raviv +iory +Mark Blakeney +Yael Mintz +Lumír 'Frenzy' Balhar +Lukas Klenk +sizhky +Andrej730 diff --git a/README.md b/README.md index 6112552..35df35a 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ -# PySnooper - Never use print for debugging again # +# PySnooper - Never use print for debugging again -[![Travis CI](https://img.shields.io/travis/cool-RR/PySnooper/master.svg)](https://travis-ci.org/cool-RR/PySnooper) +**PySnooper** is a poor man's debugger. If you've used Bash, it's like `set -x` for Python, except it's fancier. -**PySnooper** is a poor man's debugger. - -You're trying to figure out why your Python code isn't doing what you think it should be doing. You'd love to use a full-fledged debugger with breakpoints and watches, but you can't be bothered to set one up right now. +Your story: You're trying to figure out why your Python code isn't doing what you think it should be doing. You'd love to use a full-fledged debugger with breakpoints and watches, but you can't be bothered to set one up right now. You want to know which lines are running and which aren't, and what the values of the local variables are. @@ -14,7 +12,7 @@ Most people would use `print` lines, in strategic locations, some of them showin What makes **PySnooper** stand out from all other code intelligence tools? You can use it in your shitty, sprawling enterprise codebase without having to do any setup. Just slap the decorator on, as shown below, and redirect the output to a dedicated log file by specifying its path as the first argument. -# Example # +## Example We're writing a function that converts a number to binary, by returning a list of bits. Let's snoop on it by adding the `@pysnooper.snoop()` decorator: @@ -36,35 +34,7 @@ number_to_bits(6) ``` The output to stderr is: -``` -Source path:... /my_code/foo.py -Starting var:.. number = 6 -15:29:11.327032 call 4 def number_to_bits(number): -15:29:11.327032 line 5 if number: -15:29:11.327032 line 6 bits = [] -New var:....... bits = [] -15:29:11.327032 line 7 while number: -15:29:11.327032 line 8 number, remainder = divmod(number, 2) -New var:....... remainder = 0 -Modified var:.. number = 3 -15:29:11.327032 line 9 bits.insert(0, remainder) -Modified var:.. bits = [0] -15:29:11.327032 line 7 while number: -15:29:11.327032 line 8 number, remainder = divmod(number, 2) -Modified var:.. number = 1 -Modified var:.. remainder = 1 -15:29:11.327032 line 9 bits.insert(0, remainder) -Modified var:.. bits = [1, 0] -15:29:11.327032 line 7 while number: -15:29:11.327032 line 8 number, remainder = divmod(number, 2) -Modified var:.. number = 0 -15:29:11.327032 line 9 bits.insert(0, remainder) -Modified var:.. bits = [1, 1, 0] -15:29:11.327032 line 7 while number: -15:29:11.327032 line 10 return bits -15:29:11.327032 return 10 return bits -Return value:.. [1, 1, 0] -``` +![](https://i.imgur.com/TrF3VVj.jpg) Or if you don't want to trace an entire function, you can wrap the relevant part in a `with` block: @@ -99,9 +69,10 @@ New var:....... upper = 832 74 453.0 832 New var:....... mid = 453.0 09:37:35.882486 line 13 print(lower, mid, upper) +Elapsed time: 00:00:00.000344 ``` -# Features # +## Features If stderr is not easily accessible for you, you can redirect the output to a file: @@ -117,112 +88,67 @@ See values of some expressions that aren't local variables: @pysnooper.snoop(watch=('foo.bar', 'self.x["whatever"]')) ``` -Expand values to see all their attributes or items of lists/dictionaries: - -```python -@pysnooper.snoop(watch_explode=('foo', 'self')) -``` - -This will output lines like: - -``` -Modified var:.. foo[2] = 'whatever' -New var:....... self.baz = 8 -``` - -(see [Advanced Usage](#advanced-usage) for more control) - Show snoop lines for functions that your function calls: ```python @pysnooper.snoop(depth=2) ``` -Start all snoop lines with a prefix, to grep for them easily: +**See [Advanced Usage](https://github.com/cool-RR/PySnooper/blob/master/ADVANCED_USAGE.md) for more options.** <------ -```python -@pysnooper.snoop(prefix='ZZZ ') -``` -On multi-threaded apps identify which thread are snooped in output: +## Installation with Pip -```python -@pysnooper.snoop(thread_info=True) -``` +The best way to install **PySnooper** is with Pip: -PySnooper supports decorating generators. - -If you decorate a class with `snoop`, it'll automatically apply the decorator to all the methods. (Not including properties and other special cases.) - -You can also customize the repr of an object: - -```python -def large(l): - return isinstance(l, list) and len(l) > 5 - -def print_list_size(l): - return 'list(size={})'.format(len(l)) - -def print_ndarray(a): - return 'ndarray(shape={}, dtype={})'.format(a.shape, a.dtype) - -@pysnooper.snoop(custom_repr=((large, print_list_size), (numpy.ndarray, print_ndarray))) -def sum_to_x(x): - l = list(range(x)) - a = numpy.zeros((10,10)) - return sum(l) - -sum_to_x(10000) -``` - -You will get `l = list(size=10000)` for the list, and `a = ndarray(shape=(10, 10), dtype=float64)` for the ndarray. -The `custom_repr` are matched in order, if one condition matches, no further conditions will be checked. - -# Installation # - -You can install **PySnooper** by: - -* pip: ```console $ pip install pysnooper ``` -* conda with conda-forge channel: +## Other installation options + +Conda with conda-forge channel: + ```console $ conda install -c conda-forge pysnooper ``` -# Advanced Usage # - -`watch_explode` will automatically guess how to expand the expression passed to it based on its class. You can be more specific by using one of the following classes: - -```python -import pysnooper - -@pysnooper.snoop(watch=( - pysnooper.Attrs('x'), # attributes - pysnooper.Keys('y'), # mapping (e.g. dict) items - pysnooper.Indices('z'), # sequence (e.g. list/tuple) items -)) -``` - -Exclude specific keys/attributes/indices with the `exclude` parameter, e.g. `Attrs('x', exclude=('_foo', '_bar'))`. - -Add a slice after `Indices` to only see the values within that slice, e.g. `Indices('z')[-3:]`. +Arch Linux: ```console -$ export PYSNOOPER_DISABLED=1 # This makes PySnooper not do any snooping +$ yay -S python-pysnooper ``` -# License # +Fedora Linux: + +```console +$ dnf install python3-pysnooper +``` + + +## Citing PySnooper + +If you use PySnooper in academic work, please use this citation format: + +```bibtex +@software{rachum2019pysnooper, + title={PySnooper: Never use print for debugging again}, + author={Rachum, Ram and Hall, Alex and Yanokura, Iori and others}, + year={2019}, + month={jun}, + publisher={PyCon Israel}, + doi={10.5281/zenodo.10462459}, + url={https://github.com/cool-RR/PySnooper} +} +``` + + +## License Copyright (c) 2019 Ram Rachum and collaborators, released under the MIT license. -I provide [Development services in Python and Django](https://chipmunkdev.com -) and I [give Python workshops](http://pythonworkshops.co/) to teach people -Python and related topics. -# Media Coverage # +## Media Coverage [Hacker News thread](https://news.ycombinator.com/item?id=19717786) and [/r/Python Reddit thread](https://www.reddit.com/r/Python/comments/bg0ida/pysnooper_never_use_print_for_debugging_again/) (22 April 2019) diff --git a/misc/IDE files/PySnooper.wpr b/misc/IDE files/PySnooper.wpr index b3bf5c9..64e9d0b 100644 --- a/misc/IDE files/PySnooper.wpr +++ b/misc/IDE files/PySnooper.wpr @@ -1,26 +1,27 @@ #!wing -#!version=7.0 +#!version=11.0 ################################################################## # Wing project file # ################################################################## [project attributes] proj.directory-list = [{'dirloc': loc('../..'), - 'excludes': [u'PySnooper.egg-info', - u'dist', - u'build'], + 'excludes': ['dist', + '.tox', + 'htmlcov', + 'build', + '.ipynb_checkpoints', + 'PySnooper.egg-info'], 'filter': '*', 'include_hidden': False, 'recursive': True, 'watch_for_changes': True}] proj.file-type = 'shared' proj.home-dir = loc('../..') -proj.launch-config = {loc('../../../../../../../Program Files/Python37/Scripts/pasteurize-script.py'): ('p'\ - 'roject', - (u'"c:\\Users\\Administrator\\Documents\\Python Projects\\PySnooper\\pysnooper" "c:\\Users\\Administrator\\Documents\\Python Projects\\PySnooper\\tests"', +proj.launch-config = {loc('../../../../../../Program Files/Python37/Scripts/pasteurize-script.py'): ('project', + ('"c:\\Users\\Administrator\\Documents\\Python Projects\\PySnooper\\pysnooper" "c:\\Users\\Administrator\\Documents\\Python Projects\\PySnooper\\tests"', '')), - loc('../../../../../Dropbox/Scripts and shortcuts/_simplify3d_add_m600.py'): ('p'\ - 'roject', - (u'"C:\\Users\\Administrator\\Dropbox\\Desktop\\foo.gcode"', + loc('../../../../../Dropbox/Scripts and shortcuts/_simplify3d_add_m600.py'): ('project', + ('"C:\\Users\\Administrator\\Dropbox\\Desktop\\foo.gcode"', ''))} testing.auto-test-file-specs = (('regex', 'pysnooper/tests.*/test[^./]*.py.?$'),) diff --git a/misc/generate_authors.py b/misc/generate_authors.py index 582e077..f196843 100644 --- a/misc/generate_authors.py +++ b/misc/generate_authors.py @@ -19,6 +19,11 @@ You probably want to run it this way: import subprocess import sys +# This is used for people who show up more than once: +deny_list = frozenset(( + 'Lumir Balhar', +)) + def drop_recurrences(iterable): s = set() @@ -37,10 +42,10 @@ def iterate_authors_by_chronological_order(branch): stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) log_lines = log_call.stdout.decode('utf-8').split('\n') - - return drop_recurrences( - (line.strip().split(";")[1] for line in log_lines) - ) + + authors = tuple(line.strip().split(";")[1] for line in log_lines) + authors = (author for author in authors if author not in deny_list) + return drop_recurrences(authors) def print_authors(branch): @@ -54,5 +59,4 @@ if __name__ == '__main__': branch = sys.argv[1] except IndexError: branch = 'master' - print_authors(branch) diff --git a/misc/output.jpg b/misc/output.jpg new file mode 100644 index 0000000..9df3d19 Binary files /dev/null and b/misc/output.jpg differ diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..fed528d --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" diff --git a/pysnooper/__init__.py b/pysnooper/__init__.py index d590647..027a7f2 100644 --- a/pysnooper/__init__.py +++ b/pysnooper/__init__.py @@ -24,7 +24,7 @@ import collections __VersionInfo = collections.namedtuple('VersionInfo', ('major', 'minor', 'micro')) -__version__ = '0.2.7' +__version__ = '1.2.3' __version_info__ = __VersionInfo(*(map(int, __version__.split('.')))) del collections, __VersionInfo # Avoid polluting the namespace diff --git a/pysnooper/pycompat.py b/pysnooper/pycompat.py index 1748ccd..58c0822 100644 --- a/pysnooper/pycompat.py +++ b/pysnooper/pycompat.py @@ -6,6 +6,7 @@ import abc import os import inspect import sys +import datetime as datetime_module PY3 = (sys.version_info[0] == 3) PY2 = not PY3 @@ -47,15 +48,51 @@ try: except AttributeError: iscoroutinefunction = lambda whatever: False # Lolz +try: + isasyncgenfunction = inspect.isasyncgenfunction +except AttributeError: + isasyncgenfunction = lambda whatever: False # Lolz + if PY3: string_types = (str,) text_type = str + binary_type = bytes else: string_types = (basestring,) text_type = unicode + binary_type = str + try: from collections import abc as collections_abc except ImportError: # Python 2.7 import collections as collections_abc + +if sys.version_info[:2] >= (3, 6): + time_isoformat = datetime_module.time.isoformat +else: + def time_isoformat(time, timespec='microseconds'): + assert isinstance(time, datetime_module.time) + if timespec != 'microseconds': + raise NotImplementedError + result = '{:02d}:{:02d}:{:02d}.{:06d}'.format( + time.hour, time.minute, time.second, time.microsecond + ) + assert len(result) == 15 + return result + + +def timedelta_format(timedelta): + time = (datetime_module.datetime.min + timedelta).time() + return time_isoformat(time, timespec='microseconds') + +def timedelta_parse(s): + hours, minutes, seconds, microseconds = map( + int, + s.replace('.', ':').split(':') + ) + return datetime_module.timedelta(hours=hours, minutes=minutes, + seconds=seconds, + microseconds=microseconds) + diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index f9625c3..59509f2 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -20,18 +20,28 @@ if pycompat.PY2: ipython_filename_pattern = re.compile('^$') +ansible_filename_pattern = re.compile(r'^(.+\.zip)[/|\\](ansible[/|\\]modules[/|\\].+\.py)$') +ipykernel_filename_pattern = re.compile(r'^/var/folders/.*/ipykernel_[0-9]+/[0-9]+.py$') +RETURN_OPCODES = { + 'RETURN_GENERATOR', 'RETURN_VALUE', 'RETURN_CONST', + 'INSTRUMENTED_RETURN_GENERATOR', 'INSTRUMENTED_RETURN_VALUE', + 'INSTRUMENTED_RETURN_CONST', 'YIELD_VALUE', 'INSTRUMENTED_YIELD_VALUE' +} -def get_local_reprs(frame, watch=(), custom_repr=()): +def get_local_reprs(frame, watch=(), custom_repr=(), max_length=None, normalize=False): code = frame.f_code - vars_order = code.co_varnames + code.co_cellvars + code.co_freevars + tuple(frame.f_locals.keys()) + vars_order = (code.co_varnames + code.co_cellvars + code.co_freevars + + tuple(frame.f_locals.keys())) - result_items = [(key, utils.get_shortish_repr(value, custom_repr=custom_repr)) for key, value in frame.f_locals.items()] + result_items = [(key, utils.get_shortish_repr(value, custom_repr, + max_length, normalize)) + for key, value in frame.f_locals.items()] result_items.sort(key=lambda key_value: vars_order.index(key_value[0])) result = collections.OrderedDict(result_items) for variable in watch: - result.update(sorted(variable.items(frame))) + result.update(sorted(variable.items(frame, normalize))) return result @@ -64,7 +74,16 @@ def get_path_and_source_from_frame(frame): source = source.splitlines() if source is None: ipython_filename_match = ipython_filename_pattern.match(file_name) - if ipython_filename_match: + ansible_filename_match = ansible_filename_pattern.match(file_name) + ipykernel_filename_match = ipykernel_filename_pattern.match(file_name) + if ipykernel_filename_match: + try: + import linecache + _, _, source, _ = linecache.cache.get(file_name) + source = [line.rstrip() for line in source] # remove '\n' at the end + except Exception: + pass + elif ipython_filename_match: entry_number = int(ipython_filename_match.group(1)) try: import IPython @@ -74,6 +93,13 @@ def get_path_and_source_from_frame(frame): source = source_chunk.splitlines() except Exception: pass + elif ansible_filename_match: + try: + import zipfile + archive_file = zipfile.ZipFile(ansible_filename_match.group(1), 'r') + source = archive_file.read(ansible_filename_match.group(2).replace('\\', '/')).splitlines() + except Exception: + pass else: try: with open(file_name, 'rb') as fp: @@ -185,20 +211,32 @@ class Tracer: Customize how values are represented as strings:: - @pysnooper.snoop(custom_repr=((type1, custom_repr_func1), (condition2, custom_repr_func2), ...)) + @pysnooper.snoop(custom_repr=((type1, custom_repr_func1), + (condition2, custom_repr_func2), ...)) + + Variables and exceptions get truncated to 100 characters by default. You + can customize that: + + @pysnooper.snoop(max_variable_length=200) + + You can also use `max_variable_length=None` to never truncate them. + + Show timestamps relative to start time rather than wall time:: + + @pysnooper.snoop(relative_time=True) + + The output is colored for easy viewing by default, except on Windows + (but can be enabled by setting `color=True`). + + Disable colors like so: + + @pysnooper.snoop(color=False) ''' - def __init__( - self, - output=None, - watch=(), - watch_explode=(), - depth=1, - prefix='', - overwrite=False, - thread_info=False, - custom_repr=(), - ): + def __init__(self, output=None, watch=(), watch_explode=(), depth=1, + prefix='', overwrite=False, thread_info=False, custom_repr=(), + max_variable_length=100, normalize=False, relative_time=False, + color=sys.platform in ('linux', 'linux2', 'cygwin', 'darwin')): self._write = get_write_function(output, overwrite) self.watch = [ @@ -209,6 +247,7 @@ class Tracer: for v in utils.ensure_tuple(watch_explode) ] self.frame_to_local_reprs = {} + self.start_times = {} self.depth = depth self.prefix = prefix self.thread_info = thread_info @@ -222,6 +261,35 @@ class Tracer: custom_repr = (custom_repr,) self.custom_repr = custom_repr self.last_source_path = None + self.max_variable_length = max_variable_length + self.normalize = normalize + self.relative_time = relative_time + self.color = color and (output is None) + + if self.color: + self._FOREGROUND_BLUE = '\x1b[34m' + self._FOREGROUND_CYAN = '\x1b[36m' + self._FOREGROUND_GREEN = '\x1b[32m' + self._FOREGROUND_MAGENTA = '\x1b[35m' + self._FOREGROUND_RED = '\x1b[31m' + self._FOREGROUND_RESET = '\x1b[39m' + self._FOREGROUND_YELLOW = '\x1b[33m' + self._STYLE_BRIGHT = '\x1b[1m' + self._STYLE_DIM = '\x1b[2m' + self._STYLE_NORMAL = '\x1b[22m' + self._STYLE_RESET_ALL = '\x1b[0m' + else: + self._FOREGROUND_BLUE = '' + self._FOREGROUND_CYAN = '' + self._FOREGROUND_GREEN = '' + self._FOREGROUND_MAGENTA = '' + self._FOREGROUND_RED = '' + self._FOREGROUND_RESET = '' + self._FOREGROUND_YELLOW = '' + self._STYLE_BRIGHT = '' + self._STYLE_DIM = '' + self._STYLE_NORMAL = '' + self._STYLE_RESET_ALL = '' def __call__(self, function_or_class): if DISABLED: @@ -267,7 +335,8 @@ class Tracer: method, incoming = gen.throw, e if pycompat.iscoroutinefunction(function): - # return decorate(function, coroutine_wrapper) + raise NotImplementedError + if pycompat.isasyncgenfunction(function): raise NotImplementedError elif inspect.isgeneratorfunction(function): return generator_wrapper @@ -281,6 +350,7 @@ class Tracer: def __enter__(self): if DISABLED: return + thread_global.__dict__.setdefault('depth', -1) calling_frame = inspect.currentframe().f_back if not self._is_internal_frame(calling_frame): calling_frame.f_trace = self.trace @@ -290,6 +360,7 @@ class Tracer: 'original_trace_functions', [] ) stack.append(sys.gettrace()) + self.start_times[calling_frame] = datetime_module.datetime.now() sys.settrace(self.trace) def __exit__(self, exc_type, exc_value, exc_traceback): @@ -301,6 +372,25 @@ class Tracer: self.target_frames.discard(calling_frame) self.frame_to_local_reprs.pop(calling_frame, None) + ### Writing elapsed time: ############################################# + # # + _FOREGROUND_YELLOW = self._FOREGROUND_YELLOW + _STYLE_DIM = self._STYLE_DIM + _STYLE_NORMAL = self._STYLE_NORMAL + _STYLE_RESET_ALL = self._STYLE_RESET_ALL + + start_time = self.start_times.pop(calling_frame) + duration = datetime_module.datetime.now() - start_time + elapsed_time_string = pycompat.timedelta_format(duration) + indent = ' ' * 4 * (thread_global.depth + 1) + self.write( + '{indent}{_FOREGROUND_YELLOW}{_STYLE_DIM}' + 'Elapsed time: {_STYLE_NORMAL}{elapsed_time_string}' + '{_STYLE_RESET_ALL}'.format(**locals()) + ) + # # + ### Finished writing elapsed time. #################################### + def _is_internal_frame(self, frame): return frame.f_code.co_filename == Tracer.__enter__.__code__.co_filename @@ -337,45 +427,86 @@ class Tracer: else: return None - thread_global.__dict__.setdefault('depth', -1) + # # + ### Finished checking whether we should trace this line. ############## + if event == 'call': thread_global.depth += 1 indent = ' ' * 4 * thread_global.depth - # # - ### Finished checking whether we should trace this line. ############## + _FOREGROUND_BLUE = self._FOREGROUND_BLUE + _FOREGROUND_CYAN = self._FOREGROUND_CYAN + _FOREGROUND_GREEN = self._FOREGROUND_GREEN + _FOREGROUND_MAGENTA = self._FOREGROUND_MAGENTA + _FOREGROUND_RED = self._FOREGROUND_RED + _FOREGROUND_RESET = self._FOREGROUND_RESET + _FOREGROUND_YELLOW = self._FOREGROUND_YELLOW + _STYLE_BRIGHT = self._STYLE_BRIGHT + _STYLE_DIM = self._STYLE_DIM + _STYLE_NORMAL = self._STYLE_NORMAL + _STYLE_RESET_ALL = self._STYLE_RESET_ALL + + ### Making timestamp: ################################################# + # # + if self.normalize: + timestamp = ' ' * 15 + elif self.relative_time: + try: + start_time = self.start_times[frame] + except KeyError: + start_time = self.start_times[frame] = \ + datetime_module.datetime.now() + duration = datetime_module.datetime.now() - start_time + timestamp = pycompat.timedelta_format(duration) + else: + timestamp = pycompat.time_isoformat( + datetime_module.datetime.now().time(), + timespec='microseconds' + ) + # # + ### Finished making timestamp. ######################################## - now_string = datetime_module.datetime.now().time().isoformat() line_no = frame.f_lineno source_path, source = get_path_and_source_from_frame(frame) + source_path = source_path if not self.normalize else os.path.basename(source_path) if self.last_source_path != source_path: - self.write(u'{indent}Source path:... {source_path}'. - format(**locals())) + self.write(u'{_FOREGROUND_YELLOW}{_STYLE_DIM}{indent}Source path:... ' + u'{_STYLE_NORMAL}{source_path}' + u'{_STYLE_RESET_ALL}'.format(**locals())) self.last_source_path = source_path source_line = source[line_no - 1] thread_info = "" if self.thread_info: + if self.normalize: + raise NotImplementedError("normalize is not supported with " + "thread_info") current_thread = threading.current_thread() thread_info = "{ident}-{name} ".format( - ident=current_thread.ident, name=current_thread.getName()) + ident=current_thread.ident, name=current_thread.name) thread_info = self.set_thread_info_padding(thread_info) ### Reporting newish and modified variables: ########################## # # old_local_reprs = self.frame_to_local_reprs.get(frame, {}) self.frame_to_local_reprs[frame] = local_reprs = \ - get_local_reprs(frame, watch=self.watch, custom_repr=self.custom_repr) + get_local_reprs(frame, + watch=self.watch, custom_repr=self.custom_repr, + max_length=self.max_variable_length, + normalize=self.normalize, + ) newish_string = ('Starting var:.. ' if event == 'call' else 'New var:....... ') for name, value_repr in local_reprs.items(): if name not in old_local_reprs: - self.write('{indent}{newish_string}{name} = {value_repr}'.format( - **locals())) + self.write('{indent}{_FOREGROUND_GREEN}{_STYLE_DIM}' + '{newish_string}{_STYLE_NORMAL}{name} = ' + '{value_repr}{_STYLE_RESET_ALL}'.format(**locals())) elif old_local_reprs[name] != value_repr: - self.write('{indent}Modified var:.. {name} = {value_repr}'.format( - **locals())) + self.write('{indent}{_FOREGROUND_GREEN}{_STYLE_DIM}' + 'Modified var:.. {_STYLE_NORMAL}{name} = ' + '{value_repr}{_STYLE_RESET_ALL}'.format(**locals())) # # ### Finished newish and modified variables. ########################### @@ -411,30 +542,38 @@ class Tracer: ended_by_exception = ( event == 'return' and arg is None - and (opcode.opname[code_byte] - not in ('RETURN_VALUE', 'YIELD_VALUE')) + and opcode.opname[code_byte] not in RETURN_OPCODES ) if ended_by_exception: - self.write('{indent}Call ended by exception'. + self.write('{_FOREGROUND_RED}{indent}Call ended by exception{_STYLE_RESET_ALL}'. format(**locals())) else: - self.write(u'{indent}{now_string} {thread_info}{event:9} ' - u'{line_no:4} {source_line}'.format(**locals())) + self.write(u'{indent}{_STYLE_DIM}{timestamp} {thread_info}{event:9} ' + u'{line_no:4}{_STYLE_RESET_ALL} {source_line}'.format(**locals())) if event == 'return': - del self.frame_to_local_reprs[frame] + self.frame_to_local_reprs.pop(frame, None) + self.start_times.pop(frame, None) thread_global.depth -= 1 if not ended_by_exception: - return_value_repr = utils.get_shortish_repr(arg, custom_repr=self.custom_repr) - self.write('{indent}Return value:.. {return_value_repr}'. + return_value_repr = utils.get_shortish_repr(arg, + custom_repr=self.custom_repr, + max_length=self.max_variable_length, + normalize=self.normalize, + ) + self.write('{indent}{_FOREGROUND_CYAN}{_STYLE_DIM}' + 'Return value:.. {_STYLE_NORMAL}{return_value_repr}' + '{_STYLE_RESET_ALL}'. format(**locals())) if event == 'exception': exception = '\n'.join(traceback.format_exception_only(*arg[:2])).strip() - exception = utils.truncate(exception, utils.MAX_EXCEPTION_LENGTH) - self.write('{indent}{exception}'. - format(**locals())) + if self.max_variable_length: + exception = utils.truncate(exception, self.max_variable_length) + self.write('{indent}{_FOREGROUND_RED}Exception:..... ' + '{_STYLE_BRIGHT}{exception}' + '{_STYLE_RESET_ALL}'.format(**locals())) return self.trace diff --git a/pysnooper/utils.py b/pysnooper/utils.py index b569698..ff9b9e8 100644 --- a/pysnooper/utils.py +++ b/pysnooper/utils.py @@ -2,13 +2,11 @@ # This program is distributed under the MIT license. import abc +import re import sys from .pycompat import ABC, string_types, collections_abc -MAX_VARIABLE_LENGTH = 100 -MAX_EXCEPTION_LENGTH = 200 - def _check_methods(C, *methods): mro = C.__mro__ for method in methods: @@ -58,23 +56,35 @@ def get_repr_function(item, custom_repr): return repr -def get_shortish_repr(item, custom_repr=()): +DEFAULT_REPR_RE = re.compile(r' at 0x[a-f0-9A-F]{4,}') + + +def normalize_repr(item_repr): + """Remove memory address (0x...) from a default python repr""" + return DEFAULT_REPR_RE.sub('', item_repr) + + +def get_shortish_repr(item, custom_repr=(), max_length=None, normalize=False): repr_function = get_repr_function(item, custom_repr) try: r = repr_function(item) except Exception: r = 'REPR FAILED' r = r.replace('\r', '').replace('\n', '') - r = truncate(r, MAX_VARIABLE_LENGTH) + if normalize: + r = normalize_repr(r) + if max_length: + r = truncate(r, max_length) return r def truncate(string, max_length): - if len(string) > max_length: + if (max_length is None) or (len(string) <= max_length): + return string + else: left = (max_length - 3) // 2 right = max_length - 3 - left - string = u'{}...{}'.format(string[:left], string[-right:]) - return string + return u'{}...{}'.format(string[:left], string[-right:]) def ensure_tuple(x): diff --git a/pysnooper/variables.py b/pysnooper/variables.py index fe0176a..2229c38 100644 --- a/pysnooper/variables.py +++ b/pysnooper/variables.py @@ -27,15 +27,15 @@ class BaseVariable(pycompat.ABC): else: self.unambiguous_source = source - def items(self, frame): + def items(self, frame, normalize=False): try: main_value = eval(self.code, frame.f_globals or {}, frame.f_locals) except Exception: return () - return self._items(main_value) + return self._items(main_value, normalize) @abc.abstractmethod - def _items(self, key): + def _items(self, key, normalize=False): raise NotImplementedError @property @@ -51,8 +51,8 @@ class BaseVariable(pycompat.ABC): class CommonVariable(BaseVariable): - def _items(self, main_value): - result = [(self.source, utils.get_shortish_repr(main_value))] + def _items(self, main_value, normalize=False): + result = [(self.source, utils.get_shortish_repr(main_value, normalize=normalize))] for key in self._safe_keys(main_value): try: if key in self.exclude: @@ -122,7 +122,7 @@ class Indices(Keys): class Exploding(BaseVariable): - def _items(self, main_value): + def _items(self, main_value, normalize=False): if isinstance(main_value, Mapping): cls = Keys elif isinstance(main_value, Sequence): @@ -130,4 +130,4 @@ class Exploding(BaseVariable): else: cls = Attrs - return cls(self.source, self.exclude)._items(main_value) + return cls(self.source, self.exclude)._items(main_value, normalize) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..2d9580b --- /dev/null +++ b/setup.cfg @@ -0,0 +1,37 @@ +[metadata] +name = PySnooper +version = attr: pysnooper.__version__ +author = Ram Rachum +author_email = ram@rachum.com +description = A poor man's debugger for Python. +url = https://github.com/cool-RR/PySnooper +long_description = file: README.md +long_description_content_type = text/markdown +classifiers = + Environment :: Console + Intended Audience :: Developers + Programming Language :: Python :: 2.7 + Programming Language :: Python :: 3.4 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 + Programming Language :: Python :: 3.12 + Programming Language :: Python :: Implementation :: CPython + Programming Language :: Python :: Implementation :: PyPy + License :: OSI Approved :: MIT License + Operating System :: OS Independent + Topic :: Software Development :: Debuggers + +[options] +packages = find: +install_requires = file: requirements.in + +[options.packages.find] +exclude = tests* + +[options.extras_require] +tests = pytest diff --git a/setup.py b/setup.py index 7bdeafc..bbbb39e 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,9 @@ setuptools.setup( 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'License :: OSI Approved :: MIT License', diff --git a/tests/mini_toolbox/__init__.py b/tests/mini_toolbox/__init__.py index 97d6f57..3184e37 100644 --- a/tests/mini_toolbox/__init__.py +++ b/tests/mini_toolbox/__init__.py @@ -213,7 +213,6 @@ class OutputCapturer(object): # Not doing exception swallowing anywhere here. self._stderr_temp_setter.__exit__(exc_type, exc_value, exc_traceback) self._stdout_temp_setter.__exit__(exc_type, exc_value, exc_traceback) - return self output = property(lambda self: self.string_io.getvalue(), doc='''The string of output that was captured.''') diff --git a/tests/mini_toolbox/pathlib.py b/tests/mini_toolbox/pathlib.py index 69a6aa4..a3d2d98 100644 --- a/tests/mini_toolbox/pathlib.py +++ b/tests/mini_toolbox/pathlib.py @@ -10,7 +10,7 @@ import ntpath import os import posixpath import re -import six +from pysnooper import pycompat import sys try: from collections.abc import Sequence @@ -60,8 +60,8 @@ __all__ = [ def _py2_fsencode(parts): # py2 => minimal unicode support - assert six.PY2 - return [part.encode('ascii') if isinstance(part, six.text_type) + assert pycompat.PY2 + return [part.encode('ascii') if isinstance(part, pycompat.text_type) else part for part in parts] @@ -200,7 +200,7 @@ class _Flavour(object): self.join = self.sep.join def parse_parts(self, parts): - if six.PY2: + if pycompat.PY2: parts = _py2_fsencode(parts) parsed = [] sep = self.sep @@ -832,8 +832,8 @@ class PurePath(object): if isinstance(a, str): # Force-cast str subclasses to str (issue #21127) parts.append(str(a)) - # also handle unicode for PY2 (six.text_type = unicode) - elif six.PY2 and isinstance(a, six.text_type): + # also handle unicode for PY2 (pycompat.text_type = unicode) + elif pycompat.PY2 and isinstance(a, pycompat.text_type): # cast to str using filesystem encoding parts.append(a.encode(sys.getfilesystemencoding())) else: @@ -1107,7 +1107,7 @@ class PurePath(object): def __rtruediv__(self, key): return self._from_parts([key] + self._parts) - if six.PY2: + if pycompat.PY2: __div__ = __truediv__ __rdiv__ = __rtruediv__ @@ -1267,8 +1267,8 @@ class Path(PurePath): other_st = os.stat(other_path) return os.path.samestat(st, other_st) else: - filename1 = six.text_type(self) - filename2 = six.text_type(other_path) + filename1 = pycompat.text_type(self) + filename2 = pycompat.text_type(other_path) st1 = _win32_get_unique_path_id(filename1) st2 = _win32_get_unique_path_id(filename2) return st1 == st2 @@ -1406,10 +1406,10 @@ class Path(PurePath): """ Open the file in bytes mode, write to it, and close the file. """ - if not isinstance(data, six.binary_type): + if not isinstance(data, pycompat.binary_type): raise TypeError( 'data must be %s, not %s' % - (six.binary_type.__name__, data.__class__.__name__)) + (pycompat.binary_type.__name__, data.__class__.__name__)) with self.open(mode='wb') as f: return f.write(data) @@ -1417,10 +1417,10 @@ class Path(PurePath): """ Open the file in text mode, write to it, and close the file. """ - if not isinstance(data, six.text_type): + if not isinstance(data, pycompat.text_type): raise TypeError( 'data must be %s, not %s' % - (six.text_type.__name__, data.__class__.__name__)) + (pycompat.text_type.__name__, data.__class__.__name__)) with self.open(mode='w', encoding=encoding, errors=errors) as f: return f.write(data) diff --git a/tests/samples/exception.py b/tests/samples/exception.py index aa663de..58059a0 100644 --- a/tests/samples/exception.py +++ b/tests/samples/exception.py @@ -13,7 +13,7 @@ def bar(): raise -@pysnooper.snoop(depth=3) +@pysnooper.snoop(depth=3, color=False) def main(): try: bar() @@ -46,4 +46,5 @@ TypeError: bad 12:18:08.018787 line 21 pass 12:18:08.018813 return 21 pass Return value:.. None +Elapsed time: 00:00:00.000885 ''' diff --git a/tests/samples/indentation.py b/tests/samples/indentation.py index 9ea0318..d36ce60 100644 --- a/tests/samples/indentation.py +++ b/tests/samples/indentation.py @@ -1,7 +1,7 @@ import pysnooper -@pysnooper.snoop(depth=2) +@pysnooper.snoop(depth=2, color=False) def main(): f2() @@ -14,7 +14,7 @@ def f3(): f4() -@pysnooper.snoop(depth=2) +@pysnooper.snoop(depth=2, color=False) def f4(): f5() @@ -38,8 +38,10 @@ Source path:... Whatever Return value:.. None 21:10:42.299509 return 19 f5() Return value:.. None + Elapsed time: 00:00:00.000134 21:10:42.299577 return 10 f3() Return value:.. None 21:10:42.299627 return 6 f2() Return value:.. None +Elapsed time: 00:00:00.000885 ''' diff --git a/tests/samples/recursion.py b/tests/samples/recursion.py index 1570d1a..3d3d00d 100644 --- a/tests/samples/recursion.py +++ b/tests/samples/recursion.py @@ -1,7 +1,7 @@ import pysnooper -@pysnooper.snoop(depth=2) +@pysnooper.snoop(depth=2, color=False) def factorial(x): if x <= 1: return 1 @@ -18,45 +18,49 @@ def main(): expected_output = ''' Source path:... Whatever Starting var:.. x = 4 -20:28:17.875295 call 5 def factorial(x): -20:28:17.875509 line 6 if x <= 1: -20:28:17.875550 line 8 return mul(x, factorial(x - 1)) +09:31:32.691599 call 5 def factorial(x): +09:31:32.691722 line 6 if x <= 1: +09:31:32.691746 line 8 return mul(x, factorial(x - 1)) Starting var:.. x = 3 - 20:28:17.875624 call 5 def factorial(x): - 20:28:17.875668 line 6 if x <= 1: - 20:28:17.875703 line 8 return mul(x, factorial(x - 1)) + 09:31:32.691781 call 5 def factorial(x): + 09:31:32.691806 line 6 if x <= 1: + 09:31:32.691823 line 8 return mul(x, factorial(x - 1)) Starting var:.. x = 2 - 20:28:17.875771 call 5 def factorial(x): - 20:28:17.875813 line 6 if x <= 1: - 20:28:17.875849 line 8 return mul(x, factorial(x - 1)) + 09:31:32.691852 call 5 def factorial(x): + 09:31:32.691875 line 6 if x <= 1: + 09:31:32.691892 line 8 return mul(x, factorial(x - 1)) Starting var:.. x = 1 - 20:28:17.875913 call 5 def factorial(x): - 20:28:17.875953 line 6 if x <= 1: - 20:28:17.875987 line 7 return 1 - 20:28:17.876021 return 7 return 1 + 09:31:32.691918 call 5 def factorial(x): + 09:31:32.691941 line 6 if x <= 1: + 09:31:32.691961 line 7 return 1 + 09:31:32.691978 return 7 return 1 Return value:.. 1 + Elapsed time: 00:00:00.000092 Starting var:.. a = 2 Starting var:.. b = 1 - 20:28:17.876111 call 11 def mul(a, b): - 20:28:17.876151 line 12 return a * b - 20:28:17.876190 return 12 return a * b + 09:31:32.692025 call 11 def mul(a, b): + 09:31:32.692055 line 12 return a * b + 09:31:32.692075 return 12 return a * b Return value:.. 2 - 20:28:17.876235 return 8 return mul(x, factorial(x - 1)) + 09:31:32.692102 return 8 return mul(x, factorial(x - 1)) Return value:.. 2 + Elapsed time: 00:00:00.000283 Starting var:.. a = 3 Starting var:.. b = 2 - 20:28:17.876320 call 11 def mul(a, b): - 20:28:17.876359 line 12 return a * b - 20:28:17.876397 return 12 return a * b + 09:31:32.692147 call 11 def mul(a, b): + 09:31:32.692174 line 12 return a * b + 09:31:32.692193 return 12 return a * b Return value:.. 6 - 20:28:17.876442 return 8 return mul(x, factorial(x - 1)) + 09:31:32.692216 return 8 return mul(x, factorial(x - 1)) Return value:.. 6 + Elapsed time: 00:00:00.000468 Starting var:.. a = 4 Starting var:.. b = 6 - 20:28:17.876525 call 11 def mul(a, b): - 20:28:17.876563 line 12 return a * b - 20:28:17.876601 return 12 return a * b + 09:31:32.692259 call 11 def mul(a, b): + 09:31:32.692285 line 12 return a * b + 09:31:32.692304 return 12 return a * b Return value:.. 24 -20:28:17.876646 return 8 return mul(x, factorial(x - 1)) +09:31:32.692326 return 8 return mul(x, factorial(x - 1)) Return value:.. 24 +Elapsed time: 00:00:00.000760 ''' diff --git a/tests/test_chinese.py b/tests/test_chinese.py index 828ad1c..8bd76ac 100644 --- a/tests/test_chinese.py +++ b/tests/test_chinese.py @@ -16,7 +16,9 @@ from pysnooper import pycompat from pysnooper.variables import needs_parentheses from .utils import (assert_output, assert_sample_output, VariableEntry, CallEntry, LineEntry, ReturnEntry, OpcodeEntry, - ReturnValueEntry, ExceptionEntry, SourcePathEntry) + ReturnValueEntry, ExceptionEntry, ExceptionValueEntry, + SourcePathEntry, CallEndedByExceptionEntry, + ElapsedTimeEntry) from . import mini_toolbox @@ -24,7 +26,7 @@ from . import mini_toolbox def test_chinese(): with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder: path = folder / 'foo.log' - @pysnooper.snoop(path) + @pysnooper.snoop(path, color=False) def foo(): a = 1 x = '失败' @@ -44,6 +46,7 @@ def test_chinese(): VariableEntry(u'x', (u"'失败'" if pycompat.PY3 else None)), LineEntry(), ReturnEntry(), - ReturnValueEntry('7') + ReturnValueEntry('7'), + ElapsedTimeEntry(), ), ) diff --git a/tests/test_mini_toolbox.py b/tests/test_mini_toolbox.py new file mode 100644 index 0000000..7074e07 --- /dev/null +++ b/tests/test_mini_toolbox.py @@ -0,0 +1,12 @@ +# Copyright 2019 Ram Rachum and collaborators. +# This program is distributed under the MIT license. + +import pytest + +from . import mini_toolbox + + +def test_output_capturer_doesnt_swallow_exceptions(): + with pytest.raises(ZeroDivisionError): + with mini_toolbox.OutputCapturer(): + 1 / 0 diff --git a/tests/test_multiple_files/multiple_files/foo.py b/tests/test_multiple_files/multiple_files/foo.py index a5525f1..7f1f4d0 100644 --- a/tests/test_multiple_files/multiple_files/foo.py +++ b/tests/test_multiple_files/multiple_files/foo.py @@ -5,7 +5,7 @@ import pysnooper from .bar import bar_function -@pysnooper.snoop(depth=2) +@pysnooper.snoop(depth=2, color=False) def foo_function(): z = bar_function(3) return z \ No newline at end of file diff --git a/tests/test_multiple_files/test_multiple_files.py b/tests/test_multiple_files/test_multiple_files.py index 5bd233a..9f78d1b 100644 --- a/tests/test_multiple_files/test_multiple_files.py +++ b/tests/test_multiple_files/test_multiple_files.py @@ -15,7 +15,9 @@ import pysnooper from pysnooper.variables import needs_parentheses from ..utils import (assert_output, assert_sample_output, VariableEntry, CallEntry, LineEntry, ReturnEntry, OpcodeEntry, - ReturnValueEntry, ExceptionEntry, SourcePathEntry) + ReturnValueEntry, ExceptionEntry, ExceptionValueEntry, + SourcePathEntry, CallEndedByExceptionEntry, + ElapsedTimeEntry) from .. import mini_toolbox from .multiple_files import foo @@ -45,6 +47,7 @@ def test_multiple_files(): LineEntry(), ReturnEntry(), ReturnValueEntry(), + ElapsedTimeEntry(), ) ) diff --git a/tests/test_not_implemented.py b/tests/test_not_implemented.py new file mode 100644 index 0000000..052b465 --- /dev/null +++ b/tests/test_not_implemented.py @@ -0,0 +1,61 @@ +# Copyright 2019 Ram Rachum and collaborators. +# This program is distributed under the MIT license. + +import io +import textwrap +import threading +import collections +import types +import os +import sys + +from pysnooper.utils import truncate +import pytest + +import pysnooper +from pysnooper.variables import needs_parentheses +from pysnooper import pycompat +from .utils import (assert_output, assert_sample_output, VariableEntry, + CallEntry, LineEntry, ReturnEntry, OpcodeEntry, + ReturnValueEntry, ExceptionEntry, ExceptionValueEntry, + SourcePathEntry, CallEndedByExceptionEntry, + ElapsedTimeEntry) +from . import mini_toolbox + + +def test_rejecting_coroutine_functions(): + if sys.version_info[:2] <= (3, 4): + pytest.skip() + + code = textwrap.dedent(''' + async def foo(x): + return 'lol' + ''') + namespace = {} + exec(code, namespace) + foo = namespace['foo'] + + assert pycompat.iscoroutinefunction(foo) + assert not pycompat.isasyncgenfunction(foo) + with pytest.raises(NotImplementedError): + pysnooper.snoop(color=False)(foo) + + +def test_rejecting_async_generator_functions(): + if sys.version_info[:2] <= (3, 6): + pytest.skip() + + code = textwrap.dedent(''' + async def foo(x): + yield 'lol' + ''') + namespace = {} + exec(code, namespace) + foo = namespace['foo'] + + assert not pycompat.iscoroutinefunction(foo) + assert pycompat.isasyncgenfunction(foo) + with pytest.raises(NotImplementedError): + pysnooper.snoop(color=False)(foo) + + diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index ade570c..530a2f7 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -4,9 +4,11 @@ import io import textwrap import threading +import time import types import os import sys +import zipfile from pysnooper.utils import truncate import pytest @@ -15,14 +17,16 @@ import pysnooper from pysnooper.variables import needs_parentheses from .utils import (assert_output, assert_sample_output, VariableEntry, CallEntry, LineEntry, ReturnEntry, OpcodeEntry, - ReturnValueEntry, ExceptionEntry, SourcePathEntry) + ReturnValueEntry, ExceptionEntry, ExceptionValueEntry, + SourcePathEntry, CallEndedByExceptionEntry, + ElapsedTimeEntry) from . import mini_toolbox def test_string_io(): string_io = io.StringIO() - @pysnooper.snoop(string_io) + @pysnooper.snoop(string_io, color=False) def my_function(foo): x = 7 y = 8 @@ -44,13 +48,146 @@ def test_string_io(): LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), ) ) +def test_relative_time(): + snoop = pysnooper.snoop(relative_time=True, color=False) + + def foo(x): + if x == 0: + bar1(x) + qux() + return + + with snoop: + # There should be line entries for these three lines, + # no line entries for anything else in this function, + # but calls to all bar functions should be traced + foo(x - 1) + bar2(x) + qux() + int(4) + bar3(9) + return x + + @snoop + def bar1(_x): + qux() + + @snoop + def bar2(_x): + qux() + + @snoop + def bar3(_x): + qux() + + def qux(): + time.sleep(0.1) + return 9 # not traced, mustn't show up + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = foo(2) + assert result == 2 + output = output_capturer.string_io.getvalue() + assert_output( + output, + ( + # In first with + SourcePathEntry(), + VariableEntry('x', '2'), + VariableEntry('bar1'), + VariableEntry('bar2'), + VariableEntry('bar3'), + VariableEntry('foo'), + VariableEntry('qux'), + VariableEntry('snoop'), + LineEntry('foo(x - 1)'), + + # In with in recursive call + VariableEntry('x', '1'), + VariableEntry('bar1'), + VariableEntry('bar2'), + VariableEntry('bar3'), + VariableEntry('foo'), + VariableEntry('qux'), + VariableEntry('snoop'), + LineEntry('foo(x - 1)'), + + # Call to bar1 from if block outside with + VariableEntry('_x', '0'), + VariableEntry('qux'), + CallEntry('def bar1(_x):'), + LineEntry('qux()'), + ReturnEntry('qux()'), + ReturnValueEntry('None'), + ElapsedTimeEntry(0.1), + + # In with in recursive call + LineEntry('bar2(x)'), + + # Call to bar2 from within with + VariableEntry('_x', '1'), + VariableEntry('qux'), + CallEntry('def bar2(_x):'), + LineEntry('qux()'), + ReturnEntry('qux()'), + ReturnValueEntry('None'), + ElapsedTimeEntry(0.1), + + # In with in recursive call + LineEntry('qux()'), + LineEntry(source_regex="with snoop:", min_python_version=(3, 10)), + ElapsedTimeEntry(0.4), + + # Call to bar3 from after with + VariableEntry('_x', '9'), + VariableEntry('qux'), + CallEntry('def bar3(_x):'), + LineEntry('qux()'), + ReturnEntry('qux()'), + ReturnValueEntry('None'), + ElapsedTimeEntry(0.1), + + # -- Similar to previous few sections, + # -- but from first call to foo + + # In with in first call + LineEntry('bar2(x)'), + + # Call to bar2 from within with + VariableEntry('_x', '2'), + VariableEntry('qux'), + CallEntry('def bar2(_x):'), + LineEntry('qux()'), + ReturnEntry('qux()'), + ReturnValueEntry('None'), + ElapsedTimeEntry(0.1), + + # In with in first call + LineEntry('qux()'), + LineEntry(source_regex="with snoop:", min_python_version=(3, 10)), + ElapsedTimeEntry(0.7), + + # Call to bar3 from after with + VariableEntry('_x', '9'), + VariableEntry('qux'), + CallEntry('def bar3(_x):'), + LineEntry('qux()'), + ReturnEntry('qux()'), + ReturnValueEntry('None'), + ElapsedTimeEntry(0.1), + ), + ) + + def test_thread_info(): - @pysnooper.snoop(thread_info=True) + @pysnooper.snoop(thread_info=True, color=False) def my_function(foo): x = 7 y = 8 @@ -74,18 +211,22 @@ def test_thread_info(): LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), ) ) def test_multi_thread_info(): - @pysnooper.snoop(thread_info=True) + @pysnooper.snoop(thread_info=True, color=False) def my_function(foo): x = 7 y = 8 return y + x + def parse_call_content(line): + return line.split('{event:9} '.format(event='call'))[-1] + with mini_toolbox.OutputCapturer(stdout=False, stderr=True) as output_capturer: my_function('baba') @@ -98,11 +239,8 @@ def test_multi_thread_info(): output = output_capturer.string_io.getvalue() calls = [line for line in output.split("\n") if "call" in line] main_thread = calls[0] - assert len(main_thread) == len(calls[1]) - assert len(main_thread) == len(calls[2]) - main_thread_call_str = main_thread.find("call") - assert main_thread_call_str == calls[1].find("call") - assert main_thread_call_str == calls[2].find("call") + assert parse_call_content(main_thread) == parse_call_content(calls[1]) + assert parse_call_content(main_thread) == parse_call_content(calls[2]) thread_info_regex = '([0-9]+-{name}+[ ]+)' assert_output( output, @@ -125,6 +263,7 @@ def test_multi_thread_info(): name="MainThread")), ReturnEntry('return y + x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), VariableEntry('foo', value_regex="u?'bubu'"), CallEntry('def my_function(foo):', thread_info_regex=thread_info_regex.format( @@ -142,6 +281,7 @@ def test_multi_thread_info(): name="test123")), ReturnEntry('return y + x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), VariableEntry('foo', value_regex="u?'bibi'"), CallEntry('def my_function(foo):', thread_info_regex=thread_info_regex.format(name='bibi')), @@ -155,17 +295,19 @@ def test_multi_thread_info(): thread_info_regex=thread_info_regex.format(name='bibi')), ReturnEntry('return y + x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), ) ) -def test_callable(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_callable(normalize): string_io = io.StringIO() def write(msg): string_io.write(msg) - @pysnooper.snoop(write) + @pysnooper.snoop(write, normalize=normalize, color=False) def my_function(foo): x = 7 y = 8 @@ -187,13 +329,14 @@ def test_callable(): LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) - -def test_watch(): - +@pytest.mark.parametrize("normalize", (True, False)) +def test_watch(normalize): class Foo(object): def __init__(self): self.x = 2 @@ -205,7 +348,7 @@ def test_watch(): 'foo.x', 'io.__name__', 'len(foo.__dict__["x"] * "abc")', - )) + ), normalize=normalize, color=False) def my_function(): foo = Foo() for i in range(2): @@ -239,19 +382,22 @@ def test_watch(): VariableEntry('len(foo.__dict__["x"] * "abc")', '48'), LineEntry(), ReturnEntry(), - ReturnValueEntry('None') - ) + ReturnValueEntry('None'), + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_watch_explode(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_watch_explode(normalize): class Foo: def __init__(self, x, y): self.x = x self.y = y - - @pysnooper.snoop(watch_explode=('_d', '_point', 'lst + []')) + @pysnooper.snoop(watch_explode=('_d', '_point', 'lst + []'), normalize=normalize, + color=False) def my_function(): _d = {'a': 1, 'b': 2, 'c': 'ignore'} _point = Foo(x=3, y=4) @@ -289,12 +435,15 @@ def test_watch_explode(): VariableEntry('(lst + [])[3]', '10'), VariableEntry('lst + []'), ReturnEntry(), - ReturnValueEntry('None') - ) + ReturnValueEntry('None'), + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_variables_classes(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_variables_classes(normalize): class WithSlots(object): __slots__ = ('x', 'y') @@ -307,7 +456,7 @@ def test_variables_classes(): pysnooper.Attrs('_d'), # doesn't have attributes pysnooper.Attrs('_s'), pysnooper.Indices('_lst')[-3:], - )) + ), normalize=normalize, color=False) def my_function(): _d = {'a': 1, 'b': 2, 'c': 'ignore'} _s = WithSlots() @@ -338,14 +487,15 @@ def test_variables_classes(): VariableEntry('_lst[998]', '998'), VariableEntry('_lst[999]', '999'), ReturnEntry(), - ReturnValueEntry('None') - ) + ReturnValueEntry('None'), + ElapsedTimeEntry(), + ), + normalize=normalize, ) - -def test_single_watch_no_comma(): - +@pytest.mark.parametrize("normalize", (True, False)) +def test_single_watch_no_comma(normalize): class Foo(object): def __init__(self): self.x = 2 @@ -353,7 +503,7 @@ def test_single_watch_no_comma(): def square(self): self.x **= 2 - @pysnooper.snoop(watch='foo') + @pysnooper.snoop(watch='foo', normalize=normalize, color=False) def my_function(): foo = Foo() for i in range(2): @@ -380,13 +530,16 @@ def test_single_watch_no_comma(): LineEntry(), LineEntry(), ReturnEntry(), - ReturnValueEntry('None') - ) + ReturnValueEntry('None'), + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_long_variable(): - @pysnooper.snoop() +@pytest.mark.parametrize("normalize", (True, False)) +def test_long_variable(normalize): + @pysnooper.snoop(normalize=normalize, color=False) def my_function(): foo = list(range(1000)) return foo @@ -396,7 +549,7 @@ def test_long_variable(): result = my_function() assert result == list(range(1000)) output = output_capturer.string_io.getvalue() - regex = r'^\[0, 1, 2, .*\.\.\..*, 997, 998, 999\]$' + regex = r'^(?=.{100}$)\[0, 1, 2, .*\.\.\..*, 997, 998, 999\]$' assert_output( output, ( @@ -406,17 +559,78 @@ def test_long_variable(): VariableEntry('foo', value_regex=regex), LineEntry(), ReturnEntry(), - ReturnValueEntry(value_regex=regex) - ) + ReturnValueEntry(value_regex=regex), + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_repr_exception(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_long_variable_with_custom_max_variable_length(normalize): + @pysnooper.snoop(max_variable_length=200, normalize=normalize, color=False) + def my_function(): + foo = list(range(1000)) + return foo + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = my_function() + assert result == list(range(1000)) + output = output_capturer.string_io.getvalue() + regex = r'^(?=.{200}$)\[0, 1, 2, .*\.\.\..*, 997, 998, 999\]$' + assert_output( + output, + ( + SourcePathEntry(), + CallEntry('def my_function():'), + LineEntry('foo = list(range(1000))'), + VariableEntry('foo', value_regex=regex), + LineEntry(), + ReturnEntry(), + ReturnValueEntry(value_regex=regex), + ElapsedTimeEntry(), + ), + normalize=normalize, + ) + + +@pytest.mark.parametrize("normalize", (True, False)) +def test_long_variable_with_infinite_max_variable_length(normalize): + @pysnooper.snoop(max_variable_length=None, normalize=normalize, color=False) + def my_function(): + foo = list(range(1000)) + return foo + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = my_function() + assert result == list(range(1000)) + output = output_capturer.string_io.getvalue() + regex = r'^(?=.{1000,100000}$)\[0, 1, 2, [^.]+ 997, 998, 999\]$' + assert_output( + output, + ( + SourcePathEntry(), + CallEntry('def my_function():'), + LineEntry('foo = list(range(1000))'), + VariableEntry('foo', value_regex=regex), + LineEntry(), + ReturnEntry(), + ReturnValueEntry(value_regex=regex), + ElapsedTimeEntry(), + ), + normalize=normalize, + ) + + +@pytest.mark.parametrize("normalize", (True, False)) +def test_repr_exception(normalize): class Bad(object): def __repr__(self): 1 / 0 - @pysnooper.snoop() + @pysnooper.snoop(normalize=normalize, color=False) def my_function(): bad = Bad() @@ -434,12 +648,15 @@ def test_repr_exception(): LineEntry('bad = Bad()'), VariableEntry('bad', value='REPR FAILED'), ReturnEntry(), - ReturnValueEntry('None') - ) + ReturnValueEntry('None'), + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_depth(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_depth(normalize): string_io = io.StringIO() def f4(x4): @@ -454,7 +671,7 @@ def test_depth(): result2 = f3(x2) return result2 - @pysnooper.snoop(string_io, depth=3) + @pysnooper.snoop(string_io, depth=3, normalize=normalize, color=False) def f1(x1): result1 = f2(x1) return result1 @@ -495,16 +712,20 @@ def test_depth(): LineEntry(), ReturnEntry(), ReturnValueEntry('20'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_method_and_prefix(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_method_and_prefix(normalize): class Baz(object): def __init__(self): self.x = 2 - @pysnooper.snoop(watch=('self.x',), prefix='ZZZ') + @pysnooper.snoop(watch=('self.x',), prefix='ZZZ', normalize=normalize, + color=False) def square(self): foo = 7 self.x **= 2 @@ -532,16 +753,19 @@ def test_method_and_prefix(): LineEntry(prefix='ZZZ'), ReturnEntry(prefix='ZZZ'), ReturnValueEntry(prefix='ZZZ'), + ElapsedTimeEntry(prefix='ZZZ'), ), - prefix='ZZZ' + prefix='ZZZ', + normalize=normalize, ) -def test_file_output(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_file_output(normalize): with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder: path = folder / 'foo.log' - @pysnooper.snoop(path) + @pysnooper.snoop(path, normalize=normalize, color=False) def my_function(_foo): x = 7 y = 8 @@ -564,19 +788,22 @@ def test_file_output(): LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_confusing_decorator_lines(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_confusing_decorator_lines(normalize): string_io = io.StringIO() def empty_decorator(function): return function @empty_decorator - @pysnooper.snoop(string_io, - depth=2) # Multi-line decorator for extra confusion! + @pysnooper.snoop(string_io, normalize=normalize, + depth=2, color=False) @empty_decorator @empty_decorator def my_function(foo): @@ -607,13 +834,16 @@ def test_confusing_decorator_lines(): # back in my_function ReturnEntry(), ReturnValueEntry('15'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_lambda(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_lambda(normalize): string_io = io.StringIO() - my_function = pysnooper.snoop(string_io)(lambda x: x ** 2) + my_function = pysnooper.snoop(string_io, normalize=normalize, color=False)(lambda x: x ** 2) result = my_function(7) assert result == 49 output = string_io.getvalue() @@ -626,7 +856,9 @@ def test_lambda(): LineEntry(source_regex='^my_function = pysnooper.*'), ReturnEntry(source_regex='^my_function = pysnooper.*'), ReturnValueEntry('49'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) @@ -637,7 +869,7 @@ def test_unavailable_source(): python_file_path = folder / ('%s.py' % (module_name,)) content = textwrap.dedent(u''' import pysnooper - @pysnooper.snoop() + @pysnooper.snoop(color=False) def f(x): return x ''') @@ -659,6 +891,7 @@ def test_unavailable_source(): LineEntry('SOURCE IS UNAVAILABLE'), ReturnEntry('SOURCE IS UNAVAILABLE'), ReturnValueEntry('7'), + ElapsedTimeEntry(), ) ) @@ -668,7 +901,7 @@ def test_no_overwrite_by_default(): path = folder / 'foo.log' with path.open('w') as output_file: output_file.write(u'lala') - @pysnooper.snoop(str(path)) + @pysnooper.snoop(str(path), color=False) def my_function(foo): x = 7 y = 8 @@ -692,6 +925,7 @@ def test_no_overwrite_by_default(): LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), ) ) @@ -701,7 +935,7 @@ def test_overwrite(): path = folder / 'foo.log' with path.open('w') as output_file: output_file.write(u'lala') - @pysnooper.snoop(str(path), overwrite=True) + @pysnooper.snoop(str(path), overwrite=True, color=False) def my_function(foo): x = 7 y = 8 @@ -725,6 +959,7 @@ def test_overwrite(): LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), VariableEntry('foo', value_regex="u?'baba'"), CallEntry('def my_function(foo):'), @@ -735,6 +970,7 @@ def test_overwrite(): LineEntry('return y + x'), ReturnEntry('return y + x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), ) ) @@ -742,7 +978,7 @@ def test_overwrite(): def test_error_in_overwrite_argument(): with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder: with pytest.raises(Exception, match='can only be used when writing'): - @pysnooper.snoop(overwrite=True) + @pysnooper.snoop(overwrite=True, color=False) def my_function(foo): x = 7 y = 8 @@ -766,9 +1002,10 @@ def test_needs_parentheses(): assert needs_parentheses('x if z else y') -def test_with_block(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_with_block(normalize): # Testing that a single Tracer can handle many mixed uses - snoop = pysnooper.snoop() + snoop = pysnooper.snoop(normalize=normalize, color=False) def foo(x): if x == 0: @@ -838,6 +1075,7 @@ def test_with_block(): LineEntry('qux()'), ReturnEntry('qux()'), ReturnValueEntry('None'), + ElapsedTimeEntry(), # In with in recursive call LineEntry('bar2(x)'), @@ -849,9 +1087,12 @@ def test_with_block(): LineEntry('qux()'), ReturnEntry('qux()'), ReturnValueEntry('None'), + ElapsedTimeEntry(), # In with in recursive call LineEntry('qux()'), + LineEntry(source_regex="with snoop:", min_python_version=(3, 10)), + ElapsedTimeEntry(), # Call to bar3 from after with VariableEntry('_x', '9'), @@ -860,6 +1101,7 @@ def test_with_block(): LineEntry('qux()'), ReturnEntry('qux()'), ReturnValueEntry('None'), + ElapsedTimeEntry(), # -- Similar to previous few sections, # -- but from first call to foo @@ -874,9 +1116,12 @@ def test_with_block(): LineEntry('qux()'), ReturnEntry('qux()'), ReturnValueEntry('None'), + ElapsedTimeEntry(), # In with in first call LineEntry('qux()'), + LineEntry(source_regex="with snoop:", min_python_version=(3, 10)), + ElapsedTimeEntry(), # Call to bar3 from after with VariableEntry('_x', '9'), @@ -885,11 +1130,14 @@ def test_with_block(): LineEntry('qux()'), ReturnEntry('qux()'), ReturnValueEntry('None'), + ElapsedTimeEntry(), ), + normalize=normalize, ) -def test_with_block_depth(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_with_block_depth(normalize): string_io = io.StringIO() def f4(x4): @@ -906,7 +1154,7 @@ def test_with_block_depth(): def f1(x1): str(3) - with pysnooper.snoop(string_io, depth=3): + with pysnooper.snoop(string_io, depth=3, normalize=normalize, color=False): result1 = f2(x1) return result1 @@ -920,6 +1168,7 @@ def test_with_block_depth(): VariableEntry(), VariableEntry(), VariableEntry(), + VariableEntry(), LineEntry('result1 = f2(x1)'), VariableEntry(), @@ -941,10 +1190,16 @@ def test_with_block_depth(): LineEntry(), ReturnEntry(), ReturnValueEntry('20'), - ) + VariableEntry(min_python_version=(3, 10)), + LineEntry(source_regex="with pysnooper.snoop.*", min_python_version=(3, 10)), + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_cellvars(): + +@pytest.mark.parametrize("normalize", (True, False)) +def test_cellvars(normalize): string_io = io.StringIO() def f2(a): @@ -958,7 +1213,7 @@ def test_cellvars(): return f3(a) def f1(a): - with pysnooper.snoop(string_io, depth=4): + with pysnooper.snoop(string_io, depth=4, normalize=normalize, color=False): result1 = f2(a) return result1 @@ -972,6 +1227,7 @@ def test_cellvars(): VariableEntry(), VariableEntry(), VariableEntry(), + VariableEntry(), LineEntry('result1 = f2(a)'), VariableEntry(), @@ -1003,10 +1259,16 @@ def test_cellvars(): ReturnValueEntry(), ReturnEntry(), ReturnValueEntry(), - ) + VariableEntry(min_python_version=(3, 10)), + LineEntry(source_regex="with pysnooper.snoop.*", min_python_version=(3, 10)), + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_var_order(): + +@pytest.mark.parametrize("normalize", (True, False)) +def test_var_order(normalize): string_io = io.StringIO() def f(one, two, three, four): @@ -1016,7 +1278,7 @@ def test_var_order(): five, six, seven = 5, 6, 7 - with pysnooper.snoop(string_io, depth=2): + with pysnooper.snoop(string_io, depth=2, normalize=normalize, color=False): result = f(1, 2, 3, 4) output = string_io.getvalue() @@ -1026,6 +1288,7 @@ def test_var_order(): SourcePathEntry(), VariableEntry(), VariableEntry(), + VariableEntry(), LineEntry('result = f(1, 2, 3, 4)'), VariableEntry("one", "1"), @@ -1046,7 +1309,11 @@ def test_var_order(): VariableEntry("seven", "7"), ReturnEntry(), ReturnValueEntry(), - ) + VariableEntry("result", "None", min_python_version=(3, 10)), + LineEntry(source_regex="with pysnooper.snoop.*", min_python_version=(3, 10)), + ElapsedTimeEntry(), + ), + normalize=normalize, ) @@ -1080,7 +1347,7 @@ def test_generator(): original_tracer_active = lambda: (sys.gettrace() is original_tracer) - @pysnooper.snoop(string_io) + @pysnooper.snoop(string_io, color=False) def f(x1): assert not original_tracer_active() x2 = (yield x1) @@ -1119,6 +1386,7 @@ def test_generator(): LineEntry(), ReturnEntry(), ReturnValueEntry('0'), + ElapsedTimeEntry(), # Pause and resume: @@ -1135,6 +1403,7 @@ def test_generator(): LineEntry(), ReturnEntry(), ReturnValueEntry('2'), + ElapsedTimeEntry(), # Pause and resume: @@ -1150,12 +1419,13 @@ def test_generator(): LineEntry(), ReturnEntry(), ReturnValueEntry(None), - + ElapsedTimeEntry(), ) ) -def test_custom_repr(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_custom_repr(normalize): string_io = io.StringIO() def large(l): @@ -1173,7 +1443,8 @@ def test_custom_repr(): @pysnooper.snoop(string_io, custom_repr=( (large, print_list_size), (dict, print_dict), - (evil_condition, lambda x: 'I am evil'))) + (evil_condition, lambda x: 'I am evil')), + normalize=normalize, color=False) def sum_to_x(x): l = list(range(x)) a = {'1': 1, '2': 2} @@ -1195,13 +1466,18 @@ def test_custom_repr(): LineEntry(), ReturnEntry(), ReturnValueEntry('49995000'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_custom_repr_single(): + +@pytest.mark.parametrize("normalize", (True, False)) +def test_custom_repr_single(normalize): string_io = io.StringIO() - @pysnooper.snoop(string_io, custom_repr=(list, lambda l: 'foofoo!')) + @pysnooper.snoop(string_io, custom_repr=(list, lambda l: 'foofoo!'), + normalize=normalize, color=False) def sum_to_x(x): l = list(range(x)) return 7 @@ -1220,7 +1496,9 @@ def test_custom_repr_single(): LineEntry(), ReturnEntry(), ReturnValueEntry('7'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) @@ -1233,16 +1511,21 @@ def test_disable(): return x + y with mini_toolbox.TempValueSetter((pysnooper.tracer, 'DISABLED'), True): - with pysnooper.snoop(string_io): + tracer = pysnooper.snoop(string_io, color=False) + with tracer: result = my_function('baba') + my_decorated_function = tracer(my_function) + my_decorated_function('booboo') + output = string_io.getvalue() assert not output -def test_class(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_class(normalize): string_io = io.StringIO() - @pysnooper.snoop(string_io) + @pysnooper.snoop(string_io, normalize=normalize, color=False) class MyClass(object): def __init__(self): self.x = 7 @@ -1259,12 +1542,13 @@ def test_class(): output, ( SourcePathEntry(), - VariableEntry('self', value_regex="u?.+MyClass object at"), + VariableEntry('self', value_regex="u?.+MyClass object"), CallEntry('def __init__(self):'), LineEntry('self.x = 7'), ReturnEntry('self.x = 7'), ReturnValueEntry('None'), - VariableEntry('self', value_regex="u?.+MyClass object at"), + ElapsedTimeEntry(), + VariableEntry('self', value_regex="u?.+MyClass object"), VariableEntry('foo', value_regex="u?'baba'"), CallEntry('def my_method(self, foo):'), LineEntry('y = 8'), @@ -1272,10 +1556,14 @@ def test_class(): LineEntry('return y + self.x'), ReturnEntry('return y + self.x'), ReturnValueEntry('15'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_class_with_decorated_method(): + +@pytest.mark.parametrize("normalize", (True, False)) +def test_class_with_decorated_method(normalize): string_io = io.StringIO() def decorator(function): @@ -1284,7 +1572,7 @@ def test_class_with_decorated_method(): return result return wrapper - @pysnooper.snoop(string_io) + @pysnooper.snoop(string_io, normalize=normalize, color=False) class MyClass(object): def __init__(self): self.x = 7 @@ -1302,25 +1590,29 @@ def test_class_with_decorated_method(): output, ( SourcePathEntry(), - VariableEntry('self', value_regex="u?.+MyClass object at"), + VariableEntry('self', value_regex="u?.+MyClass object"), CallEntry('def __init__(self):'), LineEntry('self.x = 7'), ReturnEntry('self.x = 7'), ReturnValueEntry('None'), + ElapsedTimeEntry(), VariableEntry('args', value_regex=r"\(<.+>, 'baba'\)"), VariableEntry('kwargs', value_regex=r"\{\}"), - VariableEntry('function', value_regex="u?.+my_method at"), + VariableEntry('function', value_regex="u?.+my_method"), CallEntry('def wrapper(*args, **kwargs):'), LineEntry('result = function(*args, **kwargs)'), VariableEntry('result', '15'), LineEntry('return result'), ReturnEntry('return result'), ReturnValueEntry('15'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_class_with_decorated_method_and_snoop_applied_to_method(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_class_with_decorated_method_and_snoop_applied_to_method(normalize): string_io = io.StringIO() def decorator(function): @@ -1329,13 +1621,13 @@ def test_class_with_decorated_method_and_snoop_applied_to_method(): return result return wrapper - @pysnooper.snoop(string_io) + @pysnooper.snoop(string_io, normalize=normalize, color=False) class MyClass(object): def __init__(self): self.x = 7 @decorator - @pysnooper.snoop(string_io) + @pysnooper.snoop(string_io, normalize=normalize, color=False) def my_method(self, foo): y = 8 return y + self.x @@ -1348,18 +1640,19 @@ def test_class_with_decorated_method_and_snoop_applied_to_method(): output, ( SourcePathEntry(), - VariableEntry('self', value_regex="u?.*MyClass object at"), + VariableEntry('self', value_regex="u?.*MyClass object"), CallEntry('def __init__(self):'), LineEntry('self.x = 7'), ReturnEntry('self.x = 7'), ReturnValueEntry('None'), + ElapsedTimeEntry(), VariableEntry('args', value_regex=r"u?\(<.+>, 'baba'\)"), VariableEntry('kwargs', value_regex=r"u?\{\}"), - VariableEntry('function', value_regex="u?.*my_method at"), + VariableEntry('function', value_regex="u?.*my_method"), CallEntry('def wrapper(*args, **kwargs):'), LineEntry('result = function(*args, **kwargs)'), SourcePathEntry(), - VariableEntry('self', value_regex="u?.*MyClass object at"), + VariableEntry('self', value_regex="u?.*MyClass object"), VariableEntry('foo', value_regex="u?'baba'"), CallEntry('def my_method(self, foo):'), LineEntry('y = 8'), @@ -1367,18 +1660,22 @@ def test_class_with_decorated_method_and_snoop_applied_to_method(): LineEntry('return y + self.x'), ReturnEntry('return y + self.x'), ReturnValueEntry('15'), + ElapsedTimeEntry(), VariableEntry('result', '15'), LineEntry('return result'), ReturnEntry('return result'), ReturnValueEntry('15'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_class_with_property(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_class_with_property(normalize): string_io = io.StringIO() - @pysnooper.snoop(string_io) + @pysnooper.snoop(string_io, normalize=normalize, color=False) class MyClass(object): def __init__(self): self._x = 0 @@ -1420,37 +1717,43 @@ def test_class_with_property(): output, ( SourcePathEntry(), - VariableEntry('self', value_regex="u?.*MyClass object at"), + VariableEntry('self', value_regex="u?.*MyClass object"), CallEntry('def __init__(self):'), LineEntry('self._x = 0'), ReturnEntry('self._x = 0'), ReturnValueEntry('None'), + ElapsedTimeEntry(), # Called from getter - VariableEntry('self', value_regex="u?.*MyClass object at"), + VariableEntry('self', value_regex="u?.*MyClass object"), CallEntry('def plain_method(self):'), LineEntry('pass'), ReturnEntry('pass'), ReturnValueEntry('None'), + ElapsedTimeEntry(), # Called from setter - VariableEntry('self', value_regex="u?.*MyClass object at"), + VariableEntry('self', value_regex="u?.*MyClass object"), CallEntry('def plain_method(self):'), LineEntry('pass'), ReturnEntry('pass'), ReturnValueEntry('None'), + ElapsedTimeEntry(), # Called from deleter - VariableEntry('self', value_regex="u?.*MyClass object at"), + VariableEntry('self', value_regex="u?.*MyClass object"), CallEntry('def plain_method(self):'), LineEntry('pass'), ReturnEntry('pass'), ReturnValueEntry('None'), - ) + ElapsedTimeEntry(), + ), + normalize=normalize, ) -def test_snooping_on_class_does_not_cause_base_class_to_be_snooped(): +@pytest.mark.parametrize("normalize", (True, False)) +def test_snooping_on_class_does_not_cause_base_class_to_be_snooped(normalize): string_io = io.StringIO() class UnsnoopedBaseClass(object): @@ -1460,7 +1763,7 @@ def test_snooping_on_class_does_not_cause_base_class_to_be_snooped(): def method_on_base_class(self): self.method_on_base_class_was_called = True - @pysnooper.snoop(string_io) + @pysnooper.snoop(string_io, normalize=normalize, color=False) class MyClass(UnsnoopedBaseClass): def method_on_child_class(self): self.method_on_base_class() @@ -1476,10 +1779,289 @@ def test_snooping_on_class_does_not_cause_base_class_to_be_snooped(): output, ( SourcePathEntry(), - VariableEntry('self', value_regex="u?.*MyClass object at"), + VariableEntry('self', value_regex="u?.*MyClass object"), CallEntry('def method_on_child_class(self):'), LineEntry('self.method_on_base_class()'), ReturnEntry('self.method_on_base_class()'), ReturnValueEntry('None'), + ElapsedTimeEntry(), + ), + normalize=normalize, + ) + + +def test_normalize(): + string_io = io.StringIO() + + class A: + def __init__(self, a): + self.a = a + + @pysnooper.snoop(string_io, normalize=True, color=False) + def add(): + a = A(19) + b = A(22) + res = a.a + b.a + return res + + add() + output = string_io.getvalue() + assert_output( + output, + ( + SourcePathEntry('test_pysnooper.py'), + VariableEntry('A', value_regex=r""), + CallEntry('def add():'), + LineEntry('a = A(19)'), + VariableEntry('a', value_regex=r"<.*\.A (?:object|instance)>"), + LineEntry('b = A(22)'), + VariableEntry('b', value_regex=r"<.*\.A (?:object|instance)>"), + LineEntry('res = a.a + b.a'), + VariableEntry('res', value="41"), + LineEntry('return res'), + ReturnEntry('return res'), + ReturnValueEntry('41'), + ElapsedTimeEntry(), + ) + ) + + +def test_normalize_prefix(): + string_io = io.StringIO() + _prefix = 'ZZZZ' + + class A: + def __init__(self, a): + self.a = a + + @pysnooper.snoop(string_io, normalize=True, prefix=_prefix, color=False) + def add(): + a = A(19) + b = A(22) + res = a.a + b.a + return res + + add() + output = string_io.getvalue() + assert_output( + output, + ( + SourcePathEntry('test_pysnooper.py', prefix=_prefix), + VariableEntry('A', value_regex=r"", prefix=_prefix), + CallEntry('def add():', prefix=_prefix), + LineEntry('a = A(19)', prefix=_prefix), + VariableEntry('a', value_regex=r"<.*\.A (?:object|instance)>", prefix=_prefix), + LineEntry('b = A(22)', prefix=_prefix), + VariableEntry('b', value_regex=r"<.*\.A (?:object|instance)>", prefix=_prefix), + LineEntry('res = a.a + b.a', prefix=_prefix), + VariableEntry('res', value="41", prefix=_prefix), + LineEntry('return res', prefix=_prefix), + ReturnEntry('return res', prefix=_prefix), + ReturnValueEntry('41', prefix=_prefix), + ElapsedTimeEntry(prefix=_prefix), + ) + ) + + +def test_normalize_thread_info(): + string_io = io.StringIO() + + class A: + def __init__(self, a): + self.a = a + + @pysnooper.snoop(string_io, normalize=True, thread_info=True, color=False) + def add(): + a = A(19) + b = A(22) + res = a.a + b.a + return res + + with pytest.raises(NotImplementedError): + add() + + +def test_exception(): + string_io = io.StringIO() + @pysnooper.snoop(string_io, color=False) + def f(): + x = 8 + raise MemoryError + + with pytest.raises(MemoryError): + f() + + output = string_io.getvalue() + assert_output( + output, + ( + SourcePathEntry(), + CallEntry(), + LineEntry(), + VariableEntry(), + LineEntry(), + ExceptionEntry(), + ExceptionValueEntry('MemoryError'), + CallEndedByExceptionEntry(), + ElapsedTimeEntry(), ) ) + + +def test_exception_on_entry(): + @pysnooper.snoop(color=False) + def f(x): + pass + + with pytest.raises(TypeError): + f() + + +def test_valid_zipfile(): + with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder, \ + mini_toolbox.TempSysPathAdder(str(folder)): + module_name = 'my_valid_zip_module' + zip_name = 'valid.zip' + zip_base_path = mini_toolbox.pathlib.Path('ansible/modules') + python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) + os.makedirs(str(folder / zip_name / zip_base_path)) + try: + sys.path.insert(0, str(folder / zip_name / zip_base_path)) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + + python_file_path.write_text(content) + + module = __import__(module_name) + + with zipfile.ZipFile(str(folder / 'foo_bar.zip'), 'w') as myZipFile: + myZipFile.write(str(folder / zip_name / zip_base_path / ('%s.py' % (module_name))), \ + '%s/%s.py' % (zip_base_path, module_name,), \ + zipfile.ZIP_DEFLATED) + + python_file_path.unlink() + folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) + folder.joinpath('foo_bar.zip').rename(folder.joinpath(zip_name)) + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('def f(x):'), + LineEntry('return x'), + ReturnEntry('return x'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) + ) + finally: + sys.path.remove(str(folder / zip_name / zip_base_path)) + + +def test_invalid_zipfile(): + with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder, \ + mini_toolbox.TempSysPathAdder(str(folder)): + module_name = 'my_invalid_zip_module' + zip_name = 'invalid.zip' + zip_base_path = mini_toolbox.pathlib.Path('invalid/modules/path') + python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) + os.makedirs(str(folder / zip_name / zip_base_path)) + try: + sys.path.insert(0, str(folder / zip_name / zip_base_path)) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + python_file_path.write_text(content) + + module = __import__(module_name) + + with zipfile.ZipFile(str(folder / 'foo_bar.zip'), 'w') as myZipFile: + myZipFile.write(str(folder / zip_name / zip_base_path / ('%s.py' % (module_name))), \ + str(zip_base_path / ('%s.py' % (module_name,))), \ + zipfile.ZIP_DEFLATED) + + python_file_path.unlink() + folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) + folder.joinpath('foo_bar.zip').rename(folder.joinpath(zip_name)) + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('SOURCE IS UNAVAILABLE'), + LineEntry('SOURCE IS UNAVAILABLE'), + ReturnEntry('SOURCE IS UNAVAILABLE'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) + ) + finally: + sys.path.remove(str(folder / zip_name / zip_base_path)) + + +def test_valid_damaged_zipfile(): + with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder, \ + mini_toolbox.TempSysPathAdder(str(folder)): + module_name = 'my_damaged_module' + zip_name = 'damaged.zip' + zip_base_path = mini_toolbox.pathlib.Path('ansible/modules') + python_file_path = folder / zip_name / zip_base_path / ('%s.py' % (module_name)) + os.makedirs(str(folder / zip_name / zip_base_path)) + try: + sys.path.insert(0, str(folder / zip_name / zip_base_path)) + content = textwrap.dedent(u''' + import pysnooper + @pysnooper.snoop(color=False) + def f(x): + return x + ''') + python_file_path.write_text(content) + + module = __import__(module_name) + + python_file_path.unlink() + folder.joinpath(zip_name).rename(folder.joinpath('%s.delete' % (zip_name))) + + folder.joinpath(zip_name).write_text(u'I am not a zip file') + + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = getattr(module, 'f')(7) + assert result == 7 + output = output_capturer.output + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(stage='starting'), + CallEntry('SOURCE IS UNAVAILABLE'), + LineEntry('SOURCE IS UNAVAILABLE'), + ReturnEntry('SOURCE IS UNAVAILABLE'), + ReturnValueEntry('7'), + ElapsedTimeEntry(), + ) + ) + finally: + sys.path.remove(str(folder / zip_name / zip_base_path)) diff --git a/tests/test_utils/test_regex.py b/tests/test_utils/test_regex.py new file mode 100644 index 0000000..17d8805 --- /dev/null +++ b/tests/test_utils/test_regex.py @@ -0,0 +1,72 @@ +# Copyright 2022 Ram Rachum and collaborators. +# This program is distributed under the MIT license. + +import pysnooper +from pysnooper.tracer import ansible_filename_pattern + +def test_ansible_filename_pattern(): + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/modules/my_module.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_with.zip_name.zip' + source_code_file = 'ansible/modules/my_module.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/my/new/path/payload.zip' + source_code_file = 'ansible/modules/my_module.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/modules/in/new/path/my_module.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/modules/my_module_is_called_.py.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = 'C:\\Users\\vagrant\\AppData\\Local\\Temp\\pysnooperw5c2lg35\\valid.zip' + source_code_file = 'ansible\\modules\\my_valid_zip_module.py' + file_name = '%s\\%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name).group(1) == archive_file + assert ansible_filename_pattern.match(file_name).group(2) == source_code_file + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ANSIBLE/modules/my_module.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/modules/my_module.PY' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.Zip' + source_code_file = 'ansible/modules/my_module.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = 'ansible/my_module.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None + + archive_file = '/tmp/ansible_my_module_payload_xyz1234/ansible_my_module_payload.zip' + source_code_file = '' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None + + archive_file = '' + source_code_file = 'ansible/modules/my_module.py' + file_name = '%s/%s' % (archive_file, source_code_file) + assert ansible_filename_pattern.match(file_name) is None diff --git a/tests/utils.py b/tests/utils.py index 6c03e74..03dd32c 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,9 +1,12 @@ # Copyright 2019 Ram Rachum and collaborators. # This program is distributed under the MIT license. - +import os import re import abc import inspect +import sys + +from pysnooper.utils import DEFAULT_REPR_RE try: from itertools import zip_longest @@ -28,13 +31,24 @@ def get_function_arguments(function, exclude=()): class _BaseEntry(pysnooper.pycompat.ABC): - def __init__(self, prefix=''): + def __init__(self, prefix='', min_python_version=None, max_python_version=None): self.prefix = prefix + self.min_python_version = min_python_version + self.max_python_version = max_python_version @abc.abstractmethod def check(self, s): pass + def is_compatible_with_current_python_version(self): + compatible = True + if self.min_python_version and self.min_python_version > sys.version_info: + compatible = False + if self.max_python_version and self.max_python_version < sys.version_info: + compatible = False + + return compatible + def __repr__(self): init_arguments = get_function_arguments(self.__init__, exclude=('self',)) @@ -51,8 +65,11 @@ class _BaseEntry(pysnooper.pycompat.ABC): class _BaseValueEntry(_BaseEntry): - def __init__(self, prefix=''): - _BaseEntry.__init__(self, prefix=prefix) + def __init__(self, prefix='', min_python_version=None, + max_python_version=None): + _BaseEntry.__init__(self, prefix=prefix, + min_python_version=min_python_version, + max_python_version=max_python_version) self.line_pattern = re.compile( r"""^%s(?P(?: {4})*)(?P[^:]*):""" r"""\.{2,7} (?P.*)$""" % (re.escape(self.prefix),) @@ -75,10 +92,53 @@ class _BaseValueEntry(_BaseEntry): self._check_content(content)) +class ElapsedTimeEntry(_BaseEntry): + def __init__(self, elapsed_time_value=None, tolerance=0.2, prefix='', + min_python_version=None, max_python_version=None): + _BaseEntry.__init__(self, prefix=prefix, + min_python_version=min_python_version, + max_python_version=max_python_version) + self.line_pattern = re.compile( + r"""^%s(?P(?: {4})*)Elapsed time: (?P