summaryrefslogtreecommitdiff
path: root/compat.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-09-16 23:22:53 +0200
committerAnthon van der Neut <anthon@mnt.org>2018-09-16 23:22:53 +0200
commit9c93fb71f5787f222a850cfb0a400b70cadd78c2 (patch)
tree6fa1d452d0e40d012b3a9fc103b62db68f705720 /compat.py
parent8227f3f7f151e981def0c7f698cc8f9c7e64149f (diff)
downloadruamel.yaml-9c93fb71f5787f222a850cfb0a400b70cadd78c2.tar.gz
nprint object with set_max_print to limit recursion
Diffstat (limited to 'compat.py')
-rw-r--r--compat.py33
1 files changed, 30 insertions, 3 deletions
diff --git a/compat.py b/compat.py
index 6f708cf..a9ac6d3 100644
--- a/compat.py
+++ b/compat.py
@@ -7,8 +7,10 @@ from __future__ import print_function
import sys
import os
import types
+import traceback
from abc import abstractmethod
+
# fmt: off
if False: # MYPY
from typing import Any, Dict, Optional, List, Union, BinaryIO, IO, Text, Tuple, Optional # NOQA
@@ -183,11 +185,36 @@ def dbg(val=None):
return _debug & val
-def nprint(*args, **kw):
- # type: (Any, Any) -> None
- if bool(_debug):
+class Nprint(object):
+ def __init__(self):
+ # type: () -> None
+ self._max_print = None
+ self._count = None
+
+ def __call__(self, *args, **kw):
+ # type: (Any, Any) -> None
+ if not bool(_debug):
+ return
dbgprint = print # to fool checking for print statements by dv utility
dbgprint(*args, **kw)
+ sys.stdout.flush()
+ if self._max_print is not None:
+ if self._count is None:
+ self._count = self._max_print
+ self._count -= 1
+ if self._count == 0:
+ dbgprint('forced exit\n')
+ traceback.print_stack()
+ sys.stdout.flush()
+ sys.exit(0)
+
+ def set_max_print(self, i):
+ # type: (int) -> None
+ self._max_print = i
+ self._count = None
+
+
+nprint = Nprint()
# char checkers following production rules