Try to read and rewrite *fn* and return the code object.
(config, name)
| 108 | |
| 109 | |
| 110 | def _rewrite_test(config, name): |
| 111 | """Try to read and rewrite *fn* and return the code object.""" |
| 112 | state = _get_state(config) |
| 113 | |
| 114 | source = importer.get_source(name) |
| 115 | if source is None: |
| 116 | return None |
| 117 | |
| 118 | path = importer.get_filename(name) |
| 119 | |
| 120 | try: |
| 121 | tree = ast.parse(source, filename=path) |
| 122 | except SyntaxError: |
| 123 | # Let this pop up again in the real import. |
| 124 | state.trace("failed to parse: %r" % (path,)) |
| 125 | return None |
| 126 | rewrite.rewrite_asserts(tree, py.path.local(path), config) |
| 127 | try: |
| 128 | co = compile(tree, path, "exec", dont_inherit=True) |
| 129 | except SyntaxError: |
| 130 | # It's possible that this error is from some bug in the |
| 131 | # assertion rewriting, but I don't know of a fast way to tell. |
| 132 | state.trace("failed to compile: %r" % (path,)) |
| 133 | return None |
| 134 | return co |
no test coverage detected