[Python-checkins] r51668 - in python/branches/bcannon-objcap: BRANCHNEWS Include/fileobject.h Lib/test/test_descr.py Modules/bz2module.c Objects/fileobject.c Python/bltinmodule.c

brett.cannon python-checkins at python.org
Thu Aug 31 20:48:47 CEST 2006


Author: brett.cannon
Date: Thu Aug 31 20:48:46 2006
New Revision: 51668
Modified:
 python/branches/bcannon-objcap/BRANCHNEWS
 python/branches/bcannon-objcap/Include/fileobject.h
 python/branches/bcannon-objcap/Lib/test/test_descr.py
 python/branches/bcannon-objcap/Modules/bz2module.c
 python/branches/bcannon-objcap/Objects/fileobject.c
 python/branches/bcannon-objcap/Python/bltinmodule.c
Log:
Revert back to rev. 51655 state to start from scratch for new approach to
removing the 'file' objects initializer.
Modified: python/branches/bcannon-objcap/BRANCHNEWS
==============================================================================
--- python/branches/bcannon-objcap/BRANCHNEWS	(original)
+++ python/branches/bcannon-objcap/BRANCHNEWS	Thu Aug 31 20:48:46 2006
@@ -5,17 +5,5 @@
 Core and builtins
 -----------------
 
-* rev. 51656: Remove initializer from 'file'. By leaving tp_new alone you can
- still subclass 'file' (although its usefulness as a subclass is doubtful when
- its tp_init is empty and that is what actually opens the file descriptor).
- Created a new function, PyFile_UnsafeOpen() which is what the built-in open()
- function is now set to. Also changed the bz2 module to use it.
-
- Still need to decide how to handle subclasses of 'file' so they are not
- totally useless (special function that calls the needed initializer on a
- 'file' object?). Also need to come up with C API that opens files through
- the built-in open() instead of doing it directly so as to not bypass
- security.
-
 * rev. 51392: Introduce objcap module to hold removed functions/methods. Begin
 with moving object.__subclasses__().
Modified: python/branches/bcannon-objcap/Include/fileobject.h
==============================================================================
--- python/branches/bcannon-objcap/Include/fileobject.h	(original)
+++ python/branches/bcannon-objcap/Include/fileobject.h	Thu Aug 31 20:48:46 2006
@@ -45,8 +45,6 @@
 PyAPI_FUNC(int) PyFile_WriteString(const char *, PyObject *);
 PyAPI_FUNC(int) PyObject_AsFileDescriptor(PyObject *);
 
-PyAPI_FUNC(PyObject *) PyFile_UnsafeOpen(PyObject *, PyObject *, PyObject *);
-
 /* The default encoding used by the platform file system APIs
 If non-NULL, this is different than the default encoding for strings
 */
Modified: python/branches/bcannon-objcap/Lib/test/test_descr.py
==============================================================================
--- python/branches/bcannon-objcap/Lib/test/test_descr.py	(original)
+++ python/branches/bcannon-objcap/Lib/test/test_descr.py	Thu Aug 31 20:48:46 2006
@@ -2452,14 +2452,10 @@
 
 lineno = 0
 ateof = 0
-
- def __init__(self, path):
- self.__file = open(path)
-
 def readline(self):
 if self.ateof:
 return ""
- s = file.readline(self.__file)
+ s = file.readline(self)
 # Next line works too.
 # s = super(CountedInput, self).readline()
 self.lineno += 1
@@ -2504,7 +2500,7 @@
 # note: as of Python 2.3, dict() no longer has an "items" keyword arg
 
 for constructor in (int, float, long, complex, str, unicode,
- tuple, list):
+ tuple, list, file):
 try:
 constructor(bogus_keyword_arg=1)
 except TypeError:
Modified: python/branches/bcannon-objcap/Modules/bz2module.c
==============================================================================
--- python/branches/bcannon-objcap/Modules/bz2module.c	(original)
+++ python/branches/bcannon-objcap/Modules/bz2module.c	Thu Aug 31 20:48:46 2006
@@ -1293,7 +1293,6 @@
 	static char *kwlist[] = {"filename", "mode", "buffering",
 "compresslevel", 0};
 	PyObject *name;
-	PyObject *file_open_args;
 	char *mode = "r";
 	int buffering = -1;
 	int compresslevel = 9;
@@ -1354,10 +1353,8 @@
 
 	mode = (mode_char == 'r') ? "rb" : "wb";
 
-	file_open_args = Py_BuildValue("Osi", name, mode, buffering);
-	if (!file_open_args)
-	 goto error;
-	self->file = PyFile_UnsafeOpen(NULL, file_open_args, NULL);
+	self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)",
+					 name, mode, buffering);
 	if (self->file == NULL)
 		return -1;
 
Modified: python/branches/bcannon-objcap/Objects/fileobject.c
==============================================================================
--- python/branches/bcannon-objcap/Objects/fileobject.c	(original)
+++ python/branches/bcannon-objcap/Objects/fileobject.c	Thu Aug 31 20:48:46 2006
@@ -273,7 +273,6 @@
 	return (PyObject *)f;
 }
 
-
 PyObject *
 PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
 {
@@ -1967,28 +1966,25 @@
 	return self;
 }
 
-/*
- Open a file with no regards to whether it should be allowed.
-
- Used as the implementation of built-in open().
-*/
-PyObject *
-PyFile_UnsafeOpen(PyObject *self, PyObject *args, PyObject *kwds)
+static int
+file_init(PyObject *self, PyObject *args, PyObject *kwds)
 {
-	PyFileObject *foself; 
-	PyObject *ret = NULL;
+	PyFileObject *foself = (PyFileObject *)self;
+	int ret = 0;
 	static char *kwlist[] = {"name", "mode", "buffering", 0};
 	char *name = NULL;
 	char *mode = "r";
 	int bufsize = -1;
 	int wideargument = 0;
 
-	self = file_new(&PyFile_Type, args, kwds);
-	if (!self)
-	 return NULL;
-	foself = (PyFileObject *)self;
-
 	assert(PyFile_Check(self));
+	if (foself->f_fp != NULL) {
+		/* Have to close the existing file first. */
+		PyObject *closeresult = file_close(foself);
+		if (closeresult == NULL)
+			return -1;
+		Py_DECREF(closeresult);
+	}
 
 #ifdef Py_WIN_WIDE_FILENAMES
 	if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
@@ -2014,13 +2010,13 @@
 						 Py_FileSystemDefaultEncoding,
 						 &name,
 						 &mode, &bufsize))
-			return NULL;
+			return -1;
 
 /* We parse again to get the name as a PyObject */
 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file", 
 kwlist, &o_name, &mode, 
 &bufsize))
- return NULL;
+ return -1;
 
 		if (fill_file_fields(foself, NULL, o_name, mode,
 				 fclose) == NULL)
@@ -2030,11 +2026,10 @@
 		goto Error;
 	foself->f_setbuf = NULL;
 	PyFile_SetBufSize(self, bufsize);
-	ret = self;
 	goto Done;
 
 Error:
-	ret = NULL;
+	ret = -1;
 	/* fall through */
 Done:
 	PyMem_Free(name); /* free the encoded string */
@@ -2101,7 +2096,7 @@
 	0,					/* tp_descr_get */
 	0,					/* tp_descr_set */
 	0,					/* tp_dictoffset */
-	0,					/* tp_init */
+	file_init,				/* tp_init */
 	PyType_GenericAlloc,			/* tp_alloc */
 	file_new,				/* tp_new */
 	PyObject_Del, /* tp_free */
Modified: python/branches/bcannon-objcap/Python/bltinmodule.c
==============================================================================
--- python/branches/bcannon-objcap/Python/bltinmodule.c	(original)
+++ python/branches/bcannon-objcap/Python/bltinmodule.c	Thu Aug 31 20:48:46 2006
@@ -1341,29 +1341,16 @@
 Return the octal representation of an integer or long integer.");
 
 
-/* PyFile_UnsafeOpen() used as open(). */
+static PyObject *
+builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
+{
+	return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
+}
 
-PyDoc_VAR(open_doc) =
-PyDoc_STR(
-"file(name[, mode[, buffering]]) -> file object\n"
-"\n"
-"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
-"writing or appending. The file will be created if it doesn't exist\n"
-"when opened for writing or appending; it will be truncated when\n"
-"opened for writing. Add a 'b' to the mode for binary files.\n"
-"Add a '+' to the mode to allow simultaneous reading and writing.\n"
-"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
-"buffered, and larger numbers specify the buffer size.\n"
-)
-PyDoc_STR(
-"Add a 'U' to mode to open the file for input with universal newline\n"
-"support. Any line ending in the input file will be seen as a '\\n'\n"
-"in Python. Also, a file so opened gains the attribute 'newlines';\n"
-"the value for this attribute is one of None (no newline read yet),\n"
-"'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
-"\n"
-"'U' cannot be combined with 'w' or '+' mode.\n"
-);
+PyDoc_STRVAR(open_doc,
+"open(name[, mode[, buffering]]) -> file object\n\
+\n\
+Open a file using the file() type, returns a file object.");
 
 
 static PyObject *
@@ -2272,7 +2259,7 @@
 	{"max",		(PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
 	{"min",		(PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
 	{"oct",		builtin_oct, METH_O, oct_doc},
- 	{"open",	(PyCFunction)PyFile_UnsafeOpen, METH_VARARGS | METH_KEYWORDS, open_doc},
+ 	{"open",	(PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
 	{"ord",		builtin_ord, METH_O, ord_doc},
 	{"pow",		builtin_pow, METH_VARARGS, pow_doc},
 	{"range",	builtin_range, METH_VARARGS, range_doc},


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /