Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
21c3911b
Commit
21c3911b
authored
May 11, 2018
by
sizmailov
Committed by
Wenzel Jakob
Jun 11, 2019
Browse files
add signed overload for `py::slice::compute`
parent
22859bb8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
0 deletions
+39
-0
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+7
-0
tests/test_sequences_and_iterators.cpp
tests/test_sequences_and_iterators.cpp
+19
-0
tests/test_sequences_and_iterators.py
tests/test_sequences_and_iterators.py
+13
-0
No files found.
include/pybind11/pytypes.h
View file @
21c3911b
...
@@ -1133,6 +1133,13 @@ public:
...
@@ -1133,6 +1133,13 @@ public:
(
ssize_t
*
)
stop
,
(
ssize_t
*
)
step
,
(
ssize_t
*
)
stop
,
(
ssize_t
*
)
step
,
(
ssize_t
*
)
slicelength
)
==
0
;
(
ssize_t
*
)
slicelength
)
==
0
;
}
}
bool
compute
(
ssize_t
length
,
ssize_t
*
start
,
ssize_t
*
stop
,
ssize_t
*
step
,
ssize_t
*
slicelength
)
const
{
return
PySlice_GetIndicesEx
((
PYBIND11_SLICE_OBJECT
*
)
m_ptr
,
length
,
start
,
stop
,
step
,
slicelength
)
==
0
;
}
};
};
class
capsule
:
public
object
{
class
capsule
:
public
object
{
...
...
tests/test_sequences_and_iterators.cpp
View file @
21c3911b
...
@@ -71,6 +71,25 @@ py::list test_random_access_iterator(PythonType x) {
...
@@ -71,6 +71,25 @@ py::list test_random_access_iterator(PythonType x) {
}
}
TEST_SUBMODULE
(
sequences_and_iterators
,
m
)
{
TEST_SUBMODULE
(
sequences_and_iterators
,
m
)
{
// test_sliceable
class
Sliceable
{
public:
Sliceable
(
int
n
)
:
size
(
n
)
{}
int
start
,
stop
,
step
;
int
size
;
};
py
::
class_
<
Sliceable
>
(
m
,
"Sliceable"
)
.
def
(
py
::
init
<
int
>
())
.
def
(
"__getitem__"
,[](
const
Sliceable
&
s
,
py
::
slice
slice
)
{
ssize_t
start
,
stop
,
step
,
slicelength
;
if
(
!
slice
.
compute
(
s
.
size
,
&
start
,
&
stop
,
&
step
,
&
slicelength
))
throw
py
::
error_already_set
();
int
istart
=
static_cast
<
int
>
(
start
);
int
istop
=
static_cast
<
int
>
(
stop
);
int
istep
=
static_cast
<
int
>
(
step
);
return
std
::
make_tuple
(
istart
,
istop
,
istep
);
})
;
// test_sequence
// test_sequence
class
Sequence
{
class
Sequence
{
...
...
tests/test_sequences_and_iterators.py
View file @
21c3911b
...
@@ -33,6 +33,19 @@ def test_generalized_iterators():
...
@@ -33,6 +33,19 @@ def test_generalized_iterators():
next
(
it
)
next
(
it
)
def
test_sliceable
():
sliceable
=
m
.
Sliceable
(
100
)
assert
sliceable
[::]
==
(
0
,
100
,
1
)
assert
sliceable
[
10
::]
==
(
10
,
100
,
1
)
assert
sliceable
[:
10
:]
==
(
0
,
10
,
1
)
assert
sliceable
[::
10
]
==
(
0
,
100
,
10
)
assert
sliceable
[
-
10
::]
==
(
90
,
100
,
1
)
assert
sliceable
[:
-
10
:]
==
(
0
,
90
,
1
)
assert
sliceable
[::
-
10
]
==
(
99
,
-
1
,
-
10
)
assert
sliceable
[
50
:
60
:
1
]
==
(
50
,
60
,
1
)
assert
sliceable
[
50
:
60
:
-
1
]
==
(
50
,
60
,
-
1
)
def
test_sequence
():
def
test_sequence
():
cstats
=
ConstructorStats
.
get
(
m
.
Sequence
)
cstats
=
ConstructorStats
.
get
(
m
.
Sequence
)
...
...
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