'succeed iff str is a valid piece of code'

def assertValid(self, str, symbol='single'):

    if is_jython:
        code = compile_command(str, '<input>', symbol)
        self.assertTrue(code)
        if (symbol == 'single'):
            (d, r) = ({}, {})
            saved_stdout = sys.stdout
            sys.stdout = io.StringIO()
            try:
                exec code in d
                exec compile(str, '<input>', 'single') in r
            finally:
                sys.stdout = saved_stdout
        elif (symbol == 'eval'):
            ctx = {'a': 2}
            d = {'value': eval(code, ctx)}
            r = {'value': eval(str, ctx)}
        self.assertEqual(unify_callables(r), unify_callables(d))
    else:
        expected = compile(str, '<input>', symbol, PyCF_DONT_IMPLY_DEDENT)
        self.assertEqual(compile_command(str, '<input>', symbol), expected)
