pytorch/c10
Elton Leander Pinto aa857850bb Add check_env, getenv api (#59052)
Summary:
Related Issue: https://github.com/pytorch/pytorch/issues/57691
This PR introduces an API for checking environment variables:

```c++
optional<bool> check_env(const char *name)
```
Reads the environment variable name and returns
- `optional<true>`,                       if set equal to "1"
- `optional<false>`,                      if set equal to "0"
- `nullopt`,   otherwise

Issues a warning if the environment variable was set to any value other than 0 or 1

Pull Request resolved: https://github.com/pytorch/pytorch/pull/59052

Test Plan:
Manually run the following test case:

- Apply this diff to the repo
```
 diff --git a/torch/csrc/Exceptions.cpp b/torch/csrc/Exceptions.cpp
index d008643f70..990d254f0d 100644
 --- a/torch/csrc/Exceptions.cpp
+++ b/torch/csrc/Exceptions.cpp
@@ -9,6 +9,9 @@

 #include <torch/csrc/THP.h>

+#include <c10/util/Optional.h>
+#include <c10/util/env.h>
+
 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
 PyObject *THPException_FatalError;

@@ -23,18 +26,7 @@ bool THPException_init(PyObject *module)
 namespace torch {

 static bool compute_cpp_stack_traces_enabled() {
-  auto envar = std::getenv("TORCH_SHOW_CPP_STACKTRACES");
-  if (envar) {
-    if (strcmp(envar, "0") == 0) {
-      return false;
-    }
-    if (strcmp(envar, "1") == 0) {
-      return true;
-    }
-    TORCH_WARN("ignoring invalid value for TORCH_SHOW_CPP_STACKTRACES: ", envar,
-               " valid values are 0 or 1.");
-  }
-  return false;
+ return c10::utils::check_env("TORCH_SHOW_CPP_STACKTRACES").value_or(false);
 }

 bool get_cpp_stacktraces_enabled() {
```
This patch replaces the prior `std::getenv` usage in `torch/csrc/Exceptions.cpp` to use the new api.
- Run the following python3 script
```python
import torch

print(torch.__version__) # should print local version (not release)

a1 = torch.tensor([1,2,3])
a2 = torch.tensor([2])

a1 @ a2
```
using the following commands
```bash
python3 test.py # should not output CPP trace
TORCH_SHOW_CPP_STACKTRACES=1 python3 test.py # should output CPP trace
```

Reviewed By: ngimel

Differential Revision: D28799873

Pulled By: 1ntEgr8

fbshipit-source-id: 3e23353f48679ba8ce0364c049420ba4ff86ff09
2021-06-01 12:24:14 -07:00
..
benchmark [PyTorch] Autoformat c10 (#56830) 2021-04-30 21:23:28 -07:00
core Detect overflow in numel of sparse COO tensor (#57492) 2021-05-25 22:16:21 -07:00
cuda [Caffe2] Introduce c10::CudaError for CUDA Exceptions (#57609) 2021-05-06 14:28:45 -07:00
hip Formatting cmake (to lowercase without space for if/elseif/else/endif) (#35521) 2020-03-27 14:25:17 -07:00
macros Define the SYCL device version __assert_fail when the NDEBUG defined. (#58906) 2021-05-26 12:48:37 -07:00
mobile [PyTorch] Autoformat c10 (#56830) 2021-04-30 21:23:28 -07:00
test [AutoAccept][Codemod][FBSourceClangFormatLinter] Daily arc lint --take CLANGFORMAT 2021-05-10 03:39:31 -07:00
util Add check_env, getenv api (#59052) 2021-06-01 12:24:14 -07:00
CMakeLists.txt Combine backtrace print into one string to avoid interleaving. (#56961) 2021-05-12 15:52:05 -07:00