Left: | ||
Right: |
OLD | NEW |
---|---|
1 | 1 |
2 /* Module object implementation */ | 2 /* Module object implementation */ |
3 | 3 |
4 #include "Python.h" | 4 #include "Python.h" |
5 #include "structmember.h" | 5 #include "structmember.h" |
6 | 6 |
7 static Py_ssize_t max_module_number; | 7 static Py_ssize_t max_module_number; |
8 | 8 |
9 typedef struct { | 9 typedef struct { |
10 PyObject_HEAD | 10 PyObject_HEAD |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 Py_DECREF(nameobj); | 49 Py_DECREF(nameobj); |
50 PyObject_GC_Track(m); | 50 PyObject_GC_Track(m); |
51 return (PyObject *)m; | 51 return (PyObject *)m; |
52 | 52 |
53 fail: | 53 fail: |
54 Py_XDECREF(nameobj); | 54 Py_XDECREF(nameobj); |
55 Py_DECREF(m); | 55 Py_DECREF(m); |
56 return NULL; | 56 return NULL; |
57 } | 57 } |
58 | 58 |
59 static char api_version_warning[] = | |
60 "Python C API version mismatch for module %.100s:\ | |
61 This Python has API version %d, module %.100s has version %d."; | |
62 | |
63 PyObject * | 59 PyObject * |
64 PyModule_Create2(struct PyModuleDef* module, int module_api_version) | 60 PyModule_Create2(struct PyModuleDef* module, int module_api_version) |
65 { | 61 { |
66 PyObject *d, *v, *n; | 62 PyObject *d, *v, *n; |
67 PyMethodDef *ml; | 63 PyMethodDef *ml; |
68 const char* name; | 64 const char* name; |
69 PyModuleObject *m; | 65 PyModuleObject *m; |
70 if (!Py_IsInitialized()) | 66 if (!Py_IsInitialized()) |
71 Py_FatalError("Interpreter not initialized (version mismatch?)"); | 67 Py_FatalError("Interpreter not initialized (version mismatch?)"); |
72 if (PyType_Ready(&moduledef_type) < 0) | 68 if (PyType_Ready(&moduledef_type) < 0) |
73 return NULL; | 69 return NULL; |
74 if (module->m_base.m_index == 0) { | 70 if (module->m_base.m_index == 0) { |
75 max_module_number++; | 71 max_module_number++; |
76 Py_REFCNT(module) = 1; | 72 Py_REFCNT(module) = 1; |
77 Py_TYPE(module) = &moduledef_type; | 73 Py_TYPE(module) = &moduledef_type; |
78 module->m_base.m_index = max_module_number; | 74 module->m_base.m_index = max_module_number; |
79 } | 75 } |
80 name = module->m_name; | 76 name = module->m_name; |
81 if (module_api_version != PYTHON_API_VERSION) { | 77 if (module_api_version != PYTHON_API_VERSION) { |
82 char message[512]; | 78 int err; |
83 PyOS_snprintf(message, sizeof(message), | 79 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Antoine Pitrou
2010/07/30 13:35:47
Is there any point in making this change?
haypo
2010/07/30 20:47:04
I prefer PyErr_WarnFormat() because the caller doe
| |
84 api_version_warning, name, | 80 "Python C API version mismatch for module %.100s: " |
85 PYTHON_API_VERSION, name, | 81 "This Python has API version %d, module %.100s has version %d.", |
86 module_api_version); | 82 name, |
87 if (PyErr_WarnEx(PyExc_RuntimeWarning, message, 1)) | 83 PYTHON_API_VERSION, name, module_api_version); |
84 if (err) | |
88 return NULL; | 85 return NULL; |
89 } | 86 } |
90 /* Make sure name is fully qualified. | 87 /* Make sure name is fully qualified. |
91 | 88 |
92 This is a bit of a hack: when the shared library is loaded, | 89 This is a bit of a hack: when the shared library is loaded, |
93 the module name is "package.module", but the module calls | 90 the module name is "package.module", but the module calls |
94 PyModule_Create*() with just "module" for the name. The shared | 91 PyModule_Create*() with just "module" for the name. The shared |
95 library loader squirrels away the true name of the module in | 92 library loader squirrels away the true name of the module in |
96 _Py_PackageContext, and PyModule_Create*() will substitute this | 93 _Py_PackageContext, and PyModule_Create*() will substitute this |
97 (if the name actually matches). | 94 (if the name actually matches). |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 if (d == NULL || | 181 if (d == NULL || |
185 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL || | 182 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL || |
186 !PyUnicode_Check(nameobj)) | 183 !PyUnicode_Check(nameobj)) |
187 { | 184 { |
188 PyErr_SetString(PyExc_SystemError, "nameless module"); | 185 PyErr_SetString(PyExc_SystemError, "nameless module"); |
189 return NULL; | 186 return NULL; |
190 } | 187 } |
191 return _PyUnicode_AsString(nameobj); | 188 return _PyUnicode_AsString(nameobj); |
192 } | 189 } |
193 | 190 |
194 static PyObject* | 191 PyObject* |
195 module_getfilename(PyObject *m) | 192 PyModule_GetFilenameObject(PyObject *m) |
amaury
2010/07/31 11:33:01
This function should be documented; especially sin
| |
196 { | 193 { |
197 PyObject *d; | 194 PyObject *d; |
198 PyObject *fileobj; | 195 PyObject *fileobj; |
199 if (!PyModule_Check(m)) { | 196 if (!PyModule_Check(m)) { |
200 PyErr_BadArgument(); | 197 PyErr_BadArgument(); |
201 return NULL; | 198 return NULL; |
202 } | 199 } |
203 d = ((PyModuleObject *)m)->md_dict; | 200 d = ((PyModuleObject *)m)->md_dict; |
204 if (d == NULL || | 201 if (d == NULL || |
205 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL || | 202 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL || |
206 !PyUnicode_Check(fileobj)) | 203 !PyUnicode_Check(fileobj)) |
207 { | 204 { |
208 PyErr_SetString(PyExc_SystemError, "module filename missing"); | 205 PyErr_SetString(PyExc_SystemError, "module filename missing"); |
209 return NULL; | 206 return NULL; |
210 } | 207 } |
211 return fileobj; | 208 return fileobj; |
212 } | 209 } |
213 | 210 |
214 const char * | 211 const char * |
215 PyModule_GetFilename(PyObject *m) | 212 PyModule_GetFilename(PyObject *m) |
216 { | 213 { |
217 PyObject *fileobj; | 214 PyObject *fileobj; |
218 fileobj = module_getfilename(m); | 215 fileobj = PyModule_GetFilenameObject(m); |
219 if (fileobj == NULL) | 216 if (fileobj == NULL) |
220 return NULL; | 217 return NULL; |
221 return _PyUnicode_AsString(fileobj); | 218 return _PyUnicode_AsString(fileobj); |
222 } | 219 } |
223 | 220 |
224 PyModuleDef* | 221 PyModuleDef* |
225 PyModule_GetDef(PyObject* m) | 222 PyModule_GetDef(PyObject* m) |
226 { | 223 { |
227 if (!PyModule_Check(m)) { | 224 if (!PyModule_Check(m)) { |
228 PyErr_BadArgument(); | 225 PyErr_BadArgument(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 module_repr(PyModuleObject *m) | 346 module_repr(PyModuleObject *m) |
350 { | 347 { |
351 const char *name; | 348 const char *name; |
352 PyObject *filename; | 349 PyObject *filename; |
353 | 350 |
354 name = PyModule_GetName((PyObject *)m); | 351 name = PyModule_GetName((PyObject *)m); |
355 if (name == NULL) { | 352 if (name == NULL) { |
356 PyErr_Clear(); | 353 PyErr_Clear(); |
357 name = "?"; | 354 name = "?"; |
358 } | 355 } |
359 filename = module_getfilename((PyObject *)m); | 356 filename = PyModule_GetFilenameObject((PyObject *)m); |
360 if (filename == NULL) { | 357 if (filename == NULL) { |
361 PyErr_Clear(); | 358 PyErr_Clear(); |
362 return PyUnicode_FromFormat("<module '%s' (built-in)>", name); | 359 return PyUnicode_FromFormat("<module '%s' (built-in)>", name); |
363 } | 360 } |
364 return PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename); | 361 return PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename); |
365 } | 362 } |
366 | 363 |
367 static int | 364 static int |
368 module_traverse(PyModuleObject *m, visitproc visit, void *arg) | 365 module_traverse(PyModuleObject *m, visitproc visit, void *arg) |
369 { | 366 { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
430 0, /* tp_base */ | 427 0, /* tp_base */ |
431 0, /* tp_dict */ | 428 0, /* tp_dict */ |
432 0, /* tp_descr_get */ | 429 0, /* tp_descr_get */ |
433 0, /* tp_descr_set */ | 430 0, /* tp_descr_set */ |
434 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ | 431 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ |
435 (initproc)module_init, /* tp_init */ | 432 (initproc)module_init, /* tp_init */ |
436 PyType_GenericAlloc, /* tp_alloc */ | 433 PyType_GenericAlloc, /* tp_alloc */ |
437 PyType_GenericNew, /* tp_new */ | 434 PyType_GenericNew, /* tp_new */ |
438 PyObject_GC_Del, /* tp_free */ | 435 PyObject_GC_Del, /* tp_free */ |
439 }; | 436 }; |
OLD | NEW |