def getRealFactory(factory):
    """Get the real factory.

    Sometimes the original factory is masked by functions. If the function
    keeps track of the original factory, use it.
    """
    while hasattr(factory, 'factory'):
        factory = factory.factory
    if not hasattr(factory, '__name__'):
        return factory.__class__
    return factory
