Seems like I'm on a roll right now .. I have a case which crashes PyQt 4.5
under debug-built Python 2.6 on Ubuntu 9.04 x86-64. The attached script
contains the case, and backtrace.txt gdb's backtrace output.

The case is extracted from a unit test of mine, and I've simply pasted code
from a mock module which it depends on, so there's a lot of cruft in the
file. What matters is the class "Test", and apparently what triggers the
crash is its assignment of the variable "_qbase".

It is possible that my QMock class confuses PyQt in some way, but I guess it
shouldn't crash :)

Arve
import inspect
import re
from PyQt4.QtCore import *

def _getattr_recursive(obj, attr, include_bases):
    try: return getattr(obj, attr)
    except AttributeError:
        if include_bases:
            for cls in obj.__bases__:
                try: return _getattr_recursive(cls, attr, include_bases)
                except AttributeError: continue

def get_members(obj, predicate=None, include_bases=True):
    """ Replacement for inspect.getmembers.
    @param predicate: If specified, a function which takes a class attribute and
    indicates (True/False) whether or not it should be included.
    @return: Dictionary of members.
    """
    mems = {}
    for attr in dir(obj):
        val = _getattr_recursive(obj, attr, include_bases)
        if predicate is None or predicate(val):
            mems[attr] = val
    return mems

class MockCallable:
    """ Intercept/record a call.

    The call is delegated to either the mock's dictionary of mock return
    values that was passed in to the constructor, or a handcrafted method
    of a Mock subclass.
    """
    def __init__(self, name, mock, handcrafted=False):
        """
        @param name: Name of callable.
        @param mock: Parent mock.
        @param handcrafted: ?
        """
        self.name = name
        self.mock = mock
        self.handcrafted = handcrafted
        self.__exc = None

    def __call__(self,  *params, **kwparams):
        self.mock._mockCheckInterfaceCall(self.name, params, kwparams)
        thisCall = self.recordCall(params,kwparams)
        self.checkExpectations(thisCall, params, kwparams)
        return self.makeCall(params, kwparams)

    def setRaises(self, exc, after=None, until=None):
        """ Set an exception that should be raised when called. """
        self.__exc = (exc, after, until)

    def recordCall(self, params, kwparams):
        """
        Record the MockCall in an ordered list of all calls, and an ordered
        list of calls for that method name.
        """
        thisCall = MockCall(self.name, params, kwparams)
        calls = self.mock.mockCalledMethods.setdefault(self.name, [])
        calls.append(thisCall)
        self.mock.mockAllCalledMethods.append(thisCall)
        return thisCall

    def makeCall(self, params, kwparams):
        if self.__exc is not None:
            exc, after, until = self.__exc
            # The call is recorded before it is made
            idx = len(self.mock.mockCalledMethods.get(self.name, [])) - 1
            if (after is None or idx > after) and (until is None or
                idx < until):
                raise exc

        if self.handcrafted and self.name not in self.mock.mockReturnValues:
            allPosParams = (self.mock,) + params
            func = _findFunc(self.mock.__class__, self.name)
            if not func:
                raise NotImplementedError
            return func(*allPosParams, **kwparams)
        else:
            # First see if there is a match for these specific call
            # parameters.
            for args, kwds, returnVal in self.mock.mockReturnValuesArgs.get(
                self.name, []):
                if args == params and kwds == kwparams:
                    break
            else:
                # Go for the generic match
                returnVal = self.mock.mockReturnValues.get(self.name)
            if isinstance(returnVal, ReturnValuesBase):
                returnVal = returnVal.next()
            return returnVal

    def checkExpectations(self, thisCall, params, kwparams):
        if self.name in self.mock.mockExpectations:
            callsMade = len(self.mock.mockCalledMethods[self.name])
            for (expectation, after, until) in self.mock.mockExpectations[
                    self.name]:
                if callsMade > after and (until==0 or callsMade < until):
                    assert expectation(self.mock, thisCall, len(
                            self.mock.mockAllCalledMethods)-1), \
                            'Expectation failed: '+str(thisCall)

class Mock(object):
    """ The Mock class simulates any other class for testing purposes.

    All method calls are stored for later examination.
    @cvar mockInstances: Dictionary of all mock instances, indexed on class to
    discern different mock subclasses.
    @cvar _MockRealClass: For subclasses, indicate the class that is being
    mocked.
    """
    mockInstances = {}
    _MockRealClass = None

    def __init__(self, returnValues=None, properties=None, realClass=None,
            name=None, dontMock=[], attributes=None):
        """ Constructor.

        Methods that are not in the returnValues dictionary will return None.
        You may also supply a class whose interface is being mocked.  All calls
        will be checked to see if they appear in the original interface. Any
        calls to methods not appearing in the real class will raise a
        MockInterfaceError.  Any calls that would fail due to non-matching
        parameter lists will also raise a MockInterfaceError.  Both of these
        help to prevent the Mock class getting out of sync with the class it is
        Mocking.
        @param returnValues: Define return values for mocked methods.
        @param properties: Define return values for mocked properties.
        @param realClass: Specify the mocked class.
        @param name: Optionally specify mock's name.
        @param dontMock: Optionally a specify a set of methods to not mock.
        @param attributes: Define mocked attributes.
        @raise MockInterfaceError: An inconsistency was detected in the
        mock's interface.
        """
        if returnValues is None:
            returnValues = {}
        if properties is None:
            properties = {}
        if attributes is None:
            attributes = {}

        self.mockCalledMethods = {}
        self.mockAllCalledMethods = []
        self.mockReturnValues = returnValues
        self.mockReturnValuesArgs = {}
        self.mockExpectations = {}
        self.__realClassMethods = self.__realClassProperties = {}
        self.__name = name
        self.__methods = {}    # Keep a cache of methods
        self.__dontMock = dontMock
        self.__attributes = {}
        if realClass is None:
            realClass = self._MockRealClass
        self.__realClass = realClass
        if realClass is not None:
            # Verify interface versus mocked class
            if not inspect.isclass(realClass):
                raise TypeError(realClass)
            if issubclass(realClass, (MockCallable, Mock)):
                raise TypeError(realClass)
            # We treat all callable class members as methods
            self.__realClassMethods = get_members(realClass,
                    callable)

            # Verify that mocked methods exist in real class
            for retMethod in self.mockReturnValues.keys():
                if not self.__realClassMethods.has_key(retMethod):
                    raise MockInterfaceError("Return value supplied for method \
'%s' that was not in the original class (%s)" % (retMethod, realClass.__name__))

            # Verify that mocked properties exist in real class

            realprops = self.__realClassProperties = \
                get_members(realClass, inspect.isdatadescriptor)
            for name in properties:
                if  name not in realprops:
                    raise MockInterfaceError("'%s' is not a property of '%s'" %
                            (name, realClass))

            # Now properties
            mockprops = get_members(self.__class__,
                inspect.isdatadescriptor)
            for name, prop in mockprops.items():
                if name.startswith("mock"):
                    continue
                if name not in realprops:
                    raise MockInterfaceError("'%s' is not a property of '%s'" %
                            (name, realClass))

            self.__realClassProperties = properties

        self.__setupSubclassMethodInterceptors()

        # Attributes
        for k, v in attributes.items():
            self.__attributes[k] = v

        # Record this instance among all mock instances
        tp = type(self)
        if not tp in Mock.mockInstances:
            Mock.mockInstances[tp] = []
        Mock.mockInstances[tp].append(self)

    def __str__(self):
        if self.__name is not None:
            return self.__name
        return object.__str__(self)

    def __getattr__(self, name):
        """ Override in order to mock class methods.

        This is called as the last resort, before an AttributeError would
        otherwise be raised.
        """
        try: return self.__dict__["_Mock__realClassProperties"][name]
        except KeyError: pass
        try: return self.__dict__["_Mock__attributes"][name]
        except KeyError: pass
        name_lower = name.lower()
        if name_lower.startswith("mock") or name_lower.startswith("_mock"):
            # Don't mock mock-methods!
            raise AttributeError(name)

        if (self.__realClass is not None and name not in self.__realClassMethods
            or name in self.__dontMock):
            raise AttributeError("%s: %s" % (self.__class__.__name__, name))

        # Keep a cache of methods for this object, so that references to the
        # mock's "methods" don't go out of scope
        return self.__methods.setdefault(name, MockCallable(name, self))

    def __call__(self, *args, **kwds):
        """ Allow calling directly. """
        return self.__getattr__("__call__")(*args, **kwds)

    @property
    def mockAccessedAttrs(self):
        """ All accessed attributes.
        """
        return self.__methods.keys()

    @classmethod
    def mockGetAllInstances(cls):
        """ Get all instances of this mock class.
        """
        return cls.mockInstances.get(cls, [])

    def mockClearCalls(self):
        """ Clear all calls registered so far. """
        self.mockAllCalledMethods = []
        self.mockCalledMethods.clear()

    def mockSetReturnValue(self, name, value):
        """ Set a return value for a method.
        """
        self.mockReturnValues[name] = value

    def mockSetReturnValueWithArgs(self, name, value, args=(), kwds={}):
        """ Set a return value for a method with certain arguments.
        """
        try: retVals = self.mockReturnValuesArgs[name]
        except KeyError: retVals = self.mockReturnValuesArgs[name] = []
        # Eliminiate eventual stale entry
        for i, (a, k, v) in enumerate(retVals[:]):
            if (a, k) == (args, kwds):
                del retVals[i]
        retVals.append((args, kwds, value))

    def mockAddReturnValues(self, **methodReturnValues ):
        self.mockReturnValues.update(methodReturnValues)

    def mockSetExpectation(self, name, testFn, after=0, until=0):
        """ Set an expectation for a method call. """
        self.mockExpectations.setdefault(name, []).append((testFn, after,
                until))

    def mockSetRaises(self, name, exc, until=None, after=None):
        """ Set an exception to be raised by a method.
        @param until: Optionally specify a call index until which the
        exception will be raised.
        @param after: Optionally specify a call index after which the exception
        will be raised.
        """
        mock_callable = self.__getattr__(name)
        assert isinstance(mock_callable, MockCallable), \
                "Expected MockCallable, got %r" % (mock_callable)
        mock_callable.setRaises(exc, until=until, after=after)

    def mockGetCall(self, idx):
        """ Get a certain L{call<MockCall>} that was made. """
        return self.mockAllCalledMethods[idx]

    def mockGetNamedCall(self, name, idx):
        """ Get a L{call<MockCall>} to a certain method that was made. """
        return self.mockGetNamedCalls(name)[idx]

    def mockGetAllCalls(self):
        """ Get all calls.
        @return: List of L{MockCall} objects, representing all the methods in
        the order they were called.
        """
        return self.mockAllCalledMethods

    def mockGetNamedCalls(self, methodName):
        """ Get all calls to a certain method.
        @return: List of L{MockCall} objects, representing all the calls to the
        named method in the order they werecalled.
        """
        return self.mockCalledMethods.get(methodName, [])

    def mockCheckCall(self, tester, index, name, *args, **kwargs):
        """ Test that the index-th call had the specified name and
        parameters.
        """
        try: call = self.mockAllCalledMethods[index]
        except IndexError:
            tester.fail("No call with index %d" % index)
        tester.assertEqual(name, call.name, "Expected call number %d to \
be to %s, but it was to %s instead" % (index, name, call.name,))
        call.checkArgs(tester, *args, **kwargs)

    def mockCheckCalls(self, tester, calls):
        """ Test that a specified sequence of calls were made.
        @param tester: The test case.
        @param calls: A sequence of (name, args, kwargs) tuples.
        """
        numCalls, numExpected = len(self.mockAllCalledMethods), len(calls)
        if numCalls != numExpected:
            if numCalls < numExpected:
                tester.fail("No more than %d calls were made (expected %d)" %
                        (numCalls, numExpected))
            else:
                tester.fail("%d calls were made, expected %d" %
                        (numCalls, numExpected))

        for i, call in enumerate(calls):
            name = call[0]
            try: args = call[1]
            except IndexError: args = ()
            try: kwds = call[2]
            except IndexError: kwds = {}
            self.mockCheckCall(tester, i, name, *args, **kwds)

    def mockCheckNamedCall(self, tester, methodName, index, *args, **kwargs):
        """ Test that the index-th call to a certain method had the specified
        parameters.
        @raise IndexError: No call with this index.
        """
        self.mockCalledMethods.get(methodName, [])
        try: call = self.mockCalledMethods.get(methodName, [])[index]
        except IndexError:
            raise ValueError("No call to %s with index %d" % (methodName, index))
        call.checkArgs(tester, *args, **kwargs)

    def mockCheckNamedCalls(self, tester, methodName, calls):
        """ Test that a specified sequence of calls to a certain method were
        made.
        @param tester: The test case.
        @param methodName: The method's name.
        @param calls: A sequence of (args, kwargs) tuples.
        """
        numCalls, numExpected = len(self.mockCalledMethods.get(methodName,
                [])), len(calls)
        if numCalls != numExpected:
            if numCalls < numExpected:
                tester.fail("No more than %d calls were made (expected %d)" %
                        (numCalls, numExpected))
            else:
                tester.fail("%d calls were made, expected %d" %
                        (numCalls, numExpected))

        for i, call in enumerate(calls):
            try: args = call[0]
            except IndexError: args = ()
            try: kwds = call[1]
            except IndexError: kwds = {}
            self.mockCheckNamedCall(tester, methodName, i, *args, **kwds)

    def __setupSubclassMethodInterceptors(self):
        """ Install MockCallables for subclass methods.
        """
        methods = get_members(self.__class__, inspect.isroutine)
        baseMethods = get_members(Mock, inspect.ismethod)
        # The other half of this pattern should match private methods, based on
        # the naming convention _<class name>__<method name>
        reIgnore = re.compile(r"(?:mock|_[^_]+__).+")
        for name in methods:
            # Filter methods of Mock base class, private methods and methods
            # that start with "mock"
            if (name not in baseMethods and not reIgnore.match(name)
                and name not in self.__dontMock):
                self.__dict__[name] = MockCallable(name, self, handcrafted=True)

    def _mockCheckInterfaceCall(self, name, callParams, callKwParams):
        """ Check that a call to a method of the given name to the original
        class with the given parameters would not fail.

        If it would fail, raise a MockInterfaceError.
        Based on the Python 2.3.3 Reference Manual section 5.3.4: Calls.
        """
        if self.__realClass is None:
            return
        if not self.__realClassMethods.has_key(name):
            raise MockInterfaceError("Calling mock method '%s' that was not \
found in the original class (%s)" % (name, self.__realClass.__name__))

        func = self.__realClassMethods[name]
        try:
            args, varargs, varkw, defaults = inspect.getargspec(func)
        except TypeError:
            # func is not a Python function. It is probably a builtin,
            # such as __repr__ or __coerce__. TODO: Checking?
            # For now assume params are OK.
            return

        # callParams doesn't include self; args does include self.
        numPosCallParams = 1 + len(callParams)

        if numPosCallParams > len(args) and not varargs:
            raise MockInterfaceError("Original %s() takes at most %s arguments \
(%s given)" %
                (name, len(args), numPosCallParams))

        # Get the number of positional arguments that appear in the call,
        # also check for duplicate parameters and unknown parameters
        numPosSeen = _getNumPosSeenAndCheck(numPosCallParams, callKwParams,
            args, varkw)

        lenArgsNoDefaults = len(args) - len(defaults or [])
        if numPosSeen < lenArgsNoDefaults:
            raise MockInterfaceError("Original %s() takes at least %s \
arguments (%s given)" % (name, lenArgsNoDefaults, numPosSeen))

class QMock(QObject, Mock):
    """ Mock class that also inherits from QObject.
    """
    __connections = {}

    def __init__(self, *args, **kwds):
        QObject.__init__(self)
        Mock.__init__(self, *args, **kwds)

    @classmethod
    def mock_clear_connections(cls):
        cls.__connections.clear()

    def mock_is_connected(self, slot, signal):
        return slot in self.__connections.setdefault(self, {}
            ).setdefault(signal, [])

    @classmethod
    def connect(cls, emitter, signal, slot):
        cls.__connections.setdefault(emitter, {}).setdefault(signal, []).append(
            slot)

    def emit(self, signal, *args):
        """ Allow signal emission.
        """
        # XXX: Consider slot signature?
        for slot in self.__connections.setdefault(self, {}).setdefault(
            signal, []):
            slot(*args)
        # QObject.emit(self, signal, *args)

class Test(QMock):
    _qbase = QMock()

    def __init__(self):
        QMock.__init__(self)
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to