disabled.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. This disables builtin functions (and one exception class) which are
  3. removed from Python 3.3.
  4. This module is designed to be used like this::
  5. from future.builtins.disabled import *
  6. This disables the following obsolete Py2 builtin functions::
  7. apply, cmp, coerce, execfile, file, input, long,
  8. raw_input, reduce, reload, unicode, xrange
  9. We don't hack __builtin__, which is very fragile because it contaminates
  10. imported modules too. Instead, we just create new functions with
  11. the same names as the obsolete builtins from Python 2 which raise
  12. NameError exceptions when called.
  13. Note that both ``input()`` and ``raw_input()`` are among the disabled
  14. functions (in this module). Although ``input()`` exists as a builtin in
  15. Python 3, the Python 2 ``input()`` builtin is unsafe to use because it
  16. can lead to shell injection. Therefore we shadow it by default upon ``from
  17. future.builtins.disabled import *``, in case someone forgets to import our
  18. replacement ``input()`` somehow and expects Python 3 semantics.
  19. See the ``future.builtins.misc`` module for a working version of
  20. ``input`` with Python 3 semantics.
  21. (Note that callable() is not among the functions disabled; this was
  22. reintroduced into Python 3.2.)
  23. This exception class is also disabled:
  24. StandardError
  25. """
  26. from __future__ import division, absolute_import, print_function
  27. from future import utils
  28. OBSOLETE_BUILTINS = ['apply', 'chr', 'cmp', 'coerce', 'execfile', 'file',
  29. 'input', 'long', 'raw_input', 'reduce', 'reload',
  30. 'unicode', 'xrange', 'StandardError']
  31. def disabled_function(name):
  32. '''
  33. Returns a function that cannot be called
  34. '''
  35. def disabled(*args, **kwargs):
  36. '''
  37. A function disabled by the ``future`` module. This function is
  38. no longer a builtin in Python 3.
  39. '''
  40. raise NameError('obsolete Python 2 builtin {0} is disabled'.format(name))
  41. return disabled
  42. if not utils.PY3:
  43. for fname in OBSOLETE_BUILTINS:
  44. locals()[fname] = disabled_function(fname)
  45. __all__ = OBSOLETE_BUILTINS
  46. else:
  47. __all__ = []