@@ -21,7 +21,7 @@ from sftp_handles cimport SFTPFile, SFTPDir
2121from sftp_attributes cimport SFTPAttributes
2222from sftp_statvfs cimport SFTPStatVFS
2323from utils cimport handle_ssh_error_codes, to_bytes
24- from .exceptions import SFTPError
24+ from .exceptions import SFTPError, SFTPHandleError
2525
2626cimport c_sftp
2727from c_ssh cimport ssh_get_error, ssh_get_error_code, timeval
@@ -98,7 +98,7 @@ cdef class SFTP:
9898 with nogil:
9999 c_dir = c_sftp.sftp_opendir(self ._sftp, c_path)
100100 if c_dir is NULL :
101- raise SFTPError (ssh_get_error(self .session._session))
101+ raise SFTPHandleError (ssh_get_error(self .session._session))
102102 _dir = SFTPDir.from_ptr(c_dir, self )
103103 return _dir
104104
@@ -134,7 +134,7 @@ cdef class SFTP:
134134 with nogil:
135135 c_file = c_sftp.sftp_open(self ._sftp, c_path, accesstype, mode)
136136 if c_file is NULL :
137- raise SFTPError (ssh_get_error(self .session._session))
137+ raise SFTPHandleError (ssh_get_error(self .session._session))
138138 _file = SFTPFile.from_ptr(c_file, self )
139139 return _file
140140
@@ -144,15 +144,19 @@ cdef class SFTP:
144144 cdef int rc
145145 with nogil:
146146 rc = c_sftp.sftp_unlink(self ._sftp, c_path)
147- return handle_ssh_error_codes(rc, self .session._session)
147+ if rc < 0 :
148+ raise SFTPError(ssh_get_error(self .session._session))
149+ return rc
148150
149151 def rmdir (self , path not None ):
150152 cdef bytes b_path = to_bytes(path)
151153 cdef const char * c_path = b_path
152154 cdef int rc
153155 with nogil:
154156 rc = c_sftp.sftp_rmdir(self ._sftp, c_path)
155- return handle_ssh_error_codes(rc, self .session._session)
157+ if rc < 0 :
158+ raise SFTPError(ssh_get_error(self .session._session))
159+ return rc
156160
157161 def mkdir (self , path not None , c_sftp.mode_t mode ):
158162 cdef bytes b_path = to_bytes(path)
@@ -170,7 +174,9 @@ cdef class SFTP:
170174 cdef int rc
171175 with nogil:
172176 rc = c_sftp.sftp_rename(self ._sftp, c_orig, c_newname)
173- return handle_ssh_error_codes(rc, self .session._session)
177+ if rc < 0 :
178+ raise SFTPError(ssh_get_error(self .session._session))
179+ return rc
174180
175181 def setstat (self , path not None , SFTPAttributes attr ):
176182 cdef bytes b_path = to_bytes(path)
@@ -187,15 +193,19 @@ cdef class SFTP:
187193 cdef int rc
188194 with nogil:
189195 rc = c_sftp.sftp_chown(self ._sftp, c_path, owner, group)
190- return handle_ssh_error_codes(rc, self .session._session)
196+ if rc < 0 :
197+ raise SFTPError(ssh_get_error(self .session._session))
198+ return rc
191199
192200 def chmod (self , path not None , c_sftp.mode_t mode ):
193201 cdef bytes b_path = to_bytes(path)
194202 cdef const char * c_path = b_path
195203 cdef int rc
196204 with nogil:
197205 rc = c_sftp.sftp_chmod(self ._sftp, c_path, mode)
198- return handle_ssh_error_codes(rc, self .session._session)
206+ if rc < 0 :
207+ raise SFTPError(ssh_get_error(self .session._session))
208+ return rc
199209
200210 def utimes (self , path not None , long seconds , long microseconds ):
201211 cdef bytes b_path = to_bytes(path)
@@ -211,7 +221,9 @@ cdef class SFTP:
211221 _val.tv_usec = microseconds
212222 rc = c_sftp.sftp_utimes(self ._sftp, c_path, _val)
213223 free(_val)
214- return handle_ssh_error_codes(rc, self .session._session)
224+ if rc < 0 :
225+ raise SFTPError(ssh_get_error(self .session._session))
226+ return rc
215227
216228 def symlink (self , source not None , dest not None ):
217229 cdef bytes b_source = to_bytes(source)
@@ -221,7 +233,9 @@ cdef class SFTP:
221233 cdef int rc
222234 with nogil:
223235 rc = c_sftp.sftp_symlink(self ._sftp, c_source, c_dest)
224- return handle_ssh_error_codes(rc, self .session._session)
236+ if rc < 0 :
237+ raise SFTPError(ssh_get_error(self .session._session))
238+ return rc
225239
226240 def readlink (self , path not None ):
227241 cdef bytes b_path = to_bytes(path)
@@ -231,9 +245,7 @@ cdef class SFTP:
231245 with nogil:
232246 _link = c_sftp.sftp_readlink(self ._sftp, c_path)
233247 if _link is NULL :
234- return handle_ssh_error_codes(
235- ssh_get_error_code(self .session._session),
236- self .session._session)
248+ raise SFTPError(ssh_get_error(self .session._session))
237249 b_link = _link
238250 return b_link
239251
@@ -245,9 +257,7 @@ cdef class SFTP:
245257 with nogil:
246258 c_vfs = c_sftp.sftp_statvfs(self ._sftp, c_path)
247259 if c_vfs is NULL :
248- return handle_ssh_error_codes(
249- ssh_get_error_code(self .session._session),
250- self .session._session)
260+ raise SFTPError(ssh_get_error(self .session._session))
251261 vfs = SFTPStatVFS.from_ptr(c_vfs, self )
252262 return vfs
253263
@@ -259,14 +269,12 @@ cdef class SFTP:
259269 with nogil:
260270 _rpath = c_sftp.sftp_canonicalize_path(self ._sftp, c_path)
261271 if _rpath is NULL :
262- return handle_ssh_error_codes(
263- ssh_get_error_code(self .session._session),
264- self .session._session)
272+ raise SFTPError(ssh_get_error(self .session._session))
265273 b_rpath = _rpath
266274 return b_rpath
267275
268276 def server_version (self ):
269277 cdef int rc
270278 with nogil:
271279 rc = c_sftp.sftp_server_version(self ._sftp)
272- return rc
280+ return handle_ssh_error_codes(rc, self .session._session)
0 commit comments