Removing MSVC C4996 from pragma block at the top of pybind11.h (#3129)

* Removing MSVC C4996 from pragma block at the top of pybind11.h

* localtime_thread_safe, PYBIND11_COMPAT_STRDUP

* Adding #include <ctime> (attempt to fix MSVC 2015, 2017 errors).
This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-07-26 11:28:36 -07:00 committed by Henry Schreiner
parent ee3ecb8ae2
commit b193d42c32
No known key found for this signature in database
GPG Key ID: B9D0E45146A241E8
3 changed files with 42 additions and 14 deletions

View File

@ -11,9 +11,14 @@
#pragma once #pragma once
#include "pybind11.h" #include "pybind11.h"
#include <chrono>
#include <cmath> #include <cmath>
#include <ctime> #include <ctime>
#include <chrono> #include <mutex>
#include <time.h>
#include <datetime.h> #include <datetime.h>
// Backport the PyDateTime_DELTA functions from Python3.3 if required // Backport the PyDateTime_DELTA functions from Python3.3 if required
@ -95,6 +100,22 @@ public:
PYBIND11_TYPE_CASTER(type, _("datetime.timedelta")); PYBIND11_TYPE_CASTER(type, _("datetime.timedelta"));
}; };
inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) {
#if defined(__STDC_WANT_LIB_EXT1__) || defined(_MSC_VER)
if (localtime_s(buf, time))
return nullptr;
return buf;
#else
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
std::tm *tm_ptr = localtime(time);
if (tm_ptr != nullptr) {
*buf = *tm_ptr;
}
return tm_ptr;
#endif
}
// This is for casting times on the system clock into datetime.datetime instances // This is for casting times on the system clock into datetime.datetime instances
template <typename Duration> class type_caster<std::chrono::time_point<std::chrono::system_clock, Duration>> { template <typename Duration> class type_caster<std::chrono::time_point<std::chrono::system_clock, Duration>> {
public: public:
@ -162,16 +183,10 @@ public:
// (https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t) // (https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t)
std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src - us)); std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src - us));
// std::localtime returns a pointer to a static internal std::tm object on success, std::tm localtime;
// or null pointer otherwise std::tm *localtime_ptr = localtime_thread_safe(&tt, &localtime);
std::tm *localtime_ptr = std::localtime(&tt);
if (!localtime_ptr) if (!localtime_ptr)
throw cast_error("Unable to represent system_clock in local time"); throw cast_error("Unable to represent system_clock in local time");
// this function uses static memory so it's best to copy it out asap just in case
// otherwise other code that is using localtime may break this (not just python code)
std::tm localtime = *localtime_ptr;
return PyDateTime_FromDateAndTime(localtime.tm_year + 1900, return PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
localtime.tm_mon + 1, localtime.tm_mon + 1,
localtime.tm_mday, localtime.tm_mday,

View File

@ -125,6 +125,11 @@
# endif # endif
#endif #endif
// https://en.cppreference.com/w/c/chrono/localtime
#if defined(__STDC_LIB_EXT1__) && !defined(__STDC_WANT_LIB_EXT1__)
# define __STDC_WANT_LIB_EXT1__
#endif
#include <Python.h> #include <Python.h>
#include <frameobject.h> #include <frameobject.h>
#include <pythread.h> #include <pythread.h>

View File

@ -19,7 +19,6 @@
# pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
# pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
# pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
# pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified # pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified
# pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only)
#elif defined(__GNUG__) && !defined(__clang__) #elif defined(__GNUG__) && !defined(__clang__)
@ -43,6 +42,8 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <string.h>
#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) #if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914))
# define PYBIND11_STD_LAUNDER std::launder # define PYBIND11_STD_LAUNDER std::launder
# define PYBIND11_HAS_STD_LAUNDER 1 # define PYBIND11_HAS_STD_LAUNDER 1
@ -56,6 +57,12 @@
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
#if defined(_MSC_VER)
# define PYBIND11_COMPAT_STRDUP _strdup
#else
# define PYBIND11_COMPAT_STRDUP strdup
#endif
/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
class cpp_function : public function { class cpp_function : public function {
public: public:
@ -253,7 +260,7 @@ protected:
std::free(s); std::free(s);
} }
char *operator()(const char *s) { char *operator()(const char *s) {
auto t = strdup(s); auto t = PYBIND11_COMPAT_STRDUP(s);
strings.push_back(t); strings.push_back(t);
return t; return t;
} }
@ -497,7 +504,8 @@ protected:
auto *func = (PyCFunctionObject *) m_ptr; auto *func = (PyCFunctionObject *) m_ptr;
std::free(const_cast<char *>(func->m_ml->ml_doc)); std::free(const_cast<char *>(func->m_ml->ml_doc));
// Install docstring if it's non-empty (when at least one option is enabled) // Install docstring if it's non-empty (when at least one option is enabled)
func->m_ml->ml_doc = signatures.empty() ? nullptr : strdup(signatures.c_str()); func->m_ml->ml_doc
= signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
if (rec->is_method) { if (rec->is_method) {
m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr()); m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
@ -1498,7 +1506,7 @@ public:
detail::process_attributes<Extra...>::init(extra..., rec_fget); detail::process_attributes<Extra...>::init(extra..., rec_fget);
if (rec_fget->doc && rec_fget->doc != doc_prev) { if (rec_fget->doc && rec_fget->doc != doc_prev) {
free(doc_prev); free(doc_prev);
rec_fget->doc = strdup(rec_fget->doc); rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc);
} }
} }
if (rec_fset) { if (rec_fset) {
@ -1506,7 +1514,7 @@ public:
detail::process_attributes<Extra...>::init(extra..., rec_fset); detail::process_attributes<Extra...>::init(extra..., rec_fset);
if (rec_fset->doc && rec_fset->doc != doc_prev) { if (rec_fset->doc && rec_fset->doc != doc_prev) {
free(doc_prev); free(doc_prev);
rec_fset->doc = strdup(rec_fset->doc); rec_fset->doc = PYBIND11_COMPAT_STRDUP(rec_fset->doc);
} }
if (! rec_active) rec_active = rec_fset; if (! rec_active) rec_active = rec_fset;
} }