mirror of
				https://github.com/python/cpython.git
				synced 2025-11-01 06:01:29 +00:00 
			
		
		
		
	Rip out the file object's implementation.
Fixed test_import.py while I was at it. However, there's still a problem in import.c -- get_file() can leak a FILE struct (not a file descriptor though). I'm not sure how to fix this; closing the FILE* closes the file descriptor, and that's the wrong thing to do when there's still a Python file object keeping the file descriptor open. I also would rather not mess with dup(), as it won't port to Windows.
This commit is contained in:
		
							parent
							
								
									2d5c219fe0
								
							
						
					
					
						commit
						da5b8f2d28
					
				
					 14 changed files with 106 additions and 2537 deletions
				
			
		|  | @ -1,5 +1,4 @@ | |||
| 
 | ||||
| /* File object interface */ | ||||
| /* File object interface (what's left of it -- see io.py) */ | ||||
| 
 | ||||
| #ifndef Py_FILEOBJECT_H | ||||
| #define Py_FILEOBJECT_H | ||||
|  | @ -7,59 +6,20 @@ | |||
| extern "C" { | ||||
| #endif | ||||
| 
 | ||||
| typedef struct { | ||||
| 	PyObject_HEAD | ||||
| 	FILE *f_fp; | ||||
| 	PyObject *f_name; | ||||
| 	PyObject *f_mode; | ||||
| 	int (*f_close)(FILE *); | ||||
| 	int f_binary;		/* Flag which indicates whether the file is 
 | ||||
| 				   open in binary (1) or text (0) mode */ | ||||
| 	char* f_buf;		/* Allocated readahead buffer */ | ||||
| 	char* f_bufend;		/* Points after last occupied position */ | ||||
| 	char* f_bufptr;		/* Current buffer position */ | ||||
| 	char *f_setbuf;		/* Buffer for setbuf(3) and setvbuf(3) */ | ||||
| 	int f_univ_newline;	/* Handle any newline convention */ | ||||
| 	int f_newlinetypes;	/* Types of newlines seen */ | ||||
| 	int f_skipnextlf;	/* Skip next \n */ | ||||
| 	PyObject *f_encoding; | ||||
| 	PyObject *weakreflist; /* List of weak references */ | ||||
| } PyFileObject; | ||||
| #define PY_STDIOTEXTMODE "b" | ||||
| 
 | ||||
| PyAPI_DATA(PyTypeObject) PyFile_Type; | ||||
| 
 | ||||
| #define PyFile_Check(op) PyObject_TypeCheck(op, &PyFile_Type) | ||||
| #define PyFile_CheckExact(op) ((op)->ob_type == &PyFile_Type) | ||||
| 
 | ||||
| PyAPI_FUNC(PyObject *) PyFile_FromString(char *, char *); | ||||
| PyAPI_FUNC(void) PyFile_SetBufSize(PyObject *, int); | ||||
| PyAPI_FUNC(int) PyFile_SetEncoding(PyObject *, const char *); | ||||
| PyAPI_FUNC(PyObject *) PyFile_FromFile(FILE *, char *, char *, | ||||
|                                              int (*)(FILE *)); | ||||
| PyAPI_FUNC(FILE *) PyFile_AsFile(PyObject *); | ||||
| PyAPI_FUNC(PyObject *) PyFile_Name(PyObject *); | ||||
| PyAPI_FUNC(PyObject *) PyFile_FromFile(FILE *, char *, char *, int (*)(FILE*)); | ||||
| PyAPI_FUNC(PyObject *) PyFile_GetLine(PyObject *, int); | ||||
| PyAPI_FUNC(int) PyFile_WriteObject(PyObject *, PyObject *, int); | ||||
| PyAPI_FUNC(int) PyFile_WriteString(const char *, PyObject *); | ||||
| PyAPI_FUNC(int) PyObject_AsFileDescriptor(PyObject *); | ||||
| PyAPI_FUNC(char *) Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *); | ||||
| 
 | ||||
| /* The default encoding used by the platform file system APIs
 | ||||
|    If non-NULL, this is different than the default encoding for strings | ||||
| */ | ||||
| PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding; | ||||
| 
 | ||||
| /* Routines to replace fread() and fgets() which accept any of \r, \n
 | ||||
|    or \r\n as line terminators. | ||||
| */ | ||||
| #define PY_STDIOTEXTMODE "b" | ||||
| char *Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *); | ||||
| size_t Py_UniversalNewlineFread(char *, size_t, FILE *, PyObject *); | ||||
| 
 | ||||
| /* A routine to do sanity checking on the file mode string.  returns
 | ||||
|    non-zero on if an exception occurred | ||||
| */ | ||||
| int _PyFile_SanitizeMode(char *mode); | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -9,7 +9,6 @@ extern "C" { | |||
| 
 | ||||
| PyAPI_FUNC(PyObject *) PySys_GetObject(char *); | ||||
| PyAPI_FUNC(int) PySys_SetObject(char *, PyObject *); | ||||
| PyAPI_FUNC(FILE *) PySys_GetFile(char *, FILE *); | ||||
| PyAPI_FUNC(void) PySys_SetArgv(int, char **); | ||||
| PyAPI_FUNC(void) PySys_SetPath(char *); | ||||
| 
 | ||||
|  |  | |||
|  | @ -1287,12 +1287,13 @@ PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args) | |||
|    | ||||
|   if (!PyArg_ParseTuple(args, "O;fileobj", &temp)) | ||||
|     return NULL; | ||||
|   if (!PyFile_Check(temp)) { | ||||
|   PyErr_SetString(PyExc_TypeError, "argument must be a file object"); | ||||
|   return NULL; | ||||
|   } | ||||
| 
 | ||||
| #if 0 | ||||
|   return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)),  | ||||
| 			  "putwin"); | ||||
| #endif | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
|  | @ -1748,11 +1749,10 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp) | |||
| 
 | ||||
|   PyCursesInitialised | ||||
| 
 | ||||
|   if (!PyFile_Check(temp)) { | ||||
|   PyErr_SetString(PyExc_TypeError, "argument must be a file object"); | ||||
|   return NULL; | ||||
|   } | ||||
| 
 | ||||
| #if 0 | ||||
|   win = getwin(PyFile_AsFile(temp)); | ||||
| 
 | ||||
|   if (win == NULL) { | ||||
|  | @ -1761,6 +1761,7 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp) | |||
|   } | ||||
| 
 | ||||
|   return PyCursesWindow_New(win); | ||||
| #endif | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
|  |  | |||
|  | @ -1075,6 +1075,7 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args) | |||
| 		offset -= self->pos; | ||||
| 	} else { | ||||
| 		/* we cannot move back, so rewind the stream */ | ||||
| 		FILE *fp = NULL; /* XXX temporary!!! */ | ||||
| 		BZ2_bzReadClose(&bzerror, self->fp); | ||||
| 		if (bzerror != BZ_OK) { | ||||
| 			Util_CatchBZ2Error(bzerror); | ||||
|  | @ -1086,7 +1087,7 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args) | |||
| 		Py_DECREF(ret); | ||||
| 		ret = NULL; | ||||
| 		self->pos = 0; | ||||
| 		self->fp = BZ2_bzReadOpen(&bzerror, PyFile_AsFile(self->file), | ||||
| 		self->fp = BZ2_bzReadOpen(&bzerror, fp, | ||||
| 					  0, 0, NULL, 0); | ||||
| 		if (bzerror != BZ_OK) { | ||||
| 			Util_CatchBZ2Error(bzerror); | ||||
|  | @ -1286,6 +1287,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) | |||
| { | ||||
| 	static char *kwlist[] = {"filename", "mode", "buffering", | ||||
|                                        "compresslevel", 0}; | ||||
| 	FILE *fp = NULL; /* XXX temporary!!! */ | ||||
| 	PyObject *name; | ||||
| 	char *mode = "r"; | ||||
| 	int buffering = -1; | ||||
|  | @ -1347,8 +1349,8 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) | |||
| 
 | ||||
| 	mode = (mode_char == 'r') ? "rb" : "wb"; | ||||
| 
 | ||||
| 	self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)", | ||||
| 					   name, mode, buffering); | ||||
| 	self->file = NULL; /* XXX io.open(name, mode, buffering); */ | ||||
| 	PyErr_SetString(PyExc_RuntimeError, "can't open bz2 files yet"); | ||||
| 	if (self->file == NULL) | ||||
| 		return -1; | ||||
| 
 | ||||
|  | @ -1365,11 +1367,11 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) | |||
| 
 | ||||
| 	if (mode_char == 'r') | ||||
| 		self->fp = BZ2_bzReadOpen(&bzerror, | ||||
| 					  PyFile_AsFile(self->file), | ||||
| 					  fp, | ||||
| 					  0, 0, NULL, 0); | ||||
| 	else | ||||
| 		self->fp = BZ2_bzWriteOpen(&bzerror, | ||||
| 					   PyFile_AsFile(self->file), | ||||
| 					   fp, | ||||
| 					   compresslevel, 0, 0); | ||||
| 
 | ||||
| 	if (bzerror != BZ_OK) { | ||||
|  |  | |||
|  | @ -417,31 +417,6 @@ cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) | |||
| 	return NULL; | ||||
| } | ||||
| 
 | ||||
| static int | ||||
| write_file(Picklerobject *self, const char *s, Py_ssize_t  n) | ||||
| { | ||||
| 	size_t nbyteswritten; | ||||
| 
 | ||||
| 	if (s == NULL) { | ||||
| 		return 0; | ||||
| 	} | ||||
| 
 | ||||
| 	if (n > INT_MAX) { | ||||
| 		/* String too large */ | ||||
| 		return -1; | ||||
| 	} | ||||
| 
 | ||||
| 	Py_BEGIN_ALLOW_THREADS | ||||
| 	nbyteswritten = fwrite(s, sizeof(char), n, self->fp); | ||||
| 	Py_END_ALLOW_THREADS | ||||
| 	if (nbyteswritten != (size_t)n) { | ||||
| 		PyErr_SetFromErrno(PyExc_IOError); | ||||
| 		return -1; | ||||
| 	} | ||||
| 
 | ||||
| 	return (int)n; | ||||
| } | ||||
| 
 | ||||
| static int | ||||
| write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t  n) | ||||
| { | ||||
|  | @ -516,92 +491,6 @@ write_other(Picklerobject *self, const char *s, Py_ssize_t  _n) | |||
| } | ||||
| 
 | ||||
| 
 | ||||
| static Py_ssize_t | ||||
| read_file(Unpicklerobject *self, char **s, Py_ssize_t n) | ||||
| { | ||||
| 	size_t nbytesread; | ||||
| 
 | ||||
| 	if (self->buf_size == 0) { | ||||
| 		int size; | ||||
| 
 | ||||
| 		size = ((n < 32) ? 32 : n); | ||||
| 		if (!( self->buf = (char *)malloc(size))) { | ||||
| 			PyErr_NoMemory(); | ||||
| 			return -1; | ||||
| 		} | ||||
| 
 | ||||
| 		self->buf_size = size; | ||||
| 	} | ||||
| 	else if (n > self->buf_size) { | ||||
| 		char *newbuf = (char *)realloc(self->buf, n); | ||||
| 		if (!newbuf)  { | ||||
| 			PyErr_NoMemory(); | ||||
| 			return -1; | ||||
| 		} | ||||
| 		self->buf = newbuf; | ||||
| 		self->buf_size = n; | ||||
| 	} | ||||
| 
 | ||||
| 	Py_BEGIN_ALLOW_THREADS | ||||
| 	nbytesread = fread(self->buf, sizeof(char), n, self->fp); | ||||
| 	Py_END_ALLOW_THREADS | ||||
| 	if (nbytesread != (size_t)n) { | ||||
| 		if (feof(self->fp)) { | ||||
| 			PyErr_SetNone(PyExc_EOFError); | ||||
| 			return -1; | ||||
| 		} | ||||
| 
 | ||||
| 		PyErr_SetFromErrno(PyExc_IOError); | ||||
| 		return -1; | ||||
| 	} | ||||
| 
 | ||||
| 	*s = self->buf; | ||||
| 
 | ||||
| 	return n; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static Py_ssize_t | ||||
| readline_file(Unpicklerobject *self, char **s) | ||||
| { | ||||
| 	int i; | ||||
| 
 | ||||
| 	if (self->buf_size == 0) { | ||||
| 		if (!( self->buf = (char *)malloc(40))) { | ||||
| 			PyErr_NoMemory(); | ||||
| 			return -1; | ||||
| 		} | ||||
| 		self->buf_size = 40; | ||||
| 	} | ||||
| 
 | ||||
| 	i = 0; | ||||
| 	while (1) { | ||||
| 		int bigger; | ||||
| 		char *newbuf; | ||||
| 		for (; i < (self->buf_size - 1); i++) { | ||||
| 			if (feof(self->fp) || | ||||
| 			    (self->buf[i] = getc(self->fp)) == '\n') { | ||||
| 				self->buf[i + 1] = '\0'; | ||||
| 				*s = self->buf; | ||||
| 				return i + 1; | ||||
| 			} | ||||
| 		} | ||||
| 		bigger = self->buf_size << 1; | ||||
| 		if (bigger <= 0) {	/* overflow */ | ||||
| 			PyErr_NoMemory(); | ||||
| 			return -1; | ||||
| 		} | ||||
| 		newbuf = (char *)realloc(self->buf, bigger); | ||||
| 		if (!newbuf)  { | ||||
| 			PyErr_NoMemory(); | ||||
| 			return -1; | ||||
| 		} | ||||
| 		self->buf = newbuf; | ||||
| 		self->buf_size = bigger; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static Py_ssize_t | ||||
| read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t  n) | ||||
| { | ||||
|  | @ -2665,16 +2554,7 @@ newPicklerobject(PyObject *file, int proto) | |||
| 	if (!( self->memo = PyDict_New())) | ||||
| 		goto err; | ||||
| 
 | ||||
| 	if (PyFile_Check(file)) { | ||||
| 		self->fp = PyFile_AsFile(file); | ||||
| 		if (self->fp == NULL) { | ||||
| 			PyErr_SetString(PyExc_ValueError, | ||||
| 					"I/O operation on closed file"); | ||||
| 			goto err; | ||||
| 		} | ||||
| 		self->write_func = write_file; | ||||
| 	} | ||||
| 	else if (PycStringIO_OutputCheck(file)) { | ||||
|         if (PycStringIO_OutputCheck(file)) { | ||||
| 		self->write_func = write_cStringIO; | ||||
| 	} | ||||
| 	else if (file == Py_None) { | ||||
|  | @ -4988,17 +4868,7 @@ newUnpicklerobject(PyObject *f) | |||
| 	self->file = f; | ||||
| 
 | ||||
| 	/* Set read, readline based on type of f */ | ||||
| 	if (PyFile_Check(f)) { | ||||
| 		self->fp = PyFile_AsFile(f); | ||||
| 		if (self->fp == NULL) { | ||||
| 			PyErr_SetString(PyExc_ValueError, | ||||
| 					"I/O operation on closed file"); | ||||
| 			goto err; | ||||
| 		} | ||||
| 		self->read_func = read_file; | ||||
| 		self->readline_func = readline_file; | ||||
| 	} | ||||
| 	else if (PycStringIO_InputCheck(f)) { | ||||
| 	if (PycStringIO_InputCheck(f)) { | ||||
| 		self->fp = NULL; | ||||
| 		self->read_func = read_cStringIO; | ||||
| 		self->readline_func = readline_cStringIO; | ||||
|  |  | |||
|  | @ -956,10 +956,7 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f) | |||
|     FILE *fp; | ||||
|     PyObject *readmethod = NULL; | ||||
| 
 | ||||
|     if (PyFile_Check(f)) { | ||||
|         fp = PyFile_AsFile(f); | ||||
|     } | ||||
|     else { | ||||
|     { | ||||
|         fp = NULL; | ||||
|         readmethod = PyObject_GetAttrString(f, "read"); | ||||
|         if (readmethod == NULL) { | ||||
|  |  | |||
							
								
								
									
										2137
									
								
								Objects/fileobject.c
									
										
									
									
									
								
							
							
						
						
									
										2137
									
								
								Objects/fileobject.c
									
										
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -669,66 +669,6 @@ PyTokenizer_Free(struct tok_state *tok) | |||
| 	PyMem_FREE(tok); | ||||
| } | ||||
| 
 | ||||
| #if !defined(PGEN) | ||||
| static int | ||||
| tok_stdin_decode(struct tok_state *tok, char **inp) | ||||
| { | ||||
| 	PyObject *enc, *sysstdin, *decoded, *utf8; | ||||
| 	const char *encoding; | ||||
| 	char *converted; | ||||
| 
 | ||||
| 	if (PySys_GetFile((char *)"stdin", NULL) != stdin) | ||||
| 		return 0; | ||||
| 	sysstdin = PySys_GetObject("stdin"); | ||||
| 	if (sysstdin == NULL || !PyFile_Check(sysstdin)) | ||||
| 		return 0; | ||||
| 
 | ||||
| 	enc = ((PyFileObject *)sysstdin)->f_encoding; | ||||
| 	if (enc == NULL || !PyString_Check(enc)) | ||||
| 		return 0; | ||||
| 	Py_INCREF(enc); | ||||
| 
 | ||||
| 	encoding = PyString_AsString(enc); | ||||
| 	decoded = PyUnicode_Decode(*inp, strlen(*inp), encoding, NULL); | ||||
| 	if (decoded == NULL) | ||||
| 		goto error_clear; | ||||
| 
 | ||||
| 	utf8 = PyUnicode_AsEncodedString(decoded, "utf-8", NULL); | ||||
| 	Py_DECREF(decoded); | ||||
| 	if (utf8 == NULL) | ||||
| 		goto error_clear; | ||||
| 
 | ||||
| 	assert(PyBytes_Check(utf8)); | ||||
| 	converted = new_string(PyBytes_AS_STRING(utf8), | ||||
| 			       PyBytes_GET_SIZE(utf8)); | ||||
| 	Py_DECREF(utf8); | ||||
| 	if (converted == NULL) | ||||
| 		goto error_nomem; | ||||
| 
 | ||||
| 	PyMem_FREE(*inp); | ||||
| 	*inp = converted; | ||||
| 	if (tok->encoding != NULL) | ||||
| 		PyMem_FREE(tok->encoding); | ||||
| 	tok->encoding = new_string(encoding, strlen(encoding)); | ||||
| 	if (tok->encoding == NULL) | ||||
| 		goto error_nomem; | ||||
| 
 | ||||
| 	Py_DECREF(enc); | ||||
| 	return 0; | ||||
| 
 | ||||
| error_nomem: | ||||
| 	Py_DECREF(enc); | ||||
| 	tok->done = E_NOMEM; | ||||
| 	return -1; | ||||
| 
 | ||||
| error_clear: | ||||
| 	/* Fallback to iso-8859-1: for backward compatibility */ | ||||
| 	Py_DECREF(enc); | ||||
| 	PyErr_Clear(); | ||||
| 	return 0; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
| /* Get next char, updating state; error code goes into tok->done */ | ||||
| 
 | ||||
| static int | ||||
|  | @ -768,10 +708,6 @@ tok_nextc(register struct tok_state *tok) | |||
| 				PyMem_FREE(newtok); | ||||
| 				tok->done = E_EOF; | ||||
| 			} | ||||
| #if !defined(PGEN) | ||||
| 			else if (tok_stdin_decode(tok, &newtok) != 0) | ||||
| 				PyMem_FREE(newtok); | ||||
| #endif | ||||
| 			else if (tok->start != NULL) { | ||||
| 				size_t start = tok->start - tok->buf; | ||||
| 				size_t oldlen = tok->cur - tok->buf; | ||||
|  |  | |||
|  | @ -659,8 +659,7 @@ builtin_exec(PyObject *self, PyObject *args) | |||
| 		locals = globals; | ||||
| 	if (!PyString_Check(prog) && | ||||
| 	    !PyUnicode_Check(prog) && | ||||
| 	    !PyCode_Check(prog) && | ||||
| 	    !PyFile_Check(prog)) { | ||||
| 	    !PyCode_Check(prog)) { | ||||
| 		PyErr_Format(PyExc_TypeError, | ||||
| 			"exec() arg 1 must be a string, file, or code " | ||||
| 			"object, not %.100s", prog->ob_type->tp_name); | ||||
|  | @ -692,18 +691,6 @@ builtin_exec(PyObject *self, PyObject *args) | |||
| 		} | ||||
| 		v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); | ||||
| 	} | ||||
| 	else if (PyFile_Check(prog)) { | ||||
| 		FILE *fp = PyFile_AsFile(prog); | ||||
| 		char *name = PyString_AsString(PyFile_Name(prog)); | ||||
| 		PyCompilerFlags cf; | ||||
| 		cf.cf_flags = 0; | ||||
| 		if (PyEval_MergeCompilerFlags(&cf)) | ||||
| 			v = PyRun_FileFlags(fp, name, Py_file_input, globals, | ||||
| 					    locals, &cf); | ||||
| 		else | ||||
| 			v = PyRun_File(fp, name, Py_file_input, globals, | ||||
| 				       locals); | ||||
| 	} | ||||
| 	else { | ||||
| 		char *str = source_as_string(prog); | ||||
| 		PyCompilerFlags cf; | ||||
|  |  | |||
|  | @ -2764,19 +2764,21 @@ static FILE * | |||
| get_file(char *pathname, PyObject *fob, char *mode) | ||||
| { | ||||
| 	FILE *fp; | ||||
| 	if (fob == NULL) { | ||||
| 	if (mode[0] == 'U') | ||||
| 		mode = "r" PY_STDIOTEXTMODE; | ||||
| 	if (fob == NULL) { | ||||
| 		fp = fopen(pathname, mode); | ||||
| 		if (fp == NULL) | ||||
| 			PyErr_SetFromErrno(PyExc_IOError); | ||||
| 	} | ||||
| 	else { | ||||
| 		fp = PyFile_AsFile(fob); | ||||
| 		if (fp == NULL) | ||||
| 			PyErr_SetString(PyExc_ValueError, | ||||
| 					"bad/closed file object"); | ||||
| 		int fd = PyObject_AsFileDescriptor(fob); | ||||
| 		if (fd == -1) | ||||
| 			return NULL; | ||||
| 		/* XXX This will leak a FILE struct. Fix this!!!!
 | ||||
| 		   (But it doesn't leak a file descrioptor!) */ | ||||
| 		fp = fdopen(fd, mode); | ||||
| 	} | ||||
| 	if (fp == NULL) | ||||
| 		PyErr_SetFromErrno(PyExc_IOError); | ||||
| 	return fp; | ||||
| } | ||||
| 
 | ||||
|  | @ -2788,8 +2790,8 @@ imp_load_compiled(PyObject *self, PyObject *args) | |||
| 	PyObject *fob = NULL; | ||||
| 	PyObject *m; | ||||
| 	FILE *fp; | ||||
| 	if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname, | ||||
| 			      &PyFile_Type, &fob)) | ||||
| 	if (!PyArg_ParseTuple(args, "ss|O:load_compiled", | ||||
| 			      &name, &pathname, &fob)) | ||||
| 		return NULL; | ||||
| 	fp = get_file(pathname, fob, "rb"); | ||||
| 	if (fp == NULL) | ||||
|  | @ -2810,8 +2812,8 @@ imp_load_dynamic(PyObject *self, PyObject *args) | |||
| 	PyObject *fob = NULL; | ||||
| 	PyObject *m; | ||||
| 	FILE *fp = NULL; | ||||
| 	if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname, | ||||
| 			      &PyFile_Type, &fob)) | ||||
| 	if (!PyArg_ParseTuple(args, "ss|O:load_dynamic", | ||||
| 			      &name, &pathname, &fob)) | ||||
| 		return NULL; | ||||
| 	if (fob) { | ||||
| 		fp = get_file(pathname, fob, "r"); | ||||
|  | @ -2832,8 +2834,8 @@ imp_load_source(PyObject *self, PyObject *args) | |||
| 	PyObject *fob = NULL; | ||||
| 	PyObject *m; | ||||
| 	FILE *fp; | ||||
| 	if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname, | ||||
| 			      &PyFile_Type, &fob)) | ||||
| 	if (!PyArg_ParseTuple(args, "ss|O:load_source", | ||||
| 			      &name, &pathname, &fob)) | ||||
| 		return NULL; | ||||
| 	fp = get_file(pathname, fob, "r"); | ||||
| 	if (fp == NULL) | ||||
|  | @ -2873,12 +2875,7 @@ imp_load_module(PyObject *self, PyObject *args) | |||
| 	if (fob == Py_None) | ||||
| 		fp = NULL; | ||||
| 	else { | ||||
| 		if (!PyFile_Check(fob)) { | ||||
| 			PyErr_SetString(PyExc_ValueError, | ||||
| 				"load_module arg#2 should be a file or None"); | ||||
| 			return NULL; | ||||
| 		} | ||||
| 		fp = get_file(pathname, fob, mode); | ||||
| 		fp = get_file(NULL, fob, mode); | ||||
| 		if (fp == NULL) | ||||
| 			return NULL; | ||||
| 	} | ||||
|  |  | |||
|  | @ -1138,47 +1138,25 @@ PyMarshal_WriteObjectToString(PyObject *x, int version) | |||
| static PyObject * | ||||
| marshal_dump(PyObject *self, PyObject *args) | ||||
| { | ||||
| 	WFILE wf; | ||||
| 	/* XXX Quick hack -- need to do this differently */ | ||||
| 	PyObject *x; | ||||
| 	PyObject *f; | ||||
| 	int version = Py_MARSHAL_VERSION; | ||||
| 	PyObject *s; | ||||
| 	PyObject *res; | ||||
| 	if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version)) | ||||
| 		return NULL; | ||||
| 	if (!PyFile_Check(f)) { | ||||
| 		/* XXX Quick hack -- need to do this differently */ | ||||
| 		PyObject *s = PyMarshal_WriteObjectToString(x, version); | ||||
| 		PyObject *res = NULL; | ||||
| 		if (s != NULL) { | ||||
| 	s = PyMarshal_WriteObjectToString(x, version); | ||||
| 	if (s == NULL) | ||||
| 		return NULL; | ||||
| 	res = PyObject_CallMethod(f, "write", "O", s); | ||||
| 	Py_DECREF(s); | ||||
| 		} | ||||
| 	return res; | ||||
| 	} | ||||
| 	wf.fp = PyFile_AsFile(f); | ||||
| 	wf.str = NULL; | ||||
| 	wf.ptr = wf.end = NULL; | ||||
| 	wf.error = 0; | ||||
| 	wf.depth = 0; | ||||
| 	wf.strings = (version > 0) ? PyDict_New() : 0; | ||||
| 	wf.version = version; | ||||
| 	w_object(x, &wf); | ||||
| 	Py_XDECREF(wf.strings); | ||||
| 	if (wf.error) { | ||||
| 		PyErr_SetString(PyExc_ValueError, | ||||
| 				(wf.error==1)?"unmarshallable object" | ||||
| 				:"object too deeply nested to marshal"); | ||||
| 		return NULL; | ||||
| 	} | ||||
| 	Py_INCREF(Py_None); | ||||
| 	return Py_None; | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
| marshal_load(PyObject *self, PyObject *f) | ||||
| { | ||||
| 	RFILE rf; | ||||
| 	PyObject *result; | ||||
| 	if (!PyFile_Check(f)) { | ||||
| 	/* XXX Quick hack -- need to do this differently */ | ||||
| 	PyObject *data, *result; | ||||
| 	RFILE rf; | ||||
|  | @ -1207,13 +1185,6 @@ marshal_load(PyObject *self, PyObject *f) | |||
| 	Py_DECREF(rf.strings); | ||||
| 	Py_DECREF(data); | ||||
| 	return result; | ||||
| 	} | ||||
| 	rf.fp = PyFile_AsFile(f); | ||||
| 	rf.strings = PyList_New(0); | ||||
| 	rf.depth = 0; | ||||
| 	result = read_object(&rf); | ||||
| 	Py_DECREF(rf.strings); | ||||
| 	return result; | ||||
| } | ||||
| 
 | ||||
| static PyObject * | ||||
|  |  | |||
|  | @ -155,7 +155,6 @@ Py_InitializeEx(int install_sigs) | |||
| #if defined(HAVE_LANGINFO_H) && defined(CODESET) | ||||
| 	char *codeset; | ||||
| 	char *saved_locale; | ||||
| 	PyObject *sys_stream, *sys_isatty; | ||||
| #endif | ||||
| 	extern void _Py_ReadyTypes(void); | ||||
| 
 | ||||
|  | @ -273,39 +272,6 @@ Py_InitializeEx(int install_sigs) | |||
| 	free(saved_locale); | ||||
| 
 | ||||
| 	if (codeset) { | ||||
| 		sys_stream = PySys_GetObject("stdin"); | ||||
| 		sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); | ||||
| 		if (!sys_isatty) | ||||
| 			PyErr_Clear(); | ||||
| 		if(sys_isatty && PyObject_IsTrue(sys_isatty) && | ||||
| 		   PyFile_Check(sys_stream)) { | ||||
| 			if (!PyFile_SetEncoding(sys_stream, codeset)) | ||||
| 				Py_FatalError("Cannot set codeset of stdin"); | ||||
| 		} | ||||
| 		Py_XDECREF(sys_isatty); | ||||
| 
 | ||||
| 		sys_stream = PySys_GetObject("stdout"); | ||||
| 		sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); | ||||
| 		if (!sys_isatty) | ||||
| 			PyErr_Clear(); | ||||
| 		if(sys_isatty && PyObject_IsTrue(sys_isatty) && | ||||
| 		   PyFile_Check(sys_stream)) { | ||||
| 			if (!PyFile_SetEncoding(sys_stream, codeset)) | ||||
| 				Py_FatalError("Cannot set codeset of stdout"); | ||||
| 		} | ||||
| 		Py_XDECREF(sys_isatty); | ||||
| 
 | ||||
| 		sys_stream = PySys_GetObject("stderr"); | ||||
| 		sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); | ||||
| 		if (!sys_isatty) | ||||
| 			PyErr_Clear(); | ||||
| 		if(sys_isatty && PyObject_IsTrue(sys_isatty) && | ||||
| 		   PyFile_Check(sys_stream)) { | ||||
| 			if (!PyFile_SetEncoding(sys_stream, codeset)) | ||||
| 				Py_FatalError("Cannot set codeset of stderr"); | ||||
| 		} | ||||
| 		Py_XDECREF(sys_isatty); | ||||
| 
 | ||||
| 		if (!Py_FileSystemDefaultEncoding) | ||||
| 			Py_FileSystemDefaultEncoding = codeset; | ||||
| 		else | ||||
|  |  | |||
|  | @ -55,18 +55,6 @@ PySys_GetObject(char *name) | |||
| 	return PyDict_GetItemString(sd, name); | ||||
| } | ||||
| 
 | ||||
| FILE * | ||||
| PySys_GetFile(char *name, FILE *def) | ||||
| { | ||||
| 	FILE *fp = NULL; | ||||
| 	PyObject *v = PySys_GetObject(name); | ||||
| 	if (v != NULL && PyFile_Check(v)) | ||||
| 		fp = PyFile_AsFile(v); | ||||
| 	if (fp == NULL) | ||||
| 		fp = def; | ||||
| 	return fp; | ||||
| } | ||||
| 
 | ||||
| int | ||||
| PySys_SetObject(char *name, PyObject *v) | ||||
| { | ||||
|  | @ -1353,15 +1341,12 @@ mywrite(char *name, FILE *fp, const char *format, va_list va) | |||
| { | ||||
| 	PyObject *file; | ||||
| 	PyObject *error_type, *error_value, *error_traceback; | ||||
| 	char buffer[1001]; | ||||
| 	int written; | ||||
| 
 | ||||
| 	PyErr_Fetch(&error_type, &error_value, &error_traceback); | ||||
| 	file = PySys_GetObject(name); | ||||
| 	if (file == NULL || PyFile_AsFile(file) == fp) | ||||
| 		vfprintf(fp, format, va); | ||||
| 	else { | ||||
| 		char buffer[1001]; | ||||
| 		const int written = PyOS_vsnprintf(buffer, sizeof(buffer), | ||||
| 						   format, va); | ||||
| 	written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); | ||||
| 	if (PyFile_WriteString(buffer, file) != 0) { | ||||
| 		PyErr_Clear(); | ||||
| 		fputs(buffer, fp); | ||||
|  | @ -1373,7 +1358,6 @@ mywrite(char *name, FILE *fp, const char *format, va_list va) | |||
| 			fputs(truncated, fp); | ||||
| 		} | ||||
| 	} | ||||
| 	} | ||||
| 	PyErr_Restore(error_type, error_value, error_traceback); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -24,8 +24,8 @@ mkdir -p OUT | |||
| >BAD | ||||
| >SKIPPED | ||||
| 
 | ||||
| # The -uall flag (edit this file to change). | ||||
| UALL="-uall" | ||||
| # The -u flag (edit this file to change). | ||||
| UFLAG="-unetwork" | ||||
| 
 | ||||
| # Compute the list of tests to run. | ||||
| case $# in | ||||
|  | @ -41,7 +41,7 @@ esac | |||
| for T in $TESTS | ||||
| do | ||||
|     echo -n $T | ||||
|     if $PYTHON Lib/test/regrtest.py $UALL $T >OUT/$T.out 2>&1 | ||||
|     if $PYTHON Lib/test/regrtest.py $UFLAG $T >OUT/$T.out 2>&1 | ||||
|     then | ||||
| 	if grep -q "1 test skipped:" OUT/$T.out | ||||
| 	then | ||||
|  | @ -55,6 +55,6 @@ do | |||
| 	echo " BAD" | ||||
|         echo $T >>BAD | ||||
| 	echo "---------- Re-running test in verbose mode ----------" >>OUT/$T | ||||
| 	$PYTHON Lib/test/regrtest.py -v $UALL $T >>OUT/$T.out 2>&1 | ||||
| 	$PYTHON Lib/test/regrtest.py -v $UFLAG $T >>OUT/$T.out 2>&1 | ||||
|     fi | ||||
| done | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Guido van Rossum
						Guido van Rossum