annotate Lib/macurl2path.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 06b9a9e75a6c
children
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
48334
5a416a6417d3 Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 43020
diff changeset
5 import urllib.parse
3651
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 #
55657
6ac2a64e826b Fixing Issue6711 - macurl2path has typos that raise AttributeError
Senthil Kumaran <orsenthil@gmail.com>
parents: 48334
diff changeset
16 tp = urllib.parse.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':
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 40775
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] == '//':
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 40775
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
55657
6ac2a64e826b Fixing Issue6711 - macurl2path has typos that raise AttributeError
Senthil Kumaran <orsenthil@gmail.com>
parents: 48334
diff changeset
50 return urllib.parse.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:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 40775
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):
48334
5a416a6417d3 Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 43020
diff changeset
76 # We want to quote slashes
55657
6ac2a64e826b Fixing Issue6711 - macurl2path has typos that raise AttributeError
Senthil Kumaran <orsenthil@gmail.com>
parents: 48334
diff changeset
77 return urllib.parse.quote(component[:31], safe='')