Skip to content

Commit e052ff0

Browse files
committed
Removing MSVC C4996 from pragma block at the top of pybind11.h
1 parent e58c689 commit e052ff0

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
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: 18 additions & 6 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
@@ -76,8 +77,18 @@ inline bool apply_exception_translators(std::forward_list<ExceptionTranslator>&
7677
return false;
7778
}
7879

79-
PYBIND11_NAMESPACE_END(detail)
80+
#if defined(_MSC_VER)
81+
# pragma warning(push)
82+
# pragma warning(disable : 4996) // warning C4996: The POSIX name for this item is deprecated.
83+
#endif
84+
85+
inline char *strdup_helper(const char *s) { return strdup(s); }
8086

87+
#if defined(_MSC_VER)
88+
# pragma warning(pop)
89+
#endif
90+
91+
PYBIND11_NAMESPACE_END(detail)
8192

8293
/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
8394
class cpp_function : public function {
@@ -276,7 +287,7 @@ class cpp_function : public function {
276287
std::free(s);
277288
}
278289
char *operator()(const char *s) {
279-
auto t = strdup(s);
290+
auto t = detail::strdup_helper(s);
280291
strings.push_back(t);
281292
return t;
282293
}
@@ -520,7 +531,8 @@ class cpp_function : public function {
520531
auto *func = (PyCFunctionObject *) m_ptr;
521532
std::free(const_cast<char *>(func->m_ml->ml_doc));
522533
// Install docstring if it's non-empty (when at least one option is enabled)
523-
func->m_ml->ml_doc = signatures.empty() ? nullptr : strdup(signatures.c_str());
534+
func->m_ml->ml_doc
535+
= signatures.empty() ? nullptr : detail::strdup_helper(signatures.c_str());
524536

525537
if (rec->is_method) {
526538
m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
@@ -1525,15 +1537,15 @@ class class_ : public detail::generic_type {
15251537
detail::process_attributes<Extra...>::init(extra..., rec_fget);
15261538
if (rec_fget->doc && rec_fget->doc != doc_prev) {
15271539
free(doc_prev);
1528-
rec_fget->doc = strdup(rec_fget->doc);
1540+
rec_fget->doc = detail::strdup_helper(rec_fget->doc);
15291541
}
15301542
}
15311543
if (rec_fset) {
15321544
char *doc_prev = rec_fset->doc;
15331545
detail::process_attributes<Extra...>::init(extra..., rec_fset);
15341546
if (rec_fset->doc && rec_fset->doc != doc_prev) {
15351547
free(doc_prev);
1536-
rec_fset->doc = strdup(rec_fset->doc);
1548+
rec_fset->doc = detail::strdup_helper(rec_fset->doc);
15371549
}
15381550
if (! rec_active) rec_active = rec_fset;
15391551
}

0 commit comments

Comments
 (0)