def _run_mach(self, args, provider_file=None, entry_point=None, context_handler=None): m = Mach(os.getcwd()) m.define_category('testing', 'Mach unittest', 'Testing for mach core', 10) m.populate_context_handler = context_handler if provider_file: m.load_commands_from_file( os.path.join(self.provider_dir, provider_file)) if entry_point: m.load_commands_from_entry_point(entry_point) stdout = StringIO() stderr = StringIO() stdout.encoding = 'UTF-8' stderr.encoding = 'UTF-8' try: result = m.run(args, stdout=stdout, stderr=stderr) except SystemExit: result = None return (result, stdout.getvalue(), stderr.getvalue())
def _run_mach(self, args): m = mach.main.Mach(os.getcwd()) stdout = StringIO() stderr = StringIO() stdout.encoding = 'UTF-8' stderr.encoding = 'UTF-8' result = m.run(args, stdout=stdout, stderr=stderr) return (result, stdout.getvalue(), stderr.getvalue())
def _run_mach(self, argv, *args, **kwargs): m = self.get_mach(*args, **kwargs) stdout = StringIO() stderr = StringIO() stdout.encoding = 'UTF-8' stderr.encoding = 'UTF-8' try: result = m.run(argv, stdout=stdout, stderr=stderr) except SystemExit: result = None return (result, stdout.getvalue(), stderr.getvalue())
def check_cpaste(code, should_fail=False): """Execute code via 'cpaste' and ensure it was executed, unless should_fail is set. """ _ip.user_ns['code_ran'] = False src = StringIO() if not hasattr(src, 'encoding'): # IPython expects stdin to have an encoding attribute src.encoding = None src.write('\n') src.write(code) src.write('\n--\n') src.seek(0) stdin_save = sys.stdin sys.stdin = src try: context = tt.AssertPrints if should_fail else tt.AssertNotPrints with context("Traceback (most recent call last)"): _ip.magic('cpaste') if not should_fail: assert _ip.user_ns['code_ran'] finally: sys.stdin = stdin_save
def FlagsForFile(filename): mach = mach_module.get_mach() out = StringIO() out.encoding = None mach.run(['compileflags', filename], stdout=out, stderr=out) return {'flags': shlex.split(out.getvalue()), 'do_cache': True}
def test_error_encoding_sys(): # The encoding should come from sys.stdout (not sys.stderr) # because it is set there. if sys.version_info[:2] != (2, 6): raise nose.SkipTest('only needed for python 2.6') data = '\xc3\xa9' u_data = data.decode('utf-8') class MyApp(App): def __init__(self): super(MyApp, self).__init__( description='testing', version='0.1', command_manager=CommandManager('tests'), ) stdout = StringIO() stdout.encoding = 'utf-8' stderr = StringIO() getdefaultlocale = mock.Mock(return_value=('ignored', 'utf-16')) with mock.patch('sys.stdout', stdout): with mock.patch('sys.stderr', stderr): with mock.patch('locale.getdefaultlocale', getdefaultlocale): app = MyApp() app.stderr.write(u_data) actual = stderr.getvalue() assert data == actual
def generic_test_show_path_info_unicode(self, encoding): """generic test for the output of --info with unicode paths """ path = os.path.join(self.root_dir, u"ñoño".encode('utf-8')) mdid = self.fs.create(path, "") self.fs.set_node_id(path, "uuid1") mdobj = self.fs.get_by_mdid(mdid) self.fs.create_partial(mdobj.node_id, mdobj.share_id) fh = self.fs.get_partial_for_writing(mdobj.node_id, mdobj.share_id) fh.write("foobar") fh.close() self.fs.commit_partial(mdobj.node_id, mdobj.share_id, "localhash") self.fs.remove_partial("uuid1", "") if encoding is not None: path = path.decode(encoding) else: path = path.decode('utf-8') d = self.tool.get_metadata(path) out = StringIO() out.encoding = encoding expected = """ File: %(path_info)s crc32: None generation: None info_created: %(info_created)s info_is_partial: %(info_is_partial)s info_last_downloaded: %(info_last_downloaded)s info_last_partial_created: %(info_last_partial_created)s info_last_partial_removed: %(info_last_partial_removed)s info_node_id_assigned: %(info_node_id_assigned)s is_dir: %(is_dir)s local_hash: %(local_hash)s mdid: %(mdid)s node_id: %(node_id)s path: %(path)s server_hash: %(server_hash)s share_id: %(share_id)s size: None stat: %(stat)s """ # the callback, pylint: disable-msg=C0111 def callback(result): if encoding is not None: result.update(dict(path_info=path)) else: result.update(dict(path_info=path)) for k, v in result.iteritems(): self.assertIsInstance(v, unicode) value = expected % result self.assertEqual(out.getvalue(), value) # helper callback, pylint: disable-msg=C0111 def show(result): show_path_info(result, path, out) return result d.addCallback(show) d.addCallback(callback) return d
def inner(argv, *args, **kwargs): m = get_mach(*args, **kwargs) stdout = StringIO() stderr = StringIO() if sys.version_info < (3, 0): stdout.encoding = "UTF-8" stderr.encoding = "UTF-8" try: result = m.run(argv, stdout=stdout, stderr=stderr) except SystemExit: result = None return (result, stdout.getvalue(), stderr.getvalue())
def FlagsForFile(filename): mach = mach_module.get_mach() out = StringIO() out.encoding = None mach.run(['compileflags', filename], stdout=out, stderr=out) return { 'flags': shlex.split(out.getvalue()), 'do_cache': True }
def FlagsForFile(filename): mach = mach_module.get_mach() out = StringIO() out.encoding = None mach.run(['compileflags', filename], stdout=out, stderr=out) flag_list = shlex.split(out.getvalue()) final_flags = [checkFlag(x) for x in flag_list if checkFlag(x)] return {'flags': final_flags, 'do_cache': True}
def _run_mach(self, args, provider_file=None, entry_point=None, context_handler=None): m = Mach(os.getcwd()) m.define_category('testing', 'Mach unittest', 'Testing for mach core', 10) m.populate_context_handler = context_handler if provider_file: m.load_commands_from_file(os.path.join(self.provider_dir, provider_file)) if entry_point: m.load_commands_from_entry_point(entry_point) stdout = StringIO() stderr = StringIO() stdout.encoding = 'UTF-8' stderr.encoding = 'UTF-8' try: result = m.run(args, stdout=stdout, stderr=stderr) except SystemExit: result = None return (result, stdout.getvalue(), stderr.getvalue())
def FlagsForFile(filename): mach = mach_module.get_mach() out = StringIO() out.encoding = None mach.run(['compileflags', filename], stdout=out, stderr=out) flag_list = shlex.split(out.getvalue()) final_flags = [checkFlag(x) for x in flag_list if checkFlag(x)] return { 'flags': final_flags, 'do_cache': True }
def FlagsForFile(filename): mach = mach_module.get_mach() out = StringIO() out.encoding = None mach.run(["compileflags", filename], stdout=out, stderr=out) flag_list = shlex.split(out.getvalue()) # This flag is added by Fennec for android build and causes ycmd to fail to parse the file. # Removing this flag is a workaround until ycmd starts to handle this flag properly. # https://github.com/Valloric/YouCompleteMe/issues/1490 final_flags = [x for x in flag_list if not x.startswith("-march=armv")] return {"flags": final_flags, "do_cache": True}
def FlagsForFile(filename): mach = mach_module.get_mach() out = StringIO() out.encoding = None mach.run(['compileflags', filename], stdout=out, stderr=out) flag_list = shlex.split(out.getvalue()) # This flag is added by Fennec for android build and causes ycmd to fail to parse the file. # Removing this flag is a workaround until ycmd starts to handle this flag properly. # https://github.com/Valloric/YouCompleteMe/issues/1490 final_flags = [x for x in flag_list if not x.startswith('-march=armv')] return {'flags': final_flags, 'do_cache': True}
def FlagsForFile(filename): mach = mach_module.get_mach() out = StringIO() # Mach calls sys.stdout.fileno(), so we need to fake it when capturing it. # Returning an invalid file descriptor does the trick. out.fileno = lambda: -1 out.encoding = None mach.run(['compileflags', filename], stdout=out, stderr=out) flag_list = shlex.split(out.getvalue()) # This flag is added by Fennec for android build and causes ycmd to fail to parse the file. # Removing this flag is a workaround until ycmd starts to handle this flag properly. # https://github.com/Valloric/YouCompleteMe/issues/1490 final_flags = [x for x in flag_list if not x.startswith('-march=armv')] return { 'flags': final_flags, 'do_cache': True }
def generic_show_dirty_nodes(self, encoding, empty=False): """Test dirty nodes output.""" # create some nodes path1 = os.path.join(self.root_dir, u'ñoño-1'.encode('utf-8')) self.main.fs.create(path1, "") path2 = os.path.join(self.root_dir, u'ñoño-2'.encode('utf-8')) mdid2 = self.main.fs.create(path2, "") path3 = os.path.join(self.root_dir, "path3") self.main.fs.create(path3, "") path4 = os.path.join(self.root_dir, "path4") mdid4 = self.main.fs.create(path4, "") if not empty: # dirty some self.main.fs.set_by_mdid(mdid2, dirty=True) self.main.fs.set_by_mdid(mdid4, dirty=True) dirty_nodes = yield self.tool.get_dirty_nodes() out = StringIO() out.encoding = encoding # sort the list dirty_nodes.sort(key=itemgetter('mdid')) show_dirty_nodes(dirty_nodes, out) node_line_tpl = ( "mdid: %(mdid)s volume_id: %(share_id)s node_id: %(node_id)s " "is_dir: %(is_dir)s path: %(path)s\n") if not empty: expected = " Dirty nodes:\n%s" lines = [] for mdid in sorted([mdid4, mdid2]): mdobj = self.main.fs.get_by_mdid(mdid) d = mdobj.__dict__ if encoding is not None: d['path'] = d['path'].decode(encoding) else: d['path'] = d['path'].decode('utf-8') lines.append(node_line_tpl % d) value = expected % ''.join(lines) else: value = " No dirty nodes.\n" self.assertEqual(out.getvalue(), value)
def test_output_encoding_sys(): # The encoding should come from sys.stdout because it is set # there. if sys.version_info[:2] != (2, 6): raise nose.SkipTest("only needed for python 2.6") data = "\xc3\xa9" u_data = data.decode("utf-8") class MyApp(App): def __init__(self): super(MyApp, self).__init__(description="testing", version="0.1", command_manager=CommandManager("tests")) stdout = StringIO() stdout.encoding = "utf-8" getdefaultlocale = mock.Mock(return_value=("ignored", "utf-16")) with mock.patch("sys.stdout", stdout): with mock.patch("locale.getdefaultlocale", getdefaultlocale): app = MyApp() app.stdout.write(u_data) actual = stdout.getvalue() assert data == actual
def run_cli_native(verb, *args, **kwargs): """ Run a Tahoe-LAFS CLI command specified as bytes (on Python 2) or Unicode (on Python 3); basically, it accepts a native string. Most code should prefer ``run_cli_unicode`` which deals with all the necessary encoding considerations. :param runner.Options options: The options instance to use to parse the given arguments. :param native_str verb: The command to run. For example, ``"create-node"``. :param [native_str] args: The arguments to pass to the command. For example, ``("--hostname=localhost",)``. :param [native_str] nodeargs: Extra arguments to pass to the Tahoe executable before ``verb``. :param bytes|unicode stdin: Text or bytes to pass to the command via stdin. :param NoneType|str encoding: The name of an encoding which stdout and stderr will be configured to use. ``None`` means matching default behavior for the given Python version. :param bool return_bytes: If False, stdout/stderr is native string, matching native behavior. If True, stdout/stderr are returned as bytes. """ options = kwargs.pop("options", runner.Options()) nodeargs = kwargs.pop("nodeargs", []) encoding = kwargs.pop("encoding", None) or getattr(sys.stdout, "encoding") or "utf-8" return_bytes = kwargs.pop("return_bytes", False) verb = maybe_unicode_to_argv(verb) args = [maybe_unicode_to_argv(a) for a in args] nodeargs = [maybe_unicode_to_argv(a) for a in nodeargs] precondition( all( isinstance(arg, argv_type) for arg in [verb] + nodeargs + list(args)), "arguments to run_cli must be {argv_type} -- convert using unicode_to_argv" .format(argv_type=argv_type), verb=verb, args=args, nodeargs=nodeargs, ) argv = ["tahoe"] + nodeargs + [verb] + list(args) stdin = kwargs.get("stdin", "") if PY2: # The original behavior, the Python 2 behavior, is to accept either # bytes or unicode and try to automatically encode or decode as # necessary. This works okay for ASCII and if LANG is set # appropriately. These aren't great constraints so we should move # away from this behavior. # # The encoding attribute doesn't change StringIO behavior on Python 2, # but it's there for realism of the emulation. stdin = StringIO(stdin) stdin.encoding = encoding stdout = StringIO() stdout.encoding = encoding stderr = StringIO() stderr.encoding = encoding else: # The new behavior, the Python 3 behavior, is to accept unicode and # encode it using a specific encoding. For older versions of Python 3, # the encoding is determined from LANG (bad) but for newer Python 3, # the encoding is either LANG if it supports full Unicode, otherwise # utf-8 (good). Tests can pass in different encodings to exercise # different behaviors. if isinstance(stdin, str): stdin = stdin.encode(encoding) stdin = TextIOWrapper(BytesIO(stdin), encoding) stdout = TextIOWrapper(BytesIO(), encoding) stderr = TextIOWrapper(BytesIO(), encoding) d = defer.succeed(argv) d.addCallback( partial( runner.parse_or_exit, options, ), stdout=stdout, stderr=stderr, ) d.addCallback( runner.dispatch, stdin=stdin, stdout=stdout, stderr=stderr, ) def _done(rc, stdout=stdout, stderr=stderr): if return_bytes and PY3: stdout = stdout.buffer stderr = stderr.buffer return 0, _getvalue(stdout), _getvalue(stderr) def _err(f, stdout=stdout, stderr=stderr): f.trap(SystemExit) if return_bytes and PY3: stdout = stdout.buffer stderr = stderr.buffer return f.value.code, _getvalue(stdout), _getvalue(stderr) d.addCallbacks(_done, _err) return d