Skip to content

IOError('could not get source code') with @responses.activate decorator on Python 2.7 #195

@medmunds

Description

@medmunds

In Python 2.7, test cases using the @responses.activate decorator die with an IOError in inspect.getsourcelines (added in v2.4.0).

These tests work with earlier releases; the problem still exists in v2.5.1 (latest as of this report).

I'm guessing this is similar to (but not fixed by) #187.

To reproduce

$ python --version
Python 2.7.16
$ pip install unittest-xml-reporting==2.5.1 requests responses mock

tests.py:

import unittest
import xmlrunner
import responses

class DecoratedTests(unittest.TestCase):
    @responses.activate
    def test_responses(self):
        self.assertEqual(1 + 1, 2)

if __name__ == '__main__':
    unittest.main(
        testRunner=xmlrunner.XMLTestRunner(output='test-reports'),
        failfast=False, buffer=False, catchbreak=False)

Results

$ python tests.py
Running tests...
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests.py", line 15, in <module>
    failfast=False, buffer=False, catchbreak=False)
  [... snip -- full stack trace included below]
  File "...site-packages/xmlrunner/result.py", line 273, in startTest
    _, self.lineno = inspect.getsourcelines(test_method)
  [... snip]
  File "...python2.7/inspect.py", line 537, in findsource
    raise IOError('could not get source code')
IOError: could not get source code

Expected

No error. (Try repeating with pip install -U unittest-xml-reporting==2.3.2.)

More info

Full stack trace
Traceback (most recent call last):
  File "tests.py", line 15, in <module>
    failfast=False, buffer=False, catchbreak=False)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 95, in __init__
    self.runTests()
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 232, in runTests
    self.result = testRunner.run(self.test)
  File "/Users/medmunds/.local/share/virtualenvs/xmlrunner-bug-xAnTk8wk/lib/python2.7/site-packages/xmlrunner/runner.py", line 70, in run
    test(result)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/suite.py", line 70, in __call__
    return self.run(*args, **kwds)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/suite.py", line 108, in run
    test(result)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/suite.py", line 70, in __call__
    return self.run(*args, **kwds)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/suite.py", line 108, in run
    test(result)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 393, in __call__
    return self.run(*args, **kwds)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 304, in run
    result.startTest(self)
  File "/Users/medmunds/.local/share/virtualenvs/xmlrunner-bug-xAnTk8wk/lib/python2.7/site-packages/xmlrunner/result.py", line 273, in startTest
    _, self.lineno = inspect.getsourcelines(test_method)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 689, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 537, in findsource
    raise IOError('could not get source code')
IOError: could not get source code
pip list
Package                Version
---------------------- --------
certifi                2019.3.9
chardet                3.0.4
cookies                2.2.1
funcsigs               1.0.2
idna                   2.8
mock                   2.0.0
pbr                    5.1.3
pip                    19.0.3
requests               2.21.0
responses              0.10.6
setuptools             41.0.0
six                    1.12.0
unittest-xml-reporting 2.5.1
urllib3                1.24.1
wheel                  0.33.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions