Skip to content

Commit

Permalink
Mass checkin of universal newline support.
Browse files Browse the repository at this point in the history
Highlights: import and friends will understand any of \r, \n and \r\n
as end of line. Python file input will do the same if you use mode 'U'.
Everything can be disabled by configuring with --without-universal-newlines.

See PEP278 for details.
  • Loading branch information
jackjansen committed Apr 14, 2002
1 parent dcd2dc2 commit 7b8c754
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 35 deletions.
20 changes: 19 additions & 1 deletion Include/fileobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ typedef struct {
PyObject *f_mode;
int (*f_close)(FILE *);
int f_softspace; /* Flag used by 'print' command */
int f_binary; /* Flag which indicates whether the file is
int f_binary; /* Flag which indicates whether the file is open
open in binary (1) or test (0) mode */
#ifdef WITH_UNIVERSAL_NEWLINES
int f_univ_newline; /* Handle any newline convention */
int f_newlinetypes; /* Types of newlines seen */
int f_skipnextlf; /* Skip next \n */
#endif
} PyFileObject;

extern DL_IMPORT(PyTypeObject) PyFile_Type;
Expand All @@ -40,6 +45,19 @@ extern DL_IMPORT(int) PyObject_AsFileDescriptor(PyObject *);
*/
extern DL_IMPORT(const char *) Py_FileSystemDefaultEncoding;

#ifdef WITH_UNIVERSAL_NEWLINES
/* 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(void *, size_t, FILE *, PyObject *);
#else
#define PY_STDIOTEXTMODE ""
#define Py_UniversalNewlineFgets(buf, len, fp, obj) (fgets((buf), (len), (fp)))
#define Py_UniversalNewlineFread(buf, len, fp, obj) \
(fread((buf), 1, (len), (fp)))
#endif /* WITH_UNIVERSAL_NEWLINES */
#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Lib/linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def updatecache(filename):
## print '*** Cannot stat', filename, ':', msg
return []
try:
fp = open(fullname, 'r')
fp = open(fullname, 'rU')
lines = fp.readlines()
fp.close()
except IOError, msg:
Expand Down
2 changes: 1 addition & 1 deletion Lib/py_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def compile(file, cfile=None, dfile=None):
"""
import os, marshal, __builtin__
f = open(file)
f = open(file, 'U')
try:
timestamp = long(os.fstat(f.fileno())[8])
except AttributeError:
Expand Down
4 changes: 2 additions & 2 deletions Mac/Python/macmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ Py_Main(int argc, char **argv, char *filename)
Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);

if (filename != NULL) {
if ((fp = fopen(filename, "r")) == NULL) {
if ((fp = fopen(filename, "r" PY_STDIOTEXTMODE)) == NULL) {
fprintf(stderr, "%s: can't open file '%s'\n",
argv[0], filename);
PyMac_Exit(2);
Expand All @@ -630,7 +630,7 @@ Py_Main(int argc, char **argv, char *filename)
PySys_SetArgv(argc, argv);

if (filename == NULL && isatty((int)fileno(fp))) {
FILE *fp = fopen(STARTUP, "r");
FILE *fp = fopen(STARTUP, "r" PY_STDIOTEXTMODE);
if (fp != NULL) {
(void) PyRun_SimpleFile(fp, STARTUP);
PyErr_Clear();
Expand Down
Loading

0 comments on commit 7b8c754

Please sign in to comment.