comparison Python/import.c @ 46644:ea594306ea85

Issue #1950: Fixed misusage of PyUnicode_AsString().
author Alexandre Vassalotti <alexandre@peadrop.com>
date Sat, 03 May 2008 18:24:43 +0000
parents a51b329245b1
children 23ab586c427a
comparison
equal deleted inserted replaced
46639:bd91000c8e73 46644:ea594306ea85
2129 *p_buflen = 0; 2129 *p_buflen = 0;
2130 pkgname = PyDict_GetItem(globals, pkgstr); 2130 pkgname = PyDict_GetItem(globals, pkgstr);
2131 2131
2132 if ((pkgname != NULL) && (pkgname != Py_None)) { 2132 if ((pkgname != NULL) && (pkgname != Py_None)) {
2133 /* __package__ is set, so use it */ 2133 /* __package__ is set, so use it */
2134 char *pkgname_str;
2134 Py_ssize_t len; 2135 Py_ssize_t len;
2136
2135 if (!PyUnicode_Check(pkgname)) { 2137 if (!PyUnicode_Check(pkgname)) {
2136 PyErr_SetString(PyExc_ValueError, 2138 PyErr_SetString(PyExc_ValueError,
2137 "__package__ set to non-string"); 2139 "__package__ set to non-string");
2138 return NULL; 2140 return NULL;
2139 } 2141 }
2140 len = PyUnicode_GET_SIZE(pkgname); 2142 pkgname_str = PyUnicode_AsStringAndSize(pkgname, &len);
2141 if (len == 0) { 2143 if (len == 0) {
2142 if (level > 0) { 2144 if (level > 0) {
2143 PyErr_SetString(PyExc_ValueError, 2145 PyErr_SetString(PyExc_ValueError,
2144 "Attempted relative import in non-package"); 2146 "Attempted relative import in non-package");
2145 return NULL; 2147 return NULL;
2149 if (len > MAXPATHLEN) { 2151 if (len > MAXPATHLEN) {
2150 PyErr_SetString(PyExc_ValueError, 2152 PyErr_SetString(PyExc_ValueError,
2151 "Package name too long"); 2153 "Package name too long");
2152 return NULL; 2154 return NULL;
2153 } 2155 }
2154 strcpy(buf, PyUnicode_AsString(pkgname)); 2156 strcpy(buf, pkgname_str);
2155 } else { 2157 } else {
2156 /* __package__ not set, so figure it out and set it */ 2158 /* __package__ not set, so figure it out and set it */
2157 modname = PyDict_GetItem(globals, namestr); 2159 modname = PyDict_GetItem(globals, namestr);
2158 if (modname == NULL || !PyUnicode_Check(modname)) 2160 if (modname == NULL || !PyUnicode_Check(modname))
2159 return Py_None; 2161 return Py_None;
2160 2162
2161 modpath = PyDict_GetItem(globals, pathstr); 2163 modpath = PyDict_GetItem(globals, pathstr);
2162 if (modpath != NULL) { 2164 if (modpath != NULL) {
2163 /* __path__ is set, so modname is already the package name */ 2165 /* __path__ is set, so modname is already the package name */
2164 Py_ssize_t len = PyUnicode_GET_SIZE(modname); 2166 char *modname_str;
2167 Py_ssize_t len;
2165 int error; 2168 int error;
2169
2170 modname_str = PyUnicode_AsStringAndSize(modname, &len);
2166 if (len > MAXPATHLEN) { 2171 if (len > MAXPATHLEN) {
2167 PyErr_SetString(PyExc_ValueError, 2172 PyErr_SetString(PyExc_ValueError,
2168 "Module name too long"); 2173 "Module name too long");
2169 return NULL; 2174 return NULL;
2170 } 2175 }
2171 strcpy(buf, PyUnicode_AsString(modname)); 2176 strcpy(buf, modname_str);
2172 error = PyDict_SetItem(globals, pkgstr, modname); 2177 error = PyDict_SetItem(globals, pkgstr, modname);
2173 if (error) { 2178 if (error) {
2174 PyErr_SetString(PyExc_ValueError, 2179 PyErr_SetString(PyExc_ValueError,
2175 "Could not set __package__"); 2180 "Could not set __package__");
2176 return NULL; 2181 return NULL;