Kaynağa Gözat

Fix some issues with new VFS open() implementation

rdb 10 yıl önce
ebeveyn
işleme
49088fb7ce

+ 1 - 1
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
 

+ 21 - 13
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()
-            filename.setBinary()
+        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:
-            # 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)
+            # 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:
+            filename.setText()
+
+        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:

+ 2 - 2
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.
 ////////////////////////////////////////////////////////////////////

+ 5 - 2
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;

+ 1 - 0
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"

+ 39 - 0
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<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

+ 40 - 0
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<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