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
006d8b66
Commit
006d8b66
authored
Aug 13, 2016
by
Ivan Smirnov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add casting operators between py::str / py::bytes
parent
3768b6ab
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
0 deletions
+44
-0
example/example-python-types.cpp
+20
-0
example/example-python-types.py
+5
-0
example/example-python-types.ref
+5
-0
include/pybind11/pytypes.h
+14
-0
No files found.
example/example-python-types.cpp
View file @
006d8b66
...
@@ -139,6 +139,22 @@ public:
...
@@ -139,6 +139,22 @@ public:
throw
std
::
runtime_error
(
"This exception was intentionally thrown."
);
throw
std
::
runtime_error
(
"This exception was intentionally thrown."
);
}
}
py
::
bytes
get_bytes_from_string
()
{
return
std
::
string
(
"foo"
);
}
py
::
bytes
get_bytes_from_str
()
{
return
py
::
str
(
std
::
string
(
"bar"
));
}
py
::
str
get_str_from_string
()
{
return
std
::
string
(
"baz"
);
}
py
::
str
get_str_from_bytes
()
{
return
py
::
bytes
(
std
::
string
(
"boo"
));
}
static
int
value
;
static
int
value
;
static
const
int
value2
;
static
const
int
value2
;
};
};
...
@@ -167,6 +183,10 @@ void init_ex_python_types(py::module &m) {
...
@@ -167,6 +183,10 @@ void init_ex_python_types(py::module &m) {
.
def
(
"pair_passthrough"
,
&
ExamplePythonTypes
::
pair_passthrough
,
"Return a pair in reversed order"
)
.
def
(
"pair_passthrough"
,
&
ExamplePythonTypes
::
pair_passthrough
,
"Return a pair in reversed order"
)
.
def
(
"tuple_passthrough"
,
&
ExamplePythonTypes
::
tuple_passthrough
,
"Return a triple in reversed order"
)
.
def
(
"tuple_passthrough"
,
&
ExamplePythonTypes
::
tuple_passthrough
,
"Return a triple in reversed order"
)
.
def
(
"throw_exception"
,
&
ExamplePythonTypes
::
throw_exception
,
"Throw an exception"
)
.
def
(
"throw_exception"
,
&
ExamplePythonTypes
::
throw_exception
,
"Throw an exception"
)
.
def
(
"get_bytes_from_string"
,
&
ExamplePythonTypes
::
get_bytes_from_string
,
"py::bytes from std::string"
)
.
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_static
(
"new_instance"
,
&
ExamplePythonTypes
::
new_instance
,
"Return an instance"
)
.
def_static
(
"new_instance"
,
&
ExamplePythonTypes
::
new_instance
,
"Return an instance"
)
.
def_readwrite_static
(
"value"
,
&
ExamplePythonTypes
::
value
,
"Static value member"
)
.
def_readwrite_static
(
"value"
,
&
ExamplePythonTypes
::
value
,
"Static value member"
)
.
def_readonly_static
(
"value2"
,
&
ExamplePythonTypes
::
value2
,
"Static value member (readonly)"
)
.
def_readonly_static
(
"value2"
,
&
ExamplePythonTypes
::
value2
,
"Static value member (readonly)"
)
...
...
example/example-python-types.py
View file @
006d8b66
...
@@ -72,3 +72,8 @@ cstats = ConstructorStats.get(ExamplePythonTypes)
...
@@ -72,3 +72,8 @@ cstats = ConstructorStats.get(ExamplePythonTypes)
print
(
"Instances not destroyed:"
,
cstats
.
alive
())
print
(
"Instances not destroyed:"
,
cstats
.
alive
())
instance
=
None
instance
=
None
print
(
"Instances not destroyed:"
,
cstats
.
alive
())
print
(
"Instances not destroyed:"
,
cstats
.
alive
())
print
(
instance
.
get_bytes_from_string
()
.
decode
())
print
(
instance
.
get_bytes_from_str
()
.
decode
())
print
(
instance
.
get_str_from_string
())
print
(
instance
.
get_str_from_bytes
())
example/example-python-types.ref
View file @
006d8b66
...
@@ -138,3 +138,8 @@ __module__(example.ExamplePythonTypes.get_set) = example
...
@@ -138,3 +138,8 @@ __module__(example.ExamplePythonTypes.get_set) = example
Instances not destroyed: 1
Instances not destroyed: 1
### ExamplePythonTypes @ 0x1045b80 destroyed
### ExamplePythonTypes @ 0x1045b80 destroyed
Instances not destroyed: 0
Instances not destroyed: 0
Destructing ExamplePythonTypes
foo
bar
baz
boo
include/pybind11/pytypes.h
View file @
006d8b66
...
@@ -339,6 +339,8 @@ inline iterator handle::begin() const { return iterator(PyObject_GetIter(ptr()),
...
@@ -339,6 +339,8 @@ inline iterator handle::begin() const { return iterator(PyObject_GetIter(ptr()),
inline
iterator
handle
::
end
()
const
{
return
iterator
(
nullptr
,
false
);
}
inline
iterator
handle
::
end
()
const
{
return
iterator
(
nullptr
,
false
);
}
inline
detail
::
args_proxy
handle
::
operator
*
()
const
{
return
detail
::
args_proxy
(
*
this
);
}
inline
detail
::
args_proxy
handle
::
operator
*
()
const
{
return
detail
::
args_proxy
(
*
this
);
}
class
bytes
;
class
str
:
public
object
{
class
str
:
public
object
{
public
:
public
:
PYBIND11_OBJECT_DEFAULT
(
str
,
object
,
detail
::
PyUnicode_Check_Permissive
)
PYBIND11_OBJECT_DEFAULT
(
str
,
object
,
detail
::
PyUnicode_Check_Permissive
)
...
@@ -367,6 +369,8 @@ public:
...
@@ -367,6 +369,8 @@ 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
);
}
}
operator
bytes
()
const
;
};
};
inline
pybind11
::
str
handle
::
str
()
const
{
inline
pybind11
::
str
handle
::
str
()
const
{
...
@@ -395,8 +399,18 @@ public:
...
@@ -395,8 +399,18 @@ public:
pybind11_fail
(
"Unable to extract bytes contents!"
);
pybind11_fail
(
"Unable to extract bytes contents!"
);
return
std
::
string
(
buffer
,
(
size_t
)
length
);
return
std
::
string
(
buffer
,
(
size_t
)
length
);
}
}
operator
pybind11
::
str
()
const
;
};
};
inline
str
::
operator
bytes
()
const
{
return
bytes
((
std
::
string
)
*
this
);
}
inline
bytes
::
operator
pybind11
::
str
()
const
{
return
pybind11
::
str
((
std
::
string
)
*
this
);
}
class
none
:
public
object
{
class
none
:
public
object
{
public
:
public
:
PYBIND11_OBJECT
(
none
,
object
,
detail
::
PyNone_Check
)
PYBIND11_OBJECT
(
none
,
object
,
detail
::
PyNone_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