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
c22fe428
Commit
c22fe428
authored
Aug 13, 2016
by
Ivan Smirnov
Browse files
Change str/bytes cast operators to ctors
parent
89ec7f3e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
12 deletions
+12
-12
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+12
-12
No files found.
include/pybind11/pytypes.h
View file @
c22fe428
...
...
@@ -357,6 +357,8 @@ public:
str
(
const
std
::
string
&
s
)
:
str
(
s
.
data
(),
s
.
size
())
{
}
str
(
const
bytes
&
b
);
operator
std
::
string
()
const
{
object
temp
=
*
this
;
if
(
PyUnicode_Check
(
m_ptr
))
{
...
...
@@ -370,8 +372,6 @@ public:
pybind11_fail
(
"Unable to extract string contents! (invalid type)"
);
return
std
::
string
(
buffer
,
(
size_t
)
length
);
}
operator
bytes
()
const
;
};
inline
pybind11
::
str
handle
::
str
()
const
{
...
...
@@ -394,6 +394,8 @@ public:
bytes
(
const
std
::
string
&
s
)
:
bytes
(
s
.
data
(),
s
.
size
())
{
}
bytes
(
const
pybind11
::
str
&
s
);
operator
std
::
string
()
const
{
char
*
buffer
;
ssize_t
length
;
...
...
@@ -401,14 +403,12 @@ public:
pybind11_fail
(
"Unable to extract bytes contents!"
);
return
std
::
string
(
buffer
,
(
size_t
)
length
);
}
operator
pybind11
::
str
()
const
;
};
inline
str
::
operator
bytes
(
)
const
{
object
temp
=
*
thi
s
;
if
(
PyUnicode_Check
(
m_
ptr
))
{
temp
=
object
(
PyUnicode_AsUTF8String
(
m_
ptr
),
false
);
inline
bytes
::
bytes
(
const
pybind11
::
str
&
s
)
{
object
temp
=
s
;
if
(
PyUnicode_Check
(
s
.
ptr
()
))
{
temp
=
object
(
PyUnicode_AsUTF8String
(
s
.
ptr
()
),
false
);
if
(
!
temp
)
pybind11_fail
(
"Unable to extract string contents! (encoding issue)"
);
}
...
...
@@ -419,18 +419,18 @@ inline str::operator bytes() const {
auto
obj
=
object
(
PYBIND11_BYTES_FROM_STRING_AND_SIZE
(
buffer
,
length
),
false
);
if
(
!
obj
)
pybind11_fail
(
"Could not allocate bytes object!"
);
return
obj
;
m_ptr
=
obj
.
release
().
ptr
()
;
}
inline
bytes
::
operator
pybind11
::
str
(
)
const
{
inline
str
::
str
(
const
bytes
&
b
)
{
char
*
buffer
;
ssize_t
length
;
if
(
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
m_
ptr
,
&
buffer
,
&
length
))
if
(
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
b
.
ptr
()
,
&
buffer
,
&
length
))
pybind11_fail
(
"Unable to extract bytes contents!"
);
auto
obj
=
object
(
PyUnicode_FromStringAndSize
(
buffer
,
(
ssize_t
)
length
),
false
);
if
(
!
obj
)
pybind11_fail
(
"Could not allocate string object!"
);
return
obj
;
m_ptr
=
obj
.
release
().
ptr
()
;
}
class
none
:
public
object
{
...
...
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