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
7f2b5768
Commit
7f2b5768
authored
13 years ago
by
Brett Cannon
Browse files
Options
Browse Files
Download
Plain Diff
Merge
parents
4fd3f0c8
c264e3ee
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
42 deletions
+52
-42
Lib/importlib/__init__.py
Lib/importlib/__init__.py
+11
-32
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+35
-4
Lib/importlib/abc.py
Lib/importlib/abc.py
+2
-2
Lib/importlib/test/source/test_abc_loader.py
Lib/importlib/test/source/test_abc_loader.py
+4
-4
No files found.
Lib/importlib/__init__.py
View file @
7f2b5768
...
...
@@ -26,8 +26,15 @@ import os
import
re
import
tokenize
# To simplify imports in test code
_w_long
=
_bootstrap
.
_w_long
_r_long
=
_bootstrap
.
_r_long
# Bootstrap help #####################################################
# TODO: Expose from import.c, else handle encode/decode as _os.environ returns
# bytes.
def
_case_ok
(
directory
,
check
):
"""Check if the directory contains something matching 'check'.
...
...
@@ -36,37 +43,13 @@ def _case_ok(directory, check):
"""
if
'PYTHONCASEOK'
in
os
.
environ
:
return
True
elif
check
in
os
.
listdir
(
directory
if
directory
else
os
.
getcwd
()):
if
not
directory
:
directory
=
os
.
getcwd
()
if
check
in
os
.
listdir
(
directory
):
return
True
return
False
def
_w_long
(
x
):
"""Convert a 32-bit integer to little-endian.
XXX Temporary until marshal's long functions are exposed.
"""
x
=
int
(
x
)
int_bytes
=
[]
int_bytes
.
append
(
x
&
0xFF
)
int_bytes
.
append
((
x
>>
8
)
&
0xFF
)
int_bytes
.
append
((
x
>>
16
)
&
0xFF
)
int_bytes
.
append
((
x
>>
24
)
&
0xFF
)
return
bytearray
(
int_bytes
)
def
_r_long
(
int_bytes
):
"""Convert 4 bytes in little-endian to an integer.
XXX Temporary until marshal's long function are exposed.
"""
x
=
int_bytes
[
0
]
x
|=
int_bytes
[
1
]
<<
8
x
|=
int_bytes
[
2
]
<<
16
x
|=
int_bytes
[
3
]
<<
24
return
x
_bootstrap
.
_case_ok
=
_case_ok
# Required built-in modules.
...
...
@@ -94,10 +77,6 @@ from os import sep
# For os.path.join replacement; pull from Include/osdefs.h:SEP .
_bootstrap
.
path_sep
=
sep
_bootstrap
.
_case_ok
=
_case_ok
marshal
.
_w_long
=
_w_long
marshal
.
_r_long
=
_r_long
# Public API #########################################################
...
...
This diff is collapsed.
Click to expand it.
Lib/importlib/_bootstrap.py
View file @
7f2b5768
...
...
@@ -18,6 +18,37 @@ work. One should use importlib as the public-facing version of this module.
# Bootstrap-related code ######################################################
# TODO: Expose from marshal
def
_w_long
(
x
):
"""Convert a 32-bit integer to little-endian.
XXX Temporary until marshal's long functions are exposed.
"""
x
=
int
(
x
)
int_bytes
=
[]
int_bytes
.
append
(
x
&
0xFF
)
int_bytes
.
append
((
x
>>
8
)
&
0xFF
)
int_bytes
.
append
((
x
>>
16
)
&
0xFF
)
int_bytes
.
append
((
x
>>
24
)
&
0xFF
)
return
bytearray
(
int_bytes
)
# TODO: Expose from marshal
def
_r_long
(
int_bytes
):
"""Convert 4 bytes in little-endian to an integer.
XXX Temporary until marshal's long function are exposed.
"""
x
=
int_bytes
[
0
]
x
|=
int_bytes
[
1
]
<<
8
x
|=
int_bytes
[
2
]
<<
16
x
|=
int_bytes
[
3
]
<<
24
return
x
# XXX Could also expose Modules/getpath.c:joinpath()
def
_path_join
(
*
args
):
"""Replacement for os.path.join."""
...
...
@@ -353,14 +384,14 @@ class _LoaderBasics:
except
KeyError
:
pass
else
:
if
marshal
.
_r_long
(
raw_timestamp
)
!=
source_mtime
:
if
_r_long
(
raw_timestamp
)
!=
source_mtime
:
raise
ImportError
(
"bytecode is stale for {}"
.
format
(
fullname
))
try
:
source_size
=
source_stats
[
'size'
]
&
0xFFFFFFFF
except
KeyError
:
pass
else
:
if
marshal
.
_r_long
(
raw_size
)
!=
source_size
:
if
_r_long
(
raw_size
)
!=
source_size
:
raise
ImportError
(
"bytecode is stale for {}"
.
format
(
fullname
))
# Can't return the code object as errors from marshal loading need to
# propagate even when source is available.
...
...
@@ -472,8 +503,8 @@ class SourceLoader(_LoaderBasics):
# their own cached file format, this block of code will most likely
# throw an exception.
data
=
bytearray
(
imp
.
get_magic
())
data
.
extend
(
marshal
.
_w_long
(
source_mtime
))
data
.
extend
(
marshal
.
_w_long
(
len
(
source_bytes
)))
data
.
extend
(
_w_long
(
source_mtime
))
data
.
extend
(
_w_long
(
len
(
source_bytes
)))
data
.
extend
(
marshal
.
dumps
(
code_object
))
try
:
self
.
set_data
(
bytecode_path
,
data
)
...
...
This diff is collapsed.
Click to expand it.
Lib/importlib/abc.py
View file @
7f2b5768
...
...
@@ -260,7 +260,7 @@ class PyPycLoader(PyLoader):
raw_timestamp
=
data
[
4
:
8
]
if
len
(
raw_timestamp
)
<
4
:
raise
EOFError
(
"bad timestamp in {}"
.
format
(
fullname
))
pyc_timestamp
=
marshal
.
_r_long
(
raw_timestamp
)
pyc_timestamp
=
_bootstrap
.
_r_long
(
raw_timestamp
)
bytecode
=
data
[
8
:]
# Verify that the magic number is valid.
if
imp
.
get_magic
()
!=
magic
:
...
...
@@ -292,7 +292,7 @@ class PyPycLoader(PyLoader):
# Generate bytecode and write it out.
if
not
sys
.
dont_write_bytecode
:
data
=
bytearray
(
imp
.
get_magic
())
data
.
extend
(
marshal
.
_w_long
(
source_timestamp
))
data
.
extend
(
_bootstrap
.
_w_long
(
source_timestamp
))
data
.
extend
(
marshal
.
dumps
(
code_object
))
self
.
write_bytecode
(
fullname
,
data
)
return
code_object
...
...
This diff is collapsed.
Click to expand it.
Lib/importlib/test/source/test_abc_loader.py
View file @
7f2b5768
...
...
@@ -42,8 +42,8 @@ class SourceLoaderMock(SourceOnlyLoaderMock):
self
.
bytecode_path
=
imp
.
cache_from_source
(
self
.
path
)
self
.
source_size
=
len
(
self
.
source
)
data
=
bytearray
(
magic
)
data
.
extend
(
marshal
.
_w_long
(
self
.
source_mtime
))
data
.
extend
(
marshal
.
_w_long
(
self
.
source_size
))
data
.
extend
(
importlib
.
_w_long
(
self
.
source_mtime
))
data
.
extend
(
importlib
.
_w_long
(
self
.
source_size
))
code_object
=
compile
(
self
.
source
,
self
.
path
,
'exec'
,
dont_inherit
=
True
)
data
.
extend
(
marshal
.
dumps
(
code_object
))
...
...
@@ -658,8 +658,8 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
if
bytecode_written
:
self
.
assertIn
(
self
.
cached
,
self
.
loader
.
written
)
data
=
bytearray
(
imp
.
get_magic
())
data
.
extend
(
marshal
.
_w_long
(
self
.
loader
.
source_mtime
))
data
.
extend
(
marshal
.
_w_long
(
self
.
loader
.
source_size
))
data
.
extend
(
importlib
.
_w_long
(
self
.
loader
.
source_mtime
))
data
.
extend
(
importlib
.
_w_long
(
self
.
loader
.
source_size
))
data
.
extend
(
marshal
.
dumps
(
code_object
))
self
.
assertEqual
(
self
.
loader
.
written
[
self
.
cached
],
bytes
(
data
))
...
...
This diff is collapsed.
Click to expand it.
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