Hi, I found SWIP Python bindings of APIs using svn_stream_t * in _client,
_delta, _diff, _fs, _ra and _repos don't accept core.svn_stream_t proxy
objects, so I modified %typemap(in) svn_stream_t *WRAPPED_STRAM to accept
them as well as file like objects.

The patch below is destined to trunk@1848063, contains test using
core.svn_stream_t object, core.Stream() object for svn_stream_t argments.
(test for using file like object and None type is already exists)

--- Start of patch ---
commit 8c438d48c979efb494072864acc0ddb400f4792d
Author: FUTATSUKI Yasuhito <futat...@yf.bsdclub.org>
Date:   Wed Dec 5 02:46:17 2018 +0900

    On branch improve_swig_py_stream_IF: swig-py: allow svn.core.svn_stream_t
    object for svn_stream_t * interface
* subversion/bindings/swig/include/svn_types.swg
      (%typemap(in) svn_stream_t *WRAPPED_STREAM)
      allow svn.core.svn_stream_t proxy object for svn_stream_t * in args.
      this typemap is used by _client, _delta, _diff, _fs, _ra, and _repos
      modules.

diff --git a/subversion/bindings/swig/include/svn_types.swg 
b/subversion/bindings/swig/include/svn_types.swg
index 4ad5d1658b..91ebb8dd23 100644
--- a/subversion/bindings/swig/include/svn_types.swg
+++ b/subversion/bindings/swig/include/svn_types.swg
@@ -941,7 +941,62 @@ svn_ ## TYPE ## _swig_rb_closed(VALUE self)
#ifdef SWIGPYTHON
 %typemap(in) svn_stream_t *WRAPPED_STREAM {
-    $1 = svn_swig_py_make_stream ($input, _global_pool);
+    if ($input == Py_None) {
+        $1 = NULL;
+    }
+    else {
+        PyObject *libsvn_core;
+        PyObject *py_stream_t;
+        libsvn_core = PyImport_ImportModule("libsvn.core");
+        if (PyErr_Occurred()) {
+            Py_XDECREF(libsvn_core);
+            SWIG_fail;
+        }
+        py_stream_t = PyObject_GetAttrString(libsvn_core, "svn_stream_t");
+        if (PyErr_Occurred()) {
+            Py_XDECREF(py_stream_t);
+            Py_DECREF(libsvn_core);
+            SWIG_fail;
+        }
+        if (PyObject_IsInstance($input, py_stream_t)) {
+            $1 = (svn_stream_t *)svn_swig_py_must_get_ptr(
+                                    $input, $1_descriptor, $argnum);
+            if (PyErr_Occurred()) {
+                Py_DECREF(py_stream_t);
+                Py_DECREF(libsvn_core);
+                SWIG_fail;
+            }
+            Py_DECREF(py_stream_t);
+            Py_DECREF(libsvn_core);
+        }
+        else if (PyObject_HasAttrString($input, "_stream")){
+            PyObject *_stream = PyObject_GetAttrString($input, "_stream");
+            if (PyObject_IsInstance(_stream, py_stream_t)) {
+                $1 = (svn_stream_t *)svn_swig_py_must_get_ptr(
+                                        _stream, $1_descriptor,
+                                        $argnum);
+                if (PyErr_Occurred()) {
+                    PyErr_Clear();
+                    $1 = NULL;
+                }
+                Py_DECREF(_stream);
+                Py_DECREF(py_stream_t);
+                Py_DECREF(libsvn_core);
+            }
+        }
+        if ($1 == NULL) {
+            if (   PyObject_HasAttrString($input, "read")
+                || PyObject_HasAttrString($input, "write")) {
+                $1 = svn_swig_py_make_stream ($input, _global_pool);
+            }
+            else {
+                PyErr_SetString(PyExc_TypeError,
+                                "expecting a svn_stream_t"
+                                " or file like object");
+                SWIG_fail;
+            }
+        }
+    }
 }
 #endif
commit c757e91aa6e5b30acc2e63bd8c3c3b144b09c1f7
Author: FUTATSUKI Yasuhito <futat...@yf.bsdclub.org>
Date:   Wed Dec 5 02:18:03 2018 +0900

    On branch improve_swig_py_stream_IF: add unit test svn_stream_t * I/F
* subversion/bindings/swig/python/tests/delta.py
      (DeltaTestCase.testTxWindowHandler_stream_IF): New test for svn_stream_t *
      interface wrapper accept svn.core.svn_stream_t proxy object.
      (DeltaTestCase.testTxWindowHandler_Stream_IF): New test for svn_stream_t *
      interface wrapper accept svn.core.Stream wrapper object.

diff --git a/subversion/bindings/swig/python/tests/delta.py 
b/subversion/bindings/swig/python/tests/delta.py
index 162cf322de..7f6ba6782f 100644
--- a/subversion/bindings/swig/python/tests/delta.py
+++ b/subversion/bindings/swig/python/tests/delta.py
@@ -19,6 +19,8 @@
 #
 #
 import unittest, setup_path
+import os
+import tempfile
 import svn.delta
 import svn.core
 from sys import version_info # For Python version check
@@ -47,6 +49,59 @@ class DeltaTestCase(unittest.TestCase):
        svn.delta.tx_apply(src_stream, target_stream, None)
     window_handler(None, baton)
+ def testTxWindowHandler_stream_IF(self):
+    """Test tx_invoke_window_handler, with svn.core.svn_stream_t object"""
+    pool = svn.core.Pool()
+    in_str = "hello world"
+    src_stream = svn.core.svn_stream_from_stringbuf(in_str)
+    content_str = "bye world"
+    content_stream = svn.core.svn_stream_from_stringbuf(content_str)
+    fd, fname = tempfile.mkstemp()
+    os.close(fd)
+    try:
+      target_stream = svn.core.svn_stream_from_aprfile2(fname, False)
+      window_handler, baton = \
+          svn.delta.tx_apply(src_stream, target_stream, None)
+      svn.delta.tx_send_stream(content_stream, window_handler, baton, pool)
+      fp = open(fname, 'rb')
+      out_str = fp.read()
+      fp.close()
+      self.assertEqual(content_str, out_str)
+    finally:
+      del pool
+      try:
+        os.remove(fname)
+      except OSError:
+        pass
+
+  def testTxWindowHandler_Stream_IF(self):
+    """Test tx_invoke_window_handler, with svn.core.Stream object"""
+    pool = svn.core.Pool()
+    in_str = "hello world"
+    src_stream = svn.core.Stream(
+                    svn.core.svn_stream_from_stringbuf(in_str))
+    content_str = "bye world"
+    content_stream = svn.core.Stream(
+                    svn.core.svn_stream_from_stringbuf(content_str))
+    fd, fname = tempfile.mkstemp()
+    os.close(fd)
+    try:
+      target_stream = svn.core.Stream(
+                    svn.core.svn_stream_from_aprfile2(fname, False))
+      window_handler, baton = \
+          svn.delta.tx_apply(src_stream, target_stream, None)
+      svn.delta.tx_send_stream(content_stream, window_handler, baton, None)
+      fp = open(fname, 'rb')
+      out_str = fp.read()
+      fp.close()
+      self.assertEqual(content_str, out_str)
+    finally:
+      del pool
+      try:
+        os.remove(fname)
+      except OSError:
+        pass
+
   def testTxdeltaWindowT(self):
     """Test the svn_txdelta_window_t wrapper."""
     a = StringIO("abc\ndef\n")

--- END of patch ---
Regards,
--
Yasuthito FUTATSUKI

Reply via email to