Handle __builtins__ both as dict and as module

According to http://docs.python.org/2/reference/executionmodel.html:

    By default, when in the __main__ module, __builtins__ is the built-in module
    __builtin__ (note: no 's'); when in any other module, __builtins__ is an alias
    for the dictionary of the __builtin__ module itself.

Therefore, this code needs to account for both cases.