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
c67278b5
Commit
c67278b5
authored
Mar 04, 2015
by
Chris Toshok
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for relative imports, and bring our import api closer to cpython.
parent
af25c3d3
Changes
29
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
29 changed files
with
289 additions
and
79 deletions
+289
-79
src/core/cfg.cpp
src/core/cfg.cpp
+7
-3
src/runtime/builtin_modules/builtins.cpp
src/runtime/builtin_modules/builtins.cpp
+1
-4
src/runtime/capi.cpp
src/runtime/capi.cpp
+1
-1
src/runtime/import.cpp
src/runtime/import.cpp
+223
-56
src/runtime/import.h
src/runtime/import.h
+1
-0
test/tests/name_mangling.py
test/tests/name_mangling.py
+1
-3
test/tests/package_import_in_nonpackage.py
test/tests/package_import_in_nonpackage.py
+1
-4
test/tests/package_test.py
test/tests/package_test.py
+2
-5
test/tests/relative_import1.py
test/tests/relative_import1.py
+1
-0
test/tests/relative_import1_pkg/__init__.py
test/tests/relative_import1_pkg/__init__.py
+4
-0
test/tests/relative_import1_pkg/string.py
test/tests/relative_import1_pkg/string.py
+7
-0
test/tests/relative_import2.py
test/tests/relative_import2.py
+1
-0
test/tests/relative_import2_pkg/__init__.py
test/tests/relative_import2_pkg/__init__.py
+1
-0
test/tests/relative_import2_pkg/foo.py
test/tests/relative_import2_pkg/foo.py
+1
-0
test/tests/relative_import3.py
test/tests/relative_import3.py
+1
-0
test/tests/relative_import3_pkg/A/__init__.py
test/tests/relative_import3_pkg/A/__init__.py
+1
-0
test/tests/relative_import3_pkg/A/stuff.py
test/tests/relative_import3_pkg/A/stuff.py
+6
-0
test/tests/relative_import3_pkg/B/__init__.py
test/tests/relative_import3_pkg/B/__init__.py
+1
-0
test/tests/relative_import3_pkg/B/name2.py
test/tests/relative_import3_pkg/B/name2.py
+1
-0
test/tests/relative_import3_pkg/C.py
test/tests/relative_import3_pkg/C.py
+7
-0
test/tests/relative_import3_pkg/C/__init__.py
test/tests/relative_import3_pkg/C/__init__.py
+1
-0
test/tests/relative_import3_pkg/C/name1.py
test/tests/relative_import3_pkg/C/name1.py
+5
-0
test/tests/relative_import3_pkg/C/name3.py
test/tests/relative_import3_pkg/C/name3.py
+1
-0
test/tests/relative_import3_pkg/__init__.py
test/tests/relative_import3_pkg/__init__.py
+3
-0
test/tests/relative_import4.py
test/tests/relative_import4.py
+1
-0
test/tests/relative_import4_pkg/__init__.py
test/tests/relative_import4_pkg/__init__.py
+3
-0
test/tests/test_package/absolute_import.py
test/tests/test_package/absolute_import.py
+2
-1
test/tests/test_package/absolute_import_with_future.py
test/tests/test_package/absolute_import_with_future.py
+2
-1
test/tests/test_package/intrapackage_import.py
test/tests/test_package/intrapackage_import.py
+2
-1
No files found.
src/core/cfg.cpp
View file @
c67278b5
...
...
@@ -1279,7 +1279,13 @@ public:
import
->
args
.
push_back
(
new
AST_Num
());
static_cast
<
AST_Num
*>
(
import
->
args
[
0
])
->
num_type
=
AST_Num
::
INT
;
static_cast
<
AST_Num
*>
(
import
->
args
[
0
])
->
n_int
=
-
1
;
int
level
;
if
(
!
(
future_flags
&
FF_ABSOLUTE_IMPORT
))
level
=
-
1
;
else
level
=
0
;
static_cast
<
AST_Num
*>
(
import
->
args
[
0
])
->
n_int
=
level
;
import
->
args
.
push_back
(
new
AST_LangPrimitive
(
AST_LangPrimitive
::
NONE
));
import
->
args
.
push_back
(
new
AST_Str
(
a
->
name
.
str
()));
...
...
@@ -1316,8 +1322,6 @@ public:
}
bool
visit_importfrom
(
AST_ImportFrom
*
node
)
override
{
RELEASE_ASSERT
(
node
->
level
==
0
,
""
);
AST_LangPrimitive
*
import
=
new
AST_LangPrimitive
(
AST_LangPrimitive
::
IMPORT_NAME
);
import
->
lineno
=
node
->
lineno
;
import
->
col_offset
=
node
->
col_offset
;
...
...
src/runtime/builtin_modules/builtins.cpp
View file @
c67278b5
...
...
@@ -472,9 +472,6 @@ Box* bltinImport(Box* name, Box* globals, Box* locals, Box** args) {
Box
*
fromlist
=
args
[
0
];
Box
*
level
=
args
[
1
];
RELEASE_ASSERT
(
globals
==
None
,
"not implemented"
);
RELEASE_ASSERT
(
locals
==
None
,
"not implemented"
);
if
(
name
->
cls
!=
str_cls
)
{
raiseExcHelper
(
TypeError
,
"__import__() argument 1 must be string, not %s"
,
getTypeName
(
name
));
}
...
...
@@ -483,7 +480,7 @@ Box* bltinImport(Box* name, Box* globals, Box* locals, Box** args) {
raiseExcHelper
(
TypeError
,
"an integer is required"
);
}
return
import
(((
BoxedInt
*
)
level
)
->
n
,
fromlist
,
&
static_cast
<
BoxedString
*>
(
name
)
->
s
);
return
import
ModuleLevel
(
&
static_cast
<
BoxedString
*>
(
name
)
->
s
,
globals
,
fromlist
,
((
BoxedInt
*
)
level
)
->
n
);
}
Box
*
delattrFunc
(
Box
*
obj
,
Box
*
_str
)
{
...
...
src/runtime/capi.cpp
View file @
c67278b5
...
...
@@ -838,7 +838,7 @@ extern "C" PyObject* PyImport_Import(PyObject* module_name) noexcept {
RELEASE_ASSERT
(
module_name
->
cls
==
str_cls
,
""
);
try
{
return
import
(
-
1
,
None
,
&
static_cast
<
BoxedString
*>
(
module_name
)
->
s
);
return
import
ModuleLevel
(
&
static_cast
<
BoxedString
*>
(
module_name
)
->
s
,
None
,
None
,
-
1
);
}
catch
(
ExcInfo
e
)
{
Py_FatalError
(
"unimplemented"
);
}
...
...
src/runtime/import.cpp
View file @
c67278b5
This diff is collapsed.
Click to expand it.
src/runtime/import.h
View file @
c67278b5
...
...
@@ -20,6 +20,7 @@
namespace
pyston
{
extern
"C"
Box
*
import
(
int
level
,
Box
*
from_imports
,
const
std
::
string
*
module_name
);
extern
Box
*
importModuleLevel
(
std
::
string
*
module_name
,
Box
*
globals
,
Box
*
from_imports
,
int
level
);
}
#endif
test/tests/name_mangling.py
View file @
c67278b5
...
...
@@ -82,9 +82,7 @@ class MyClass(object):
# Except if it's a dotted name:
import
__foo.__bar
except
ImportError
,
e
:
# CPython says "no module named __foo.__bar__" but Pyston (and PyPy) say "no module named __foo".
# Canonicalize it slightly:
print
e
.
message
.
split
(
'.'
)[
0
]
print
e
.
message
# names inside classes with mangled names don't get the mangled class name:
class
MyClass
(
object
):
...
...
test/tests/package_import_in_nonpackage.py
View file @
c67278b5
# expected: fail
# - crashes rather than throws an error
try
:
from
.
import
doesnt_exist
except
Import
Error
,
e
:
except
Value
Error
,
e
:
print
e
test/tests/package_test.py
View file @
c67278b5
# expected: fail
# - packages not supported
# - intra-package imports ("from . import foo") not supported
# - "from __future__ import absolute_import" not supported
import
os
import
test_package
print
test_package
print
1
,
test_package
.
__name__
,
os
.
path
.
normpath
(
test_package
.
__file__
)
import
test_package.intrapackage_import
import
test_package.absolute_import
...
...
test/tests/relative_import1.py
0 → 100644
View file @
c67278b5
import
relative_import1_pkg
test/tests/relative_import1_pkg/__init__.py
0 → 100644
View file @
c67278b5
# Import names from pkg.string
from
.string
import
name1
,
name2
# Import pkg.string
#from . import string
test/tests/relative_import1_pkg/string.py
0 → 100644
View file @
c67278b5
print
"string.py"
def
name1
():
pass
def
name2
():
pass
test/tests/relative_import2.py
0 → 100644
View file @
c67278b5
from
relative_import2_pkg
import
foo
test/tests/relative_import2_pkg/__init__.py
0 → 100644
View file @
c67278b5
print
"hello"
test/tests/relative_import2_pkg/foo.py
0 → 100644
View file @
c67278b5
print
"more?"
test/tests/relative_import3.py
0 → 100644
View file @
c67278b5
import
relative_import3_pkg
test/tests/relative_import3_pkg/A/__init__.py
0 → 100644
View file @
c67278b5
print
"A.__init__"
test/tests/relative_import3_pkg/A/stuff.py
0 → 100644
View file @
c67278b5
print
"A.stuff"
all
=
[
'func1'
]
def
func1
():
pass
test/tests/relative_import3_pkg/B/__init__.py
0 → 100644
View file @
c67278b5
print
"B.__init__"
test/tests/relative_import3_pkg/B/name2.py
0 → 100644
View file @
c67278b5
print
"B.name2"
test/tests/relative_import3_pkg/C.py
0 → 100644
View file @
c67278b5
print
"C.py"
all
=
[
'name1'
]
def
name1
():
pass
test/tests/relative_import3_pkg/C/__init__.py
0 → 100644
View file @
c67278b5
print
"C.__init__"
test/tests/relative_import3_pkg/C/name1.py
0 → 100644
View file @
c67278b5
#from . import name3
#from ..B import name2
print
"C.name1.py"
test/tests/relative_import3_pkg/C/name3.py
0 → 100644
View file @
c67278b5
print
"C.name3.py"
test/tests/relative_import3_pkg/__init__.py
0 → 100644
View file @
c67278b5
print
"relative_import3_package.__init__"
from
A.stuff
import
func1
from
C
import
name1
test/tests/relative_import4.py
0 → 100644
View file @
c67278b5
import
relative_import4_pkg
test/tests/relative_import4_pkg/__init__.py
0 → 100644
View file @
c67278b5
import
re
print
type
(
re
.
compile
)
test/tests/test_package/absolute_import.py
View file @
c67278b5
import
os
import
import_target
print
import_target
print
3
,
import_target
.
__name__
,
os
.
path
.
normpath
(
import_target
.
__file__
)
test/tests/test_package/absolute_import_with_future.py
View file @
c67278b5
from
__future__
import
absolute_import
import
os
import
import_target
print
import_target
print
4
,
import_target
.
__name__
,
os
.
path
.
normpath
(
import_target
.
__file__
)
test/tests/test_package/intrapackage_import.py
View file @
c67278b5
import
os
from
.
import
import_target
print
import_target
print
2
,
import_target
.
__name__
,
os
.
path
.
normpath
(
import_target
.
__file__
)
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