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
5612a0c1
Commit
5612a0c1
authored
May 01, 2016
by
Wenzel Jakob
Browse files
generalized str::operator std::string() to accept 'bytes'(3.x)/'string'(2.7)
parent
fc92d82b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
11 deletions
+20
-11
include/pybind11/common.h
include/pybind11/common.h
+2
-0
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+18
-11
No files found.
include/pybind11/common.h
View file @
5612a0c1
...
...
@@ -81,6 +81,7 @@
#define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
#define PYBIND11_BYTES_AS_STRING_AND_SIZE PyBytes_AsStringAndSize
#define PYBIND11_BYTES_AS_STRING PyBytes_AsString
#define PYBIND11_BYTES_CHECK PyBytes_Check
#define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
#define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
#define PYBIND11_LONG_AS_UNSIGNED_LONGLONG(o) PyLong_AsUnsignedLongLong(o)
...
...
@@ -98,6 +99,7 @@
#define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize
#define PYBIND11_BYTES_AS_STRING_AND_SIZE PyString_AsStringAndSize
#define PYBIND11_BYTES_AS_STRING PyString_AsString
#define PYBIND11_BYTES_CHECK PyString_Check
#define PYBIND11_LONG_CHECK(o) (PyInt_Check(o) || PyLong_Check(o))
#define PYBIND11_LONG_AS_LONGLONG(o) (PyInt_Check(o) ? (long long) PyLong_AsLong(o) : PyLong_AsLongLong(o))
#define PYBIND11_LONG_AS_UNSIGNED_LONGLONG(o) (PyInt_Check(o) ? (unsigned long long) PyLong_AsUnsignedLong(o) : PyLong_AsUnsignedLongLong(o))
...
...
include/pybind11/pytypes.h
View file @
5612a0c1
...
...
@@ -163,7 +163,7 @@ public:
return
object
(
result
,
true
);
}
template
<
typename
T
>
inline
T
cast
()
const
{
return
operator
object
().
cast
<
T
>
();
}
template
<
typename
T
>
T
cast
()
const
{
return
operator
object
().
cast
<
T
>
();
}
private:
handle
list
;
size_t
index
;
...
...
@@ -188,7 +188,7 @@ public:
return
object
(
result
,
true
);
}
template
<
typename
T
>
inline
T
cast
()
const
{
return
operator
object
().
cast
<
T
>
();
}
template
<
typename
T
>
T
cast
()
const
{
return
operator
object
().
cast
<
T
>
();
}
private:
handle
tuple
;
size_t
index
;
...
...
@@ -225,6 +225,8 @@ inline bool PyIterable_Check(PyObject *obj) {
inline
bool
PyNone_Check
(
PyObject
*
o
)
{
return
o
==
Py_None
;
}
inline
bool
PyUnicode_Check_Permissive
(
PyObject
*
o
)
{
return
PyUnicode_Check
(
o
)
||
PYBIND11_BYTES_CHECK
(
o
);
}
NAMESPACE_END
(
detail
)
#define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, CvtStmt) \
...
...
@@ -316,21 +318,26 @@ inline iterator handle::end() const { return iterator(nullptr, false); }
class
str
:
public
object
{
public:
PYBIND11_OBJECT_DEFAULT
(
str
,
object
,
PyUnicode_Check
)
PYBIND11_OBJECT_DEFAULT
(
str
,
object
,
detail
::
PyUnicode_Check_Permissive
)
str
(
const
std
::
string
&
s
)
:
object
(
PyUnicode_FromStringAndSize
(
s
.
c_str
(),
s
.
length
()),
false
)
{
if
(
!
m_ptr
)
pybind11_fail
(
"Could not allocate string object!"
);
}
operator
std
::
string
()
const
{
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
return
PyUnicode_AsUTF8
(
m_ptr
);
#else
object
temp
(
PyUnicode_AsUTF8String
(
m_ptr
),
false
);
if
(
temp
.
ptr
()
==
nullptr
)
pybind11_fail
(
"Unable to extract string contents!"
);
return
PYBIND11_BYTES_AS_STRING
(
temp
.
ptr
());
#endif
object
temp
=
*
this
;
if
(
PyUnicode_Check
(
m_ptr
))
{
temp
=
object
(
PyUnicode_AsUTF8String
(
m_ptr
),
false
);
if
(
!
temp
)
pybind11_fail
(
"Unable to extract string contents! (encoding issue)"
);
}
char
*
buffer
;
ssize_t
length
;
int
err
=
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
temp
.
ptr
(),
&
buffer
,
&
length
);
if
(
err
==
-
1
)
pybind11_fail
(
"Unable to extract string contents! (invalid type)"
);
return
std
::
string
(
buffer
,
length
);
}
};
...
...
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