77#include < libshm.h>
88#include < TH/TH.h>
99
10+ #include " torch/csrc/utils/python_strings.h"
11+
1012#ifdef WITH_CUDNN
1113#include " cudnn/Module.h"
1214#endif
@@ -73,13 +75,9 @@ static PyObject * THPModule_initNames(PyObject *self, PyObject *arg)
7375
7476 THPObjectPtr module_name = PyObject_GetAttrString (obj, " __module__" );
7577 if (!module_name) return NULL ;
76- #if PY_MAJOR_VERSION == 2
77- THPUtils_assert (PyString_Check (module_name.get ()), " expected __module__ to be a string" );
78- std::string name = PyString_AS_STRING (module_name.get ());
79- #else
80- THPUtils_assert (PyUnicode_Check (module_name.get ()), " expected __module__ to be a string" );
81- std::string name = PyUnicode_AsUTF8 (module_name.get ());
82- #endif
78+ THPUtils_assert (THPUtils_checkString (module_name.get ()),
79+ " expected __module__ to be a string" );
80+ std::string name = THPUtils_unpackString (module_name.get ());
8381 names.push_back (name + " ." + type->tp_name );
8482 type->tp_name = names.back ().c_str ();
8583 }
@@ -89,15 +87,13 @@ static PyObject * THPModule_initNames(PyObject *self, PyObject *arg)
8987static bool THPModule_assignStateless (PyObject *self)
9088{
9189#define INIT_STATELESS (type ) \
92- stateless = PyObject_Call ((PyObject*)&TH_CONCAT_2 (type, TensorStatelessType), arg , NULL ); \
90+ stateless = PyObject_CallFunctionObjArgs ((PyObject*)&TH_CONCAT_2 (type, TensorStatelessType), NULL ); \
9391 if (!stateless) { \
94- THPUtils_setError (" stateless method initialization error" ); \
9592 return false ; \
9693 } \
9794 if (PyObject_SetAttrString (TH_CONCAT_3 (THP,type,TensorClass), THP_STATELESS_ATTRIBUTE_NAME, stateless) == -1 ) { \
98- THPUtils_setError ( " stateless method initialization error (on assignment) " ); \
95+ return false ; \
9996 }
100- PyObject *arg = PyTuple_New (0 );
10197 PyObject *stateless;
10298 INIT_STATELESS (Double);
10399 INIT_STATELESS (Float);
@@ -107,23 +103,25 @@ static bool THPModule_assignStateless(PyObject *self)
107103 INIT_STATELESS (Short);
108104 INIT_STATELESS (Char);
109105 INIT_STATELESS (Byte);
110- Py_DECREF (arg);
111106 return true ;
112107#undef INIT_STATELESS
113108}
114109//
115110// Callback for python part. Used for additional initialization of python classes
116111static PyObject * THPModule_initExtension (PyObject *self, PyObject *shm_manager_path)
117112{
118- if (!THPUtils_checkBytes (shm_manager_path)) {
113+ HANDLE_TH_ERRORS
114+ if (!THPUtils_checkString (shm_manager_path)) {
119115 THPUtils_setError (" initialization error - expected bytes/string object as shm_manager_path!" );
120116 return NULL ;
121117 }
122- libshm_init (THPUtils_bytesAsString (shm_manager_path));
118+ std::string path = THPUtils_unpackString (shm_manager_path);
119+ libshm_init (path.c_str ());
123120 if (!THPModule_loadClasses (self)) return NULL ;
124121 if (!THPModule_assignStateless (self)) return NULL ;
125122 if (!THPAutograd_initFunctions (self)) return NULL ;
126- return PyBool_FromLong (true );
123+ Py_RETURN_NONE;
124+ END_HANDLE_TH_ERRORS
127125}
128126
129127static PyObject * THPModule_getNumThreads (PyObject *module )
@@ -429,22 +427,6 @@ PyObject *THPModule_safeCall(PyObject *_unused, PyObject *args, PyObject *kwargs
429427 return result;
430428}
431429
432- static std::string parseString (PyObject *obj)
433- {
434- if (PyBytes_Check (obj)) {
435- return std::string (PyBytes_AS_STRING (obj));
436- #if PY_MAJOR_VERSION == 3
437- } else if (PyUnicode_Check (obj)) {
438- return std::string (PyUnicode_AsUTF8 (obj));
439- #else
440- } else if (PyUnicode_Check (obj)) {
441- THPObjectPtr utf8 = PyUnicode_AsUTF8String (obj);
442- return std::string (PyBytes_AS_STRING (utf8.get ()));
443- #endif
444- }
445- return " <invalid string>" ;
446- }
447-
448430PyObject *THPModule_addDocStr (PyObject *_unused, PyObject *args)
449431{
450432 // adds a __doc__ string to a function, similar to numpy's arr_add_docstring
@@ -455,8 +437,11 @@ PyObject *THPModule_addDocStr(PyObject *_unused, PyObject *args)
455437 return NULL ;
456438 }
457439
458- all_docs.push_back (parseString (doc_obj));
459- const char * doc_str = all_docs.back ().c_str ();
440+ const char * doc_str = " <invalid string>" ;
441+ if (THPUtils_checkString (doc_obj)) {
442+ all_docs.push_back (THPUtils_unpackString (doc_obj));
443+ doc_str = all_docs.back ().c_str ();
444+ }
460445
461446 if (Py_TYPE (obj) == &PyCFunction_Type) {
462447 PyCFunctionObject* f = (PyCFunctionObject *)obj;
@@ -499,7 +484,6 @@ extern PyObject * THCPModule_seedAll(PyObject *_unused);
499484extern PyObject * THCPModule_initialSeed (PyObject *_unused);
500485extern PyObject * THCPModule_cudaHostAllocator (PyObject *_unused);
501486extern PyObject * THCPModule_cudaSynchronize (PyObject *_unused);
502- extern PyObject * THCPModule_getLibPath (PyObject *_unused);
503487extern PyObject * THCPModule_cudaSleep (PyObject *_unused, PyObject *cycles);
504488extern PyObject * THCPModule_cudaLockMutex (PyObject *module );
505489extern PyObject * THCPModule_cudaUnlockMutex (PyObject *module );
@@ -532,7 +516,6 @@ static PyMethodDef TorchMethods[] = {
532516 {" _cuda_initialSeed" , (PyCFunction)THCPModule_initialSeed, METH_NOARGS, NULL },
533517 {" _cuda_cudaHostAllocator" , (PyCFunction)THCPModule_cudaHostAllocator, METH_NOARGS, NULL },
534518 {" _cuda_synchronize" , (PyCFunction)THCPModule_cudaSynchronize, METH_NOARGS, NULL },
535- {" _cuda_getLibPath" , (PyCFunction)THCPModule_getLibPath, METH_NOARGS, NULL },
536519 {" _cuda_sleep" , (PyCFunction)THCPModule_cudaSleep, METH_O, NULL },
537520 {" _cuda_sparse_init" , (PyCFunction)THCSPModule_initExtension, METH_NOARGS, NULL },
538521 {" _cuda_lock_mutex" , (PyCFunction)THCPModule_cudaLockMutex, METH_NOARGS, NULL },
0 commit comments