def __init__(self, stream, needsVfsClose=False): self.__stream = stream self.__needsVfsClose = needsVfsClose self.__reader = None self.__writer = None self.__lastWrite = False if isinstance(stream, core.Istream): self.__reader = core.StreamReader(stream, False) if isinstance(stream, core.Ostream): self.__writer = core.StreamWriter(stream, False) self.__lastWrite = True self.__write = self.__writer.appendData
def __init__(self, stream, needsVfsClose=False): self.__stream = stream self.__needsVfsClose = needsVfsClose self.__reader = None self.__writer = None self.__lastWrite = False if isinstance(stream, core.Istream): self.__reader = core.StreamReader(stream, False) if isinstance(stream, core.Ostream): self.__writer = core.StreamWriter(stream, False) self.__lastWrite = True if sys.version_info >= (3, 0): # In Python 3, we use appendData, which only accepts bytes. self.__write = self.__writer.appendData else: # In Python 2.7, we also accept unicode objects, which are # implicitly converted to C++ strings. self.__write = self.__writer.write
def __init__(self, filename, mode = 'r', bufsize = None, autoUnwrap = False): self.__stream = None self.__needsVfsClose = False self.__reader = None self.__writer = None self.closed = True self.encoding = None self.errors = None self.__lastWrite = False self.mode = mode self.name = None self.filename = None self.newlines = None self.softspace = False readMode = False writeMode = False if isinstance(filename, core.Istream) or isinstance(filename, core.Ostream): # If we were given a stream instead of a filename, assign # it directly. self.__stream = filename readMode = isinstance(filename, core.Istream) writeMode = isinstance(filename, core.Ostream) elif isinstance(filename, core.VirtualFile): # We can also "open" a VirtualFile object for reading. self.__stream = filename.openReadFile(autoUnwrap) if not self.__stream: message = 'Could not read virtual file %s' % (repr(filename)) raise IOError, message self.__needsVfsClose = True readMode = True else: # Otherwise, we must have been given a filename. Open it. if isinstance(filename, types.StringTypes): # If a raw string is given, assume it's an os-specific # filename. filename = core.Filename.fromOsSpecific(filename) else: # If a Filename is given, make a writable copy anyway. filename = core.Filename(filename) self.filename = filename self.name = filename.toOsSpecific() if sys.version_info >= (3, 0): # Python 3 is much stricter than Python 2, which lets # unknown flags fall through. for ch in mode: if not ch in 'rwxabt+U': raise IOError("invalid mode: " + mode) binary = False if 'b' in mode and 't' in mode: raise IOError("can't have text and binary mode at once") if 'b' in mode: # Strip 'b'. This means a binary file. i = mode.index('b') mode = mode[:i] + mode[i + 1:] binary = True elif 't' in mode: # Strip 't'. This means a text file (redundant, yes). i = mode.index('t') mode = mode[:i] + mode[i + 1:] binary = False if 'U' in mode: # Strip 'U'. We don't use it; universal-newline support # is built into Panda, and can't be changed at runtime. i = mode.index('U') mode = mode[:i] + mode[i + 1:] binary = False if mode == '': mode = 'r' # Per Python docs, we insist this is true. modeType = mode[0] assert modeType in 'rwa' if binary: filename.setBinary() else: filename.setText() # Actually open the streams, taking care to # ignore unknown chars in the mode string. # We already asserted that it starts with a mode # char above, so locate the '+' if modeType == 'w' and '+' in mode: self.__stream = _vfs.openReadWriteFile(filename, True) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError(message) readMode = True writeMode = True elif modeType == 'a' and '+' in mode: self.__stream = _vfs.openReadAppendFile(filename) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError(message) readMode = True writeMode = True elif modeType == 'r' and '+' in mode: self.__stream = _vfs.openReadWriteFile(filename, False) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError(message) readMode = True writeMode = True elif modeType == 'w': self.__stream = _vfs.openWriteFile(filename, autoUnwrap, True) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError(message) writeMode = True elif modeType == 'a': self.__stream = _vfs.openAppendFile(filename) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError(message) writeMode = True elif modeType == 'r': self.__stream = _vfs.openReadFile(filename, autoUnwrap) if not self.__stream: if not _vfs.exists(filename): message = 'No such file: %s' % (filename) else: message = 'Could not open %s for reading' % (filename) raise IOError(message) readMode = True else: # should not get here unless there's a bug above raise IOError("Unhandled mode flags: " + mode) self.__needsVfsClose = True if readMode: self.__reader = core.StreamReader(self.__stream, False) if writeMode: self.__writer = core.StreamWriter(self.__stream, False) self.__lastWrite = True
def __init__(self, filename, mode = 'r', bufsize = None, autoUnwrap = False): self.__stream = None self.__needsVfsClose = False self.__reader = None self.__writer = None self.closed = True self.encoding = None self.errors = None self.__lastWrite = False self.mode = mode self.name = None self.filename = None self.newlines = None self.softspace = False readMode = False writeMode = False if isinstance(filename, core.Istream) or isinstance(filename, core.Ostream): # If we were given a stream instead of a filename, assign # it directly. self.__stream = filename readMode = isinstance(filename, core.Istream) writeMode = isinstance(filename, core.Ostream) elif isinstance(filename, core.VirtualFile): # We can also "open" a VirtualFile object for reading. self.__stream = filename.openReadFile(autoUnwrap) if not self.__stream: message = 'Could not read virtual file %s' % (repr(filename)) raise IOError, message self.__needsVfsClose = True readMode = True else: # Otherwise, we must have been given a filename. Open it. if isinstance(filename, types.StringTypes): # If a raw string is given, assume it's an os-specific # filename. filename = core.Filename.fromOsSpecific(filename) else: # If a Filename is given, make a writable copy anyway. filename = core.Filename(filename) self.filename = filename self.name = filename.toOsSpecific() binary = False if 'b' in mode: # Strip 'b'. This means a binary file. i = mode.index('b') mode = mode[:i] + mode[i + 1:] binary = True if 'U' in mode: # Strip 'U'. We don't use it; universal-newline support # is built into Panda, and can't be changed at runtime. i = mode.index('U') mode = mode[:i] + mode[i + 1:] binary = False if mode == '': mode = 'r' # Per Python docs, we insist this is true. assert mode[0] in 'rwa' if binary: filename.setBinary() else: filename.setText() # Actually open the streams. if mode == 'w': self.__stream = _vfs.openWriteFile(filename, autoUnwrap, True) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError, message writeMode = True elif mode == 'a': self.__stream = _vfs.openAppendFile(filename) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError, message writeMode = True elif mode == 'w+': self.__stream = _vfs.openReadWriteFile(filename, True) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError, message readMode = True writeMode = True elif mode == 'a+': self.__stream = _vfs.openReadAppendFile(filename) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError, message readMode = True writeMode = True elif mode == 'r+': self.__stream = _vfs.openReadWriteFile(filename, False) if not self.__stream: message = 'Could not open %s for writing' % (filename) raise IOError, message readMode = True writeMode = True elif mode == 'r': self.__stream = _vfs.openReadFile(filename, autoUnwrap) if not self.__stream: if not _vfs.exists(filename): message = 'No such file: %s' % (filename) else: message = 'Could not open %s for reading' % (filename) raise IOError, message readMode = True self.__needsVfsClose = True if readMode: self.__reader = core.StreamReader(self.__stream, False) if writeMode: self.__writer = core.StreamWriter(self.__stream, False) self.__lastWrite = True