summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Chabot <simon.chabot@logilab.fr>2019-11-28 18:57:56 +0100
committerSimon Chabot <simon.chabot@logilab.fr>2019-11-28 18:57:56 +0100
commite57d20105ecf9a17552eaddd74368a84ba839457 (patch)
treeaaeda39b24e0aa49b40418a08b1780335a82baf4
parent181715113d104f59d31d93fe0bd246fa815e6d4c (diff)
downloadlogilab-common-e57d20105ecf9a17552eaddd74368a84ba839457.tar.gz
[shellutils, fix] Deprecate 'input' as argument of RawInput in favor of 'input_function'
When no parameter was given to the class, None was used as the `input`. It used to work because when `input` (the argument) was None `raw_input` of six was used instead. (-> self._input = input or raw_input). Now that six has been dropped, we cannot have `input=None` in the arguments list anymore and use the builtin `input` at the same time. This patch suggests to deprecate this argument in favor of a new one called `input_function`. If no `input_function`, let's use the `input` builtin. For backward compatibility, if `input` should be given, it's caught in the `kwargs` and used as `input_function`, and a deprecation warning is shown.
-rw-r--r--logilab/common/shellutils.py12
-rw-r--r--test/unittest_shellutils.py7
2 files changed, 17 insertions, 2 deletions
diff --git a/logilab/common/shellutils.py b/logilab/common/shellutils.py
index 44f34ef..792ae45 100644
--- a/logilab/common/shellutils.py
+++ b/logilab/common/shellutils.py
@@ -35,6 +35,7 @@ import errno
import string
import random
import subprocess
+import warnings
from os.path import exists, isdir, islink, basename, join
from logilab.common import STD_BLACKLIST, _handle_blacklist
@@ -338,8 +339,15 @@ class progress(object):
class RawInput(object):
- def __init__(self, input=None, printer=None):
- self._input = input
+ def __init__(self, input_function=None, printer=None, **kwargs):
+ if 'input' in kwargs:
+ input_function = kwargs.pop('input')
+ warnings.warn(
+ "'input' argument is deprecated,"
+ "use 'input_function' instead",
+ DeprecationWarning,
+ )
+ self._input = input_function or input
self._print = printer
def ask(self, question, options, default):
diff --git a/test/unittest_shellutils.py b/test/unittest_shellutils.py
index 4ae76c7..49b06c7 100644
--- a/test/unittest_shellutils.py
+++ b/test/unittest_shellutils.py
@@ -20,6 +20,7 @@
import sys, os, tempfile, shutil
from os.path import join, dirname, abspath
import datetime, time
+from unittest.mock import patch
from logilab.common.testlib import TestCase, unittest_main
@@ -178,6 +179,12 @@ class RawInputTC(TestCase):
null_printer = lambda x: None
self.qa = RawInput(self.auto_input, null_printer)
+ def test_ask_using_builtin_input(self):
+ with patch('builtins.input', return_value='no'):
+ qa = RawInput()
+ answer = qa.ask('text', ('yes', 'no'), 'yes')
+ self.assertEqual(answer, 'no')
+
def test_ask_default(self):
self.input_answer = ''
answer = self.qa.ask('text', ('yes', 'no'), 'yes')