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
67157369
Commit
67157369
authored
Aug 14, 2016
by
Ivan Smirnov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add handle::repr() method
parent
3c3533b4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
25 additions
and
0 deletions
+25
-0
docs/changelog.rst
+1
-0
example/example-python-types.cpp
+6
-0
example/example-python-types.py
+6
-0
example/example-python-types.ref
+2
-0
include/pybind11/pytypes.h
+10
-0
No files found.
docs/changelog.rst
View file @
67157369
...
...
@@ -27,6 +27,7 @@ Breaking changes queued for v2.0.0 (Not yet released)
* ``make_iterator()`` improvements for better compatibility with various types
(now uses prefix increment operator)
* ``arg()`` now accepts a wider range of argument types for default values
* Added ``repr()`` method to the ``handle`` class.
* Added support for registering structured dtypes via ``PYBIND11_NUMPY_DTYPE()`` macro.
* Added ``PYBIND11_STR_TYPE`` macro which maps to the ``builtins.str`` type.
* Added a simplified ``buffer_info`` constructor for 1-dimensional buffers.
...
...
example/example-python-types.cpp
View file @
67157369
...
...
@@ -155,6 +155,11 @@ public:
return
(
py
::
str
)
py
::
bytes
(
"boo"
,
3
);
}
void
test_print
(
const
py
::
object
&
obj
)
{
std
::
cout
<<
obj
.
str
()
<<
std
::
endl
;
std
::
cout
<<
obj
.
repr
()
<<
std
::
endl
;
}
static
int
value
;
static
const
int
value2
;
};
...
...
@@ -187,6 +192,7 @@ void init_ex_python_types(py::module &m) {
.
def
(
"get_bytes_from_str"
,
&
ExamplePythonTypes
::
get_bytes_from_str
,
"py::bytes from py::str"
)
.
def
(
"get_str_from_string"
,
&
ExamplePythonTypes
::
get_str_from_string
,
"py::str from std::string"
)
.
def
(
"get_str_from_bytes"
,
&
ExamplePythonTypes
::
get_str_from_bytes
,
"py::str from py::bytes"
)
.
def
(
"test_print"
,
&
ExamplePythonTypes
::
test_print
,
"test the print function"
)
.
def_static
(
"new_instance"
,
&
ExamplePythonTypes
::
new_instance
,
"Return an instance"
)
.
def_readwrite_static
(
"value"
,
&
ExamplePythonTypes
::
value
,
"Static value member"
)
.
def_readonly_static
(
"value2"
,
&
ExamplePythonTypes
::
value2
,
"Static value member (readonly)"
)
...
...
example/example-python-types.py
View file @
67157369
...
...
@@ -71,6 +71,12 @@ print(instance.get_bytes_from_str().decode())
print
(
instance
.
get_str_from_string
()
.
encode
()
.
decode
())
print
(
instance
.
get_str_from_bytes
()
.
encode
()
.
decode
())
class
A
(
object
):
__str__
=
lambda
_
:
'this is a str'
__repr__
=
lambda
_
:
'this is a repr'
instance
.
test_print
(
A
())
from
example
import
ConstructorStats
cstats
=
ConstructorStats
.
get
(
ExamplePythonTypes
)
...
...
example/example-python-types.ref
View file @
67157369
...
...
@@ -139,6 +139,8 @@ foo
bar
baz
boo
this is a str
this is a repr
Instances not destroyed: 1
### ExamplePythonTypes @ 0x1045b80 destroyed
Instances not destroyed: 0
include/pybind11/pytypes.h
View file @
67157369
...
...
@@ -38,6 +38,7 @@ public:
inline
detail
::
accessor
attr
(
handle
key
)
const
;
inline
detail
::
accessor
attr
(
const
char
*
key
)
const
;
inline
pybind11
::
str
str
()
const
;
inline
pybind11
::
str
repr
()
const
;
template
<
typename
T
>
T
cast
()
const
;
template
<
return_value_policy
policy
=
return_value_policy
::
automatic_reference
,
typename
...
Args
>
#if __cplusplus > 201103L
...
...
@@ -383,6 +384,15 @@ inline pybind11::str handle::str() const {
return
pybind11
::
str
(
strValue
,
false
);
}
inline
pybind11
::
str
handle
::
repr
()
const
{
PyObject
*
strValue
=
PyObject_Repr
(
m_ptr
);
#if PY_MAJOR_VERSION < 3
PyObject
*
unicode
=
PyUnicode_FromEncodedObject
(
strValue
,
"utf-8"
,
nullptr
);
Py_XDECREF
(
strValue
);
strValue
=
unicode
;
#endif
return
pybind11
::
str
(
strValue
,
false
);
}
class
bytes
:
public
object
{
public
:
PYBIND11_OBJECT_DEFAULT
(
bytes
,
object
,
PYBIND11_BYTES_CHECK
)
...
...
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