MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ConvertOneFloat

Function ConvertOneFloat

tensorflow/python/lib/core/py_seq_tensor.cc:395–438  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

393
394template <class T>
395const char* ConvertOneFloat(PyObject* v, T* out) {
396 if (PyErr_Occurred()) {
397 return nullptr;
398 }
399 if (TF_PREDICT_TRUE(PyFloat_Check(v))) {
400 const double as_double = PyFloat_AS_DOUBLE(v);
401 *out = static_cast<T>(as_double);
402 // Check for overflow.
403 if (TF_PREDICT_FALSE(CheckForOverflow<T>(as_double, out))) {
404 return ErrorOutOfRangeDouble;
405 }
406 return nullptr;
407 }
408#if PY_MAJOR_VERSION < 3
409 if (PyInt_Check(v)) {
410 *out = static_cast<T>(PyInt_AS_LONG(v));
411 return nullptr;
412 }
413#endif
414 if (PyLong_Check(v)) {
415 *out = static_cast<T>(PyLong_AsDouble(v));
416 if (PyErr_Occurred()) return ErrorOutOfRangeDouble;
417 return nullptr;
418 }
419 if (PyIsInstance(v, &PyFloatingArrType_Type)) { // NumPy float types
420 Safe_PyObjectPtr as_float = make_safe(PyNumber_Float(v));
421 if (PyErr_Occurred()) {
422 return nullptr;
423 }
424 return ConvertOneFloat<T>(as_float.get(), out);
425 }
426 if (PyIsInstance(v, &PyIntegerArrType_Type)) { // NumPy integers
427#if PY_MAJOR_VERSION < 3
428 Safe_PyObjectPtr as_int = make_safe(PyNumber_Int(v));
429#else
430 Safe_PyObjectPtr as_int = make_safe(PyNumber_Long(v));
431#endif
432 if (PyErr_Occurred()) {
433 return nullptr;
434 }
435 return ConvertOneFloat<T>(as_int.get(), out);
436 }
437 return ErrorMixedTypes;
438}
439
440DEFINE_HELPER(ConvertDouble, double, DT_DOUBLE, ConvertOneFloat<double>);
441DEFINE_HELPER(ConvertFloat, float, DT_FLOAT, ConvertOneFloat<float>);

Callers

nothing calls this directly

Calls 3

PyIsInstanceFunction · 0.85
make_safeFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected