Files
pytorch/torch/csrc/utils/python_strings.h
Edward Yang 517c7c9861 Canonicalize all includes in PyTorch. (#14849)
Summary:
Anywhere we used #include "foo.h", we now say #include <foo.h>
Paths are adjusted to be rooted out of aten/src, torch/lib, or
the root level directory.

I modified CMakeLists.txt by hand to remove TH and THC from
the include paths.

I used the following script to do the canonicalization:

```
  import subprocess
  import re
  import os.path

  files = subprocess.check_output(['git', 'ls-files']).decode('utf-8').rstrip().split('\n')
  for fn in files:
      if not any(fn.endswith(suff) for suff in ['.cu', '.cpp', '.in', '.h', '.hpp', '.cu', '.cuh', '.cc']):
          continue
      if not any(fn.startswith(pref) for pref in ["aten/", "torch/"]):
          continue
      with open(fn, 'r') as f:
          c = f.read()
      def fmt(p):
          return "#include <{}>".format(p)
      def repl(m):
          p = m.group(1)
          if p in ["dlfcn.h", "unistd.h", "nvrtc.h", "cuda.h", "cuda_runtime.h", "cstdint", "cudnn.h", "Python.h", "cusparse.h", "cuda_runtime_api.h", "cuda_fp16.h", "cublas_v2.h", "stdint.h", "curand_kernel.h"]:
              return fmt(p)
          if any(p.startswith(pref) for pref in ["torch/csrc", "c10/", "ATen/", "caffe2/", "TH/", "THC/", "Eigen/", "gtest/", "zdl/", "gloo/", "onnx/", "miopen/"]):
              return fmt(p)
          for root in ["aten/src", "torch/lib", ""]:
              for bad_root in [os.path.dirname(fn), "aten/src/TH", "aten/src/THC", "torch/csrc"]:
                  new_p = os.path.relpath(os.path.join(bad_root, p), root)
                  if not new_p.startswith("../") and (os.path.exists(os.path.join(root, new_p)) or os.path.exists(os.path.join(root, new_p + ".in"))):
                      return fmt(new_p)
          print("ERROR: ", fn, p)
          return m.group(0)
      new_c = re.sub(r'#include "([^"]+)"', repl, c)
      if new_c != c:
          print(fn)
          with open(fn, 'w') as f:
              f.write(new_c)
```

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14849

Reviewed By: dzhulgakov

Differential Revision: D13363445

Pulled By: ezyang

fbshipit-source-id: 52361f878a672785f9306c9e9ab2513128092b68
2018-12-08 19:38:30 -08:00

60 lines
1.8 KiB
C++

#pragma once
#include <torch/csrc/python_headers.h>
#include <stdexcept>
#include <string>
#include <torch/csrc/utils/object_ptr.h>
// Utilities for handling Python strings. Note that PyString, when defined, is
// the same as PyBytes.
// Returns true if obj is a bytes/str or unicode object
// As of Python 3.6, this does not require the GIL
inline bool THPUtils_checkString(PyObject* obj) {
return PyBytes_Check(obj) || PyUnicode_Check(obj);
}
// Unpacks PyBytes (PyString) or PyUnicode as std::string
// PyBytes are unpacked as-is. PyUnicode is unpacked as UTF-8.
// NOTE: this method requires the GIL
inline std::string THPUtils_unpackString(PyObject* obj) {
if (PyBytes_Check(obj)) {
size_t size = PyBytes_GET_SIZE(obj);
return std::string(PyBytes_AS_STRING(obj), size);
}
if (PyUnicode_Check(obj)) {
#if PY_MAJOR_VERSION == 2
THPObjectPtr bytes(PyUnicode_AsUTF8String(obj));
if (!bytes) {
throw std::runtime_error("error unpacking string as utf-8");
}
size_t size = PyBytes_GET_SIZE(bytes.get());
return std::string(PyBytes_AS_STRING(bytes.get()), size);
#else
Py_ssize_t size;
const char* data = PyUnicode_AsUTF8AndSize(obj, &size);
if (!data) {
throw std::runtime_error("error unpacking string as utf-8");
}
return std::string(data, (size_t)size);
#endif
}
throw std::runtime_error("unpackString: expected bytes or unicode object");
}
inline PyObject* THPUtils_packString(const char* str) {
#if PY_MAJOR_VERSION == 2
return PyString_FromString(str);
#else
return PyUnicode_FromString(str);
#endif
}
inline PyObject* THPUtils_packString(const std::string& str) {
#if PY_MAJOR_VERSION == 2
return PyString_FromStringAndSize(str.c_str(), str.size());
#else
return PyUnicode_FromStringAndSize(str.c_str(), str.size());
#endif
}