diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2017-03-14 13:40:14 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2017-03-14 13:40:14 -0400 |
commit | 13ed869c6be652f0b7729cebb26583d03c7ab12a (patch) | |
tree | 4081c110d336d83ccb0f2d83c004a249a46e3c3f | |
parent | e626cb5cf730217fbedc3d06b80215c09bbe7013 (diff) | |
download | python-coveragepy-git-13ed869c6be652f0b7729cebb26583d03c7ab12a.tar.gz |
Minimal IronPython support.
IronPython is weird: 2.7.7 has "str is unicode", and unicode.encode produces
unicode! f_lasti is missing, and frame globals are missing.
-rw-r--r-- | CHANGES.rst | 4 | ||||
-rw-r--r-- | coverage/control.py | 6 | ||||
-rw-r--r-- | coverage/env.py | 1 | ||||
-rw-r--r-- | coverage/python.py | 8 | ||||
-rw-r--r-- | coverage/pytracer.py | 2 | ||||
-rw-r--r-- | doc/index.rst | 2 | ||||
-rw-r--r-- | metacov.ini | 4 |
7 files changed, 24 insertions, 3 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 02924aae..6fca636a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -16,6 +16,10 @@ Unreleased also continue measurement. Both `issue 79`_ and `issue 448`_ described this problem, and have been fixed. +- Minimal IronPython support. You should be able to run IronPython programs + under ``coverage run``, though you will still have to do the reporting phase + with CPython. + - Coverage.py has long had a special hack to support CPython's need to measure the coverage of the standard library tests. This code was not installed by kitted versions of coverage.py. Now it is. diff --git a/coverage/control.py b/coverage/control.py index d42cbdcb..4cc76459 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -393,6 +393,10 @@ class Coverage(object): can't be determined, None is returned. """ + if module_globals is None: # pragma: only ironpython + # IronPython doesn't provide globals: https://github.com/IronLanguages/main/issues/1296 + module_globals = {} + dunder_name = module_globals.get('__name__', None) if isinstance(dunder_name, str) and dunder_name != '__main__': @@ -441,7 +445,7 @@ class Coverage(object): # .pyc files can be moved after compilation (for example, by being # installed), we look for __file__ in the frame and prefer it to the # co_filename value. - dunder_file = frame.f_globals.get('__file__') + dunder_file = frame.f_globals and frame.f_globals.get('__file__') if dunder_file: filename = source_for_file(dunder_file) if original_filename and not original_filename.startswith('<'): diff --git a/coverage/env.py b/coverage/env.py index 528c774a..4699a1e5 100644 --- a/coverage/env.py +++ b/coverage/env.py @@ -17,6 +17,7 @@ if PYPY: PYPYVERSION = sys.pypy_version_info JYTHON = (platform.python_implementation() == 'Jython') +IRONPYTHON = (platform.python_implementation() == 'IronPython') # Python versions. PYVERSION = sys.version_info diff --git a/coverage/python.py b/coverage/python.py index f75be60a..9418c386 100644 --- a/coverage/python.py +++ b/coverage/python.py @@ -26,7 +26,13 @@ def read_python_source(filename): """ with open(filename, "rb") as f: - return f.read().replace(b"\r\n", b"\n").replace(b"\r", b"\n") + source = f.read() + + if env.IRONPYTHON: + # IronPython reads Unicode strings even for "rb" files. + source = bytes(source) + + return source.replace(b"\r\n", b"\n").replace(b"\r", b"\n") @contract(returns='unicode') diff --git a/coverage/pytracer.py b/coverage/pytracer.py index 3cf956fd..6dae0148 100644 --- a/coverage/pytracer.py +++ b/coverage/pytracer.py @@ -101,7 +101,7 @@ class PyTracer(object): # function calls and re-entering generators. The f_lasti field is # -1 for calls, and a real offset for generators. Use <0 as the # line number for calls, and the real line number for generators. - if frame.f_lasti < 0: + if getattr(frame, 'f_lasti', -1) < 0: self.last_line = -frame.f_code.co_firstlineno else: self.last_line = frame.f_lineno diff --git a/doc/index.rst b/doc/index.rst index 29f79d75..98bd6a70 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -82,6 +82,8 @@ not. * Jython 2.7.1, though only for running code, not reporting. + * IronPython 2.7.7, though only for running code, not reporting. + **This is a pre-release build. The usual warnings about possible bugs apply.** The latest stable version is coverage.py 4.2, `described here`_. diff --git a/metacov.ini b/metacov.ini index b25af6eb..55d0225e 100644 --- a/metacov.ini +++ b/metacov.ini @@ -39,11 +39,15 @@ exclude_lines = pragma: only jython skip.*Jython + # IronPython isn't included in metacoverage. + pragma: only ironpython + partial_branches = pragma: part covered pragma: if failure if env.TESTING: if .* env.JYTHON + if .* env.IRONPYTHON ignore_errors = true precision = 1 |