Skip to content

Commit f4bb841

Browse files
author
jackjansen
committed
Mass checkin of universal newline support.
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. git-svn-id: http://svn.python.org/projects/python/trunk@26390 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent de6fc5f commit f4bb841

15 files changed

Lines changed: 390 additions & 35 deletions

File tree

Include/fileobject.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ typedef struct {
1414
PyObject *f_mode;
1515
int (*f_close)(FILE *);
1616
int f_softspace; /* Flag used by 'print' command */
17-
int f_binary; /* Flag which indicates whether the file is
17+
int f_binary; /* Flag which indicates whether the file is open
1818
open in binary (1) or test (0) mode */
19+
#ifdef WITH_UNIVERSAL_NEWLINES
20+
int f_univ_newline; /* Handle any newline convention */
21+
int f_newlinetypes; /* Types of newlines seen */
22+
int f_skipnextlf; /* Skip next \n */
23+
#endif
1924
} PyFileObject;
2025

2126
extern DL_IMPORT(PyTypeObject) PyFile_Type;
@@ -40,6 +45,19 @@ extern DL_IMPORT(int) PyObject_AsFileDescriptor(PyObject *);
4045
*/
4146
extern DL_IMPORT(const char *) Py_FileSystemDefaultEncoding;
4247

48+
#ifdef WITH_UNIVERSAL_NEWLINES
49+
/* Routines to replace fread() and fgets() which accept any of \r, \n
50+
or \r\n as line terminators.
51+
*/
52+
#define PY_STDIOTEXTMODE "b"
53+
char *Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
54+
size_t Py_UniversalNewlineFread(void *, size_t, FILE *, PyObject *);
55+
#else
56+
#define PY_STDIOTEXTMODE ""
57+
#define Py_UniversalNewlineFgets(buf, len, fp, obj) (fgets((buf), (len), (fp)))
58+
#define Py_UniversalNewlineFread(buf, len, fp, obj) \
59+
(fread((buf), 1, (len), (fp)))
60+
#endif /* WITH_UNIVERSAL_NEWLINES */
4361
#ifdef __cplusplus
4462
}
4563
#endif

Lib/linecache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def updatecache(filename):
9090
## print '*** Cannot stat', filename, ':', msg
9191
return []
9292
try:
93-
fp = open(fullname, 'r')
93+
fp = open(fullname, 'rU')
9494
lines = fp.readlines()
9595
fp.close()
9696
except IOError, msg:

Lib/py_compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def compile(file, cfile=None, dfile=None):
4444
4545
"""
4646
import os, marshal, __builtin__
47-
f = open(file)
47+
f = open(file, 'U')
4848
try:
4949
timestamp = long(os.fstat(f.fileno())[8])
5050
except AttributeError:

Mac/Python/macmain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ Py_Main(int argc, char **argv, char *filename)
611611
Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
612612

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

632632
if (filename == NULL && isatty((int)fileno(fp))) {
633-
FILE *fp = fopen(STARTUP, "r");
633+
FILE *fp = fopen(STARTUP, "r" PY_STDIOTEXTMODE);
634634
if (fp != NULL) {
635635
(void) PyRun_SimpleFile(fp, STARTUP);
636636
PyErr_Clear();

0 commit comments

Comments
 (0)