Skip to content

Commit 73b8b43

Browse files
committed
style: clang-tidy: default checks and fix bug in iostream deconstruction
``` /pybind11/include/pybind11/iostream.h:71:9: warning: Call to virtual method 'pythonbuf::sync' during destruction bypasses virtual dispatch [clang-analyzer-optin.cplusplus.VirtualCall] sync(); ^ /pybind11/tests/test_iostream.cpp:72:5: note: Calling '~scoped_ostream_redirect' }); ```
1 parent 660e682 commit 73b8b43

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

.clang-tidy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
FormatStyle: file
22

33
Checks: '
4-
-*,
54
llvm-namespace-comment,
65
modernize-use-override,
76
readability-container-size-empty,

include/pybind11/iostream.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ class pythonbuf : public std::streambuf {
3838
return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof();
3939
}
4040

41-
int sync() override {
41+
// This function must be non-virtual to be called in a destructor. If the
42+
// rare MSVC test failure shows up with this version, then this should be
43+
// simplified to a fully qualified call.
44+
int _sync() {
4245
if (pbase() != pptr()) {
4346
// This subtraction cannot be negative, so dropping the sign
4447
str line(pbase(), static_cast<size_t>(pptr() - pbase()));
@@ -54,6 +57,10 @@ class pythonbuf : public std::streambuf {
5457
return 0;
5558
}
5659

60+
int sync() override {
61+
return _sync();
62+
}
63+
5764
public:
5865

5966
pythonbuf(object pyostream, size_t buffer_size = 1024)
@@ -68,7 +75,7 @@ class pythonbuf : public std::streambuf {
6875

6976
/// Sync before destroy
7077
~pythonbuf() override {
71-
sync();
78+
_sync();
7279
}
7380
};
7481

0 commit comments

Comments
 (0)