mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	Removed fatal errors from Py_Initmodule4() (and thus from
Py_Initmodule(), which is a macro wrapper around it). The return value is now a NULL pointer if the initialization failed. This may make old modules fail with a SEGFAULT, since they don't expect this kind of failure. That's OK, since (a) it "never" happens, and (b) they would fail with a fatal error otherwise, anyway. Tons of extension modules should now check the return value of Py_Initmodule*() -- that's on my TODO list.
This commit is contained in:
		
							parent
							
								
									aee094cc60
								
							
						
					
					
						commit
						40b33c648a
					
				
					 1 changed files with 12 additions and 12 deletions
				
			
		|  | @ -39,13 +39,17 @@ typedef extended va_double; | |||
| typedef double va_double; | ||||
| #endif | ||||
| 
 | ||||
| /* initmodule4() parameters:
 | ||||
| /* Py_InitModule4() parameters:
 | ||||
|    - name is the module name | ||||
|    - methods is the list of top-level functions | ||||
|    - doc is the documentation string | ||||
|    - passthrough is passed as self to functions defined in the module | ||||
|    - api_version is the value of PYTHON_API_VERSION at the time the | ||||
|      module was compiled | ||||
| 
 | ||||
|    Return value is a borrowed reference to the module object; or NULL | ||||
|    if an error occurred (in Python 1.4 and before, errors were fatal). | ||||
|    Errors may still leak memory. | ||||
| */ | ||||
| 
 | ||||
| static char api_version_warning[] = | ||||
|  | @ -65,25 +69,21 @@ Py_InitModule4(name, methods, doc, passthrough, module_api_version) | |||
| 	if (module_api_version != PYTHON_API_VERSION) | ||||
| 		fprintf(stderr, api_version_warning, | ||||
| 			name, PYTHON_API_VERSION, name, module_api_version); | ||||
| 	if ((m = PyImport_AddModule(name)) == NULL) { | ||||
| 		fprintf(stderr, "initializing module: %s\n", name); | ||||
| 		Py_FatalError("can't create a module"); | ||||
| 	} | ||||
| 	if ((m = PyImport_AddModule(name)) == NULL) | ||||
| 		return NULL; | ||||
| 	d = PyModule_GetDict(m); | ||||
| 	for (ml = methods; ml->ml_name != NULL; ml++) { | ||||
| 		v = PyCFunction_New(ml, passthrough); | ||||
| 		if (v == NULL || | ||||
| 		    PyDict_SetItemString(d, ml->ml_name, v) != 0) | ||||
| 		{ | ||||
| 			fprintf(stderr, "initializing module: %s\n", name); | ||||
| 			Py_FatalError("can't initialize module"); | ||||
| 		} | ||||
| 		if (v == NULL) | ||||
| 			return NULL; | ||||
| 		if (PyDict_SetItemString(d, ml->ml_name, v) != 0) | ||||
| 			return NULL; | ||||
| 		Py_DECREF(v); | ||||
| 	} | ||||
| 	if (doc != NULL) { | ||||
| 		v = PyString_FromString(doc); | ||||
| 		if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) | ||||
| 			Py_FatalError("can't add doc string"); | ||||
| 			return NULL; | ||||
| 		Py_DECREF(v); | ||||
| 	} | ||||
| 	return m; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Guido van Rossum
						Guido van Rossum