home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) import __builtin__ import commands import doctest import inspect import logging import os import re import sys import traceback import unittest from inspect import getmodule from StringIO import StringIO from doctest import REPORTING_FLAGS, REPORT_ONLY_FIRST_FAILURE, _unittest_reportflags, DocTestRunner, _extract_future_flags, pdb, _OutputRedirectingPdb, _exception_traceback, linecache import nose.core as nose from nose.plugins import doctests, Plugin from nose.util import anyp, getpackage, test_address, resolve_name, tolist log = logging.getLogger(__name__) def default_argv(): UserConfig = UserConfig import IPython ipcdir = os.path.dirname(UserConfig.__file__) ipconf = os.path.join(ipcdir, 'ipythonrc') return [ '--colors=NoColor', '--noterm_title', '-rcfile=%s' % ipconf] class py_file_finder(object): def __init__(self, test_filename): self.test_filename = test_filename def __call__(self, name): get_py_filename = get_py_filename import IPython.genutils try: return get_py_filename(name) except IOError: test_dir = os.path.dirname(self.test_filename) new_path = os.path.join(test_dir, name) return get_py_filename(new_path) def _run_ns_sync(self, arg_s, runner = None): try: fname = _run_ns_sync.test_filename except AttributeError: fname = arg_s finder = py_file_finder(fname) out = _ip.IP.magic_run_ori(arg_s, runner, finder) if hasattr(_run_ns_sync, 'test_globs'): _run_ns_sync.test_globs.update(_ip.user_ns) return out class ipnsdict(dict): def __init__(self, *a): dict.__init__(self, *a) self._savedict = { } def clear(self): dict.clear(self) self.update(self._savedict) def _checkpoint(self): self._savedict.clear() self._savedict.update(self) def update(self, other): self._checkpoint() dict.update(self, other) self.pop('_', None) self['__builtins__'] = __builtin__ def start_ipython(): if hasattr(start_ipython, 'already_called'): return None start_ipython.already_called = True import new import IPython def xsys(cmd): cmd = _ip.IP.var_expand(cmd, depth = 1) sys.stdout.write(commands.getoutput(cmd)) sys.stdout.flush() _displayhook = sys.displayhook _excepthook = sys.excepthook _main = sys.modules.get('__main__') argv = default_argv() (user_ns, global_ns) = IPython.ipapi.make_user_namespaces(ipnsdict(), dict()) IPython.Shell.IPShell(argv, user_ns, global_ns) sys.modules['__main__'] = _main sys.displayhook = _displayhook sys.excepthook = _excepthook _ip = IPython.ipapi.get() __builtin__._ip = _ip _ip.system = xsys im = new.instancemethod(_run_ns_sync, _ip.IP, _ip.IP.__class__) _ip.IP.magic_run_ori = _ip.IP.magic_run _ip.IP.magic_run = im history = history import IPython history.init_ipython(_ip) if not hasattr(_ip.IP, 'magic_history'): raise RuntimeError("Can't load magics, aborting") hasattr(_ip.IP, 'magic_history') start_ipython() def is_extension_module(filename): return os.path.splitext(filename)[1].lower() in ('.so', '.pyd') class DocTestSkip(object): ds_skip = 'Doctest to skip.\n >>> 1 #doctest: +SKIP\n ' def __init__(self, obj): self.obj = obj def __getattribute__(self, key): if key == '__doc__': return DocTestSkip.ds_skip return getattr(object.__getattribute__(self, 'obj'), key) class DocTestFinder(doctest.DocTestFinder): def _from_module(self, module, object): if module is None: return True if inspect.isfunction(object): return module.__dict__ is object.func_globals if inspect.isbuiltin(object): return module.__name__ == object.__module__ if inspect.isclass(object): return module.__name__ == object.__module__ if inspect.ismethod(object): return module.__name__ == object.im_class.__module__ if inspect.getmodule(object) is not None: return module is inspect.getmodule(object) if hasattr(object, '__module__'): return module.__name__ == object.__module__ if isinstance(object, property): return True raise ValueError('object must be a class or function') def _find(self, tests, obj, name, module, source_lines, globs, seen): if hasattr(obj, 'skip_doctest'): obj = DocTestSkip(obj) doctest.DocTestFinder._find(self, tests, obj, name, module, source_lines, globs, seen) isroutine = isroutine isclass = isclass ismodule = ismodule import inspect if inspect.ismodule(obj) and self._recurse: for valname, val in obj.__dict__.items(): valname1 = '%s.%s' % (name, valname) if (isroutine(val) or isclass(val)) and self._from_module(module, val): self._find(tests, val, valname1, module, source_lines, globs, seen) continue if inspect.isclass(obj) and self._recurse: for valname, val in obj.__dict__.items(): if isinstance(val, staticmethod): val = getattr(obj, valname) if isinstance(val, classmethod): val = getattr(obj, valname).im_func if (inspect.isfunction(val) and inspect.isclass(val) and inspect.ismethod(val) or isinstance(val, property)) and self._from_module(module, val): valname = '%s.%s' % (name, valname) self._find(tests, val, valname, module, source_lines, globs, seen) continue class IPDoctestOutputChecker(doctest.OutputChecker): random_re = re.compile('#\\s*random\\s+') def check_output(self, want, got, optionflags): ret = doctest.OutputChecker.check_output(self, want, got, optionflags) if not ret and self.random_re.search(want): return True return ret class DocTestCase(doctests.DocTestCase): def __init__(self, test, optionflags = 0, setUp = None, tearDown = None, checker = None, obj = None, result_var = '_'): self._result_var = result_var doctests.DocTestCase.__init__(self, test, optionflags = optionflags, setUp = setUp, tearDown = tearDown, checker = checker) self._dt_optionflags = optionflags self._dt_checker = checker self._dt_test = test self._dt_setUp = setUp self._dt_tearDown = tearDown runner = IPDocTestRunner(optionflags = optionflags, checker = checker, verbose = False) self._dt_runner = runner self._ori_dir = os.getcwd() def runTest(self): test = self._dt_test runner = self._dt_runner old = sys.stdout new = StringIO() optionflags = self._dt_optionflags if not optionflags & REPORTING_FLAGS: optionflags |= _unittest_reportflags try: curdir = os.getcwd() os.chdir(self._ori_dir) runner.DIVIDER = '-' * 70 (failures, tries) = runner.run(test, out = new.write, clear_globs = False) finally: sys.stdout = old os.chdir(curdir) if failures: raise self.failureException(self.format_failure(new.getvalue())) failures def setUp(self): if isinstance(self._dt_test.examples[0], IPExample): _ip.IP.user_ns.update(self._dt_test.globs) self._dt_test.globs = _ip.IP.user_ns super(DocTestCase, self).setUp() def tearDown(self): try: super(DocTestCase, self).tearDown() except AttributeError: exc = None if exc.args[0] != self._result_var: raise exc.args[0] != self._result_var class IPExample(doctest.Example): pass class IPExternalExample(doctest.Example): def __init__(self, source, want, exc_msg = None, lineno = 0, indent = 0, options = None): doctest.Example.__init__(self, source, want, exc_msg, lineno, indent, options) self.source += '\n' class IPDocTestParser(doctest.DocTestParser): _PS1_PY = '>>>' _PS2_PY = '\\.\\.\\.' _PS1_IP = 'In\\ \\[\\d+\\]:' _PS2_IP = '\\ \\ \\ \\.\\.\\.+:' _RE_TPL = '\n # Source consists of a PS1 line followed by zero or more PS2 lines.\n (?P<source>\n (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*) # PS1 line\n (?:\\n [ ]* (?P<ps2> %s) .*)*) # PS2 lines\n \\n? # a newline\n # Want consists of any non-blank lines that do not start with PS1.\n (?P<want> (?:(?![ ]*$) # Not a blank line\n (?![ ]*%s) # Not a line starting with PS1\n (?![ ]*%s) # Not a line starting with PS2\n .*$\\n? # But any other line\n )*)\n ' _EXAMPLE_RE_PY = re.compile(_RE_TPL % (_PS1_PY, _PS2_PY, _PS1_PY, _PS2_PY), re.MULTILINE | re.VERBOSE) _EXAMPLE_RE_IP = re.compile(_RE_TPL % (_PS1_IP, _PS2_IP, _PS1_IP, _PS2_IP), re.MULTILINE | re.VERBOSE) _RANDOM_TEST = re.compile('#\\s*all-random\\s+') _EXTERNAL_IP = re.compile('#\\s*ipdoctest:\\s*EXTERNAL') def ip2py(self, source): out = [] newline = out.append for lnum, line in enumerate(source.strip().splitlines()): newline(_ip.IP.prefilter(line, lnum > 0)) newline('') return '\n'.join(out) def parse(self, string, name = '<string>'): string = string.expandtabs() min_indent = self._min_indent(string) output = [] (charno, lineno) = (0, 0) if self._RANDOM_TEST.search(string): random_marker = '\n# random' else: random_marker = '' ip2py = False terms = list(self._EXAMPLE_RE_PY.finditer(string)) if terms: Example = doctest.Example else: terms = list(self._EXAMPLE_RE_IP.finditer(string)) if self._EXTERNAL_IP.search(string): Example = IPExternalExample else: Example = IPExample ip2py = True for m in terms: output.append(string[charno:m.start()]) lineno += string.count('\n', charno, m.start()) (source, options, want, exc_msg) = self._parse_example(m, name, lineno, ip2py) want += random_marker if Example is IPExternalExample: options[doctest.NORMALIZE_WHITESPACE] = True want += '\n' if not self._IS_BLANK_OR_COMMENT(source): output.append(Example(source, want, exc_msg, lineno = lineno, indent = min_indent + len(m.group('indent')), options = options)) lineno += string.count('\n', m.start(), m.end()) charno = m.end() output.append(string[charno:]) return output def _parse_example(self, m, name, lineno, ip2py = False): indent = len(m.group('indent')) source_lines = m.group('source').split('\n') ps1 = m.group('ps1') ps2 = m.group('ps2') ps1_len = len(ps1) self._check_prompt_blank(source_lines, indent, name, lineno, ps1_len) if ps2: self._check_prefix(source_lines[1:], ' ' * indent + ps2, name, lineno) source = []([ sl[indent + ps1_len + 1:] for sl in source_lines ]) want = m.group('want') want_lines = want.split('\n') if len(want_lines) > 1 and re.match(' *$', want_lines[-1]): del want_lines[-1] self._check_prefix(want_lines, ' ' * indent, name, lineno + len(source_lines)) want_lines[0] = re.sub('Out\\[\\d+\\]: \\s*?\\n?', '', want_lines[0]) want = []([ wl[indent:] for wl in want_lines ]) m = self._EXCEPTION_RE.match(want) options = self._find_options(source, name, lineno) return (source, options, want, exc_msg) def _check_prompt_blank(self, lines, indent, name, lineno, ps1_len): space_idx = indent + ps1_len min_len = space_idx + 1 for i, line in enumerate(lines): if len(line) >= min_len and line[space_idx] != ' ': raise ValueError('line %r of the docstring for %s lacks blank after %s: %r' % (lineno + i + 1, name, line[indent:space_idx], line)) line[space_idx] != ' ' SKIP = doctest.register_optionflag('SKIP') class IPDocTestRunner(doctest.DocTestRunner, object): def run(self, test, compileflags = None, out = None, clear_globs = True): _run_ns_sync.test_globs = test.globs _run_ns_sync.test_filename = test.filename return super(IPDocTestRunner, self).run(test, compileflags, out, clear_globs) class DocFileCase(doctest.DocFileCase): def address(self): return (self._dt_test.filename, None, None) class ExtensionDoctest(doctests.Doctest): name = 'extdoctest' enabled = True def __init__(self, exclude_patterns = None): if exclude_patterns is None: exclude_patterns = [] self.exclude_patterns = map(re.compile, exclude_patterns) doctests.Doctest.__init__(self) def options(self, parser, env = os.environ): Plugin.options(self, parser, env) parser.add_option('--doctest-tests', action = 'store_true', dest = 'doctest_tests', default = env.get('NOSE_DOCTEST_TESTS', True), help = 'Also look for doctests in test modules. Note that classes, methods and functions should have either doctests or non-doctest tests, not both. [NOSE_DOCTEST_TESTS]') parser.add_option('--doctest-extension', action = 'append', dest = 'doctestExtension', help = 'Also look for doctests in files with this extension [NOSE_DOCTEST_EXTENSION]') env_setting = env.get('NOSE_DOCTEST_EXTENSION') if env_setting is not None: parser.set_defaults(doctestExtension = tolist(env_setting)) def configure(self, options, config): Plugin.configure(self, options, config) self.doctest_tests = options.doctest_tests self.extension = tolist(options.doctestExtension) self.parser = doctest.DocTestParser() self.finder = DocTestFinder() self.checker = IPDoctestOutputChecker() self.globs = None self.extraglobs = None def loadTestsFromExtensionModule(self, filename): (bpath, mod) = os.path.split(filename) modname = os.path.splitext(mod)[0] try: sys.path.append(bpath) module = __import__(modname) tests = list(self.loadTestsFromModule(module)) finally: sys.path.pop() return tests def loadTestsFromModule(self, module): if not self.matches(module.__name__): log.debug("Doctest doesn't want module %s", module) return None tests = self.finder.find(module, globs = self.globs, extraglobs = self.extraglobs) if not tests: return None optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS tests.sort() module_file = module.__file__ for test in tests: if not test.examples: continue if not test.filename: test.filename = module_file yield DocTestCase(test, optionflags = optionflags, checker = self.checker) def loadTestsFromFile(self, filename): if is_extension_module(filename): for t in self.loadTestsFromExtensionModule(filename): yield t elif self.extension and anyp(filename.endswith, self.extension): name = os.path.basename(filename) dh = open(filename) try: doc = dh.read() finally: dh.close() test = self.parser.get_doctest(doc, globs = { '__file__': filename }, name = name, filename = filename, lineno = 0) if test.examples: yield DocFileCase(test) else: yield False def wantFile(self, filename): for pat in self.exclude_patterns: if pat.search(filename): return False if is_extension_module(filename): return True return doctests.Doctest.wantFile(self, filename) class IPythonDoctest(ExtensionDoctest): name = 'ipdoctest' enabled = True def makeTest(self, obj, parent): optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS doctests = self.finder.find(obj, module = getmodule(parent)) if doctests: for test in doctests: if len(test.examples) == 0: continue yield DocTestCase(test, obj = obj, optionflags = optionflags, checker = self.checker) def options(self, parser, env = os.environ): Plugin.options(self, parser, env) parser.add_option('--ipdoctest-tests', action = 'store_true', dest = 'ipdoctest_tests', default = env.get('NOSE_IPDOCTEST_TESTS', True), help = 'Also look for doctests in test modules. Note that classes, methods and functions should have either doctests or non-doctest tests, not both. [NOSE_IPDOCTEST_TESTS]') parser.add_option('--ipdoctest-extension', action = 'append', dest = 'ipdoctest_extension', help = 'Also look for doctests in files with this extension [NOSE_IPDOCTEST_EXTENSION]') env_setting = env.get('NOSE_IPDOCTEST_EXTENSION') if env_setting is not None: parser.set_defaults(ipdoctest_extension = tolist(env_setting)) def configure(self, options, config): Plugin.configure(self, options, config) self.doctest_tests = options.ipdoctest_tests self.extension = tolist(options.ipdoctest_extension) self.parser = IPDocTestParser() self.finder = DocTestFinder(parser = self.parser) self.checker = IPDoctestOutputChecker() self.globs = None self.extraglobs = None