|
1 | 1 | """Compatibility wrappers for Py2/Py3.""" |
2 | | - |
3 | | -import sys |
4 | | -import os |
5 | | - |
6 | | -if sys.version_info[0] < 3: |
7 | | - from UserDict import UserDict, IterableUserDict |
8 | | - from urllib import quote |
9 | | - from urllib import quote_plus |
10 | | - from urllib import unquote as urllib_unquote |
11 | | - from urllib import urlopen |
12 | | - from urlparse import urlparse |
13 | | - from collections import MutableMapping |
14 | | - |
15 | | - def unquote(uri): |
16 | | - """Specialized unquote that uses UTF-8 for parsing.""" |
17 | | - uri = uri.encode('ascii') |
18 | | - unquoted = urllib_unquote(uri) |
19 | | - return unquoted.decode('utf-8') |
20 | | - |
21 | | - # Old-style of re-raising an exception is SyntaxError in Python 3, |
22 | | - # so hide behind exec() so the Python 3 parser doesn't see it |
23 | | - exec('''def reraise(exc_type, exc_value, exc_traceback): |
24 | | - """Re-raise an exception given information from sys.exc_info() |
25 | | -
|
26 | | - Note that unlike six.reraise, this does not support replacing the |
27 | | - traceback. All arguments must come from a single sys.exc_info() call. |
28 | | - """ |
29 | | - raise exc_type, exc_value, exc_traceback |
30 | | - ''') |
31 | | - |
32 | | -else: |
33 | | - from collections import UserDict |
34 | | - IterableUserDict = UserDict |
35 | | - from urllib.parse import quote, quote_plus, unquote, urlparse |
36 | | - from urllib.request import urlopen |
37 | | - from collections.abc import MutableMapping |
38 | | - |
39 | | - def reraise(exc_type, exc_value, exc_traceback): |
40 | | - """Re-raise an exception given information from sys.exc_info() |
41 | | -
|
42 | | - Note that unlike six.reraise, this does not support replacing the |
43 | | - traceback. All arguments must come from a single sys.exc_info() call. |
44 | | - """ |
45 | | - # In Python 3, all exception info is contained in one object. |
46 | | - raise exc_value |
47 | | - |
48 | | -try: |
49 | | - from shutil import which |
50 | | -except ImportError: |
51 | | - # shutil.which() from Python 3.6 |
52 | | - # "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, |
53 | | - # 2011, 2012, 2013, 2014, 2015, 2016, 2017 Python Software Foundation; |
54 | | - # All Rights Reserved" |
55 | | - def which(cmd, mode=os.F_OK | os.X_OK, path=None): |
56 | | - """Given a command, mode, and a PATH string, return the path which |
57 | | - conforms to the given mode on the PATH, or None if there is no such |
58 | | - file. |
59 | | -
|
60 | | - `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result |
61 | | - of os.environ.get("PATH"), or can be overridden with a custom search |
62 | | - path. |
63 | | -
|
64 | | - """ |
65 | | - # Check that a given file can be accessed with the correct mode. |
66 | | - # Additionally check that `file` is not a directory, as on Windows |
67 | | - # directories pass the os.access check. |
68 | | - def _access_check(fn, mode): |
69 | | - return (os.path.exists(fn) and os.access(fn, mode) |
70 | | - and not os.path.isdir(fn)) |
71 | | - |
72 | | - # If we're given a path with a directory part, look it up directly rather |
73 | | - # than referring to PATH directories. This includes checking relative to the |
74 | | - # current directory, e.g. ./script |
75 | | - if os.path.dirname(cmd): |
76 | | - if _access_check(cmd, mode): |
77 | | - return cmd |
78 | | - return None |
79 | | - |
80 | | - if path is None: |
81 | | - path = os.environ.get("PATH", os.defpath) |
82 | | - if not path: |
83 | | - return None |
84 | | - path = path.split(os.pathsep) |
85 | | - |
86 | | - if sys.platform == "win32": |
87 | | - # The current directory takes precedence on Windows. |
88 | | - if not os.curdir in path: |
89 | | - path.insert(0, os.curdir) |
90 | | - |
91 | | - # PATHEXT is necessary to check on Windows. |
92 | | - pathext = os.environ.get("PATHEXT", "").split(os.pathsep) |
93 | | - # See if the given file matches any of the expected path extensions. |
94 | | - # This will allow us to short circuit when given "python.exe". |
95 | | - # If it does match, only test that one, otherwise we have to try |
96 | | - # others. |
97 | | - if any(cmd.lower().endswith(ext.lower()) for ext in pathext): |
98 | | - files = [cmd] |
99 | | - else: |
100 | | - files = [cmd + ext for ext in pathext] |
101 | | - else: |
102 | | - # On other platforms you don't have things like PATHEXT to tell you |
103 | | - # what file suffixes are executable, so just pass on cmd as-is. |
104 | | - files = [cmd] |
105 | | - |
106 | | - seen = set() |
107 | | - for dir in path: |
108 | | - normdir = os.path.normcase(dir) |
109 | | - if not normdir in seen: |
110 | | - seen.add(normdir) |
111 | | - for thefile in files: |
112 | | - name = os.path.join(dir, thefile) |
113 | | - if _access_check(name, mode): |
114 | | - return name |
115 | | - return None |
| 2 | +import warnings |
| 3 | + |
| 4 | +warnings.warn( |
| 5 | + DeprecationWarning, |
| 6 | + "The ldap.compat module is deprecated and will be removed in the future" |
| 7 | +) |
| 8 | + |
| 9 | +from collections import UserDict |
| 10 | +IterableUserDict = UserDict |
| 11 | +from urllib.parse import quote, quote_plus, unquote, urlparse |
| 12 | +from urllib.request import urlopen |
| 13 | +from collections.abc import MutableMapping |
| 14 | +from shutil import which |
| 15 | + |
| 16 | +def reraise(exc_type, exc_value, exc_traceback): |
| 17 | + """Re-raise an exception given information from sys.exc_info() |
| 18 | +
|
| 19 | + Note that unlike six.reraise, this does not support replacing the |
| 20 | + traceback. All arguments must come from a single sys.exc_info() call. |
| 21 | + """ |
| 22 | + # In Python 3, all exception info is contained in one object. |
| 23 | + raise exc_value |
0 commit comments