bool(mock) fails

Bug #380024 reported by Dan Wilczak
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mocker
Fix Released
Undecided
Unassigned

Bug Description

The following code fails with "TypeError: __nonzero__ should return bool or int, returned Mock"

import mocker as mk
mocker = mk.Mocker()
thing = mocker.mock()
bool(thing)
mocker.result(True)
mocker.replay()
bool(thing)
mocker.restore()
mocker.verify()

The problem is that Mock.__nonzero__ is returning a mock. The version below fixes that, by
returning True, but then you can't use expect because it needs the mock.

    def __nonzero__(self):
        try:
            result = self.__mocker_act__("nonzero")
            if type(result) is Mock:
                return True
            return result
        except MatchError, e:
            return True

Issues 196388 and 179072 seem closely related to this.

Revision history for this message
Daniel Arbuckle (djarb) wrote :

I'm also seeing this, as a result of using the 'in' operator on a mock in Python 2.6.

Failed example:
    'hi' in container # doctest: +ELLIPSIS
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib64/python2.6/doctest.py", line 1231, in __run
        compileflags, 1) in test.globs
      File "<doctest mock_container.txt[8]>", line 1, in <module>
        'hi' in container # doctest: +ELLIPSIS
    TypeError: __nonzero__ should return bool or int, returned Mock

Revision history for this message
Martin Aspeli (optilude) wrote :

This problem happens to break coverage reporting with zope.testing as well (the --coverage option to zc.recipe.testrunner). Here is the traceback:

$ ./bin/test plone.dexterity --coverage=coverage
Running zope.testing.testrunner.layer.UnitTests tests:
  Set up zope.testing.testrunner.layer.UnitTests in 0.000 seconds.
  Running:
    35/138 (25.4%)Traceback (most recent call last):
  File "/Users/optilude/Development/Plone/Code/Build/dexterity/bin/test", line 224, in ?
    zope.testing.testrunner.run((['--exit-with-status', '--auto-color', '--auto-progress']) + [
  File "/Users/optilude/.buildout/eggs/zope.testing-3.8.0-py2.4.egg/zope/testing/testrunner/__init__.py", line 32, in run
    failed = run_internal(defaults, args)
  File "/Users/optilude/.buildout/eggs/zope.testing-3.8.0-py2.4.egg/zope/testing/testrunner/__init__.py", line 45, in run_internal
    runner.run()
  File "/Users/optilude/.buildout/eggs/zope.testing-3.8.0-py2.4.egg/zope/testing/testrunner/runner.py", line 137, in run
    self.run_tests()
  File "/Users/optilude/.buildout/eggs/zope.testing-3.8.0-py2.4.egg/zope/testing/testrunner/runner.py", line 215, in run_tests
    setup_layers, self.failures, self.errors)
  File "/Users/optilude/.buildout/eggs/zope.testing-3.8.0-py2.4.egg/zope/testing/testrunner/runner.py", line 371, in run_layer
    return run_tests(options, tests, layer_name, failures, errors)
  File "/Users/optilude/.buildout/eggs/zope.testing-3.8.0-py2.4.egg/zope/testing/testrunner/runner.py", line 304, in run_tests
    test(result)
  File "/opt/python/parts/opt/lib/python2.4/unittest.py", line 281, in __call__
    return self.run(*args, **kwds)
  File "/Users/optilude/.buildout/eggs/mocker-0.10.1-py2.4.egg/mocker.py", line 136, in run_wrapper
    self.__cleanup()
  File "/Users/optilude/.buildout/eggs/mocker-0.10.1-py2.4.egg/mocker.py", line 152, in __cleanup
    self.mocker.restore()
  File "/Users/optilude/.buildout/eggs/mocker-0.10.1-py2.4.egg/mocker.py", line 449, in restore
    event.restore()
  File "/Users/optilude/.buildout/eggs/mocker-0.10.1-py2.4.egg/mocker.py", line 1584, in restore
    def restore(self):
  File "/opt/python/parts/opt/lib/python2.4/trace.py", line 592, in globaltrace_lt
    ignore_it = self.ignore.names(filename, modulename)
  File "/Users/optilude/.buildout/eggs/zope.testing-3.8.0-py2.4.egg/zope/testing/testrunner/coverage.py", line 98, in names
    filename = self._filenameFormat(filename)
  File "/Users/optilude/.buildout/eggs/zope.testing-3.8.0-py2.4.egg/zope/testing/testrunner/coverage.py", line 111, in _filenameFormat
    return os.path.abspath(filename)
  File "/opt/python/parts/opt/lib/python2.4/posixpath.py", line 402, in abspath
    if not isabs(path):
TypeError: __nonzero__ should return bool or int, returned Mock

Revision history for this message
dobey (dobey) wrote :
Changed in mocker:
status: New → Fix Committed
Changed in mocker:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.