do not throw when unicode is seen in pull request info (#18195)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18195
ghimport-source-id: 05102cb115c6bd6d141f51905e20155bcd79a908

Stack from [ghstack](https://github.com/ezyang/ghstack):
* **#18195 [build] do not throw when unicode is seen in pull request info**

Differential Revision: D14529707

fbshipit-source-id: 2f6a31b01b3a9b044fd24be466cc5325b70929ad
diff --git a/tools/build_pytorch_libs.py b/tools/build_pytorch_libs.py
index c1f08f1..de4f7a9 100644
--- a/tools/build_pytorch_libs.py
+++ b/tools/build_pytorch_libs.py
@@ -215,7 +215,11 @@
         if env_var_name.startswith('gh'):
             # github env vars use utf-8, on windows, non-ascii code may
             # cause problem, so encode first
-            my_env[env_var_name] = str(my_env[env_var_name].encode("utf-8"))
+            try:
+                my_env[env_var_name] = str(my_env[env_var_name].encode("utf-8"))
+            except UnicodeDecodeError as e:
+                shex = ':'.join('{:02x}'.format(ord(c)) for c in my_env[env_var_name])
+                sys.stderr.write('Invalid ENV[{}] = {}\n'.format(env_var_name, shex))
     check_call(cmake_args, cwd=build_dir, env=my_env)