mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-01-24 02:24:55 +00:00
Compare commits
27 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0561a89c0e | ||
|
|
05212d3092 | ||
|
|
3c0f9eb65a | ||
|
|
eee41b20f8 | ||
|
|
2931e1374a | ||
|
|
8bf06d25a1 | ||
|
|
57472b4677 | ||
|
|
d91fd2b255 | ||
|
|
05f1359427 | ||
|
|
ac74c8f020 | ||
|
|
f2c60de87f | ||
|
|
0b96becd1b | ||
|
|
591341e973 | ||
|
|
206ae83b4f | ||
|
|
8c35d81835 | ||
|
|
23d3e43f0e | ||
|
|
e1a927311b | ||
|
|
60775ff71f | ||
|
|
4224cf9694 | ||
|
|
caf4ec584a | ||
|
|
231969074e | ||
|
|
1ad8ae08b0 | ||
|
|
7ca28af18d | ||
|
|
f3d7f39af4 | ||
|
|
4e277a5a1f | ||
|
|
bea7c7a965 | ||
|
|
0f1e67b26b |
19 changed files with 475 additions and 122 deletions
|
|
@ -89,4 +89,10 @@ can customize that:
|
|||
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.
|
||||
wall time.
|
||||
|
||||
The output is colored for easy viewing by default, except on Windows. Disable colors like so:
|
||||
|
||||
```python
|
||||
@pysnooper.snoop(color=False)
|
||||
````
|
||||
|
|
|
|||
4
AUTHORS
4
AUTHORS
|
|
@ -24,3 +24,7 @@ Itamar.Raviv
|
|||
iory
|
||||
Mark Blakeney
|
||||
Yael Mintz
|
||||
Lumír 'Frenzy' Balhar
|
||||
Lukas Klenk
|
||||
sizhky
|
||||
Andrej730
|
||||
|
|
|
|||
63
README.md
63
README.md
|
|
@ -1,4 +1,4 @@
|
|||
# PySnooper - Never use print for debugging again #
|
||||
# PySnooper - Never use print for debugging again
|
||||
|
||||
**PySnooper** is a poor man's debugger. If you've used Bash, it's like `set -x` for Python, except it's fancier.
|
||||
|
||||
|
|
@ -12,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:
|
||||
|
||||
|
|
@ -34,36 +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]
|
||||
Elapsed time: 00:00:00.000001
|
||||
```
|
||||

|
||||
|
||||
Or if you don't want to trace an entire function, you can wrap the relevant part in a `with` block:
|
||||
|
||||
|
|
@ -101,7 +72,7 @@ New var:....... mid = 453.0
|
|||
Elapsed time: 00:00:00.000344
|
||||
```
|
||||
|
||||
# Features #
|
||||
## Features
|
||||
|
||||
If stderr is not easily accessible for you, you can redirect the output to a file:
|
||||
|
||||
|
|
@ -126,7 +97,7 @@ Show snoop lines for functions that your function calls:
|
|||
**See [Advanced Usage](https://github.com/cool-RR/PySnooper/blob/master/ADVANCED_USAGE.md) for more options.** <------
|
||||
|
||||
|
||||
# Installation with Pip #
|
||||
## Installation with Pip
|
||||
|
||||
The best way to install **PySnooper** is with Pip:
|
||||
|
||||
|
|
@ -134,7 +105,7 @@ The best way to install **PySnooper** is with Pip:
|
|||
$ pip install pysnooper
|
||||
```
|
||||
|
||||
# Other installation options #
|
||||
## Other installation options
|
||||
|
||||
Conda with conda-forge channel:
|
||||
|
||||
|
|
@ -154,12 +125,30 @@ Fedora Linux:
|
|||
$ dnf install python3-pysnooper
|
||||
```
|
||||
|
||||
# License #
|
||||
|
||||
## 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.
|
||||
|
||||
|
||||
# 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)
|
||||
|
|
|
|||
|
|
@ -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.?$'),)
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
BIN
misc/output.jpg
Normal file
BIN
misc/output.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 181 KiB |
3
pyproject.toml
Normal file
3
pyproject.toml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[build-system]
|
||||
requires = ["setuptools"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
|
@ -24,7 +24,7 @@ import collections
|
|||
__VersionInfo = collections.namedtuple('VersionInfo',
|
||||
('major', 'minor', 'micro'))
|
||||
|
||||
__version__ = '1.0.0'
|
||||
__version__ = '1.2.3'
|
||||
__version_info__ = __VersionInfo(*(map(int, __version__.split('.'))))
|
||||
|
||||
del collections, __VersionInfo # Avoid polluting the namespace
|
||||
|
|
|
|||
|
|
@ -20,6 +20,13 @@ if pycompat.PY2:
|
|||
|
||||
|
||||
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=(), max_length=None, normalize=False):
|
||||
|
|
@ -67,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
|
||||
|
|
@ -77,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:
|
||||
|
|
@ -202,10 +225,18 @@ class Tracer:
|
|||
|
||||
@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=(),
|
||||
max_variable_length=100, normalize=False, relative_time=False):
|
||||
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 = [
|
||||
|
|
@ -233,6 +264,32 @@ class Tracer:
|
|||
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:
|
||||
|
|
@ -317,12 +374,19 @@ class Tracer:
|
|||
|
||||
### 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}Elapsed time: {elapsed_time_string}'.format(**locals())
|
||||
'{indent}{_FOREGROUND_YELLOW}{_STYLE_DIM}'
|
||||
'Elapsed time: {_STYLE_NORMAL}{elapsed_time_string}'
|
||||
'{_STYLE_RESET_ALL}'.format(**locals())
|
||||
)
|
||||
# #
|
||||
### Finished writing elapsed time. ####################################
|
||||
|
|
@ -363,12 +427,24 @@ class Tracer:
|
|||
else:
|
||||
return None
|
||||
|
||||
# #
|
||||
### 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: #################################################
|
||||
# #
|
||||
|
|
@ -394,8 +470,9 @@ class Tracer:
|
|||
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 = ""
|
||||
|
|
@ -405,7 +482,7 @@ class Tracer:
|
|||
"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: ##########################
|
||||
|
|
@ -423,11 +500,13 @@ class Tracer:
|
|||
|
||||
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. ###########################
|
||||
|
|
@ -463,16 +542,15 @@ 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}{timestamp} {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':
|
||||
self.frame_to_local_reprs.pop(frame, None)
|
||||
|
|
@ -485,14 +563,17 @@ class Tracer:
|
|||
max_length=self.max_variable_length,
|
||||
normalize=self.normalize,
|
||||
)
|
||||
self.write('{indent}Return value:.. {return_value_repr}'.
|
||||
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()
|
||||
if self.max_variable_length:
|
||||
exception = utils.truncate(exception, self.max_variable_length)
|
||||
self.write('{indent}Exception:..... {exception}'.
|
||||
format(**locals()))
|
||||
self.write('{indent}{_FOREGROUND_RED}Exception:..... '
|
||||
'{_STYLE_BRIGHT}{exception}'
|
||||
'{_STYLE_RESET_ALL}'.format(**locals()))
|
||||
|
||||
return self.trace
|
||||
|
|
|
|||
37
setup.cfg
Normal file
37
setup.cfg
Normal 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
|
||||
1
setup.py
1
setup.py
|
|
@ -38,6 +38,7 @@ setuptools.setup(
|
|||
'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',
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ def bar():
|
|||
raise
|
||||
|
||||
|
||||
@pysnooper.snoop(depth=3)
|
||||
@pysnooper.snoop(depth=3, color=False)
|
||||
def main():
|
||||
try:
|
||||
bar()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import pysnooper
|
||||
|
||||
|
||||
@pysnooper.snoop(depth=2)
|
||||
@pysnooper.snoop(depth=2, color=False)
|
||||
def factorial(x):
|
||||
if x <= 1:
|
||||
return 1
|
||||
|
|
|
|||
|
|
@ -26,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 = '失败'
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -38,7 +38,7 @@ def test_rejecting_coroutine_functions():
|
|||
assert pycompat.iscoroutinefunction(foo)
|
||||
assert not pycompat.isasyncgenfunction(foo)
|
||||
with pytest.raises(NotImplementedError):
|
||||
pysnooper.snoop()(foo)
|
||||
pysnooper.snoop(color=False)(foo)
|
||||
|
||||
|
||||
def test_rejecting_async_generator_functions():
|
||||
|
|
@ -56,6 +56,6 @@ def test_rejecting_async_generator_functions():
|
|||
assert not pycompat.iscoroutinefunction(foo)
|
||||
assert pycompat.isasyncgenfunction(foo)
|
||||
with pytest.raises(NotImplementedError):
|
||||
pysnooper.snoop()(foo)
|
||||
pysnooper.snoop(color=False)(foo)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import time
|
|||
import types
|
||||
import os
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
from pysnooper.utils import truncate
|
||||
import pytest
|
||||
|
|
@ -25,7 +26,7 @@ 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
|
||||
|
|
@ -53,7 +54,7 @@ def test_string_io():
|
|||
|
||||
|
||||
def test_relative_time():
|
||||
snoop = pysnooper.snoop(relative_time=True)
|
||||
snoop = pysnooper.snoop(relative_time=True, color=False)
|
||||
|
||||
def foo(x):
|
||||
if x == 0:
|
||||
|
|
@ -186,7 +187,7 @@ def test_relative_time():
|
|||
|
||||
def test_thread_info():
|
||||
|
||||
@pysnooper.snoop(thread_info=True)
|
||||
@pysnooper.snoop(thread_info=True, color=False)
|
||||
def my_function(foo):
|
||||
x = 7
|
||||
y = 8
|
||||
|
|
@ -217,7 +218,7 @@ def test_thread_info():
|
|||
|
||||
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
|
||||
|
|
@ -306,7 +307,7 @@ def test_callable(normalize):
|
|||
def write(msg):
|
||||
string_io.write(msg)
|
||||
|
||||
@pysnooper.snoop(write, normalize=normalize)
|
||||
@pysnooper.snoop(write, normalize=normalize, color=False)
|
||||
def my_function(foo):
|
||||
x = 7
|
||||
y = 8
|
||||
|
|
@ -347,7 +348,7 @@ def test_watch(normalize):
|
|||
'foo.x',
|
||||
'io.__name__',
|
||||
'len(foo.__dict__["x"] * "abc")',
|
||||
), normalize=normalize)
|
||||
), normalize=normalize, color=False)
|
||||
def my_function():
|
||||
foo = Foo()
|
||||
for i in range(2):
|
||||
|
|
@ -395,7 +396,8 @@ def test_watch_explode(normalize):
|
|||
self.x = x
|
||||
self.y = y
|
||||
|
||||
@pysnooper.snoop(watch_explode=('_d', '_point', 'lst + []'), normalize=normalize)
|
||||
@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)
|
||||
|
|
@ -454,7 +456,7 @@ def test_variables_classes(normalize):
|
|||
pysnooper.Attrs('_d'), # doesn't have attributes
|
||||
pysnooper.Attrs('_s'),
|
||||
pysnooper.Indices('_lst')[-3:],
|
||||
), normalize=normalize)
|
||||
), normalize=normalize, color=False)
|
||||
def my_function():
|
||||
_d = {'a': 1, 'b': 2, 'c': 'ignore'}
|
||||
_s = WithSlots()
|
||||
|
|
@ -501,7 +503,7 @@ def test_single_watch_no_comma(normalize):
|
|||
def square(self):
|
||||
self.x **= 2
|
||||
|
||||
@pysnooper.snoop(watch='foo', normalize=normalize)
|
||||
@pysnooper.snoop(watch='foo', normalize=normalize, color=False)
|
||||
def my_function():
|
||||
foo = Foo()
|
||||
for i in range(2):
|
||||
|
|
@ -537,7 +539,7 @@ def test_single_watch_no_comma(normalize):
|
|||
|
||||
@pytest.mark.parametrize("normalize", (True, False))
|
||||
def test_long_variable(normalize):
|
||||
@pysnooper.snoop(normalize=normalize)
|
||||
@pysnooper.snoop(normalize=normalize, color=False)
|
||||
def my_function():
|
||||
foo = list(range(1000))
|
||||
return foo
|
||||
|
|
@ -566,7 +568,7 @@ def test_long_variable(normalize):
|
|||
|
||||
@pytest.mark.parametrize("normalize", (True, False))
|
||||
def test_long_variable_with_custom_max_variable_length(normalize):
|
||||
@pysnooper.snoop(max_variable_length=200, normalize=normalize)
|
||||
@pysnooper.snoop(max_variable_length=200, normalize=normalize, color=False)
|
||||
def my_function():
|
||||
foo = list(range(1000))
|
||||
return foo
|
||||
|
|
@ -595,7 +597,7 @@ def test_long_variable_with_custom_max_variable_length(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)
|
||||
@pysnooper.snoop(max_variable_length=None, normalize=normalize, color=False)
|
||||
def my_function():
|
||||
foo = list(range(1000))
|
||||
return foo
|
||||
|
|
@ -628,7 +630,7 @@ def test_repr_exception(normalize):
|
|||
def __repr__(self):
|
||||
1 / 0
|
||||
|
||||
@pysnooper.snoop(normalize=normalize)
|
||||
@pysnooper.snoop(normalize=normalize, color=False)
|
||||
def my_function():
|
||||
bad = Bad()
|
||||
|
||||
|
|
@ -669,7 +671,7 @@ def test_depth(normalize):
|
|||
result2 = f3(x2)
|
||||
return result2
|
||||
|
||||
@pysnooper.snoop(string_io, depth=3, normalize=normalize)
|
||||
@pysnooper.snoop(string_io, depth=3, normalize=normalize, color=False)
|
||||
def f1(x1):
|
||||
result1 = f2(x1)
|
||||
return result1
|
||||
|
|
@ -722,7 +724,8 @@ def test_method_and_prefix(normalize):
|
|||
def __init__(self):
|
||||
self.x = 2
|
||||
|
||||
@pysnooper.snoop(watch=('self.x',), prefix='ZZZ', normalize=normalize)
|
||||
@pysnooper.snoop(watch=('self.x',), prefix='ZZZ', normalize=normalize,
|
||||
color=False)
|
||||
def square(self):
|
||||
foo = 7
|
||||
self.x **= 2
|
||||
|
|
@ -762,7 +765,7 @@ def test_file_output(normalize):
|
|||
with mini_toolbox.create_temp_folder(prefix='pysnooper') as folder:
|
||||
path = folder / 'foo.log'
|
||||
|
||||
@pysnooper.snoop(path, normalize=normalize)
|
||||
@pysnooper.snoop(path, normalize=normalize, color=False)
|
||||
def my_function(_foo):
|
||||
x = 7
|
||||
y = 8
|
||||
|
|
@ -800,7 +803,7 @@ def test_confusing_decorator_lines(normalize):
|
|||
|
||||
@empty_decorator
|
||||
@pysnooper.snoop(string_io, normalize=normalize,
|
||||
depth=2) # Multi-line decorator for extra confusion!
|
||||
depth=2, color=False)
|
||||
@empty_decorator
|
||||
@empty_decorator
|
||||
def my_function(foo):
|
||||
|
|
@ -840,7 +843,7 @@ def test_confusing_decorator_lines(normalize):
|
|||
@pytest.mark.parametrize("normalize", (True, False))
|
||||
def test_lambda(normalize):
|
||||
string_io = io.StringIO()
|
||||
my_function = pysnooper.snoop(string_io, normalize=normalize)(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()
|
||||
|
|
@ -866,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
|
||||
''')
|
||||
|
|
@ -898,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
|
||||
|
|
@ -932,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
|
||||
|
|
@ -975,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
|
||||
|
|
@ -1002,7 +1005,7 @@ def test_needs_parentheses():
|
|||
@pytest.mark.parametrize("normalize", (True, False))
|
||||
def test_with_block(normalize):
|
||||
# Testing that a single Tracer can handle many mixed uses
|
||||
snoop = pysnooper.snoop(normalize=normalize)
|
||||
snoop = pysnooper.snoop(normalize=normalize, color=False)
|
||||
|
||||
def foo(x):
|
||||
if x == 0:
|
||||
|
|
@ -1151,7 +1154,7 @@ def test_with_block_depth(normalize):
|
|||
|
||||
def f1(x1):
|
||||
str(3)
|
||||
with pysnooper.snoop(string_io, depth=3, normalize=normalize):
|
||||
with pysnooper.snoop(string_io, depth=3, normalize=normalize, color=False):
|
||||
result1 = f2(x1)
|
||||
return result1
|
||||
|
||||
|
|
@ -1210,7 +1213,7 @@ def test_cellvars(normalize):
|
|||
return f3(a)
|
||||
|
||||
def f1(a):
|
||||
with pysnooper.snoop(string_io, depth=4, normalize=normalize):
|
||||
with pysnooper.snoop(string_io, depth=4, normalize=normalize, color=False):
|
||||
result1 = f2(a)
|
||||
return result1
|
||||
|
||||
|
|
@ -1275,7 +1278,7 @@ def test_var_order(normalize):
|
|||
|
||||
five, six, seven = 5, 6, 7
|
||||
|
||||
with pysnooper.snoop(string_io, depth=2, normalize=normalize):
|
||||
with pysnooper.snoop(string_io, depth=2, normalize=normalize, color=False):
|
||||
result = f(1, 2, 3, 4)
|
||||
|
||||
output = string_io.getvalue()
|
||||
|
|
@ -1344,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)
|
||||
|
|
@ -1441,7 +1444,7 @@ def test_custom_repr(normalize):
|
|||
(large, print_list_size),
|
||||
(dict, print_dict),
|
||||
(evil_condition, lambda x: 'I am evil')),
|
||||
normalize=normalize,)
|
||||
normalize=normalize, color=False)
|
||||
def sum_to_x(x):
|
||||
l = list(range(x))
|
||||
a = {'1': 1, '2': 2}
|
||||
|
|
@ -1473,7 +1476,8 @@ def test_custom_repr(normalize):
|
|||
def test_custom_repr_single(normalize):
|
||||
string_io = io.StringIO()
|
||||
|
||||
@pysnooper.snoop(string_io, custom_repr=(list, lambda l: 'foofoo!'), normalize=normalize)
|
||||
@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
|
||||
|
|
@ -1507,7 +1511,7 @@ def test_disable():
|
|||
return x + y
|
||||
|
||||
with mini_toolbox.TempValueSetter((pysnooper.tracer, 'DISABLED'), True):
|
||||
tracer = pysnooper.snoop(string_io)
|
||||
tracer = pysnooper.snoop(string_io, color=False)
|
||||
with tracer:
|
||||
result = my_function('baba')
|
||||
my_decorated_function = tracer(my_function)
|
||||
|
|
@ -1521,7 +1525,7 @@ def test_disable():
|
|||
def test_class(normalize):
|
||||
string_io = io.StringIO()
|
||||
|
||||
@pysnooper.snoop(string_io, normalize=normalize)
|
||||
@pysnooper.snoop(string_io, normalize=normalize, color=False)
|
||||
class MyClass(object):
|
||||
def __init__(self):
|
||||
self.x = 7
|
||||
|
|
@ -1568,7 +1572,7 @@ def test_class_with_decorated_method(normalize):
|
|||
return result
|
||||
return wrapper
|
||||
|
||||
@pysnooper.snoop(string_io, normalize=normalize)
|
||||
@pysnooper.snoop(string_io, normalize=normalize, color=False)
|
||||
class MyClass(object):
|
||||
def __init__(self):
|
||||
self.x = 7
|
||||
|
|
@ -1617,13 +1621,13 @@ def test_class_with_decorated_method_and_snoop_applied_to_method(normalize):
|
|||
return result
|
||||
return wrapper
|
||||
|
||||
@pysnooper.snoop(string_io, normalize=normalize)
|
||||
@pysnooper.snoop(string_io, normalize=normalize, color=False)
|
||||
class MyClass(object):
|
||||
def __init__(self):
|
||||
self.x = 7
|
||||
|
||||
@decorator
|
||||
@pysnooper.snoop(string_io, normalize=normalize)
|
||||
@pysnooper.snoop(string_io, normalize=normalize, color=False)
|
||||
def my_method(self, foo):
|
||||
y = 8
|
||||
return y + self.x
|
||||
|
|
@ -1671,7 +1675,7 @@ def test_class_with_decorated_method_and_snoop_applied_to_method(normalize):
|
|||
def test_class_with_property(normalize):
|
||||
string_io = io.StringIO()
|
||||
|
||||
@pysnooper.snoop(string_io, normalize=normalize)
|
||||
@pysnooper.snoop(string_io, normalize=normalize, color=False)
|
||||
class MyClass(object):
|
||||
def __init__(self):
|
||||
self._x = 0
|
||||
|
|
@ -1759,7 +1763,7 @@ def test_snooping_on_class_does_not_cause_base_class_to_be_snooped(normalize):
|
|||
def method_on_base_class(self):
|
||||
self.method_on_base_class_was_called = True
|
||||
|
||||
@pysnooper.snoop(string_io, normalize=normalize)
|
||||
@pysnooper.snoop(string_io, normalize=normalize, color=False)
|
||||
class MyClass(UnsnoopedBaseClass):
|
||||
def method_on_child_class(self):
|
||||
self.method_on_base_class()
|
||||
|
|
@ -1793,7 +1797,7 @@ def test_normalize():
|
|||
def __init__(self, a):
|
||||
self.a = a
|
||||
|
||||
@pysnooper.snoop(string_io, normalize=True)
|
||||
@pysnooper.snoop(string_io, normalize=True, color=False)
|
||||
def add():
|
||||
a = A(19)
|
||||
b = A(22)
|
||||
|
|
@ -1830,7 +1834,7 @@ def test_normalize_prefix():
|
|||
def __init__(self, a):
|
||||
self.a = a
|
||||
|
||||
@pysnooper.snoop(string_io, normalize=True, prefix=_prefix)
|
||||
@pysnooper.snoop(string_io, normalize=True, prefix=_prefix, color=False)
|
||||
def add():
|
||||
a = A(19)
|
||||
b = A(22)
|
||||
|
|
@ -1866,7 +1870,7 @@ def test_normalize_thread_info():
|
|||
def __init__(self, a):
|
||||
self.a = a
|
||||
|
||||
@pysnooper.snoop(string_io, normalize=True, thread_info=True)
|
||||
@pysnooper.snoop(string_io, normalize=True, thread_info=True, color=False)
|
||||
def add():
|
||||
a = A(19)
|
||||
b = A(22)
|
||||
|
|
@ -1879,7 +1883,7 @@ def test_normalize_thread_info():
|
|||
|
||||
def test_exception():
|
||||
string_io = io.StringIO()
|
||||
@pysnooper.snoop(string_io)
|
||||
@pysnooper.snoop(string_io, color=False)
|
||||
def f():
|
||||
x = 8
|
||||
raise MemoryError
|
||||
|
|
@ -1905,9 +1909,159 @@ def test_exception():
|
|||
|
||||
|
||||
def test_exception_on_entry():
|
||||
@pysnooper.snoop()
|
||||
@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))
|
||||
|
|
|
|||
72
tests/test_utils/test_regex.py
Normal file
72
tests/test_utils/test_regex.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue