Compare commits

...

90 commits

Author SHA1 Message Date
Ram Rachum
0561a89c0e Bump version to 1.2.3 2025-05-31 16:21:24 -07:00
Ram Rachum
05212d3092 Don't write color escape characters to file 2025-05-31 16:21:06 -07:00
Ram Rachum
3c0f9eb65a Include Andrej730 in AUTHORS 2025-04-10 23:56:22 +03:00
Ram Rachum
eee41b20f8 Bump version to 1.2.2 2025-04-10 23:54:31 +03:00
Ram Rachum
2931e1374a Upgrade Wing to 11 2025-04-10 23:53:59 +03:00
Andrej730
8bf06d25a1 Allow forcing color output on Windows using color=True 2025-04-10 23:52:31 +03:00
Ram Rachum
57472b4677 Bump version to 1.2.1 2024-09-09 10:41:35 +03:00
Ram Rachum
d91fd2b255 Update authors 2024-09-09 10:41:10 +03:00
sizhky
05f1359427 Compatibility with jupyter notebooks 2024-09-09 10:39:53 +03:00
Ram Rachum
ac74c8f020 Upgrade to Wing 10 2024-09-09 10:36:47 +03:00
Ram Rachum
f2c60de87f
Fix citation 2024-01-13 09:27:19 -08:00
Ram Rachum
0b96becd1b Change tabs to spaces 2024-01-06 12:07:35 +02:00
Ram Rachum
591341e973 Add citation 2024-01-06 12:04:17 +02:00
Ram Rachum
206ae83b4f Fix title formatting 2024-01-06 12:02:49 +02:00
Ram Rachum
8c35d81835 Add Python 3.12 to trove classifiers 2023-07-15 16:04:46 +01:00
Ram Rachum
23d3e43f0e Bring back setup.py for universal wheels 2023-07-15 16:02:46 +01:00
Ram Rachum
e1a927311b Version bump 2023-07-15 15:58:35 +01:00
Ram Rachum
60775ff71f Use Thread.name instead of deprecated getName 2023-07-15 17:57:35 +03:00
Ram Rachum
4224cf9694 Python 3.12 compat: Include new return opcodes 2023-07-15 17:53:09 +03:00
Ram Rachum
caf4ec584a Upgrade to Wing 9 2023-04-12 12:10:26 +03:00
Lumir Balhar
231969074e Add pyproject.toml and switch setup.py to setup.cfg 2023-01-03 15:15:29 +02:00
Ram Rachum
1ad8ae08b0
Add Trove classifier for Python 3.11 2022-12-11 15:01:28 +02:00
Ram Rachum
7ca28af18d Update authors 2022-04-02 16:07:32 +01:00
Ram Rachum
f3d7f39af4 Bump version 2022-04-02 16:02:58 +01:00
Lukas Klenk
4e277a5a1f
Add support for Ansible zipped source files (#226)
Add support for Ansible zipped source files
2022-04-02 17:58:48 +03:00
Ram Rachum
bea7c7a965 Bump version to 1.1.0 2022-01-14 20:37:05 +02:00
Ram Rachum
0f1e67b26b Show colored output 2022-01-14 20:36:54 +02:00
Ram Rachum
31bfc637bc Bump version to 1.0.0 2021-09-11 12:46:36 +03:00
Ram Rachum
8b0d6db21a Bump version to 0.5.0 2021-05-19 11:23:53 +03:00
Lumir Balhar
219bfc98bf Allow Python 3.10 to fail until we have a beta on Travis 2021-05-19 11:21:58 +03:00
Lumir Balhar
1c94b1af52 Make tests compatible with Python 3.10 2021-05-19 11:21:58 +03:00
Lumir Balhar
c539cbc520 Improve tests/CI configuration
- Add Python 3.10
- Fix DeprecationWarning for pytest and --strict option
- Install typing for Python 3.4
- Allow passing arguments to pytest command in tox
2021-05-19 11:21:58 +03:00
Lumír 'Frenzy' Balhar
03a51fd897 Add Fedora Linux to installation options 2021-05-14 21:46:34 +03:00
Ram Rachum
5abece033b Remove Python 3.4 tests from Travis, dependency error 2021-05-09 14:59:03 +03:00
Ram Rachum
3469baccbb Revert "Readme: Remove deleted Reddit page"
This reverts commit 887f82805f.
2021-05-09 14:48:38 +03:00
Ram Rachum
887f82805f
Readme: Remove deleted Reddit page 2021-04-04 15:13:54 +03:00
Ram Rachum
a5184c30e2 Bump version to 0.4.3 2021-02-27 11:14:08 +02:00
Ram Rachum
d6147c7dc2 Remove ad 2021-02-27 11:12:38 +02:00
Ram Rachum
dc1196efbb Get rid of six, for reals 2021-02-27 11:04:37 +02:00
Ram Rachum
fb8c0fa90a
Add comparison to set -x 2020-12-28 20:13:09 +02:00
Ram Rachum
dc04ab1626 Bump version to 0.4.2 2020-09-14 12:45:52 +03:00
Ram Rachum
bd90ac0b9c Add Yael Mintz to authors 2020-09-14 12:45:23 +03:00
Yael Mintz
f1194be092 fix #195
Fix '_thread._local' object has no attribute 'depth' raised by snooper if there's exception upon calling snooped func by initializing thread_global dict with depth when entering the trace() func
2020-09-14 12:44:11 +03:00
Ram Rachum
2f80c0f11a
Remove Travis badge 2020-06-06 00:06:00 +03:00
Ram Rachum
c154a585c4 Pypy3.6 doesn't exist on Travis 2020-05-27 22:30:11 +03:00
Ram Rachum
06f0a07e8e
Merge pull request #187 from cool-RR/2020-05-27-test-pr
foo
2020-05-27 22:21:17 +03:00
Ram Rachum
7a7766bf9d Test on more Python versions 2020-05-27 22:19:07 +03:00
Ram Rachum
f4fe0a17ed Test on Python 3.9 2020-05-27 22:16:35 +03:00
Ram Rachum
418460eb65 Update authors 2020-05-13 18:44:46 +03:00
Ram Rachum
7af9dbacb3 Rework installation readme 2020-05-13 18:43:42 +03:00
Mark Blakeney
0f11125cb2 Add installation instructions for Arch Linux 2020-05-13 18:41:46 +03:00
Ram Rachum
e4ef950090 Bump version to 0.4.1 2020-05-11 19:57:32 +03:00
Ram Rachum
473bb37a76 Add testing for exceptions 2020-05-05 14:37:01 +03:00
Ram Rachum
a602866ce1 Fix bug in OutputCapturer 2020-05-05 14:18:18 +03:00
Ram Rachum
679a77e336 Bump version to 0.4.0 2020-04-21 14:50:39 +03:00
Ram Rachum
43ed249e8c Massaging some code 2020-04-21 14:50:39 +03:00
iory
48cc9d94cd Fixed timedelta_isoformat 2020-04-21 14:50:39 +03:00
iory
0cb6df1f7b Fixed multi thread case of elapsed_time 2020-04-21 14:50:39 +03:00
iory
444ea17314 Refactor the timedelta_isoformat 2020-04-21 14:50:39 +03:00
iory
35d3bc2db1 Delete default value of timedelta_isoformat 2020-04-21 14:50:39 +03:00
iory
612e6ebed7 Add elapsed_time check for test 2020-04-21 14:50:39 +03:00
iory
0c018d868e Fixed elapsed_time 2020-04-21 14:50:39 +03:00
iory
828ffb1d3c Add time_fromisoformat 2020-04-21 14:50:39 +03:00
iory
2ac382f856 Fixed test for elapsed time entry 2020-04-21 14:50:39 +03:00
iory
4779aebbe4 Add indent of elapsed time 2020-04-21 14:50:39 +03:00
iory
b886f2b504 Enable multi call 2020-04-21 14:50:39 +03:00
iory
d94b0214f9 Rename relative_time to elapsed_time 2020-04-21 14:50:39 +03:00
iory
73c2816121 Add README.md for relative_time 2020-04-21 14:50:39 +03:00
iory
ee7be80b44 Add test for relative_time 2020-04-21 14:50:39 +03:00
iory
57cec2b9af Fixed test for elapsed time 2020-04-21 14:50:39 +03:00
iory
32183e0489 Add BasePrintEntry and ElapsedPrintEntry 2020-04-21 14:50:39 +03:00
iory
c39a68760d Add depth case 2020-04-21 14:50:39 +03:00
iory
caf1e1a63a Add timedelta_isoformat 2020-04-21 14:50:39 +03:00
iory
f822104feb Add relative_time format 2020-04-21 14:50:39 +03:00
iory
6416a11d39 Add total elapsed time 2020-04-21 14:50:39 +03:00
Ram Rachum
487fa5317e Split readme to 2 files, fix #146 2019-11-30 00:03:26 +02:00
Ram Rachum
76b7466d4d Bump version to 0.3.0 2019-11-19 19:07:02 +02:00
Ram Rachum
0af30a1ddc Update authors 2019-11-19 19:07:02 +02:00
Itamar.Raviv
0c5834196a Add normalize flag to remove machine-specific data
This allows for diffing between multiple PySnooper outputs.
2019-11-19 19:07:02 +02:00
Guoqiang Ding
c0bf4bd006 Fix unit tests on thread_info
The length of thread's ident between "MainThread" and others are not
always equal. So use another way to check it.
2019-09-17 20:20:30 +03:00
Ram Rachum
f782bab2af Bump version 2019-09-15 22:06:18 +03:00
Ram Rachum
dd196d1c99 Add Guoqiang Ding to authors 2019-09-15 22:06:18 +03:00
Ram Rachum
32c86da200 Massaging some code 2019-09-15 22:06:18 +03:00
Guoqiang Ding
e5fe6986dd Add truncate option support 2019-09-15 22:06:18 +03:00
Ram Rachum
e3e09d31b5 Add test for decorating when DISABLED = True
Fix #154
2019-09-14 13:56:44 +03:00
Ram Rachum
bd05c1686f Force time.isoformat to show microseconds every time
Fix #158
2019-09-14 11:29:26 +03:00
Ram Rachum
e2aa42bd6d Tests: Better time regex and placeholder 2019-09-14 11:29:26 +03:00
Ram Rachum
85c929285e Make tests even nicer 2019-09-14 11:29:10 +03:00
Ram Rachum
1ef8beb90b Make tests nicer 2019-09-13 21:48:11 +03:00
Ram Rachum
53bc524b7e Reject coroutine functions and async generator functions #152 2019-09-13 20:24:24 +03:00
29 changed files with 1486 additions and 372 deletions

View file

@ -3,11 +3,12 @@ language: python
python: python:
- 2.7 - 2.7
- 3.4
- 3.5 - 3.5
- 3.6 - 3.6
- 3.7 - 3.7
- 3.8-dev - 3.8
- 3.9
- 3.10-dev
- pypy2.7-6.0 - pypy2.7-6.0
- pypy3.5 - pypy3.5
@ -26,6 +27,7 @@ matrix:
- env: TOXENV=flake8 - env: TOXENV=flake8
- env: TOXENV=pylint - env: TOXENV=pylint
- env: TOXENV=bandit - env: TOXENV=bandit
- python: 3.10-dev
jobs: jobs:
include: include:

98
ADVANCED_USAGE.md Normal file
View file

@ -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)
````

View file

@ -19,3 +19,12 @@ Alexander Bersenev
Xiang Gao Xiang Gao
pikez pikez
Jonathan Reichelt Gjertsen Jonathan Reichelt Gjertsen
Guoqiang Ding
Itamar.Raviv
iory
Mark Blakeney
Yael Mintz
Lumír 'Frenzy' Balhar
Lukas Klenk
sizhky
Andrej730

158
README.md
View file

@ -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. 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'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. 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. 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: 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: The output to stderr is:
``` ![](https://i.imgur.com/TrF3VVj.jpg)
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]
```
Or if you don't want to trace an entire function, you can wrap the relevant part in a `with` block: 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 74 453.0 832
New var:....... mid = 453.0 New var:....... mid = 453.0
09:37:35.882486 line 13 print(lower, mid, upper) 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: 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"]')) @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: Show snoop lines for functions that your function calls:
```python ```python
@pysnooper.snoop(depth=2) @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 The best way to install **PySnooper** is with Pip:
@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.
# Installation #
You can install **PySnooper** by:
* pip:
```console ```console
$ pip install pysnooper $ pip install pysnooper
``` ```
* conda with conda-forge channel: ## Other installation options
Conda with conda-forge channel:
```console ```console
$ conda install -c conda-forge pysnooper $ conda install -c conda-forge pysnooper
``` ```
# Advanced Usage # Arch Linux:
`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 ```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. 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) [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) and [/r/Python Reddit thread](https://www.reddit.com/r/Python/comments/bg0ida/pysnooper_never_use_print_for_debugging_again/) (22 April 2019)

View file

@ -1,26 +1,27 @@
#!wing #!wing
#!version=7.0 #!version=11.0
################################################################## ##################################################################
# Wing project file # # Wing project file #
################################################################## ##################################################################
[project attributes] [project attributes]
proj.directory-list = [{'dirloc': loc('../..'), proj.directory-list = [{'dirloc': loc('../..'),
'excludes': [u'PySnooper.egg-info', 'excludes': ['dist',
u'dist', '.tox',
u'build'], 'htmlcov',
'build',
'.ipynb_checkpoints',
'PySnooper.egg-info'],
'filter': '*', 'filter': '*',
'include_hidden': False, 'include_hidden': False,
'recursive': True, 'recursive': True,
'watch_for_changes': True}] 'watch_for_changes': True}]
proj.file-type = 'shared' proj.file-type = 'shared'
proj.home-dir = loc('../..') proj.home-dir = loc('../..')
proj.launch-config = {loc('../../../../../../../Program Files/Python37/Scripts/pasteurize-script.py'): ('p'\ proj.launch-config = {loc('../../../../../../Program Files/Python37/Scripts/pasteurize-script.py'): ('project',
'roject', ('"c:\\Users\\Administrator\\Documents\\Python Projects\\PySnooper\\pysnooper" "c:\\Users\\Administrator\\Documents\\Python Projects\\PySnooper\\tests"',
(u'"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'\ loc('../../../../../Dropbox/Scripts and shortcuts/_simplify3d_add_m600.py'): ('project',
'roject', ('"C:\\Users\\Administrator\\Dropbox\\Desktop\\foo.gcode"',
(u'"C:\\Users\\Administrator\\Dropbox\\Desktop\\foo.gcode"',
''))} ''))}
testing.auto-test-file-specs = (('regex', testing.auto-test-file-specs = (('regex',
'pysnooper/tests.*/test[^./]*.py.?$'),) 'pysnooper/tests.*/test[^./]*.py.?$'),)

View file

@ -19,6 +19,11 @@ You probably want to run it this way:
import subprocess import subprocess
import sys import sys
# This is used for people who show up more than once:
deny_list = frozenset((
'Lumir Balhar',
))
def drop_recurrences(iterable): def drop_recurrences(iterable):
s = set() s = set()
@ -37,10 +42,10 @@ def iterate_authors_by_chronological_order(branch):
stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
) )
log_lines = log_call.stdout.decode('utf-8').split('\n') log_lines = log_call.stdout.decode('utf-8').split('\n')
return drop_recurrences( authors = tuple(line.strip().split(";")[1] for line in log_lines)
(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): def print_authors(branch):
@ -54,5 +59,4 @@ if __name__ == '__main__':
branch = sys.argv[1] branch = sys.argv[1]
except IndexError: except IndexError:
branch = 'master' branch = 'master'
print_authors(branch) print_authors(branch)

BIN
misc/output.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 KiB

3
pyproject.toml Normal file
View file

@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

View file

@ -24,7 +24,7 @@ import collections
__VersionInfo = collections.namedtuple('VersionInfo', __VersionInfo = collections.namedtuple('VersionInfo',
('major', 'minor', 'micro')) ('major', 'minor', 'micro'))
__version__ = '0.2.7' __version__ = '1.2.3'
__version_info__ = __VersionInfo(*(map(int, __version__.split('.')))) __version_info__ = __VersionInfo(*(map(int, __version__.split('.'))))
del collections, __VersionInfo # Avoid polluting the namespace del collections, __VersionInfo # Avoid polluting the namespace

View file

@ -6,6 +6,7 @@ import abc
import os import os
import inspect import inspect
import sys import sys
import datetime as datetime_module
PY3 = (sys.version_info[0] == 3) PY3 = (sys.version_info[0] == 3)
PY2 = not PY3 PY2 = not PY3
@ -47,15 +48,51 @@ try:
except AttributeError: except AttributeError:
iscoroutinefunction = lambda whatever: False # Lolz iscoroutinefunction = lambda whatever: False # Lolz
try:
isasyncgenfunction = inspect.isasyncgenfunction
except AttributeError:
isasyncgenfunction = lambda whatever: False # Lolz
if PY3: if PY3:
string_types = (str,) string_types = (str,)
text_type = str text_type = str
binary_type = bytes
else: else:
string_types = (basestring,) string_types = (basestring,)
text_type = unicode text_type = unicode
binary_type = str
try: try:
from collections import abc as collections_abc from collections import abc as collections_abc
except ImportError: # Python 2.7 except ImportError: # Python 2.7
import collections as collections_abc 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)

View file

@ -20,18 +20,28 @@ if pycompat.PY2:
ipython_filename_pattern = re.compile('^<ipython-input-([0-9]+)-.*>$') ipython_filename_pattern = re.compile('^<ipython-input-([0-9]+)-.*>$')
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 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_items.sort(key=lambda key_value: vars_order.index(key_value[0]))
result = collections.OrderedDict(result_items) result = collections.OrderedDict(result_items)
for variable in watch: for variable in watch:
result.update(sorted(variable.items(frame))) result.update(sorted(variable.items(frame, normalize)))
return result return result
@ -64,7 +74,16 @@ def get_path_and_source_from_frame(frame):
source = source.splitlines() source = source.splitlines()
if source is None: if source is None:
ipython_filename_match = ipython_filename_pattern.match(file_name) 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)) entry_number = int(ipython_filename_match.group(1))
try: try:
import IPython import IPython
@ -74,6 +93,13 @@ def get_path_and_source_from_frame(frame):
source = source_chunk.splitlines() source = source_chunk.splitlines()
except Exception: except Exception:
pass 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: else:
try: try:
with open(file_name, 'rb') as fp: with open(file_name, 'rb') as fp:
@ -185,20 +211,32 @@ class Tracer:
Customize how values are represented as strings:: 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__( def __init__(self, output=None, watch=(), watch_explode=(), depth=1,
self, prefix='', overwrite=False, thread_info=False, custom_repr=(),
output=None, max_variable_length=100, normalize=False, relative_time=False,
watch=(), color=sys.platform in ('linux', 'linux2', 'cygwin', 'darwin')):
watch_explode=(),
depth=1,
prefix='',
overwrite=False,
thread_info=False,
custom_repr=(),
):
self._write = get_write_function(output, overwrite) self._write = get_write_function(output, overwrite)
self.watch = [ self.watch = [
@ -209,6 +247,7 @@ class Tracer:
for v in utils.ensure_tuple(watch_explode) for v in utils.ensure_tuple(watch_explode)
] ]
self.frame_to_local_reprs = {} self.frame_to_local_reprs = {}
self.start_times = {}
self.depth = depth self.depth = depth
self.prefix = prefix self.prefix = prefix
self.thread_info = thread_info self.thread_info = thread_info
@ -222,6 +261,35 @@ class Tracer:
custom_repr = (custom_repr,) custom_repr = (custom_repr,)
self.custom_repr = custom_repr self.custom_repr = custom_repr
self.last_source_path = None 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): def __call__(self, function_or_class):
if DISABLED: if DISABLED:
@ -267,7 +335,8 @@ class Tracer:
method, incoming = gen.throw, e method, incoming = gen.throw, e
if pycompat.iscoroutinefunction(function): if pycompat.iscoroutinefunction(function):
# return decorate(function, coroutine_wrapper) raise NotImplementedError
if pycompat.isasyncgenfunction(function):
raise NotImplementedError raise NotImplementedError
elif inspect.isgeneratorfunction(function): elif inspect.isgeneratorfunction(function):
return generator_wrapper return generator_wrapper
@ -281,6 +350,7 @@ class Tracer:
def __enter__(self): def __enter__(self):
if DISABLED: if DISABLED:
return return
thread_global.__dict__.setdefault('depth', -1)
calling_frame = inspect.currentframe().f_back calling_frame = inspect.currentframe().f_back
if not self._is_internal_frame(calling_frame): if not self._is_internal_frame(calling_frame):
calling_frame.f_trace = self.trace calling_frame.f_trace = self.trace
@ -290,6 +360,7 @@ class Tracer:
'original_trace_functions', [] 'original_trace_functions', []
) )
stack.append(sys.gettrace()) stack.append(sys.gettrace())
self.start_times[calling_frame] = datetime_module.datetime.now()
sys.settrace(self.trace) sys.settrace(self.trace)
def __exit__(self, exc_type, exc_value, exc_traceback): def __exit__(self, exc_type, exc_value, exc_traceback):
@ -301,6 +372,25 @@ class Tracer:
self.target_frames.discard(calling_frame) self.target_frames.discard(calling_frame)
self.frame_to_local_reprs.pop(calling_frame, None) 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): def _is_internal_frame(self, frame):
return frame.f_code.co_filename == Tracer.__enter__.__code__.co_filename return frame.f_code.co_filename == Tracer.__enter__.__code__.co_filename
@ -337,45 +427,86 @@ class Tracer:
else: else:
return None return None
thread_global.__dict__.setdefault('depth', -1) # #
### Finished checking whether we should trace this line. ##############
if event == 'call': if event == 'call':
thread_global.depth += 1 thread_global.depth += 1
indent = ' ' * 4 * thread_global.depth indent = ' ' * 4 * thread_global.depth
# # _FOREGROUND_BLUE = self._FOREGROUND_BLUE
### Finished checking whether we should trace this line. ############## _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 line_no = frame.f_lineno
source_path, source = get_path_and_source_from_frame(frame) 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: if self.last_source_path != source_path:
self.write(u'{indent}Source path:... {source_path}'. self.write(u'{_FOREGROUND_YELLOW}{_STYLE_DIM}{indent}Source path:... '
format(**locals())) u'{_STYLE_NORMAL}{source_path}'
u'{_STYLE_RESET_ALL}'.format(**locals()))
self.last_source_path = source_path self.last_source_path = source_path
source_line = source[line_no - 1] source_line = source[line_no - 1]
thread_info = "" thread_info = ""
if self.thread_info: if self.thread_info:
if self.normalize:
raise NotImplementedError("normalize is not supported with "
"thread_info")
current_thread = threading.current_thread() current_thread = threading.current_thread()
thread_info = "{ident}-{name} ".format( 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) thread_info = self.set_thread_info_padding(thread_info)
### Reporting newish and modified variables: ########################## ### Reporting newish and modified variables: ##########################
# # # #
old_local_reprs = self.frame_to_local_reprs.get(frame, {}) old_local_reprs = self.frame_to_local_reprs.get(frame, {})
self.frame_to_local_reprs[frame] = local_reprs = \ 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 newish_string = ('Starting var:.. ' if event == 'call' else
'New var:....... ') 'New var:....... ')
for name, value_repr in local_reprs.items(): for name, value_repr in local_reprs.items():
if name not in old_local_reprs: if name not in old_local_reprs:
self.write('{indent}{newish_string}{name} = {value_repr}'.format( self.write('{indent}{_FOREGROUND_GREEN}{_STYLE_DIM}'
**locals())) '{newish_string}{_STYLE_NORMAL}{name} = '
'{value_repr}{_STYLE_RESET_ALL}'.format(**locals()))
elif old_local_reprs[name] != value_repr: elif old_local_reprs[name] != value_repr:
self.write('{indent}Modified var:.. {name} = {value_repr}'.format( self.write('{indent}{_FOREGROUND_GREEN}{_STYLE_DIM}'
**locals())) 'Modified var:.. {_STYLE_NORMAL}{name} = '
'{value_repr}{_STYLE_RESET_ALL}'.format(**locals()))
# # # #
### Finished newish and modified variables. ########################### ### Finished newish and modified variables. ###########################
@ -411,30 +542,38 @@ class Tracer:
ended_by_exception = ( ended_by_exception = (
event == 'return' event == 'return'
and arg is None and arg is None
and (opcode.opname[code_byte] and opcode.opname[code_byte] not in RETURN_OPCODES
not in ('RETURN_VALUE', 'YIELD_VALUE'))
) )
if ended_by_exception: 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())) format(**locals()))
else: else:
self.write(u'{indent}{now_string} {thread_info}{event:9} ' self.write(u'{indent}{_STYLE_DIM}{timestamp} {thread_info}{event:9} '
u'{line_no:4} {source_line}'.format(**locals())) u'{line_no:4}{_STYLE_RESET_ALL} {source_line}'.format(**locals()))
if event == 'return': 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 thread_global.depth -= 1
if not ended_by_exception: if not ended_by_exception:
return_value_repr = utils.get_shortish_repr(arg, custom_repr=self.custom_repr) return_value_repr = utils.get_shortish_repr(arg,
self.write('{indent}Return value:.. {return_value_repr}'. 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())) format(**locals()))
if event == 'exception': if event == 'exception':
exception = '\n'.join(traceback.format_exception_only(*arg[:2])).strip() exception = '\n'.join(traceback.format_exception_only(*arg[:2])).strip()
exception = utils.truncate(exception, utils.MAX_EXCEPTION_LENGTH) if self.max_variable_length:
self.write('{indent}{exception}'. exception = utils.truncate(exception, self.max_variable_length)
format(**locals())) self.write('{indent}{_FOREGROUND_RED}Exception:..... '
'{_STYLE_BRIGHT}{exception}'
'{_STYLE_RESET_ALL}'.format(**locals()))
return self.trace return self.trace

View file

@ -2,13 +2,11 @@
# This program is distributed under the MIT license. # This program is distributed under the MIT license.
import abc import abc
import re
import sys import sys
from .pycompat import ABC, string_types, collections_abc from .pycompat import ABC, string_types, collections_abc
MAX_VARIABLE_LENGTH = 100
MAX_EXCEPTION_LENGTH = 200
def _check_methods(C, *methods): def _check_methods(C, *methods):
mro = C.__mro__ mro = C.__mro__
for method in methods: for method in methods:
@ -58,23 +56,35 @@ def get_repr_function(item, custom_repr):
return 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) repr_function = get_repr_function(item, custom_repr)
try: try:
r = repr_function(item) r = repr_function(item)
except Exception: except Exception:
r = 'REPR FAILED' r = 'REPR FAILED'
r = r.replace('\r', '').replace('\n', '') 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 return r
def truncate(string, max_length): 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 left = (max_length - 3) // 2
right = max_length - 3 - left right = max_length - 3 - left
string = u'{}...{}'.format(string[:left], string[-right:]) return u'{}...{}'.format(string[:left], string[-right:])
return string
def ensure_tuple(x): def ensure_tuple(x):

View file

@ -27,15 +27,15 @@ class BaseVariable(pycompat.ABC):
else: else:
self.unambiguous_source = source self.unambiguous_source = source
def items(self, frame): def items(self, frame, normalize=False):
try: try:
main_value = eval(self.code, frame.f_globals or {}, frame.f_locals) main_value = eval(self.code, frame.f_globals or {}, frame.f_locals)
except Exception: except Exception:
return () return ()
return self._items(main_value) return self._items(main_value, normalize)
@abc.abstractmethod @abc.abstractmethod
def _items(self, key): def _items(self, key, normalize=False):
raise NotImplementedError raise NotImplementedError
@property @property
@ -51,8 +51,8 @@ class BaseVariable(pycompat.ABC):
class CommonVariable(BaseVariable): class CommonVariable(BaseVariable):
def _items(self, main_value): def _items(self, main_value, normalize=False):
result = [(self.source, utils.get_shortish_repr(main_value))] result = [(self.source, utils.get_shortish_repr(main_value, normalize=normalize))]
for key in self._safe_keys(main_value): for key in self._safe_keys(main_value):
try: try:
if key in self.exclude: if key in self.exclude:
@ -122,7 +122,7 @@ class Indices(Keys):
class Exploding(BaseVariable): class Exploding(BaseVariable):
def _items(self, main_value): def _items(self, main_value, normalize=False):
if isinstance(main_value, Mapping): if isinstance(main_value, Mapping):
cls = Keys cls = Keys
elif isinstance(main_value, Sequence): elif isinstance(main_value, Sequence):
@ -130,4 +130,4 @@ class Exploding(BaseVariable):
else: else:
cls = Attrs cls = Attrs
return cls(self.source, self.exclude)._items(main_value) return cls(self.source, self.exclude)._items(main_value, normalize)

37
setup.cfg Normal file
View file

@ -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

View file

@ -36,6 +36,9 @@ setuptools.setup(
'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8', '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 :: CPython',
'Programming Language :: Python :: Implementation :: PyPy', 'Programming Language :: Python :: Implementation :: PyPy',
'License :: OSI Approved :: MIT License', 'License :: OSI Approved :: MIT License',

View file

@ -213,7 +213,6 @@ class OutputCapturer(object):
# Not doing exception swallowing anywhere here. # Not doing exception swallowing anywhere here.
self._stderr_temp_setter.__exit__(exc_type, exc_value, exc_traceback) self._stderr_temp_setter.__exit__(exc_type, exc_value, exc_traceback)
self._stdout_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(), output = property(lambda self: self.string_io.getvalue(),
doc='''The string of output that was captured.''') doc='''The string of output that was captured.''')

View file

@ -10,7 +10,7 @@ import ntpath
import os import os
import posixpath import posixpath
import re import re
import six from pysnooper import pycompat
import sys import sys
try: try:
from collections.abc import Sequence from collections.abc import Sequence
@ -60,8 +60,8 @@ __all__ = [
def _py2_fsencode(parts): def _py2_fsencode(parts):
# py2 => minimal unicode support # py2 => minimal unicode support
assert six.PY2 assert pycompat.PY2
return [part.encode('ascii') if isinstance(part, six.text_type) return [part.encode('ascii') if isinstance(part, pycompat.text_type)
else part for part in parts] else part for part in parts]
@ -200,7 +200,7 @@ class _Flavour(object):
self.join = self.sep.join self.join = self.sep.join
def parse_parts(self, parts): def parse_parts(self, parts):
if six.PY2: if pycompat.PY2:
parts = _py2_fsencode(parts) parts = _py2_fsencode(parts)
parsed = [] parsed = []
sep = self.sep sep = self.sep
@ -832,8 +832,8 @@ class PurePath(object):
if isinstance(a, str): if isinstance(a, str):
# Force-cast str subclasses to str (issue #21127) # Force-cast str subclasses to str (issue #21127)
parts.append(str(a)) parts.append(str(a))
# also handle unicode for PY2 (six.text_type = unicode) # also handle unicode for PY2 (pycompat.text_type = unicode)
elif six.PY2 and isinstance(a, six.text_type): elif pycompat.PY2 and isinstance(a, pycompat.text_type):
# cast to str using filesystem encoding # cast to str using filesystem encoding
parts.append(a.encode(sys.getfilesystemencoding())) parts.append(a.encode(sys.getfilesystemencoding()))
else: else:
@ -1107,7 +1107,7 @@ class PurePath(object):
def __rtruediv__(self, key): def __rtruediv__(self, key):
return self._from_parts([key] + self._parts) return self._from_parts([key] + self._parts)
if six.PY2: if pycompat.PY2:
__div__ = __truediv__ __div__ = __truediv__
__rdiv__ = __rtruediv__ __rdiv__ = __rtruediv__
@ -1267,8 +1267,8 @@ class Path(PurePath):
other_st = os.stat(other_path) other_st = os.stat(other_path)
return os.path.samestat(st, other_st) return os.path.samestat(st, other_st)
else: else:
filename1 = six.text_type(self) filename1 = pycompat.text_type(self)
filename2 = six.text_type(other_path) filename2 = pycompat.text_type(other_path)
st1 = _win32_get_unique_path_id(filename1) st1 = _win32_get_unique_path_id(filename1)
st2 = _win32_get_unique_path_id(filename2) st2 = _win32_get_unique_path_id(filename2)
return st1 == st2 return st1 == st2
@ -1406,10 +1406,10 @@ class Path(PurePath):
""" """
Open the file in bytes mode, write to it, and close the file. 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( raise TypeError(
'data must be %s, not %s' % '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: with self.open(mode='wb') as f:
return f.write(data) return f.write(data)
@ -1417,10 +1417,10 @@ class Path(PurePath):
""" """
Open the file in text mode, write to it, and close the file. 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( raise TypeError(
'data must be %s, not %s' % '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: with self.open(mode='w', encoding=encoding, errors=errors) as f:
return f.write(data) return f.write(data)

View file

@ -13,7 +13,7 @@ def bar():
raise raise
@pysnooper.snoop(depth=3) @pysnooper.snoop(depth=3, color=False)
def main(): def main():
try: try:
bar() bar()
@ -46,4 +46,5 @@ TypeError: bad
12:18:08.018787 line 21 pass 12:18:08.018787 line 21 pass
12:18:08.018813 return 21 pass 12:18:08.018813 return 21 pass
Return value:.. None Return value:.. None
Elapsed time: 00:00:00.000885
''' '''

View file

@ -1,7 +1,7 @@
import pysnooper import pysnooper
@pysnooper.snoop(depth=2) @pysnooper.snoop(depth=2, color=False)
def main(): def main():
f2() f2()
@ -14,7 +14,7 @@ def f3():
f4() f4()
@pysnooper.snoop(depth=2) @pysnooper.snoop(depth=2, color=False)
def f4(): def f4():
f5() f5()
@ -38,8 +38,10 @@ Source path:... Whatever
Return value:.. None Return value:.. None
21:10:42.299509 return 19 f5() 21:10:42.299509 return 19 f5()
Return value:.. None Return value:.. None
Elapsed time: 00:00:00.000134
21:10:42.299577 return 10 f3() 21:10:42.299577 return 10 f3()
Return value:.. None Return value:.. None
21:10:42.299627 return 6 f2() 21:10:42.299627 return 6 f2()
Return value:.. None Return value:.. None
Elapsed time: 00:00:00.000885
''' '''

View file

@ -1,7 +1,7 @@
import pysnooper import pysnooper
@pysnooper.snoop(depth=2) @pysnooper.snoop(depth=2, color=False)
def factorial(x): def factorial(x):
if x <= 1: if x <= 1:
return 1 return 1
@ -18,45 +18,49 @@ def main():
expected_output = ''' expected_output = '''
Source path:... Whatever Source path:... Whatever
Starting var:.. x = 4 Starting var:.. x = 4
20:28:17.875295 call 5 def factorial(x): 09:31:32.691599 call 5 def factorial(x):
20:28:17.875509 line 6 if x <= 1: 09:31:32.691722 line 6 if x <= 1:
20:28:17.875550 line 8 return mul(x, factorial(x - 1)) 09:31:32.691746 line 8 return mul(x, factorial(x - 1))
Starting var:.. x = 3 Starting var:.. x = 3
20:28:17.875624 call 5 def factorial(x): 09:31:32.691781 call 5 def factorial(x):
20:28:17.875668 line 6 if x <= 1: 09:31:32.691806 line 6 if x <= 1:
20:28:17.875703 line 8 return mul(x, factorial(x - 1)) 09:31:32.691823 line 8 return mul(x, factorial(x - 1))
Starting var:.. x = 2 Starting var:.. x = 2
20:28:17.875771 call 5 def factorial(x): 09:31:32.691852 call 5 def factorial(x):
20:28:17.875813 line 6 if x <= 1: 09:31:32.691875 line 6 if x <= 1:
20:28:17.875849 line 8 return mul(x, factorial(x - 1)) 09:31:32.691892 line 8 return mul(x, factorial(x - 1))
Starting var:.. x = 1 Starting var:.. x = 1
20:28:17.875913 call 5 def factorial(x): 09:31:32.691918 call 5 def factorial(x):
20:28:17.875953 line 6 if x <= 1: 09:31:32.691941 line 6 if x <= 1:
20:28:17.875987 line 7 return 1 09:31:32.691961 line 7 return 1
20:28:17.876021 return 7 return 1 09:31:32.691978 return 7 return 1
Return value:.. 1 Return value:.. 1
Elapsed time: 00:00:00.000092
Starting var:.. a = 2 Starting var:.. a = 2
Starting var:.. b = 1 Starting var:.. b = 1
20:28:17.876111 call 11 def mul(a, b): 09:31:32.692025 call 11 def mul(a, b):
20:28:17.876151 line 12 return a * b 09:31:32.692055 line 12 return a * b
20:28:17.876190 return 12 return a * b 09:31:32.692075 return 12 return a * b
Return value:.. 2 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 Return value:.. 2
Elapsed time: 00:00:00.000283
Starting var:.. a = 3 Starting var:.. a = 3
Starting var:.. b = 2 Starting var:.. b = 2
20:28:17.876320 call 11 def mul(a, b): 09:31:32.692147 call 11 def mul(a, b):
20:28:17.876359 line 12 return a * b 09:31:32.692174 line 12 return a * b
20:28:17.876397 return 12 return a * b 09:31:32.692193 return 12 return a * b
Return value:.. 6 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 Return value:.. 6
Elapsed time: 00:00:00.000468
Starting var:.. a = 4 Starting var:.. a = 4
Starting var:.. b = 6 Starting var:.. b = 6
20:28:17.876525 call 11 def mul(a, b): 09:31:32.692259 call 11 def mul(a, b):
20:28:17.876563 line 12 return a * b 09:31:32.692285 line 12 return a * b
20:28:17.876601 return 12 return a * b 09:31:32.692304 return 12 return a * b
Return value:.. 24 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 Return value:.. 24
Elapsed time: 00:00:00.000760
''' '''

View file

@ -16,7 +16,9 @@ from pysnooper import pycompat
from pysnooper.variables import needs_parentheses from pysnooper.variables import needs_parentheses
from .utils import (assert_output, assert_sample_output, VariableEntry, from .utils import (assert_output, assert_sample_output, VariableEntry,
CallEntry, LineEntry, ReturnEntry, OpcodeEntry, CallEntry, LineEntry, ReturnEntry, OpcodeEntry,
ReturnValueEntry, ExceptionEntry, SourcePathEntry) ReturnValueEntry, ExceptionEntry, ExceptionValueEntry,
SourcePathEntry, CallEndedByExceptionEntry,
ElapsedTimeEntry)
from . import mini_toolbox from . import mini_toolbox
@ -24,7 +26,7 @@ from . import mini_toolbox
def test_chinese(): def test_chinese():
with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder: with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder:
path = folder / 'foo.log' path = folder / 'foo.log'
@pysnooper.snoop(path) @pysnooper.snoop(path, color=False)
def foo(): def foo():
a = 1 a = 1
x = '失败' x = '失败'
@ -44,6 +46,7 @@ def test_chinese():
VariableEntry(u'x', (u"'失败'" if pycompat.PY3 else None)), VariableEntry(u'x', (u"'失败'" if pycompat.PY3 else None)),
LineEntry(), LineEntry(),
ReturnEntry(), ReturnEntry(),
ReturnValueEntry('7') ReturnValueEntry('7'),
ElapsedTimeEntry(),
), ),
) )

View file

@ -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

View file

@ -5,7 +5,7 @@ import pysnooper
from .bar import bar_function from .bar import bar_function
@pysnooper.snoop(depth=2) @pysnooper.snoop(depth=2, color=False)
def foo_function(): def foo_function():
z = bar_function(3) z = bar_function(3)
return z return z

View file

@ -15,7 +15,9 @@ import pysnooper
from pysnooper.variables import needs_parentheses from pysnooper.variables import needs_parentheses
from ..utils import (assert_output, assert_sample_output, VariableEntry, from ..utils import (assert_output, assert_sample_output, VariableEntry,
CallEntry, LineEntry, ReturnEntry, OpcodeEntry, CallEntry, LineEntry, ReturnEntry, OpcodeEntry,
ReturnValueEntry, ExceptionEntry, SourcePathEntry) ReturnValueEntry, ExceptionEntry, ExceptionValueEntry,
SourcePathEntry, CallEndedByExceptionEntry,
ElapsedTimeEntry)
from .. import mini_toolbox from .. import mini_toolbox
from .multiple_files import foo from .multiple_files import foo
@ -45,6 +47,7 @@ def test_multiple_files():
LineEntry(), LineEntry(),
ReturnEntry(), ReturnEntry(),
ReturnValueEntry(), ReturnValueEntry(),
ElapsedTimeEntry(),
) )
) )

View file

@ -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)

File diff suppressed because it is too large Load diff

View file

@ -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

View file

@ -1,9 +1,12 @@
# Copyright 2019 Ram Rachum and collaborators. # Copyright 2019 Ram Rachum and collaborators.
# This program is distributed under the MIT license. # This program is distributed under the MIT license.
import os
import re import re
import abc import abc
import inspect import inspect
import sys
from pysnooper.utils import DEFAULT_REPR_RE
try: try:
from itertools import zip_longest from itertools import zip_longest
@ -28,13 +31,24 @@ def get_function_arguments(function, exclude=()):
class _BaseEntry(pysnooper.pycompat.ABC): 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.prefix = prefix
self.min_python_version = min_python_version
self.max_python_version = max_python_version
@abc.abstractmethod @abc.abstractmethod
def check(self, s): def check(self, s):
pass 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): def __repr__(self):
init_arguments = get_function_arguments(self.__init__, init_arguments = get_function_arguments(self.__init__,
exclude=('self',)) exclude=('self',))
@ -51,8 +65,11 @@ class _BaseEntry(pysnooper.pycompat.ABC):
class _BaseValueEntry(_BaseEntry): class _BaseValueEntry(_BaseEntry):
def __init__(self, prefix=''): def __init__(self, prefix='', min_python_version=None,
_BaseEntry.__init__(self, prefix=prefix) 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( self.line_pattern = re.compile(
r"""^%s(?P<indent>(?: {4})*)(?P<preamble>[^:]*):""" r"""^%s(?P<indent>(?: {4})*)(?P<preamble>[^:]*):"""
r"""\.{2,7} (?P<content>.*)$""" % (re.escape(self.prefix),) r"""\.{2,7} (?P<content>.*)$""" % (re.escape(self.prefix),)
@ -75,10 +92,53 @@ class _BaseValueEntry(_BaseEntry):
self._check_content(content)) 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<indent>(?: {4})*)Elapsed time: (?P<time>.*)""" % (
re.escape(self.prefix),
)
)
self.elapsed_time_value = elapsed_time_value
self.tolerance = tolerance
def check(self, s):
match = self.line_pattern.match(s)
if not match:
return False
timedelta = pysnooper.pycompat.timedelta_parse(match.group('time'))
if self.elapsed_time_value:
return abs(timedelta.total_seconds() - self.elapsed_time_value) \
<= self.tolerance
else:
return True
class CallEndedByExceptionEntry(_BaseEntry):
# Todo: Looking at this class, we could rework the hierarchy.
def __init__(self, prefix=''):
_BaseEntry.__init__(self, prefix=prefix)
def check(self, s):
return re.match(
r'''(?P<indent>(?: {4})*)Call ended by exception''',
s
)
class VariableEntry(_BaseValueEntry): class VariableEntry(_BaseValueEntry):
def __init__(self, name=None, value=None, stage=None, prefix='', def __init__(self, name=None, value=None, stage=None, prefix='',
name_regex=None, value_regex=None): name_regex=None, value_regex=None, min_python_version=None,
_BaseValueEntry.__init__(self, prefix=prefix) max_python_version=None):
_BaseValueEntry.__init__(self, prefix=prefix,
min_python_version=min_python_version,
max_python_version=max_python_version)
if name is not None: if name is not None:
assert name_regex is None assert name_regex is None
if value is not None: if value is not None:
@ -139,9 +199,12 @@ class VariableEntry(_BaseValueEntry):
return stage == self.stage return stage == self.stage
class ReturnValueEntry(_BaseValueEntry): class _BaseSimpleValueEntry(_BaseValueEntry):
def __init__(self, value=None, value_regex=None, prefix=''): def __init__(self, value=None, value_regex=None, prefix='',
_BaseValueEntry.__init__(self, prefix=prefix) min_python_version=None, max_python_version=None):
_BaseValueEntry.__init__(self, prefix=prefix,
min_python_version=min_python_version,
max_python_version=max_python_version)
if value is not None: if value is not None:
assert value_regex is None assert value_regex is None
@ -149,10 +212,6 @@ class ReturnValueEntry(_BaseValueEntry):
self.value_regex = (None if value_regex is None else self.value_regex = (None if value_regex is None else
re.compile(value_regex)) re.compile(value_regex))
_preamble_pattern = re.compile(
r"""^Return value$"""
)
def _check_preamble(self, preamble): def _check_preamble(self, preamble):
return bool(self._preamble_pattern.match(preamble)) return bool(self._preamble_pattern.match(preamble))
@ -167,6 +226,16 @@ class ReturnValueEntry(_BaseValueEntry):
else: else:
return True return True
class ReturnValueEntry(_BaseSimpleValueEntry):
_preamble_pattern = re.compile(
r"""^Return value$"""
)
class ExceptionValueEntry(_BaseSimpleValueEntry):
_preamble_pattern = re.compile(
r"""^Exception$"""
)
class SourcePathEntry(_BaseValueEntry): class SourcePathEntry(_BaseValueEntry):
def __init__(self, source_path=None, source_path_regex=None, prefix=''): def __init__(self, source_path=None, source_path_regex=None, prefix=''):
_BaseValueEntry.__init__(self, prefix=prefix) _BaseValueEntry.__init__(self, prefix=prefix)
@ -195,14 +264,17 @@ class SourcePathEntry(_BaseValueEntry):
class _BaseEventEntry(_BaseEntry): class _BaseEventEntry(_BaseEntry):
def __init__(self, source=None, source_regex=None, thread_info=None, def __init__(self, source=None, source_regex=None, thread_info=None,
thread_info_regex=None, prefix=''): thread_info_regex=None, prefix='', min_python_version=None,
_BaseEntry.__init__(self, prefix=prefix) max_python_version=None):
_BaseEntry.__init__(self, prefix=prefix,
min_python_version=min_python_version,
max_python_version=max_python_version)
if type(self) is _BaseEventEntry: if type(self) is _BaseEventEntry:
raise TypeError raise TypeError
if source is not None: if source is not None:
assert source_regex is None assert source_regex is None
self.line_pattern = re.compile( self.line_pattern = re.compile(
r"""^%s(?P<indent>(?: {4})*)[0-9:.]{15} """ r"""^%s(?P<indent>(?: {4})*)(?:(?:[0-9:.]{15})|(?: {15})) """
r"""(?P<thread_info>[0-9]+-[0-9A-Za-z_-]+[ ]+)?""" r"""(?P<thread_info>[0-9]+-[0-9A-Za-z_-]+[ ]+)?"""
r"""(?P<event_name>[a-z_]*) +(?P<line_number>[0-9]*) """ r"""(?P<event_name>[a-z_]*) +(?P<line_number>[0-9]*) """
r"""+(?P<source>.*)$""" % (re.escape(self.prefix,)) r"""+(?P<source>.*)$""" % (re.escape(self.prefix,))
@ -269,24 +341,57 @@ class OutputFailure(Exception):
pass pass
def assert_output(output, expected_entries, prefix=None): def verify_normalize(lines, prefix):
time_re = re.compile(r"[0-9:.]{15}")
src_re = re.compile(r'^(?: *)Source path:\.\.\. (.*)$')
for line in lines:
if DEFAULT_REPR_RE.search(line):
msg = "normalize is active, memory address should not appear"
raise OutputFailure(line, msg)
no_prefix = line.replace(prefix if prefix else '', '').strip()
if time_re.match(no_prefix):
msg = "normalize is active, time should not appear"
raise OutputFailure(line, msg)
m = src_re.match(line)
if m:
if not os.path.basename(m.group(1)) == m.group(1):
msg = "normalize is active, path should be only basename"
raise OutputFailure(line, msg)
def assert_output(output, expected_entries, prefix=None, normalize=False):
lines = tuple(filter(None, output.split('\n'))) lines = tuple(filter(None, output.split('\n')))
if expected_entries and not lines:
raise OutputFailure("Output is empty")
if prefix is not None: if prefix is not None:
for line in lines: for line in lines:
if not line.startswith(prefix): if not line.startswith(prefix):
raise OutputFailure(line) raise OutputFailure(line)
if normalize:
verify_normalize(lines, prefix)
# Filter only entries compatible with the current Python
filtered_expected_entries = []
for expected_entry in expected_entries:
if isinstance(expected_entry, _BaseEntry):
if expected_entry.is_compatible_with_current_python_version():
filtered_expected_entries.append(expected_entry)
else:
filtered_expected_entries.append(expected_entry)
expected_entries_count = len(filtered_expected_entries)
any_mismatch = False any_mismatch = False
result = '' result = ''
template = u'\n{line!s:%s} {expected_entry} {arrow}' % max(map(len, lines)) template = u'\n{line!s:%s} {expected_entry} {arrow}' % max(map(len, lines))
for expected_entry, line in zip_longest(expected_entries, lines, fillvalue=""): for expected_entry, line in zip_longest(filtered_expected_entries, lines, fillvalue=""):
mismatch = not (expected_entry and expected_entry.check(line)) mismatch = not (expected_entry and expected_entry.check(line))
any_mismatch |= mismatch any_mismatch |= mismatch
arrow = '<===' * mismatch arrow = '<===' * mismatch
result += template.format(**locals()) result += template.format(**locals())
if len(lines) != len(expected_entries): if len(lines) != expected_entries_count:
result += '\nOutput has {} lines, while we expect {} lines.'.format( result += '\nOutput has {} lines, while we expect {} lines.'.format(
len(lines), len(expected_entries)) len(lines), len(expected_entries))
@ -299,11 +404,11 @@ def assert_sample_output(module):
stderr=True) as output_capturer: stderr=True) as output_capturer:
module.main() module.main()
time = '21:10:42.298924' placeholder_time = '00:00:00.000000'
time_pattern = re.sub(r'\d', r'\\d', time) time_pattern = '[0-9:.]{15}'
def normalise(out): def normalise(out):
out = re.sub(time_pattern, time, out).strip() out = re.sub(time_pattern, placeholder_time, out).strip()
out = re.sub( out = re.sub(
r'^( *)Source path:\.\.\. .*$', r'^( *)Source path:\.\.\. .*$',
r'\1Source path:... Whatever', r'\1Source path:... Whatever',

View file

@ -6,7 +6,7 @@ envlist =
flake8 flake8
pylint pylint
bandit bandit
py{27,34,35,36,37,38,py,py3} py{27,34,35,36,37,38,39,310,py,py3}
readme readme
requirements requirements
clean clean
@ -15,7 +15,8 @@ envlist =
description = Unit tests description = Unit tests
deps = deps =
pytest pytest
commands = pytest py34: typing
commands = pytest {posargs}
[testenv:bandit] [testenv:bandit]
description = PyCQA security linter description = PyCQA security linter
@ -58,4 +59,4 @@ targets = .
exclude = .tox,build,dist,pysnooper.egg-info exclude = .tox,build,dist,pysnooper.egg-info
[pytest] [pytest]
addopts = --strict addopts = --strict-markers