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
fd6cede7
Commit
fd6cede7
authored
Aug 13, 2016
by
Ivan Smirnov
Browse files
Avoid extra allocations in operator str/bytes
parent
35c51c47
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
6 deletions
+24
-6
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+24
-6
No files found.
include/pybind11/pytypes.h
View file @
fd6cede7
...
@@ -364,8 +364,7 @@ public:
...
@@ -364,8 +364,7 @@ public:
}
}
char
*
buffer
;
char
*
buffer
;
ssize_t
length
;
ssize_t
length
;
int
err
=
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
temp
.
ptr
(),
&
buffer
,
&
length
);
if
(
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
temp
.
ptr
(),
&
buffer
,
&
length
))
if
(
err
==
-
1
)
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
);
}
}
...
@@ -394,8 +393,7 @@ public:
...
@@ -394,8 +393,7 @@ public:
operator
std
::
string
()
const
{
operator
std
::
string
()
const
{
char
*
buffer
;
char
*
buffer
;
ssize_t
length
;
ssize_t
length
;
int
err
=
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
m_ptr
,
&
buffer
,
&
length
);
if
(
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
m_ptr
,
&
buffer
,
&
length
))
if
(
err
==
-
1
)
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
);
}
}
...
@@ -404,11 +402,31 @@ public:
...
@@ -404,11 +402,31 @@ public:
};
};
inline
str
::
operator
bytes
()
const
{
inline
str
::
operator
bytes
()
const
{
return
bytes
((
std
::
string
)
*
this
);
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
;
if
(
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
temp
.
ptr
(),
&
buffer
,
&
length
))
pybind11_fail
(
"Unable to extract string contents! (invalid type)"
);
auto
obj
=
object
(
PYBIND11_BYTES_FROM_STRING_AND_SIZE
(
buffer
,
length
),
false
);
if
(
!
obj
)
pybind11_fail
(
"Could not allocate bytes object!"
);
return
obj
;
}
}
inline
bytes
::
operator
pybind11
::
str
()
const
{
inline
bytes
::
operator
pybind11
::
str
()
const
{
return
pybind11
::
str
((
std
::
string
)
*
this
);
char
*
buffer
;
ssize_t
length
;
if
(
PYBIND11_BYTES_AS_STRING_AND_SIZE
(
m_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
;
}
}
class
none
:
public
object
{
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