Mercurial > cpython-lru_cache-weakref
annotate Lib/nturl2path.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 4556f17356f2 |
| children |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10419
diff
changeset
|
1 """Convert a NT pathname to a file URL and vice versa.""" |
| 3725 | 2 |
| 3 def url2pathname(url): | |
|
36124
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
4 """OS-specific conversion from a relative URL of the 'file' scheme |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
5 to a file system path; not recommended for general use.""" |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
6 # e.g. |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
7 # ///C|/foo/bar/spam.foo |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
8 # becomes |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
9 # C:\foo\bar\spam.foo |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
10 import string, urllib |
|
36039
de0c2fea65c2
Bug #1378455: a problem of urllib using open_local_file
Georg Brandl <georg@python.org>
parents:
18777
diff
changeset
|
11 # Windows itself uses ":" even in URLs. |
|
de0c2fea65c2
Bug #1378455: a problem of urllib using open_local_file
Georg Brandl <georg@python.org>
parents:
18777
diff
changeset
|
12 url = url.replace(':', '|') |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
13 if not '|' in url: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
14 # No drive specifier, just convert slashes |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
15 if url[:4] == '////': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
16 # path is something like ////host/path/on/remote/host |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
17 # convert this to \\host\path\on\remote\host |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
18 # (notice halving of slashes at the start of the path) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
19 url = url[2:] |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
20 components = url.split('/') |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
21 # make sure not to convert quoted slashes :-) |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
22 return urllib.unquote('\\'.join(components)) |
|
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
23 comp = url.split('|') |
|
18777
4d33acbdc508
Use string.ascii_letters instead of string.letters.
Fred Drake <fdrake@acm.org>
parents:
16657
diff
changeset
|
24 if len(comp) != 2 or comp[0][-1] not in string.ascii_letters: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
25 error = 'Bad URL: ' + url |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
26 raise IOError, error |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
27 drive = comp[0][-1].upper() |
|
69346
4556f17356f2
Fix Issue11474 - url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents:
56388
diff
changeset
|
28 path = drive + ':' |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
29 components = comp[1].split('/') |
|
69346
4556f17356f2
Fix Issue11474 - url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents:
56388
diff
changeset
|
30 for comp in components: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
31 if comp: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
32 path = path + '\\' + urllib.unquote(comp) |
|
69346
4556f17356f2
Fix Issue11474 - url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents:
56388
diff
changeset
|
33 # Issue #11474: url like '/C|/' should convert into 'C:\\' |
|
4556f17356f2
Fix Issue11474 - url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents:
56388
diff
changeset
|
34 if path.endswith(':') and url.endswith('/'): |
|
4556f17356f2
Fix Issue11474 - url2pathname() handling of '/C|/' on Windows
Senthil Kumaran <orsenthil@gmail.com>
parents:
56388
diff
changeset
|
35 path += '\\' |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
36 return path |
| 3725 | 37 |
| 38 def pathname2url(p): | |
|
36124
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
39 """OS-specific conversion from a file system path to a relative URL |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
40 of the 'file' scheme; not recommended for general use.""" |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
41 # e.g. |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
42 # C:\foo\bar\spam.foo |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
43 # becomes |
|
536f24e16af2
Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents:
36039
diff
changeset
|
44 # ///C|/foo/bar/spam.foo |
|
18777
4d33acbdc508
Use string.ascii_letters instead of string.letters.
Fred Drake <fdrake@acm.org>
parents:
16657
diff
changeset
|
45 import urllib |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
46 if not ':' in p: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
47 # No drive specifier, just convert slashes and quote the name |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
48 if p[:2] == '\\\\': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
49 # path is something like \\host\path\on\remote\host |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
50 # convert this to ////host/path/on/remote/host |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
51 # (notice doubling of slashes at the start of the path) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
52 p = '\\\\' + p |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
53 components = p.split('\\') |
|
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
54 return urllib.quote('/'.join(components)) |
|
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
55 comp = p.split(':') |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
56 if len(comp) != 2 or len(comp[0]) > 1: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
57 error = 'Bad path: ' + p |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
58 raise IOError, error |
| 3725 | 59 |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
60 drive = urllib.quote(comp[0].upper()) |
|
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16460
diff
changeset
|
61 components = comp[1].split('\\') |
|
56388
278cf5c8f1fa
merge r72343. Fix for issue7043
Senthil Kumaran <orsenthil@gmail.com>
parents:
36124
diff
changeset
|
62 path = '///' + drive + ':' |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
63 for comp in components: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
64 if comp: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
65 path = path + '/' + urllib.quote(comp) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
12797
diff
changeset
|
66 return path |
