##// END OF EJS Templates
support password in input_request
MinRK -
Show More
@@ -748,6 +748,8 var IPython = (function (IPython) {
748 748 // disable any other raw_inputs, if they are left around
749 749 $("div.output_subarea.raw_input_container").remove();
750 750
751 var input_type = content.password ? 'password' : 'text';
752
751 753 area.append(
752 754 $("<div/>")
753 755 .addClass("box-flex1 output_subarea raw_input_container")
@@ -759,7 +761,7 var IPython = (function (IPython) {
759 761 .append(
760 762 $("<input/>")
761 763 .addClass("raw_input")
762 .attr('type', 'text')
764 .attr('type', input_type)
763 765 .attr("size", 47)
764 766 .keydown(function (event, ui) {
765 767 // make sure we submit on enter,
@@ -788,10 +790,15 var IPython = (function (IPython) {
788 790 var theprompt = container.find("span.raw_input_prompt");
789 791 var theinput = container.find("input.raw_input");
790 792 var value = theinput.val();
793 var echo = value;
794 // don't echo if it's a password
795 if (theinput.attr('type') == 'password') {
796 echo = '········';
797 }
791 798 var content = {
792 799 output_type : 'stream',
793 800 name : 'stdout',
794 text : theprompt.text() + value + '\n'
801 text : theprompt.text() + echo + '\n'
795 802 }
796 803 // remove form container
797 804 container.parent().remove();
@@ -6,6 +6,7
6 6
7 7 from __future__ import print_function
8 8
9 import getpass
9 10 import sys
10 11 import time
11 12 import traceback
@@ -333,7 +334,42 class Kernel(Configurable):
333 334 parent=parent,
334 335 ident=self._topic('status'),
335 336 )
337
338 def _forward_raw_input(self, ident, parent, allow_stdin):
339 """Replace raw_input. Note that is not sufficient to replace
340 raw_input in the user namespace.
341 """
342
343 if allow_stdin:
344 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
345 input = lambda prompt='': eval(raw_input(prompt))
346 _getpass = lambda prompt='', stream=None: self._raw_input(
347 prompt, ident, parent, password=True
348 )
349 else:
350 _getpass = raw_input = input = lambda prompt='' : self._no_raw_input()
351
352
353 if py3compat.PY3:
354 self._sys_raw_input = builtin_mod.input
355 builtin_mod.input = raw_input
356 else:
357 self._sys_raw_input = builtin_mod.raw_input
358 self._sys_eval_input = builtin_mod.input
359 builtin_mod.raw_input = raw_input
360 builtin_mod.input = input
361 self._save_getpass = getpass.getpass
362 getpass.getpass = _getpass
363
364 def _restore_raw_input(self):
365 # Restore raw_input
366 if py3compat.PY3:
367 builtin_mod.input = self._sys_raw_input
368 else:
369 builtin_mod.raw_input = self._sys_raw_input
370 builtin_mod.input = self._sys_eval_input
336 371
372 getpass.getpass = self._save_getpass
337 373
338 374 def execute_request(self, stream, ident, parent):
339 375 """handle an execute_request"""
@@ -354,22 +390,7 class Kernel(Configurable):
354 390
355 391 shell = self.shell # we'll need this a lot here
356 392
357 # Replace raw_input. Note that is not sufficient to replace
358 # raw_input in the user namespace.
359 if content.get('allow_stdin', False):
360 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
361 input = lambda prompt='': eval(raw_input(prompt))
362 else:
363 raw_input = input = lambda prompt='' : self._no_raw_input()
364
365 if py3compat.PY3:
366 self._sys_raw_input = builtin_mod.input
367 builtin_mod.input = raw_input
368 else:
369 self._sys_raw_input = builtin_mod.raw_input
370 self._sys_eval_input = builtin_mod.input
371 builtin_mod.raw_input = raw_input
372 builtin_mod.input = input
393 self._forward_raw_input(ident, parent, content.get('allow_stdin', False))
373 394
374 395 # Set the parent message of the display hook and out streams.
375 396 shell.set_parent(parent)
@@ -398,12 +419,7 class Kernel(Configurable):
398 419 else:
399 420 status = u'ok'
400 421 finally:
401 # Restore raw_input.
402 if py3compat.PY3:
403 builtin_mod.input = self._sys_raw_input
404 else:
405 builtin_mod.raw_input = self._sys_raw_input
406 builtin_mod.input = self._sys_eval_input
422 self._restore_raw_input()
407 423
408 424 reply_content[u'status'] = status
409 425
@@ -727,8 +743,8 class Kernel(Configurable):
727 743 stdin."""
728 744 raise StdinNotImplementedError("raw_input was called, but this "
729 745 "frontend does not support stdin.")
730
731 def _raw_input(self, prompt, ident, parent):
746
747 def _raw_input(self, prompt, ident, parent, password=False):
732 748 # Flush output before making the request.
733 749 sys.stderr.flush()
734 750 sys.stdout.flush()
@@ -743,7 +759,7 class Kernel(Configurable):
743 759 raise
744 760
745 761 # Send the input request.
746 content = json_clean(dict(prompt=prompt))
762 content = json_clean(dict(prompt=prompt, password=password))
747 763 self.session.send(self.stdin_socket, u'input_request', content, parent,
748 764 ident=ident)
749 765
General Comments 0
You need to be logged in to leave comments. Login now