forked from PythonCharmers/python-future
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.py
More file actions
98 lines (73 loc) · 3.03 KB
/
Copy pathmisc.py
File metadata and controls
98 lines (73 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
A module that brings in equivalents of the new and modified Python 3
builtins into Py2. Has no effect on Py3.
The builtin functions are:
- ``ascii`` (from Py2's future_builtins module)
- ``hex`` (from Py2's future_builtins module)
- ``oct`` (from Py2's future_builtins module)
- ``chr`` (equivalent to ``unichr`` on Py2)
- ``input`` (equivalent to ``raw_input`` on Py2)
- ``open`` (equivalent to io.open on Py2)
This function is also defined specially:
- ``isinstance``
to be compatible with the backported ``int``, ``str``, and ``bytes`` types of
``future.builtins``.
Examples::
# Compatible output from isinstance() across Py2/3:
assert isinstance(2**63, int) # long integers
assert isinstance(u'blah', str)
assert isinstance('blah', str) # with unicode_literals in effect
input()
-------
Like the new ``input()`` function from Python 3 (without eval()), except
that it returns bytes. Equivalent to Python 2's ``raw_input()``.
Warning: By default, importing this module *removes* the old Python 2
input() function entirely from ``__builtin__`` for safety. This is
because forgetting to import the new ``input`` from ``future`` might
otherwise lead to a security vulnerability (shell injection) on Python 2.
To restore it, you can retrieve it yourself from
``__builtin__._old_input``.
Fortunately, ``input()`` seems to be seldom used in the wild in Python
2...
"""
from future.builtins.backports.newint import newint
from future import utils
if utils.PY2:
from io import open
from future_builtins import ascii, oct, hex
from __builtin__ import unichr as chr
import __builtin__
# Only for backward compatibility with future v0.8.2
isinstance = __builtin__.isinstance
# The following seems like a good idea, but it may be a bit
# paranoid and the implementation may be fragile:
# Python 2's input() is unsafe and MUST not be able to be used
# accidentally by someone who expects Python 3 semantics but forgets
# to import it on Python 2. So we delete it from __builtin__. We
# keep a copy though:
__builtin__._oldinput = __builtin__.input
delattr(__builtin__, 'input')
input = raw_input
# In case some code wants to import 'callable' portably from Py3.0/3.1:
callable = __builtin__.callable
__all__ = ['ascii', 'chr', 'hex', 'input', 'isinstance', 'oct', 'open']
else:
import builtins
ascii = builtins.ascii
chr = builtins.chr
hex = builtins.hex
input = builtins.input
# Only for backward compatibility with future v0.8.2:
isinstance = builtins.isinstance
oct = builtins.oct
open = builtins.open
__all__ = []
# From Pandas, for Python versions 3.0 and 3.1 only. The callable()
# function was removed from Py3.0 and 3.1 and reintroduced into Py3.2.
try:
# callable reintroduced in later versions of Python
callable = builtins.callable
except AttributeError:
def callable(obj):
return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
__all__.append('callable')