Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
f4e322eb
Commit
f4e322eb
authored
Dec 21, 1995
by
Jack Jansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added pathname2url and url2pathname methods (only correct for unix and
mac, so far)
parent
75a14db4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
7 deletions
+32
-7
Lib/urllib.py
Lib/urllib.py
+32
-7
No files found.
Lib/urllib.py
View file @
f4e322eb
...
@@ -20,22 +20,47 @@ import regex
...
@@ -20,22 +20,47 @@ import regex
import
os
import
os
__version__
=
'1.2'
__version__
=
'1.2'
# XXXX Should I update this number? -- jack
# Helper for non-unix systems
# Helper for non-unix systems
if
os
.
name
==
'mac'
:
if
os
.
name
==
'mac'
:
def
_fixpath
(
pathname
):
def
url2pathname
(
pathname
):
"Convert /-delimited pathname to mac pathname"
#
# XXXX The .. handling should be fixed...
#
tp
=
splittype
(
pathname
)[
0
]
if
tp
and
tp
<>
'file'
:
raise
RuntimeError
,
'Cannot convert non-local URL to pathname'
components
=
string
.
split
(
pathname
,
'/'
)
components
=
string
.
split
(
pathname
,
'/'
)
if
'..'
in
components
or
'.'
in
components
or
''
in
components
[
1
:
-
1
]:
raise
RuntimeError
,
'Cannot convert URL containing ., .. or // to pathname'
if
not
components
[
0
]:
if
not
components
[
0
]:
# Absolute unix path, don't start with colon
# Absolute unix path, don't start with colon
return
string
.
join
(
components
[
1
:],
':'
)
return
string
.
join
(
components
[
1
:],
':'
)
else
:
else
:
# relative unix path, start with colon
# relative unix path, start with colon
return
':'
+
string
.
join
(
components
,
':'
)
return
':'
+
string
.
join
(
components
,
':'
)
def
pathname2url
(
pathname
):
"convert mac pathname to /-delimited pathname"
if
'/'
in
pathname
:
raise
RuntimeError
,
"Cannot convert pathname containing slashes"
components
=
string
.
split
(
pathname
,
':'
)
if
''
in
components
[
1
:
-
1
]:
raise
RuntimeError
,
"Cannot convert pathname containing ::"
# Truncate names longer than 31 bytes
components
=
map
(
lambda
x
:
x
[:
31
],
components
)
if
os
.
path
.
isabs
(
pathname
):
return
'/'
+
string
.
join
(
components
,
'/'
)
else
:
return
string
.
join
(
components
,
'/'
)
else
:
else
:
def
_fixpath
(
pathname
):
def
url2pathname
(
pathname
):
return
pathname
def
pathname2url
(
pathname
):
return
pathname
return
pathname
# This really consists of two pieces:
# This really consists of two pieces:
# (1) a class which handles opening of all sorts of URLs
# (1) a class which handles opening of all sorts of URLs
...
@@ -144,7 +169,7 @@ class URLopener:
...
@@ -144,7 +169,7 @@ class URLopener:
try
:
try
:
fp
=
self
.
open_local_file
(
url1
)
fp
=
self
.
open_local_file
(
url1
)
del
fp
del
fp
return
splithost
(
url1
)[
1
]
,
None
return
url2pathname
(
splithost
(
url1
)[
1
])
,
None
except
IOError
,
msg
:
except
IOError
,
msg
:
pass
pass
fp
=
self
.
open
(
url
)
fp
=
self
.
open
(
url
)
...
@@ -238,12 +263,12 @@ class URLopener:
...
@@ -238,12 +263,12 @@ class URLopener:
# Use local file
# Use local file
def
open_local_file
(
self
,
url
):
def
open_local_file
(
self
,
url
):
host
,
file
=
splithost
(
url
)
host
,
file
=
splithost
(
url
)
if
not
host
:
return
addinfo
(
open
(
_fixpath
(
file
),
'r'
),
noheaders
())
if
not
host
:
return
addinfo
(
open
(
url2pathname
(
file
),
'r'
),
noheaders
())
host
,
port
=
splitport
(
host
)
host
,
port
=
splitport
(
host
)
if
not
port
and
socket
.
gethostbyname
(
host
)
in
(
if
not
port
and
socket
.
gethostbyname
(
host
)
in
(
localhost
(),
thishost
()):
localhost
(),
thishost
()):
file
=
unquote
(
file
)
file
=
unquote
(
file
)
return
addinfo
(
open
(
_fixpath
(
file
),
'r'
),
noheaders
())
return
addinfo
(
open
(
url2pathname
(
file
),
'r'
),
noheaders
())
raise
IOError
,
(
'local file error'
,
'not on local host'
)
raise
IOError
,
(
'local file error'
,
'not on local host'
)
# Use FTP protocol
# Use FTP protocol
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment