diff --git a/direct/src/directnotify/Notifier.py b/direct/src/directnotify/Notifier.py index 816ab8c0df..a49bbf0d03 100644 --- a/direct/src/directnotify/Notifier.py +++ b/direct/src/directnotify/Notifier.py @@ -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 diff --git a/direct/src/stdpy/file.py b/direct/src/stdpy/file.py index 4321a01ed7..463eb8639c 100644 --- a/direct/src/stdpy/file.py +++ b/direct/src/stdpy/file.py @@ -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: diff --git a/dtool/src/prc/streamWriter.I b/dtool/src/prc/streamWriter.I index ce4500a475..4c388c387f 100644 --- a/dtool/src/prc/streamWriter.I +++ b/dtool/src/prc/streamWriter.I @@ -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. //////////////////////////////////////////////////////////////////// diff --git a/dtool/src/prc/streamWriter.h b/dtool/src/prc/streamWriter.h index b63feaf6b1..eb1f3bcf67 100644 --- a/dtool/src/prc/streamWriter.h +++ b/dtool/src/prc/streamWriter.h @@ -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; diff --git a/panda/src/express/p3express_ext_composite.cxx b/panda/src/express/p3express_ext_composite.cxx index 1dab5d4477..e034261ae3 100644 --- a/panda/src/express/p3express_ext_composite.cxx +++ b/panda/src/express/p3express_ext_composite.cxx @@ -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" diff --git a/panda/src/express/streamWriter_ext.cxx b/panda/src/express/streamWriter_ext.cxx new file mode 100644 index 0000000000..552b2a7dbd --- /dev/null +++ b/panda/src/express/streamWriter_ext.cxx @@ -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:: +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 diff --git a/panda/src/express/streamWriter_ext.h b/panda/src/express/streamWriter_ext.h new file mode 100644 index 0000000000..e8e1b270cd --- /dev/null +++ b/panda/src/express/streamWriter_ext.h @@ -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 +// Description : This class defines the extension methods for +// StreamWriter, which are called instead of +// any C++ methods with the same prototype. +//////////////////////////////////////////////////////////////////// +template<> +class Extension : public ExtensionBase { +public: + void append_data(PyObject *data); +}; + +#endif // HAVE_PYTHON + +#endif // STREAMWriter_EXT_H