[diffoscope] 02/02: Fix create_limited_print_func

Ximin Luo infinity0 at debian.org
Tue May 30 17:41:02 CEST 2017


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch experimental
in repository diffoscope.

commit 8b88f0e4887aeda1c208ea67718731a23aff7c01
Author: Ximin Luo <infinity0 at debian.org>
Date:   Tue May 30 17:40:43 2017 +0200

    Fix create_limited_print_func
---
 diffoscope/presenters/utils.py |  8 ++++----
 tests/test_presenters.py       | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index 188352e..09f1b74 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -82,16 +82,16 @@ def make_printer(path):
         output.close()
 
 def create_limited_print_func(print_func, max_page_size):
-    count = 0
+    count = [0]
 
-    def fn(val, force=False, count=count):
+    def fn(val, force=False):
         print_func(val)
 
         if force or max_page_size == 0:
             return
 
-        count += len(val)
-        if count >= max_page_size:
+        count[0] += len(val)
+        if count[0] >= max_page_size:
             raise PrintLimitReached()
 
     return fn
diff --git a/tests/test_presenters.py b/tests/test_presenters.py
index ffa0cb4..71f8226 100644
--- a/tests/test_presenters.py
+++ b/tests/test_presenters.py
@@ -22,6 +22,7 @@ import re
 import pytest
 
 from diffoscope.main import main
+from diffoscope.presenters.utils import create_limited_print_func, PrintLimitReached
 
 from .utils.data import cwd_data, get_data
 
@@ -137,3 +138,16 @@ def test_html_option_with_stdout(capsys):
     out = run(capsys, '--html', '-')
 
     assert extract_body(out) == extract_body(get_data('output.html'))
+
+def test_limited_print():
+    fake = lambda x: None
+    with pytest.raises(PrintLimitReached):
+        p = create_limited_print_func(fake, 5)
+        p("123456")
+    with pytest.raises(PrintLimitReached):
+        p = create_limited_print_func(fake, 5)
+        p("123")
+        p("456")
+    p = create_limited_print_func(fake, 5)
+    p("123")
+    p("456", force=True)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list