Fix some issues with new VFS open() implementation

This commit is contained in:
rdb 2015-09-21 15:13:21 +02:00
parent 62217c652e
commit 49088fb7ce
7 changed files with 109 additions and 18 deletions

View File

@ -235,7 +235,7 @@ class Notifier:
Prints the string to output followed by a newline.
"""
if self.streamWriter:
self.streamWriter.appendData(string + '\n')
self.streamWriter.write(string + '\n')
else:
print >> sys.stderr, string

View File

@ -61,20 +61,22 @@ def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
# We can also "open" a VirtualFile object for reading.
vfile = file
filename = vfile.getFilename()
elif isinstance(file, unicode):
# If a raw string is given, assume it's an os-specific
# filename.
filename = core.Filename.fromOsSpecificW(file)
elif isinstance(file, str):
filename = core.Filename.fromOsSpecific(file)
else:
# If a Filename is given, make a writable copy anyway.
filename = core.Filename(file)
if binary or sys.version_info >= (3, 0):
filename.setBinary()
else:
# Otherwise, we must have been given a filename. Open it.
if isinstance(file, unicode):
# If a raw string is given, assume it's an os-specific
# filename.
filename = core.Filename.fromOsSpecificW(file)
elif isinstance(file, str):
filename = core.Filename.fromOsSpecific(file)
else:
# If a Filename is given, make a writable copy anyway.
filename = core.Filename(file)
filename.setText()
filename.setBinary()
if not vfile:
vfile = _vfs.getFile(filename)
if not vfile:
@ -108,7 +110,7 @@ def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
if updating:
stream = vfile.openReadWriteFile(True)
else:
stream = vfile.openWriteFile(False)
stream = vfile.openWriteFile(False, True)
if not stream:
raise IOError("Could not open %s for writing" % (filename))
@ -133,6 +135,10 @@ def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
if binary:
return raw
# If we're in Python 2, we don't decode unicode strings by default.
if not encoding and sys.version_info < (3, 0):
return raw
line_buffering = False
if buffering == 1:
line_buffering = True
@ -165,7 +171,7 @@ class StreamIOWrapper(io.IOBase):
self.__lastWrite = False
if isinstance(stream, core.Istream):
self.__reader = core.StreamReader(self.__stream, False)
self.__reader = core.StreamReader(stream, False)
if isinstance(stream, core.Ostream):
self.__writer = core.StreamWriter(stream, False)
@ -226,6 +232,8 @@ class StreamIOWrapper(io.IOBase):
result += self.__reader.extractBytes(512)
return result
read1 = read
def readline(self, size=-1):
if not self.__reader:
if not self.__writer:

View File

@ -395,7 +395,7 @@ add_fixed_string(const string &str, size_t size) {
////////////////////////////////////////////////////////////////////
// Function: StreamWriter::append_data
// Access: Published
// Access: Public
// Description: Appends some more raw data to the end of the
// streamWriter.
////////////////////////////////////////////////////////////////////
@ -406,7 +406,7 @@ append_data(const void *data, size_t size) {
////////////////////////////////////////////////////////////////////
// Function: StreamWriter::append_data
// Access: Published
// Access: Public
// Description: Appends some more raw data to the end of the
// streamWriter.
////////////////////////////////////////////////////////////////////

View File

@ -71,13 +71,16 @@ PUBLISHED:
BLOCKING INLINE void add_fixed_string(const string &str, size_t size);
BLOCKING void pad_bytes(size_t size);
BLOCKING INLINE void append_data(const void *data, size_t size);
BLOCKING INLINE void append_data(const string &data);
EXTENSION(void append_data(PyObject *data));
BLOCKING INLINE void flush();
BLOCKING INLINE void write(const string &str);
public:
BLOCKING INLINE void append_data(const void *data, size_t size);
BLOCKING INLINE void append_data(const string &data);
private:
ostream *_out;
bool _owns_stream;

View File

@ -3,6 +3,7 @@
#include "memoryUsagePointers_ext.cxx"
#include "ramfile_ext.cxx"
#include "streamReader_ext.cxx"
#include "streamWriter_ext.cxx"
#include "typeHandle_ext.cxx"
#include "virtualFileSystem_ext.cxx"
#include "virtualFile_ext.cxx"

View File

@ -0,0 +1,39 @@
// Filename: streamWriter_ext.cxx
// Created by: rdb (19Sep15)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#include "streamWriter_ext.h"
#ifdef HAVE_PYTHON
////////////////////////////////////////////////////////////////////
// Function: StreamWriter::append_data
// Access: Published
// Description: Appends some more raw data to the end of the
// StreamWriter.
////////////////////////////////////////////////////////////////////
void Extension<StreamWriter>::
append_data(PyObject *data) {
cerr << "getting here: " << data << "\n";
Py_buffer view;
if (PyObject_GetBuffer(data, &view, PyBUF_CONTIG_RO) == -1) {
//PyErr_SetString(PyExc_TypeError,
// "append_data() requires a contiguous buffer");
return;
}
_this->append_data(view.buf, view.len);
PyBuffer_Release(&view);
}
#endif

View File

@ -0,0 +1,40 @@
// Filename: streamWriter_ext.h
// Created by: rdb (19Sep15)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#ifndef STREAMWRITER_EXT_H
#define STREAMWRITER_EXT_H
#include "dtoolbase.h"
#ifdef HAVE_PYTHON
#include "extension.h"
#include "streamWriter.h"
#include "py_panda.h"
////////////////////////////////////////////////////////////////////
// Class : Extension<StreamWriter>
// Description : This class defines the extension methods for
// StreamWriter, which are called instead of
// any C++ methods with the same prototype.
////////////////////////////////////////////////////////////////////
template<>
class Extension<StreamWriter> : public ExtensionBase<StreamWriter> {
public:
void append_data(PyObject *data);
};
#endif // HAVE_PYTHON
#endif // STREAMWriter_EXT_H