Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
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
Boxiang Sun
Pyston
Commits
cc3a1cc3
Commit
cc3a1cc3
authored
May 09, 2015
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Apparently you can override what import returns
By replacing the entry in sys.modules. pytest does this.
parent
ae7851d6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
25 additions
and
11 deletions
+25
-11
src/core/types.h
src/core/types.h
+1
-1
src/runtime/import.cpp
src/runtime/import.cpp
+12
-4
src/runtime/types.cpp
src/runtime/types.cpp
+4
-6
test/tests/sys_modules_replacement_target.py
test/tests/sys_modules_replacement_target.py
+5
-0
test/tests/sys_modules_replacement_test.py
test/tests/sys_modules_replacement_test.py
+3
-0
No files found.
src/core/types.h
View file @
cc3a1cc3
...
@@ -612,7 +612,7 @@ class BoxedClass;
...
@@ -612,7 +612,7 @@ class BoxedClass;
// TODO these shouldn't be here
// TODO these shouldn't be here
void
setupRuntime
();
void
setupRuntime
();
void
teardownRuntime
();
void
teardownRuntime
();
Box
edModule
*
createAndRunModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
);
Box
*
createAndRunModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
);
BoxedModule
*
createModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
,
const
char
*
doc
=
NULL
);
BoxedModule
*
createModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
,
const
char
*
doc
=
NULL
);
// TODO where to put this
// TODO where to put this
...
...
src/runtime/import.cpp
View file @
cc3a1cc3
...
@@ -39,7 +39,7 @@ static void removeModule(const std::string& name) {
...
@@ -39,7 +39,7 @@ static void removeModule(const std::string& name) {
d
->
d
.
erase
(
b_name
);
d
->
d
.
erase
(
b_name
);
}
}
Box
edModule
*
createAndRunModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
)
{
Box
*
createAndRunModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
)
{
BoxedModule
*
module
=
createModule
(
name
,
fn
);
BoxedModule
*
module
=
createModule
(
name
,
fn
);
AST_Module
*
ast
=
caching_parse_file
(
fn
.
c_str
());
AST_Module
*
ast
=
caching_parse_file
(
fn
.
c_str
());
...
@@ -49,10 +49,14 @@ BoxedModule* createAndRunModule(const std::string& name, const std::string& fn)
...
@@ -49,10 +49,14 @@ BoxedModule* createAndRunModule(const std::string& name, const std::string& fn)
removeModule
(
name
);
removeModule
(
name
);
raiseRaw
(
e
);
raiseRaw
(
e
);
}
}
return
module
;
Box
*
r
=
getSysModulesDict
()
->
getOrNull
(
boxString
(
name
));
if
(
!
r
)
raiseExcHelper
(
ImportError
,
"Loaded module %.200s not found in sys.modules"
,
name
.
c_str
());
return
r
;
}
}
static
Box
edModule
*
createAndRunModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
,
const
std
::
string
&
module_path
)
{
static
Box
*
createAndRunModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
,
const
std
::
string
&
module_path
)
{
BoxedModule
*
module
=
createModule
(
name
,
fn
);
BoxedModule
*
module
=
createModule
(
name
,
fn
);
Box
*
b_path
=
boxStringPtr
(
&
module_path
);
Box
*
b_path
=
boxStringPtr
(
&
module_path
);
...
@@ -69,7 +73,11 @@ static BoxedModule* createAndRunModule(const std::string& name, const std::strin
...
@@ -69,7 +73,11 @@ static BoxedModule* createAndRunModule(const std::string& name, const std::strin
removeModule
(
name
);
removeModule
(
name
);
raiseRaw
(
e
);
raiseRaw
(
e
);
}
}
return
module
;
Box
*
r
=
getSysModulesDict
()
->
getOrNull
(
boxString
(
name
));
if
(
!
r
)
raiseExcHelper
(
ImportError
,
"Loaded module %.200s not found in sys.modules"
,
name
.
c_str
());
return
r
;
}
}
#if LLVMREV < 210072
#if LLVMREV < 210072
...
...
src/runtime/types.cpp
View file @
cc3a1cc3
...
@@ -2589,18 +2589,16 @@ BoxedModule* createModule(const std::string& name, const std::string& fn, const
...
@@ -2589,18 +2589,16 @@ BoxedModule* createModule(const std::string& name, const std::string& fn, const
// Surprisingly, there are times that we need to return the existing module if
// Surprisingly, there are times that we need to return the existing module if
// one exists:
// one exists:
Box
*&
ptr
=
d
->
d
[
b_name
];
Box
*
existing
=
d
->
getOrNull
(
b_name
);
if
(
ptr
&&
isSubclass
(
ptr
->
cls
,
module_cls
))
{
if
(
existing
&&
isSubclass
(
existing
->
cls
,
module_cls
))
{
return
static_cast
<
BoxedModule
*>
(
ptr
);
return
static_cast
<
BoxedModule
*>
(
existing
);
}
else
{
ptr
=
NULL
;
}
}
BoxedModule
*
module
=
new
BoxedModule
();
BoxedModule
*
module
=
new
BoxedModule
();
moduleInit
(
module
,
boxString
(
name
),
boxString
(
doc
?
doc
:
""
));
moduleInit
(
module
,
boxString
(
name
),
boxString
(
doc
?
doc
:
""
));
module
->
giveAttr
(
"__file__"
,
boxString
(
fn
));
module
->
giveAttr
(
"__file__"
,
boxString
(
fn
));
ptr
=
module
;
d
->
d
[
b_name
]
=
module
;
return
module
;
return
module
;
}
}
...
...
test/tests/sys_modules_replacement_target.py
0 → 100644
View file @
cc3a1cc3
# skip-if: True
import
os
import
sys
sys
.
modules
[
__name__
]
=
os
test/tests/sys_modules_replacement_test.py
0 → 100644
View file @
cc3a1cc3
import
sys_modules_replacement_target
print
hasattr
(
sys_modules_replacement_target
,
"path"
)
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