Skip to content

Commit 267f86c

Browse files
committed
Removing MSVC C4996 from pragma block at the top of pybind11.h
1 parent 34f587d commit 267f86c

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

include/pybind11/chrono.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,14 @@ template <typename Duration> class type_caster<std::chrono::time_point<std::chro
164164

165165
// std::localtime returns a pointer to a static internal std::tm object on success,
166166
// or null pointer otherwise
167+
#if defined(_MSC_VER)
168+
# pragma warning(push)
169+
# pragma warning(disable : 4996) // warning C4996: The POSIX name for this item is deprecated.
170+
#endif
167171
std::tm *localtime_ptr = std::localtime(&tt);
172+
#if defined(_MSC_VER)
173+
# pragma warning(pop)
174+
#endif
168175
if (!localtime_ptr)
169176
throw cast_error("Unable to represent system_clock in local time");
170177

include/pybind11/pybind11.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
# pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
2020
# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
2121
# pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
22-
# pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
2322
# pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified
2423
# pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only)
2524
#elif defined(__GNUG__) && !defined(__clang__)
@@ -43,6 +42,8 @@
4342
#include <string>
4443
#include <utility>
4544

45+
#include <string.h>
46+
4647
#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914))
4748
# define PYBIND11_STD_LAUNDER std::launder
4849
# define PYBIND11_HAS_STD_LAUNDER 1
@@ -56,6 +57,21 @@
5657

5758
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
5859

60+
PYBIND11_NAMESPACE_BEGIN(detail)
61+
62+
#if defined(_MSC_VER)
63+
# pragma warning(push)
64+
# pragma warning(disable : 4996) // warning C4996: The POSIX name for this item is deprecated.
65+
#endif
66+
67+
inline char *strdup_helper(const char *s) { return strdup(s); }
68+
69+
#if defined(_MSC_VER)
70+
# pragma warning(pop)
71+
#endif
72+
73+
PYBIND11_NAMESPACE_END(detail)
74+
5975
/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
6076
class cpp_function : public function {
6177
public:
@@ -253,7 +269,7 @@ class cpp_function : public function {
253269
std::free(s);
254270
}
255271
char *operator()(const char *s) {
256-
auto t = strdup(s);
272+
auto t = detail::strdup_helper(s);
257273
strings.push_back(t);
258274
return t;
259275
}
@@ -497,7 +513,8 @@ class cpp_function : public function {
497513
auto *func = (PyCFunctionObject *) m_ptr;
498514
std::free(const_cast<char *>(func->m_ml->ml_doc));
499515
// Install docstring if it's non-empty (when at least one option is enabled)
500-
func->m_ml->ml_doc = signatures.empty() ? nullptr : strdup(signatures.c_str());
516+
func->m_ml->ml_doc
517+
= signatures.empty() ? nullptr : detail::strdup_helper(signatures.c_str());
501518

502519
if (rec->is_method) {
503520
m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
@@ -1498,15 +1515,15 @@ class class_ : public detail::generic_type {
14981515
detail::process_attributes<Extra...>::init(extra..., rec_fget);
14991516
if (rec_fget->doc && rec_fget->doc != doc_prev) {
15001517
free(doc_prev);
1501-
rec_fget->doc = strdup(rec_fget->doc);
1518+
rec_fget->doc = detail::strdup_helper(rec_fget->doc);
15021519
}
15031520
}
15041521
if (rec_fset) {
15051522
char *doc_prev = rec_fset->doc;
15061523
detail::process_attributes<Extra...>::init(extra..., rec_fset);
15071524
if (rec_fset->doc && rec_fset->doc != doc_prev) {
15081525
free(doc_prev);
1509-
rec_fset->doc = strdup(rec_fset->doc);
1526+
rec_fset->doc = detail::strdup_helper(rec_fset->doc);
15101527
}
15111528
if (! rec_active) rec_active = rec_fset;
15121529
}

0 commit comments

Comments
 (0)