/
usr
/
local
/
lib64
/
python3.6
/
site-packages
/
torch
/
include
/
torch
/
csrc
/
utils
/
/usr/local/lib64/python3.6/site-packages/torch/include/torch/csrc/utils
mkdir
upload
Name
Size
Mode
Actions
auto_gil.h
1034
0644
edit
dl
rm
byte_order.h
2484
0644
edit
dl
rm
crash_handler.h
1146
0644
edit
dl
rm
cuda_enabled.h
154
0644
edit
dl
rm
cuda_lazy_init.h
974
0644
edit
dl
rm
disable_torch_function.h
863
0644
edit
dl
rm
disallow_copy.h
103
0644
edit
dl
rm
init.h
324
0644
edit
dl
rm
invalid_arguments.h
302
0644
edit
dl
rm
memory.h
1173
0644
edit
dl
rm
numpy_stub.h
399
0644
edit
dl
rm
object_ptr.h
1329
0644
edit
dl
rm
out_types.h
294
0644
edit
dl
rm
pybind.h
7966
0644
edit
dl
rm
pycfunction_helpers.h
209
0644
edit
dl
rm
python_arg_parser.h
30753
0644
edit
dl
rm
python_compat.h
2904
0644
edit
dl
rm
python_dispatch.h
174
0644
edit
dl
rm
python_numbers.h
5064
0644
edit
dl
rm
python_scalars.h
2928
0644
edit
dl
rm
python_strings.h
4592
0644
edit
dl
rm
python_stub.h
56
0644
edit
dl
rm
python_tuples.h
684
0644
edit
dl
rm
six.h
1425
0644
edit
dl
rm
structseq.h
153
0644
edit
dl
rm
tensor_apply.h
431
0644
edit
dl
rm
tensor_dtypes.h
244
0644
edit
dl
rm
tensor_flatten.h
2780
0644
edit
dl
rm
tensor_layouts.h
107
0644
edit
dl
rm
tensor_list.h
196
0644
edit
dl
rm
tensor_memoryformats.h
113
0644
edit
dl
rm
tensor_new.h
1807
0644
edit
dl
rm
tensor_numpy.h
542
0644
edit
dl
rm
tensor_qschemes.h
186
0644
edit
dl
rm
tensor_types.h
491
0644
edit
dl
rm
throughput_benchmark-inl.h
5210
0644
edit
dl
rm
throughput_benchmark.h
6868
0644
edit
dl
rm
variadic.h
4394
0644
edit
dl
rm
Edit:
/usr/local/lib64/python3.6/site-packages/torch/include/torch/csrc/utils/python_strings.h
(4592B)
#pragma once #include <torch/csrc/python_headers.h> #include <stdexcept> #include <string> #include <torch/csrc/utils/object_ptr.h> #include <torch/csrc/utils/pybind.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)) { // NOLINTNEXTLINE(cppcoreguidelines-init-variables) 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); } throw std::runtime_error("unpackString: expected bytes or unicode object"); } // Unpacks PyBytes (PyString) or PyUnicode as c10::string_view // PyBytes are unpacked as-is. PyUnicode is unpacked as UTF-8. // NOTE: If `obj` is destroyed, then the non-owning c10::string_view will // become invalid. If the string needs to be accessed at any point after // `obj` is destroyed, then the c10::string_view should be copied into // a std::string, or another owning object, and kept alive. For an example, // look at how IValue and autograd nodes handle c10::string_view arguments. // NOTE: this method requires the GIL inline c10::string_view THPUtils_unpackStringView(PyObject* obj) { if (PyBytes_Check(obj)) { size_t size = PyBytes_GET_SIZE(obj); return c10::string_view(PyBytes_AS_STRING(obj), size); } if (PyUnicode_Check(obj)) { // NOLINTNEXTLINE(cppcoreguidelines-init-variables) Py_ssize_t size; const char* data = PyUnicode_AsUTF8AndSize(obj, &size); if (!data) { throw std::runtime_error("error unpacking string as utf-8"); } return c10::string_view(data, (size_t)size); } throw std::runtime_error("unpackString: expected bytes or unicode object"); } inline PyObject* THPUtils_packString(const char* str) { return PyUnicode_FromString(str); } inline PyObject* THPUtils_packString(const std::string& str) { return PyUnicode_FromStringAndSize(str.c_str(), str.size()); } inline PyObject* THPUtils_internString(const std::string& str) { return PyUnicode_InternFromString(str.c_str()); } // Precondition: THPUtils_checkString(obj) must be true inline bool THPUtils_isInterned(PyObject* obj) { return PyUnicode_CHECK_INTERNED(obj); } // Precondition: THPUtils_checkString(obj) must be true inline void THPUtils_internStringInPlace(PyObject** obj) { PyUnicode_InternInPlace(obj); } /* * Reference: https://github.com/numpy/numpy/blob/f4c497c768e0646df740b647782df463825bfd27/numpy/core/src/common/get_attr_string.h#L42 * * Stripped down version of PyObject_GetAttrString, * avoids lookups for None, tuple, and List objects, * and doesn't create a PyErr since this code ignores it. * * This can be much faster then PyObject_GetAttrString where * exceptions are not used by caller. * * 'obj' is the object to search for attribute. * * 'name' is the attribute to search for. * * Returns a py::object wrapping the return value. If the attribute lookup failed * the value will be NULL. * */ // NOLINTNEXTLINE(clang-diagnostic-unused-function) static py::object PyObject_FastGetAttrString(PyObject *obj, const char *name) { PyTypeObject *tp = Py_TYPE(obj); PyObject *res = (PyObject *)nullptr; /* Attribute referenced by (char *)name */ if (tp->tp_getattr != nullptr) { // This is OK per https://bugs.python.org/issue39620 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) res = (*tp->tp_getattr)(obj, const_cast<char*>(name)); if (res == nullptr) { PyErr_Clear(); } } /* Attribute referenced by (PyObject *)name */ else if (tp->tp_getattro != nullptr) { auto w = py::reinterpret_steal<py::object>( THPUtils_internString(name)); if (w.ptr() == nullptr) { return py::object(); } res = (*tp->tp_getattro)(obj, w.ptr()); if (res == nullptr) { PyErr_Clear(); } } return py::reinterpret_steal<py::object>(res); }
Save
cmd:
run