Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
66aa2728
Commit
66aa2728
authored
Aug 29, 2016
by
Dean Moldovan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add py::str::format() method
parent
67990d9e
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
0 deletions
+27
-0
include/pybind11/pytypes.h
+10
-0
tests/test_python_types.cpp
+8
-0
tests/test_python_types.py
+9
-0
No files found.
include/pybind11/pytypes.h
View file @
66aa2728
...
@@ -385,8 +385,18 @@ public:
...
@@ -385,8 +385,18 @@ public:
pybind11_fail
(
"Unable to extract string contents! (invalid type)"
);
pybind11_fail
(
"Unable to extract string contents! (invalid type)"
);
return
std
::
string
(
buffer
,
(
size_t
)
length
);
return
std
::
string
(
buffer
,
(
size_t
)
length
);
}
}
template
<
typename
...
Args
>
str
format
(
Args
&&
...
args
)
const
{
return
attr
(
"format"
).
cast
<
object
>
()(
std
::
forward
<
Args
>
(
args
)...);
}
};
};
inline
namespace
literals
{
/// String literal version of str
inline
str
operator
""
_s
(
const
char
*
s
,
size_t
size
)
{
return
{
s
,
size
};
}
}
inline
pybind11
::
str
handle
::
str
()
const
{
inline
pybind11
::
str
handle
::
str
()
const
{
PyObject
*
strValue
=
PyObject_Str
(
m_ptr
);
PyObject
*
strValue
=
PyObject_Str
(
m_ptr
);
#if PY_MAJOR_VERSION < 3
#if PY_MAJOR_VERSION < 3
...
...
tests/test_python_types.cpp
View file @
66aa2728
...
@@ -210,5 +210,13 @@ test_initializer python_types([](py::module &m) {
...
@@ -210,5 +210,13 @@ test_initializer python_types([](py::module &m) {
py
::
print
(
"this goes to stderr"
,
"file"
_a
=
py_stderr
);
py
::
print
(
"this goes to stderr"
,
"file"
_a
=
py_stderr
);
py
::
print
(
"flush"
,
"flush"
_a
=
true
);
py
::
print
(
"flush"
,
"flush"
_a
=
true
);
py
::
print
(
"{a} + {b} = {c}"
_s
.
format
(
"a"
_a
=
"py::print"
,
"b"
_a
=
"str.format"
,
"c"
_a
=
"this"
));
});
m
.
def
(
"test_str_format"
,
[]()
{
auto
s1
=
"{} + {} = {}"
_s
.
format
(
1
,
2
,
3
);
auto
s2
=
"{a} + {b} = {c}"
_s
.
format
(
"a"
_a
=
1
,
"b"
_a
=
2
,
"c"
_a
=
3
);
return
py
::
make_tuple
(
s1
,
s2
);
});
});
});
});
tests/test_python_types.py
View file @
66aa2728
...
@@ -231,5 +231,14 @@ def test_print(capture):
...
@@ -231,5 +231,14 @@ def test_print(capture):
*args-and-a-custom-separator
*args-and-a-custom-separator
no new line here -- next print
no new line here -- next print
flush
flush
py::print + str.format = this
"""
"""
assert
capture
.
stderr
==
"this goes to stderr"
assert
capture
.
stderr
==
"this goes to stderr"
def
test_str_api
():
from
pybind11_tests
import
test_str_format
s1
,
s2
=
test_str_format
()
assert
s1
==
"1 + 2 = 3"
assert
s1
==
s2
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