mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	Added execfile().
This commit is contained in:
		
							parent
							
								
									465c499544
								
							
						
					
					
						commit
						0f61f8a4bd
					
				
					 1 changed files with 37 additions and 0 deletions
				
			
		|  | @ -186,6 +186,42 @@ builtin_exec(self, v) | |||
| 	return exec_eval(v, file_input); | ||||
| } | ||||
| 
 | ||||
| static object * | ||||
| builtin_execfile(self, v) | ||||
| 	object *self; | ||||
| 	object *v; | ||||
| { | ||||
| 	object *str = NULL, *globals = NULL, *locals = NULL, *w; | ||||
| 	FILE* fp; | ||||
| 	int n; | ||||
| 	if (v != NULL) { | ||||
| 		if (is_stringobject(v)) | ||||
| 			str = v; | ||||
| 		else if (is_tupleobject(v) && | ||||
| 				((n = gettuplesize(v)) == 2 || n == 3)) { | ||||
| 			str = gettupleitem(v, 0); | ||||
| 			globals = gettupleitem(v, 1); | ||||
| 			if (n == 3) | ||||
| 				locals = gettupleitem(v, 2); | ||||
| 		} | ||||
| 	} | ||||
| 	if (str == NULL || !is_stringobject(str) || | ||||
| 			globals != NULL && !is_dictobject(globals) || | ||||
| 			locals != NULL && !is_dictobject(locals)) { | ||||
| 		err_setstr(TypeError, | ||||
| 		    "execfile arguments must be filename[,dict[,dict]]"); | ||||
| 		return NULL; | ||||
| 	} | ||||
| 	fp = fopen(getstringvalue(str), "r"); | ||||
| 	if (fp == NULL) { | ||||
| 		err_setstr(IOError, "execfile cannot open the file argument"); | ||||
| 		return NULL; | ||||
| 	} | ||||
| 	w = run_file(fp, getstringvalue(str), file_input, globals, locals); | ||||
| 	fclose(fp); | ||||
| 	return w; | ||||
| } | ||||
| 
 | ||||
| static object * | ||||
| builtin_float(self, v) | ||||
| 	object *self; | ||||
|  | @ -603,6 +639,7 @@ static struct methodlist builtin_methods[] = { | |||
| 	{"divmod",	builtin_divmod}, | ||||
| 	{"eval",	builtin_eval}, | ||||
| 	{"exec",	builtin_exec}, | ||||
| 	{"execfile",	builtin_execfile}, | ||||
| 	{"float",	builtin_float}, | ||||
| 	{"getattr",	builtin_getattr}, | ||||
| 	{"hex",		builtin_hex}, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Guido van Rossum
						Guido van Rossum