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
6fd62fdf
Commit
6fd62fdf
authored
Aug 01, 2015
by
Boxiang Sun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add four slice builtin functions: indices, __hash__, compare, __reduce__
parent
017fb54b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
0 deletions
+46
-0
src/runtime/types.cpp
src/runtime/types.cpp
+46
-0
No files found.
src/runtime/types.cpp
View file @
6fd62fdf
...
...
@@ -1719,6 +1719,48 @@ Box* sliceRepr(BoxedSlice* self) {
return
boxStringTwine
(
llvm
::
Twine
(
"slice("
)
+
start
->
s
()
+
", "
+
stop
->
s
()
+
", "
+
step
->
s
()
+
")"
);
}
Box
*
sliceHash
(
BoxedSlice
*
self
)
{
raiseExcHelper
(
TypeError
,
"unhashable type"
);
return
boxLong
(
-
1l
);
}
Box
*
sliceReduce
(
BoxedSlice
*
self
)
{
return
Py_BuildValue
(
"O(OOO)"
,
self
->
cls
,
self
->
start
,
self
->
stop
,
self
->
step
);
}
Box
*
sliceIndices
(
BoxedSlice
*
self
,
Box
*
len
)
{
Py_ssize_t
ilen
,
start
,
stop
,
step
,
slicelength
;
ilen
=
PyNumber_AsSsize_t
(
len
,
PyExc_OverflowError
);
if
(
ilen
==
-
1
&&
PyErr_Occurred
())
{
throwCAPIException
();
}
if
(
PySlice_GetIndicesEx
((
PySliceObject
*
)
self
,
ilen
,
&
start
,
&
stop
,
&
step
,
&
slicelength
)
<
0
)
{
throwCAPIException
();
}
return
BoxedTuple
::
create
({
boxInt
(
start
),
boxInt
(
stop
),
boxInt
(
step
)
});
}
static
int
slice_compare
(
PySliceObject
*
v
,
PySliceObject
*
w
)
noexcept
{
int
result
=
0
;
if
(
v
==
w
)
return
0
;
if
(
PyObject_Cmp
(
v
->
start
,
w
->
start
,
&
result
)
<
0
)
return
-
2
;
if
(
result
!=
0
)
return
result
;
if
(
PyObject_Cmp
(
v
->
stop
,
w
->
stop
,
&
result
)
<
0
)
return
-
2
;
if
(
result
!=
0
)
return
result
;
if
(
PyObject_Cmp
(
v
->
step
,
w
->
step
,
&
result
)
<
0
)
return
-
2
;
return
result
;
}
extern
"C"
int
PySlice_GetIndices
(
PySliceObject
*
r
,
Py_ssize_t
length
,
Py_ssize_t
*
start
,
Py_ssize_t
*
stop
,
Py_ssize_t
*
step
)
noexcept
{
/* XXX support long ints */
...
...
@@ -3541,10 +3583,14 @@ void setupRuntime() {
slice_cls
->
giveAttr
(
"__new__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
sliceNew
,
UNKNOWN
,
4
,
2
,
false
,
false
),
{
NULL
,
None
}));
slice_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
sliceRepr
,
STR
,
1
)));
slice_cls
->
giveAttr
(
"__hash__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
sliceHash
,
UNKNOWN
,
1
)));
slice_cls
->
giveAttr
(
"indices"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
sliceIndices
,
BOXED_TUPLE
,
2
)));
slice_cls
->
giveAttr
(
"__reduce__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
sliceReduce
,
BOXED_TUPLE
,
1
)));
slice_cls
->
giveAttr
(
"start"
,
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedSlice
,
start
)));
slice_cls
->
giveAttr
(
"stop"
,
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedSlice
,
stop
)));
slice_cls
->
giveAttr
(
"step"
,
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedSlice
,
step
)));
slice_cls
->
freeze
();
slice_cls
->
tp_compare
=
(
cmpfunc
)
slice_compare
;
attrwrapper_cls
->
giveAttr
(
"__setitem__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
AttrWrapper
::
setitem
,
UNKNOWN
,
3
)));
attrwrapper_cls
->
giveAttr
(
...
...
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