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
d46b6eee
Commit
d46b6eee
authored
Jul 18, 2016
by
Wenzel Jakob
Committed by
GitHub
Jul 18, 2016
Browse files
Merge pull request #285 from jagerman/fix-uninitialized-str
Fix #283: don't print first arg of constructor
parents
fbdd30e5
4e45e180
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
4 deletions
+56
-4
example/issues.cpp
example/issues.cpp
+19
-0
example/issues.py
example/issues.py
+6
-0
example/issues.ref
example/issues.ref
+6
-0
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+25
-4
No files found.
example/issues.cpp
View file @
d46b6eee
...
@@ -138,4 +138,23 @@ void init_issues(py::module &m) {
...
@@ -138,4 +138,23 @@ void init_issues(py::module &m) {
}
catch
(
std
::
runtime_error
&
)
{
}
catch
(
std
::
runtime_error
&
)
{
/* All good */
/* All good */
}
}
// Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
class
StrIssue
{
public:
StrIssue
(
int
i
)
:
val
{
i
}
{}
StrIssue
()
:
StrIssue
(
-
1
)
{}
int
value
()
const
{
return
val
;
}
private:
int
val
;
};
py
::
class_
<
StrIssue
>
si
(
m2
,
"StrIssue"
);
si
.
def
(
py
::
init
<
int
>
())
.
def
(
py
::
init
<>
())
.
def
(
"__str__"
,
[](
const
StrIssue
&
si
)
{
std
::
cout
<<
"StrIssue.__str__ called"
<<
std
::
endl
;
return
"StrIssue["
+
std
::
to_string
(
si
.
value
())
+
"]"
;
})
;
}
}
example/issues.py
View file @
d46b6eee
...
@@ -10,6 +10,7 @@ from example.issues import iterator_passthrough
...
@@ -10,6 +10,7 @@ from example.issues import iterator_passthrough
from
example.issues
import
ElementList
,
ElementA
,
print_element
from
example.issues
import
ElementList
,
ElementA
,
print_element
from
example.issues
import
expect_float
,
expect_int
from
example.issues
import
expect_float
,
expect_int
from
example.issues
import
A
,
call_f
from
example.issues
import
A
,
call_f
from
example.issues
import
StrIssue
import
gc
import
gc
print_cchar
(
"const char *"
)
print_cchar
(
"const char *"
)
...
@@ -72,3 +73,8 @@ print("Python version")
...
@@ -72,3 +73,8 @@ print("Python version")
b
=
B
()
b
=
B
()
call_f
(
b
)
call_f
(
b
)
print
(
StrIssue
(
3
))
try
:
print
(
StrIssue
(
"no"
,
"such"
,
"constructor"
))
except
TypeError
as
e
:
print
(
"Failed as expected: "
+
str
(
e
))
example/issues.ref
View file @
d46b6eee
...
@@ -18,3 +18,9 @@ Python version
...
@@ -18,3 +18,9 @@ Python version
PyA.PyA()
PyA.PyA()
PyA.f()
PyA.f()
In python f()
In python f()
StrIssue.__str__ called
StrIssue[3]
Failed as expected: Incompatible constructor arguments. The following argument types are supported:
1. example.issues.StrIssue(int)
2. example.issues.StrIssue()
Invoked with: no, such, constructor
include/pybind11/pybind11.h
View file @
d46b6eee
...
@@ -445,17 +445,38 @@ protected:
...
@@ -445,17 +445,38 @@ protected:
}
}
if
(
result
.
ptr
()
==
PYBIND11_TRY_NEXT_OVERLOAD
)
{
if
(
result
.
ptr
()
==
PYBIND11_TRY_NEXT_OVERLOAD
)
{
std
::
string
msg
=
"Incompatible
function arguments. The "
std
::
string
msg
=
"Incompatible
"
+
std
::
string
(
overloads
->
is_constructor
?
"constructor"
:
"function"
)
+
"following argument types are supported:
\n
"
;
"
arguments. The
following argument types are supported:
\n
"
;
int
ctr
=
0
;
int
ctr
=
0
;
for
(
detail
::
function_record
*
it2
=
overloads
;
it2
!=
nullptr
;
it2
=
it2
->
next
)
{
for
(
detail
::
function_record
*
it2
=
overloads
;
it2
!=
nullptr
;
it2
=
it2
->
next
)
{
msg
+=
" "
+
std
::
to_string
(
++
ctr
)
+
". "
;
msg
+=
" "
+
std
::
to_string
(
++
ctr
)
+
". "
;
msg
+=
it2
->
signature
;
bool
wrote_sig
=
false
;
if
(
overloads
->
is_constructor
)
{
// For a constructor, rewrite `(Object, arg0, ...) -> NoneType` as `Object(arg0, ...)`
std
::
string
sig
=
it2
->
signature
;
size_t
start
=
sig
.
find
(
'('
)
+
1
;
if
(
start
<
sig
.
size
())
{
// End at the , for the next argument
size_t
end
=
sig
.
find
(
", "
),
next
=
end
+
2
;
size_t
ret
=
sig
.
rfind
(
" -> "
);
// Or the ), if there is no comma:
if
(
end
>=
sig
.
size
())
next
=
end
=
sig
.
find
(
')'
);
if
(
start
<
end
&&
next
<
sig
.
size
())
{
msg
.
append
(
sig
,
start
,
end
-
start
);
msg
+=
'('
;
msg
.
append
(
sig
,
next
,
ret
-
next
);
wrote_sig
=
true
;
}
}
}
if
(
!
wrote_sig
)
msg
+=
it2
->
signature
;
msg
+=
"
\n
"
;
msg
+=
"
\n
"
;
}
}
msg
+=
" Invoked with: "
;
msg
+=
" Invoked with: "
;
tuple
args_
(
args
,
true
);
tuple
args_
(
args
,
true
);
for
(
std
::
size_t
ti
=
0
;
ti
!=
args_
.
size
();
++
ti
)
for
(
std
::
size_t
ti
=
overloads
->
is_constructor
?
1
:
0
;
ti
<
args_
.
size
();
++
ti
)
{
{
msg
+=
static_cast
<
std
::
string
>
(
static_cast
<
object
>
(
args_
[
ti
]).
str
());
msg
+=
static_cast
<
std
::
string
>
(
static_cast
<
object
>
(
args_
[
ti
]).
str
());
if
((
ti
+
1
)
!=
args_
.
size
()
)
if
((
ti
+
1
)
!=
args_
.
size
()
)
...
...
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