@@ -22,7 +22,7 @@ cimport error_codes
2222
2323from session cimport Session
2424from exceptions cimport ChannelError
25- # from utils cimport to_bytes
25+ from utils cimport to_bytes
2626
2727
2828cdef object PyChannel(c_ssh2.LIBSSH2_CHANNEL * channel, Session session):
@@ -39,6 +39,7 @@ cdef class Channel:
3939
4040 def __dealloc__ (self ):
4141 with nogil:
42+ c_ssh2.libssh2_channel_close(self ._channel)
4243 c_ssh2.libssh2_channel_free(self ._channel)
4344
4445 def pty (self , term = " vt100" ):
@@ -54,27 +55,39 @@ cdef class Channel:
5455 rc)
5556 return rc
5657
57- def execute (self , const char *command ):
58+ def execute (self , command ):
59+ """ Execute command.
60+
61+ :param command: Command to execute
62+ :type command: str
63+
64+ :raises: :py:class:`ssh2.exceptions.ChannelError` on errors executing
65+ command
66+
67+ :rtype: int
68+ """
5869 cdef int rc
70+ cdef char * _command = to_bytes(command)
5971 with nogil:
6072 rc = c_ssh2.libssh2_channel_exec(
61- self ._channel, command )
73+ self ._channel, _command )
6274 if rc != 0 and rc != c_ssh2._LIBSSH2_ERROR_EAGAIN:
6375 with gil:
6476 raise ChannelError(
6577 " Error executing command %s - error code %s " ,
6678 command, rc)
6779 return rc
6880
69- def subsystem (self , const char * subsystem ):
81+ def subsystem (self , subsystem ):
7082 """ Request subsystem from channel
7183
7284 :param subsystem: Name of subsystem
7385 :type subsystem: str"""
7486 cdef int rc
87+ cdef char * _subsystem = to_bytes(subsystem)
7588 with nogil:
7689 rc = c_ssh2.libssh2_channel_subsystem(
77- self ._channel, subsystem )
90+ self ._channel, _subsystem )
7891 if rc != 0 and rc != c_ssh2._LIBSSH2_ERROR_EAGAIN:
7992 with gil:
8093 raise ChannelError(
@@ -233,11 +246,13 @@ cdef class Channel:
233246 py_langtag = langtag[:py_langlen]
234247 return rc, py_exitsignal, py_errmsg, py_langtag
235248
236- def setenv (self , const char * varname , const char * value ):
249+ def setenv (self , varname , value ):
237250 cdef int rc
251+ cdef char * _varname = to_bytes(varname)
252+ cdef char * _value = to_bytes(value)
238253 with nogil:
239254 rc = c_ssh2.libssh2_channel_setenv(
240- self ._channel, varname, value )
255+ self ._channel, _varname, _value )
241256 return rc
242257
243258 def window_read_ex (self , unsigned long read_avail ,
@@ -267,29 +282,29 @@ cdef class Channel:
267282 rc = c_ssh2.libssh2_channel_window_write(self ._channel)
268283 return rc
269284
270- def write (self , bytes buf ):
285+ def write (self , buf ):
271286 """ Write buffer to stdin"""
272- cdef const char * _buf = buf
273- cdef size_t buflen = len (buf )
287+ cdef const char * _buf = to_bytes( buf)
288+ cdef size_t buflen = len (_buf )
274289 cdef ssize_t rc
275290 with nogil:
276291 rc = c_ssh2.libssh2_channel_write(self ._channel, _buf, buflen)
277292 return rc
278293
279- def write_ex (self , int stream_id , bytes buf ):
294+ def write_ex (self , int stream_id , buf ):
280295 """ Write buffer to specified stream id"""
281- cdef const char * _buf = buf
282- cdef size_t buflen = len (buf )
296+ cdef const char * _buf = to_bytes( buf)
297+ cdef size_t buflen = len (_buf )
283298 cdef ssize_t rc
284299 with nogil:
285300 rc = c_ssh2.libssh2_channel_write_ex(
286301 self ._channel, stream_id, _buf, buflen)
287302 return rc
288303
289- def write_stderr (self , bytes buf ):
304+ def write_stderr (self , buf ):
290305 """ Write buffer to stderr"""
291- cdef const char * _buf = buf
292- cdef size_t buflen = len (buf )
306+ cdef const char * _buf = to_bytes( buf)
307+ cdef size_t buflen = len (_buf )
293308 cdef ssize_t rc
294309 with nogil:
295310 rc = c_ssh2.libssh2_channel_write_stderr(
0 commit comments