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
7c2b76ca
Commit
7c2b76ca
authored
Aug 03, 2015
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #774 from corona10/recursive
Recursive printing
parents
ff6fe32f
b784a5cf
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
134 additions
and
79 deletions
+134
-79
src/runtime/dict.cpp
src/runtime/dict.cpp
+32
-14
src/runtime/list.cpp
src/runtime/list.cpp
+28
-26
src/runtime/set.cpp
src/runtime/set.cpp
+37
-16
src/runtime/tuple.cpp
src/runtime/tuple.cpp
+31
-23
test/tests/dict.py
test/tests/dict.py
+6
-0
No files found.
src/runtime/dict.cpp
View file @
7c2b76ca
...
...
@@ -29,23 +29,41 @@ namespace pyston {
Box
*
dictRepr
(
BoxedDict
*
self
)
{
std
::
vector
<
char
>
chars
;
chars
.
push_back
(
'{'
);
bool
first
=
true
;
for
(
const
auto
&
p
:
self
->
d
)
{
if
(
!
first
)
{
chars
.
push_back
(
','
);
int
status
=
Py_ReprEnter
((
PyObject
*
)
self
);
if
(
status
!=
0
)
{
if
(
status
<
0
)
throwCAPIException
();
chars
.
push_back
(
'{'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'}'
);
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()));
}
try
{
chars
.
push_back
(
'{'
);
bool
first
=
true
;
for
(
const
auto
&
p
:
self
->
d
)
{
if
(
!
first
)
{
chars
.
push_back
(
','
);
chars
.
push_back
(
' '
);
}
first
=
false
;
BoxedString
*
k
=
static_cast
<
BoxedString
*>
(
repr
(
p
.
first
));
BoxedString
*
v
=
static_cast
<
BoxedString
*>
(
repr
(
p
.
second
));
chars
.
insert
(
chars
.
end
(),
k
->
s
().
begin
(),
k
->
s
().
end
());
chars
.
push_back
(
':'
);
chars
.
push_back
(
' '
);
chars
.
insert
(
chars
.
end
(),
v
->
s
().
begin
(),
v
->
s
().
end
());
}
first
=
false
;
BoxedString
*
k
=
static_cast
<
BoxedString
*>
(
repr
(
p
.
first
));
BoxedString
*
v
=
static_cast
<
BoxedString
*>
(
repr
(
p
.
second
));
chars
.
insert
(
chars
.
end
(),
k
->
s
().
begin
(),
k
->
s
().
end
());
chars
.
push_back
(
':'
);
chars
.
push_back
(
' '
);
chars
.
insert
(
chars
.
end
(),
v
->
s
().
begin
(),
v
->
s
().
end
());
chars
.
push_back
(
'}'
);
}
catch
(
ExcInfo
e
)
{
Py_ReprLeave
((
PyObject
*
)
self
);
throw
e
;
}
chars
.
push_back
(
'}'
);
Py_ReprLeave
((
PyObject
*
)
self
);
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()));
}
...
...
src/runtime/list.cpp
View file @
7c2b76ca
...
...
@@ -17,8 +17,6 @@
#include <algorithm>
#include <cstring>
#include "llvm/Support/raw_ostream.h"
#include "capi/types.h"
#include "core/ast.h"
#include "core/common.h"
...
...
@@ -63,36 +61,40 @@ extern "C" PyObject* PyList_AsTuple(PyObject* v) noexcept {
}
extern
"C"
Box
*
listRepr
(
BoxedList
*
self
)
{
// TODO highly inefficient with all the string copying
std
::
string
O
(
""
);
llvm
::
raw_string_ostream
os
(
O
);
std
::
vector
<
char
>
chars
;
int
status
=
Py_ReprEnter
((
PyObject
*
)
self
);
// Implementing Recursive Print of list in same way from Cpython
int
recursive
=
Py_ReprEnter
((
PyObject
*
)
self
);
if
(
recursive
!=
0
)
{
if
(
recursive
<
0
)
return
boxString
(
os
.
str
());
if
(
status
!=
0
)
{
if
(
status
<
0
)
throwCAPIException
();
os
<<
"[...]"
;
return
boxString
(
os
.
str
());
chars
.
push_back
(
'['
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
']'
);
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()));
}
try
{
chars
.
push_back
(
'['
);
for
(
int
i
=
0
;
i
<
self
->
size
;
i
++
)
{
os
<<
'['
;
for
(
int
i
=
0
;
i
<
self
->
size
;
i
++
)
{
if
(
i
>
0
)
os
<<
", "
;
Box
*
r
=
self
->
elts
->
elts
[
i
]
->
reprICAsString
();
assert
(
r
->
cls
==
str_cls
);
BoxedString
*
s
=
static_cast
<
BoxedString
*>
(
r
);
os
<<
s
->
s
();
if
(
i
>
0
)
{
chars
.
push_back
(
','
);
chars
.
push_back
(
' '
);
}
Box
*
r
=
self
->
elts
->
elts
[
i
]
->
reprICAsString
();
assert
(
r
->
cls
==
str_cls
);
BoxedString
*
s
=
static_cast
<
BoxedString
*>
(
r
);
chars
.
insert
(
chars
.
end
(),
s
->
s
().
begin
(),
s
->
s
().
end
());
}
chars
.
push_back
(
']'
);
}
catch
(
ExcInfo
e
)
{
Py_ReprLeave
((
PyObject
*
)
self
);
throw
e
;
}
os
<<
']'
;
Py_ReprLeave
((
PyObject
*
)
self
);
return
boxString
(
os
.
str
(
));
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()
));
}
extern
"C"
Box
*
listNonzero
(
BoxedList
*
self
)
{
...
...
src/runtime/set.cpp
View file @
7c2b76ca
...
...
@@ -14,8 +14,6 @@
#include "runtime/set.h"
#include <llvm/Support/raw_ostream.h>
#include "gc/collector.h"
#include "runtime/objmodel.h"
...
...
@@ -96,29 +94,52 @@ Box* setNew(Box* _cls, Box* container) {
}
static
Box
*
_setRepr
(
BoxedSet
*
self
,
const
char
*
type_name
)
{
std
::
string
O
(
""
);
llvm
::
raw_string_ostream
os
(
O
);
std
::
vector
<
char
>
chars
;
int
status
=
Py_ReprEnter
((
PyObject
*
)
self
);
if
(
status
!=
0
)
{
if
(
status
<
0
)
return
boxString
(
os
.
str
());
throwCAPIException
();
std
::
string
ty
=
std
::
string
(
type_name
);
chars
.
insert
(
chars
.
end
(),
ty
.
begin
(),
ty
.
end
());
chars
.
push_back
(
'('
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
')'
);
os
<<
type_name
<<
"(...)"
;
return
boxString
(
os
.
str
());
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()));
}
os
<<
type_name
<<
"(["
;
bool
first
=
true
;
for
(
Box
*
elt
:
self
->
s
)
{
if
(
!
first
)
{
os
<<
", "
;
try
{
std
::
string
ty
=
std
::
string
(
type_name
);
chars
.
insert
(
chars
.
end
(),
ty
.
begin
(),
ty
.
end
());
chars
.
push_back
(
'('
);
chars
.
push_back
(
'['
);
bool
first
=
true
;
for
(
Box
*
elt
:
self
->
s
)
{
if
(
!
first
)
{
chars
.
push_back
(
','
);
chars
.
push_back
(
' '
);
}
BoxedString
*
str
=
static_cast
<
BoxedString
*>
(
repr
(
elt
));
chars
.
insert
(
chars
.
end
(),
str
->
s
().
begin
(),
str
->
s
().
end
());
first
=
false
;
}
os
<<
static_cast
<
BoxedString
*>
(
repr
(
elt
))
->
s
();
first
=
false
;
chars
.
push_back
(
']'
);
chars
.
push_back
(
')'
);
}
catch
(
ExcInfo
e
)
{
Py_ReprLeave
((
PyObject
*
)
self
);
throw
e
;
}
os
<<
"])"
;
Py_ReprLeave
((
PyObject
*
)
self
);
return
boxString
(
os
.
str
(
));
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()
));
}
Box
*
setRepr
(
BoxedSet
*
self
)
{
...
...
src/runtime/tuple.cpp
View file @
7c2b76ca
...
...
@@ -16,8 +16,6 @@
#include <algorithm>
#include "llvm/Support/raw_ostream.h"
#include "capi/typeobject.h"
#include "core/ast.h"
#include "core/common.h"
...
...
@@ -210,42 +208,52 @@ extern "C" Py_ssize_t PyTuple_Size(PyObject* op) noexcept {
}
Box
*
tupleRepr
(
BoxedTuple
*
t
)
{
assert
(
isSubclass
(
t
->
cls
,
tuple_cls
));
assert
(
isSubclass
(
t
->
cls
,
tuple_cls
));
int
n
;
std
::
string
O
(
""
);
llvm
::
raw_string_ostream
os
(
O
);
std
::
vector
<
char
>
chars
;
int
status
=
Py_ReprEnter
((
PyObject
*
)
t
);
n
=
t
->
size
();
if
(
n
==
0
)
{
os
<<
"()"
;
return
boxString
(
os
.
str
());
chars
.
push_back
(
'('
);
chars
.
push_back
(
')'
);
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()));
}
int
status
=
Py_ReprEnter
((
PyObject
*
)
t
);
if
(
status
!=
0
)
{
if
(
status
<
0
)
return
boxString
(
os
.
str
());
throwCAPIException
();
chars
.
push_back
(
'('
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
'.'
);
chars
.
push_back
(
')'
);
os
<<
"(...)"
;
return
boxString
(
os
.
str
());
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()));
}
os
<<
"("
;
try
{
chars
.
push_back
(
'('
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
i
)
{
chars
.
push_back
(
','
);
chars
.
push_back
(
' '
);
}
BoxedString
*
elt_repr
=
static_cast
<
BoxedString
*>
(
repr
(
t
->
elts
[
i
]));
chars
.
insert
(
chars
.
end
(),
elt_repr
->
s
().
begin
(),
elt_repr
->
s
().
end
());
}
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
i
)
os
<<
", "
;
if
(
n
==
1
)
chars
.
push_back
(
','
);
BoxedString
*
elt_repr
=
static_cast
<
BoxedString
*>
(
repr
(
t
->
elts
[
i
]));
os
<<
elt_repr
->
s
();
chars
.
push_back
(
')'
);
}
catch
(
ExcInfo
e
)
{
Py_ReprLeave
((
PyObject
*
)
t
);
throw
e
;
}
if
(
n
==
1
)
os
<<
","
;
os
<<
")"
;
Py_ReprLeave
((
PyObject
*
)
t
);
return
boxString
(
os
.
str
());
return
boxString
(
llvm
::
StringRef
(
&
chars
[
0
],
chars
.
size
()));
}
Box
*
tupleNonzero
(
BoxedTuple
*
self
)
{
...
...
test/tests/dict.py
View file @
7c2b76ca
...
...
@@ -230,3 +230,9 @@ while True:
except
StopIteration
:
break
print
sorted
(
l
)
#recursive printing test
d
=
dict
()
d
[
'one'
]
=
'1'
d
[
'two'
]
=
d
print
d
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