Mercurial > public > mercurial-scm > hg
comparison mercurial/scmutil.py @ 14097:ca3376f044f8
opener: add read & write utility methods
The two new methods are useful for quickly opening a file for reading
or writing. Unlike 'opener(...).read()', they ensure they the file is
immediately closed without relying on CPython reference counting.
author | Dan Villiom Podlaski Christiansen <danchr@gmail.com> |
---|---|
date | Sat, 30 Apr 2011 19:41:53 +0200 |
parents | e24b5e3c2f27 |
children | c18204fd35b0 |
comparison
equal
deleted
inserted
replaced
14096:dea93484cf9f | 14097:ca3376f044f8 |
---|---|
133 """Abstract base class; cannot be instantiated""" | 133 """Abstract base class; cannot be instantiated""" |
134 | 134 |
135 def __init__(self, *args, **kwargs): | 135 def __init__(self, *args, **kwargs): |
136 '''Prevent instantiation; don't call this from subclasses.''' | 136 '''Prevent instantiation; don't call this from subclasses.''' |
137 raise NotImplementedError('attempted instantiating ' + str(type(self))) | 137 raise NotImplementedError('attempted instantiating ' + str(type(self))) |
138 | |
139 def read(self, *args, **kwargs): | |
140 fp = self(*args, **kwargs) | |
141 try: | |
142 return fp.read() | |
143 finally: | |
144 fp.close() | |
145 | |
146 def write(self, data, *args, **kwargs): | |
147 fp = self(*args, **kwargs) | |
148 try: | |
149 return fp.write(data) | |
150 finally: | |
151 fp.close() | |
138 | 152 |
139 class opener(abstractopener): | 153 class opener(abstractopener): |
140 '''Open files relative to a base directory | 154 '''Open files relative to a base directory |
141 | 155 |
142 This class is used to hide the details of COW semantics and | 156 This class is used to hide the details of COW semantics and |