Skip to content

Commit 291ecfe

Browse files
author
gward
committed
Changes to allow passing an open file to the constructor (to support
ProcessHierarchy's changes to support reading from a remote URL in ProcessDatabase). git-svn-id: http://svn.python.org/projects/python/trunk@12950 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 238b253 commit 291ecfe

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

Lib/distutils/text_file.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414

1515
class TextFile:
16-
filename = None
17-
file = None
18-
current_line = None
1916

2017
default_options = { 'strip_comments': 1,
2118
'comment_re': re.compile (r'\s*#.*'),
@@ -26,7 +23,11 @@ class TextFile:
2623
'collapse_ws': 0,
2724
}
2825

29-
def __init__ (self, filename=None, **options):
26+
def __init__ (self, filename=None, file=None, **options):
27+
28+
if filename is None and file is None:
29+
raise RuntimeError, \
30+
"you must supply either or both of 'filename' and 'file'"
3031

3132
# set values for all options -- either from client option hash
3233
# or fallback to default_options
@@ -45,18 +46,16 @@ def __init__ (self, filename=None, **options):
4546
if not self.default_options.has_key (opt):
4647
raise KeyError, "invalid TextFile option '%s'" % opt
4748

48-
self.filename = filename
49-
if self.filename:
50-
self.open ()
51-
52-
53-
def open (self, filename=None):
54-
if not self.filename:
55-
if not filename:
56-
raise RuntimeError, "must provide a filename somehow"
57-
49+
if file is None:
50+
self.open (filename)
51+
else:
5852
self.filename = filename
53+
self.file = file
54+
self.current_line = 0 # assuming that file is at BOF!
55+
5956

57+
def open (self, filename):
58+
self.filename = filename
6059
self.file = open (self.filename, 'r')
6160
self.current_line = 0
6261

0 commit comments

Comments
 (0)