def qfile(doprint, interactive, qfile): global m # Reinitialize 'm' every time this command is run. This isn't normally # needed for command line use, but is important for the test suite. m = [] printer = PrettyPrinter(indent=4) with open(qfile, 'rb') as fp: while True: try: m.append(pickle.load(fp)) except EOFError: break if doprint: print(_('[----- start pickle -----]')) for i, obj in enumerate(m): count = i + 1 print(_('<----- start object $count ----->')) if isinstance(obj, (bytes, str)): print(obj) else: printer.pprint(obj) print(_('[----- end pickle -----]')) count = len(m) # noqa: F841 banner = _("Number of objects found (see the variable 'm'): $count") if interactive: interact(banner=banner)
def _start_python(self, overrides, banner): # Set the tab completion. with ExitStack() as resources: try: # pragma: no cover import readline, rlcompleter # noqa except ImportError: # pragma: no cover print(_('readline not available'), file=sys.stderr) pass else: readline.parse_and_bind('tab: complete') history_file_template = config.shell.history_file.strip() if len(history_file_template) > 0: # Expand substitutions. substitutions = { key.lower(): value for key, value in config.paths.items() } history_file = Template( history_file_template).safe_substitute(substitutions) with suppress(FileNotFoundError): readline.read_history_file(history_file) resources.callback( readline.write_history_file, history_file) sys.ps1 = config.shell.prompt + ' ' interact(upframe=False, banner=banner, overrides=overrides)
def _start_python(self, overrides, banner): # Set the tab completion. with ExitStack() as resources: try: # pragma: no cover import readline, rlcompleter # noqa except ImportError: # pragma: no cover print(_('readline not available'), file=sys.stderr) pass else: readline.parse_and_bind('tab: complete') history_file_template = config.shell.history_file.strip() if len(history_file_template) > 0: # Expand substitutions. substitutions = { key.lower(): value for key, value in config.paths.items() } history_file = Template( history_file_template).safe_substitute(substitutions) with suppress(FileNotFoundError): readline.read_history_file(history_file) resources.callback(readline.write_history_file, history_file) sys.ps1 = config.shell.prompt + ' ' interact(upframe=False, banner=banner, overrides=overrides)
def _start_python(self, overrides, banner): # Set the tab completion. try: import readline, rlcompleter readline.parse_and_bind('tab: complete') except ImportError: pass sys.ps1 = config.shell.prompt + ' ' interact(upframe=False, banner=banner, overrides=overrides)
def test_interact_no_upframe(self): upframed = False # noqa fp = self._enter(NamedTemporaryFile('w', encoding='utf-8')) self._enter(hackenv('PYTHONSTARTUP', fp.name)) print('print(upframed)', file=fp) fp.flush() interact(upframe=False, banner='') lines = self._stderr.getvalue().splitlines() self.assertIn("NameError: name 'upframed' is not defined", lines)
def test_interact(self): mlist = create_list('*****@*****.**') results = [] fp = self._enter(NamedTemporaryFile('w', encoding='utf-8')) self._enter(hackenv('PYTHONSTARTUP', fp.name)) print('results.append(mlist.list_id)', file=fp) fp.flush() interact() self.assertEqual(results, [mlist.list_id])
def test_interact_no_upframe(self): upframed = False # noqa: F841 fp = self._enter(NamedTemporaryFile('w', encoding='utf-8')) self._enter(hackenv('PYTHONSTARTUP', fp.name)) print('print(upframed)', file=fp) fp.flush() interact(upframe=False, banner='') lines = self._stderr.getvalue().splitlines() self.assertIn("NameError: name 'upframed' is not defined", lines)
def test_interact_multiline(self): # GL issue #224. fp = self._enter(NamedTemporaryFile('w', encoding='utf-8')) self._enter(hackenv('PYTHONSTARTUP', fp.name)) print('import sys', file=fp) print("print('hello', file=sys.stderr)", file=fp) print("print('world', file=sys.stderr)", file=fp) fp.flush() interact(banner='') lines = self._stderr.getvalue() self.assertEqual(lines, 'hello\nworld\n\n', lines)
def process(self, args): """See `ICLISubCommand`.""" printer = PrettyPrinter(indent=4) assert len(args.qfile) == 1, 'Wrong number of positional arguments' with open(args.qfile[0], 'rb') as fp: while True: try: m.append(pickle.load(fp)) except EOFError: break if args.doprint: print(_('[----- start pickle -----]')) for i, obj in enumerate(m): count = i + 1 print(_('<----- start object $count ----->')) if isinstance(obj, (bytes, str)): print(obj) else: printer.pprint(obj) print(_('[----- end pickle -----]')) count = len(m) banner = _("The variable 'm' contains $count objects") if args.interactive: interact(banner=banner)
def process(self, args): """See `ICLISubCommand`.""" printer = PrettyPrinter(indent=4) assert len(args.qfile) == 1, 'Wrong number of positional arguments' with open(args.qfile[0]) as fp: while True: try: m.append(cPickle.load(fp)) except EOFError: break if args.doprint: print(_('[----- start pickle -----]')) for i, obj in enumerate(m): count = i + 1 print(_('<----- start object $count ----->')) if isinstance(obj, basestring): print(obj) else: printer.pprint(obj) print(_('[----- end pickle -----]')) count = len(m) banner = _("The variable 'm' contains $count objects") if args.interactive: interact(banner=banner)
def test_interact_custom_banner(self): self._enter(hackenv('PYTHONSTARTUP', None)) interact(banner='Welcome') stderr = self._stderr.getvalue().splitlines() self.assertEqual(stderr[0], 'Welcome')
def test_interact_default_banner(self): self._enter(hackenv('PYTHONSTARTUP', None)) interact() stderr = self._stderr.getvalue().splitlines() banner = 'Python {} on {} '.format(sys.version, sys.platform) self.assertEqual(stderr[0], banner.splitlines()[0])