Skip to content

Commit 00e4bf2

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 c9f0a3e commit 00e4bf2

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ 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
42+
int _sync() {
4243
if (pbase() != pptr()) {
4344
// This subtraction cannot be negative, so dropping the sign
4445
str line(pbase(), static_cast<size_t>(pptr() - pbase()));
@@ -54,6 +55,10 @@ class pythonbuf : public std::streambuf {
5455
return 0;
5556
}
5657

58+
int sync() override {
59+
return _sync();
60+
}
61+
5762
public:
5863

5964
pythonbuf(object pyostream, size_t buffer_size = 1024)
@@ -68,7 +73,7 @@ class pythonbuf : public std::streambuf {
6873

6974
/// Sync before destroy
7075
~pythonbuf() override {
71-
sync();
76+
_sync();
7277
}
7378
};
7479

0 commit comments

Comments
 (0)