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
a4fe6d4e
Commit
a4fe6d4e
authored
Nov 12, 2014
by
Chris Toshok
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
load package/__init__.py if it exists, and set its __path__
parent
4c7b796a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
19 deletions
+75
-19
src/runtime/import.cpp
src/runtime/import.cpp
+75
-19
No files found.
src/runtime/import.cpp
View file @
a4fe6d4e
...
...
@@ -32,12 +32,78 @@ BoxedModule* createAndRunModule(const std::string& name, const std::string& fn)
return
module
;
}
static
BoxedModule
*
createAndRunModule
(
const
std
::
string
&
name
,
const
std
::
string
&
fn
,
const
std
::
string
&
module_path
)
{
BoxedModule
*
module
=
createModule
(
name
,
fn
);
Box
*
b_path
=
boxStringPtr
(
&
module_path
);
BoxedList
*
path_list
=
new
BoxedList
();
listAppendInternal
(
path_list
,
b_path
);
module
->
setattr
(
"__path__"
,
path_list
,
NULL
);
AST_Module
*
ast
=
caching_parse
(
fn
.
c_str
());
compileAndRunModule
(
ast
,
module
);
return
module
;
}
#if LLVMREV < 210072
#define LLVM_SYS_FS_EXISTS_CODE_OKAY(code) ((code) == 0)
#else
#define LLVM_SYS_FS_EXISTS_CODE_OKAY(code) (!(code))
#endif
static
bool
path_exists
(
const
std
::
string
&
path
)
{
#if LLVMREV < 217625
bool
exists
;
llvm_error_code
code
=
llvm
::
sys
::
fs
::
exists
(
path
,
exists
);
assert
(
LLVM_SYS_FS_EXISTS_CODE_OKAY
(
code
));
#else
bool
exists
=
llvm
::
sys
::
fs
::
exists
(
path
);
#endif
return
exists
;
}
static
BoxedModule
*
importPackageFromDirectory
(
const
std
::
string
&
name
,
const
std
::
string
&
path
)
{
llvm
::
SmallString
<
128
>
joined_path
;
llvm
::
sys
::
path
::
append
(
joined_path
,
path
,
name
);
std
::
string
dn
(
joined_path
.
str
());
llvm
::
sys
::
path
::
append
(
joined_path
,
"__init__.py"
);
std
::
string
fn
(
joined_path
.
str
());
if
(
VERBOSITY
()
>=
2
)
printf
(
"Searching for %s at %s...
\n
"
,
name
.
c_str
(),
fn
.
c_str
());
if
(
!
path_exists
(
fn
))
return
NULL
;
if
(
VERBOSITY
()
>=
1
)
printf
(
"Importing %s from %s
\n
"
,
name
.
c_str
(),
fn
.
c_str
());
return
createAndRunModule
(
name
,
fn
,
dn
);
}
static
BoxedModule
*
importFile
(
const
std
::
string
&
name
,
const
std
::
string
&
path
)
{
llvm
::
SmallString
<
128
>
joined_path
;
llvm
::
sys
::
path
::
append
(
joined_path
,
path
,
name
+
".py"
);
std
::
string
fn
(
joined_path
.
str
());
if
(
VERBOSITY
()
>=
2
)
printf
(
"Searching for %s at %s...
\n
"
,
name
.
c_str
(),
fn
.
c_str
());
if
(
!
path_exists
(
fn
))
return
NULL
;
if
(
VERBOSITY
()
>=
1
)
printf
(
"Importing %s from %s
\n
"
,
name
.
c_str
(),
fn
.
c_str
());
return
createAndRunModule
(
name
,
fn
);
}
static
Box
*
importSub
(
const
std
::
string
*
name
,
Box
*
parent_module
)
{
BoxedList
*
path_list
;
if
(
parent_module
==
NULL
)
{
...
...
@@ -60,28 +126,18 @@ static Box* importSub(const std::string* name, Box* parent_module) {
BoxedString
*
p
=
static_cast
<
BoxedString
*>
(
_p
);
joined_path
.
clear
();
llvm
::
sys
::
path
::
append
(
joined_path
,
p
->
s
,
*
name
+
".py"
);
std
::
string
f
n
(
joined_path
.
str
());
llvm
::
sys
::
path
::
append
(
joined_path
,
p
->
s
);
std
::
string
d
n
(
joined_path
.
str
());
if
(
VERBOSITY
()
>=
2
)
printf
(
"Searching for %s at %s...
\n
"
,
name
->
c_str
(),
fn
.
c_str
());
#if LLVMREV < 217625
bool
exists
;
llvm_error_code
code
=
llvm
::
sys
::
fs
::
exists
(
joined_path
.
str
(),
exists
);
assert
(
LLVM_SYS_FS_EXISTS_CODE_OKAY
(
code
));
#else
bool
exists
=
llvm
::
sys
::
fs
::
exists
(
joined_path
.
str
());
#endif
if
(
!
exists
)
continue
;
BoxedModule
*
module
;
if
(
VERBOSITY
()
>=
1
)
printf
(
"Importing %s from %s
\n
"
,
name
->
c_str
(),
fn
.
c_str
());
module
=
importPackageFromDirectory
(
*
name
,
dn
);
if
(
module
)
return
module
;
BoxedModule
*
module
=
createAndRunModule
(
*
name
,
fn
);
return
module
;
module
=
importFile
(
*
name
,
dn
);
if
(
module
)
return
module
;
}
if
(
*
name
==
"basic_test"
)
{
...
...
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