annotate Lib/macurl2path.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 536f24e16af2
children 5fda64d39540 4cdf21a0dbbc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 10889
diff changeset
1 """Macintosh-specific module for conversion between pathnames and URLs.
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 10889
diff changeset
2
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 10889
diff changeset
3 Do not import directly; use urllib instead."""
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import urllib
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import os
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
16198
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
8 __all__ = ["url2pathname","pathname2url"]
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
9
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 def url2pathname(pathname):
36124
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
11 """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: 31515
diff changeset
12 to a file system path; not recommended for general use."""
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 #
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # XXXX The .. handling should be fixed...
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 tp = urllib.splittype(pathname)[0]
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
17 if tp and tp != 'file':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
18 raise RuntimeError, 'Cannot convert non-local URL to pathname'
10889
a628c3705897 Jack Jansen's patch to support file:///path/file/name URL syntax.
Guido van Rossum <guido@python.org>
parents: 8847
diff changeset
19 # Turn starting /// into /, an empty hostname means current host
a628c3705897 Jack Jansen's patch to support file:///path/file/name URL syntax.
Guido van Rossum <guido@python.org>
parents: 8847
diff changeset
20 if pathname[:3] == '///':
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
21 pathname = pathname[2:]
10889
a628c3705897 Jack Jansen's patch to support file:///path/file/name URL syntax.
Guido van Rossum <guido@python.org>
parents: 8847
diff changeset
22 elif pathname[:2] == '//':
a628c3705897 Jack Jansen's patch to support file:///path/file/name URL syntax.
Guido van Rossum <guido@python.org>
parents: 8847
diff changeset
23 raise RuntimeError, 'Cannot convert non-local URL to pathname'
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
24 components = pathname.split('/')
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # Remove . and embedded ..
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 i = 0
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 while i < len(components):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
28 if components[i] == '.':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
29 del components[i]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
30 elif components[i] == '..' and i > 0 and \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
31 components[i-1] not in ('', '..'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
32 del components[i-1:i+1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
33 i = i-1
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
34 elif components[i] == '' and i > 0 and components[i-1] != '':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
35 del components[i]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
36 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
37 i = i+1
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 if not components[0]:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
39 # Absolute unix path, don't start with colon
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
40 rv = ':'.join(components[1:])
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
42 # relative unix path, start with colon. First replace
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
43 # leading .. by empty strings (giving ::file)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
44 i = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
45 while i < len(components) and components[i] == '..':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
46 components[i] = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
47 i = i + 1
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
48 rv = ':' + ':'.join(components)
8847
086702250153 Quote/unquote slashes in macintosh pathname components (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
49 # and finally unquote slashes and other funny characters
086702250153 Quote/unquote slashes in macintosh pathname components (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
50 return urllib.unquote(rv)
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 def pathname2url(pathname):
36124
536f24e16af2 Bug #649974: make docstrings for url2pathname consistent
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
53 """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: 31515
diff changeset
54 of the 'file' scheme; not recommended for general use."""
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 if '/' in pathname:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
56 raise RuntimeError, "Cannot convert pathname containing slashes"
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
57 components = pathname.split(':')
5556
fa9fada916b4 Handling of pathnames pointing to files on toplevel folder of disk was
Guido van Rossum <guido@python.org>
parents: 4412
diff changeset
58 # Remove empty first and/or last component
fa9fada916b4 Handling of pathnames pointing to files on toplevel folder of disk was
Guido van Rossum <guido@python.org>
parents: 4412
diff changeset
59 if components[0] == '':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
60 del components[0]
5556
fa9fada916b4 Handling of pathnames pointing to files on toplevel folder of disk was
Guido van Rossum <guido@python.org>
parents: 4412
diff changeset
61 if components[-1] == '':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
62 del components[-1]
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # Replace empty string ('::') by .. (will result in '/../' later)
5556
fa9fada916b4 Handling of pathnames pointing to files on toplevel folder of disk was
Guido van Rossum <guido@python.org>
parents: 4412
diff changeset
64 for i in range(len(components)):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
65 if components[i] == '':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
66 components[i] = '..'
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 # Truncate names longer than 31 bytes
8847
086702250153 Quote/unquote slashes in macintosh pathname components (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
68 components = map(_pncomp2url, components)
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 if os.path.isabs(pathname):
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
71 return '/' + '/'.join(components)
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 else:
16501
b11780b068fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16198
diff changeset
73 return '/'.join(components)
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
74
8847
086702250153 Quote/unquote slashes in macintosh pathname components (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
75 def _pncomp2url(component):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
76 component = urllib.quote(component[:31], safe='') # We want to quote slashes
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
77 return component
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
78
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 def test():
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 for url in ["index.html",
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
81 "bar/index.html",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
82 "/foo/bar/index.html",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
83 "/foo/bar/",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
84 "/"]:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 16501
diff changeset
85 print '%r -> %r' % (url, url2pathname(url))
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 for path in ["drive:",
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
87 "drive:dir:",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
88 "drive:dir:file",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
89 "drive:file",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
90 "file",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
91 ":file",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
92 ":dir:",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5556
diff changeset
93 ":dir:file"]:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 16501
diff changeset
94 print '%r -> %r' % (path, pathname2url(path))
3651
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 if __name__ == '__main__':
0602db1d1dff Conversions between mac paths and URLs
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 test()