You could solve the problem by using nested try .. except
-blocks:
try:
name = __builtins__.name
except NameError:
try:
name = builtins.name
except NameError:
name = __buildins__.name
# if this will fail, the exception will be raised
This is no real code, just an example, but name
will have the proper content, independent from your version. Inside of the blocks you could also import newname as oldname
or copy the values from the new global builtins
to the old __buildin__
:
try:
__builtins__ = builtins
except NameError:
try:
__builtins__ = buildins # just for example
except NameError:
__builtins__ = __buildins__
# if this will fail, the exception will be raised
Now you can use __builtins__
just as in previous python-versions.
Hope it helps!