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

brett.cannon python-checkins at python.org
Wed Aug 30 23:52:23 CEST 2006


Author: brett.cannon
Date: Wed Aug 30 23:52:22 2006
New Revision: 51656
Modified:
 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:
Remove tp_init from 'file'. Left tp_new so as to continue to allow
subclassing, although mostly useless since without tp_init you cannot actually
open any file.
Added PyFile_UnsafeOpen() and set built-in open to that. Also changed bz2
module to use it as well.
Still need to make C API calls go through built-in open() so that if it is
overridden that is used instead of blindly opening any file. Will most likely
require a new set of APIs for that.
Modified: python/branches/bcannon-objcap/Include/fileobject.h
==============================================================================
--- python/branches/bcannon-objcap/Include/fileobject.h	(original)
+++ python/branches/bcannon-objcap/Include/fileobject.h	Wed Aug 30 23:52:22 2006
@@ -45,6 +45,8 @@
 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	Wed Aug 30 23:52:22 2006
@@ -2452,10 +2452,14 @@
 
 lineno = 0
 ateof = 0
+
+ def __init__(self, path):
+ self.__file = open(path)
+
 def readline(self):
 if self.ateof:
 return ""
- s = file.readline(self)
+ s = file.readline(self.__file)
 # Next line works too.
 # s = super(CountedInput, self).readline()
 self.lineno += 1
@@ -2500,7 +2504,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, file):
+ tuple, list):
 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	Wed Aug 30 23:52:22 2006
@@ -1293,6 +1293,7 @@
 	static char *kwlist[] = {"filename", "mode", "buffering",
 "compresslevel", 0};
 	PyObject *name;
+	PyObject *file_open_args;
 	char *mode = "r";
 	int buffering = -1;
 	int compresslevel = 9;
@@ -1353,8 +1354,10 @@
 
 	mode = (mode_char == 'r') ? "rb" : "wb";
 
-	self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)",
-					 name, mode, buffering);
+	file_open_args = Py_BuildValue("Osi", name, mode, buffering);
+	if (!file_open_args)
+	 goto error;
+	self->file = PyFile_UnsafeOpen(NULL, file_open_args, NULL);
 	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	Wed Aug 30 23:52:22 2006
@@ -273,6 +273,7 @@
 	return (PyObject *)f;
 }
 
+
 PyObject *
 PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
 {
@@ -1966,25 +1967,28 @@
 	return self;
 }
 
-static int
-file_init(PyObject *self, PyObject *args, PyObject *kwds)
+/*
+ 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)
 {
-	PyFileObject *foself = (PyFileObject *)self;
-	int ret = 0;
+	PyFileObject *foself; 
+	PyObject *ret = NULL;
 	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 */
@@ -2010,13 +2014,13 @@
 						 Py_FileSystemDefaultEncoding,
 						 &name,
 						 &mode, &bufsize))
-			return -1;
+			return NULL;
 
 /* We parse again to get the name as a PyObject */
 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file", 
 kwlist, &o_name, &mode, 
 &bufsize))
- return -1;
+ return NULL;
 
 		if (fill_file_fields(foself, NULL, o_name, mode,
 				 fclose) == NULL)
@@ -2026,10 +2030,11 @@
 		goto Error;
 	foself->f_setbuf = NULL;
 	PyFile_SetBufSize(self, bufsize);
+	ret = self;
 	goto Done;
 
 Error:
-	ret = -1;
+	ret = NULL;
 	/* fall through */
 Done:
 	PyMem_Free(name); /* free the encoded string */
@@ -2096,7 +2101,7 @@
 	0,					/* tp_descr_get */
 	0,					/* tp_descr_set */
 	0,					/* tp_dictoffset */
-	file_init,				/* tp_init */
+	0,					/* 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	Wed Aug 30 23:52:22 2006
@@ -1341,16 +1341,29 @@
 Return the octal representation of an integer or long integer.");
 
 
-static PyObject *
-builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
-{
-	return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
-}
+/* PyFile_UnsafeOpen() used as open(). */
 
-PyDoc_STRVAR(open_doc,
-"open(name[, mode[, buffering]]) -> file object\n\
-\n\
-Open a file using the file() type, returns a file object.");
+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"
+);
 
 
 static PyObject *
@@ -2259,7 +2272,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)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
+ 	{"open",	(PyCFunction)PyFile_UnsafeOpen, 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 によって変換されたページ (->オリジナル) /