Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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])
Ejemplo n.º 8
0
 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])
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
 def test_interact_custom_banner(self):
     self._enter(hackenv('PYTHONSTARTUP', None))
     interact(banner='Welcome')
     stderr = self._stderr.getvalue().splitlines()
     self.assertEqual(stderr[0], 'Welcome')
Ejemplo n.º 15
0
 def test_interact_custom_banner(self):
     self._enter(hackenv('PYTHONSTARTUP', None))
     interact(banner='Welcome')
     stderr = self._stderr.getvalue().splitlines()
     self.assertEqual(stderr[0], 'Welcome')
Ejemplo n.º 16
0
 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])
Ejemplo n.º 17
0
 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])