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