def hook_wrapper(prompt):
    u'''Wrap a Python readline so it behaves like GNU readline.'''
    try:
        # call the Python hook
        res = ensure_str(readline_hook(prompt))
        # make sure it returned the right sort of thing
        if res and not isinstance(res, str):
            raise TypeError, u'readline must return a string.'
    except KeyboardInterrupt:
        # GNU readline returns 0 on keyboard interrupt
        return 0
    except EOFError:
        # It returns an empty string on EOF
        res = u''
    except:
        print >>sys.stderr, u'Readline internal error'
        traceback.print_exc()
        res = u'\n'
    # we have to make a copy because the caller expects to free the result
    p = _strdup(res)
    return p