Skip to content

Commit bc37725

Browse files
committed
Move _strerror() into utils.py + remove relics about win CE.
1 parent cdfc88e commit bc37725

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

pyftpdlib/handlers.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from .ioloop import timer
4242
from .log import debug
4343
from .log import logger
44+
from .utils import strerror
4445

4546
CR_BYTE = ord("\r")
4647

@@ -331,19 +332,6 @@
331332
del proto_cmds["SITE CHMOD"]
332333

333334

334-
def _strerror(err):
335-
if isinstance(err, EnvironmentError):
336-
try:
337-
return os.strerror(err.errno)
338-
except AttributeError:
339-
# not available on PythonCE
340-
if not hasattr(os, "strerror"):
341-
return err.strerror
342-
raise
343-
else:
344-
return str(err)
345-
346-
347335
def _is_ssl_sock(sock):
348336
return SSL is not None and isinstance(sock, SSL.Connection)
349337

@@ -975,7 +963,7 @@ def handle_error(self):
975963
# an error could occur in case we fail reading / writing
976964
# from / to file (e.g. file system gets full)
977965
except _FileReadWriteError as err:
978-
error = _strerror(err.errno)
966+
error = strerror(err.errno)
979967
except Exception:
980968
# some other exception occurred; we don't want to provide
981969
# confidential error messages
@@ -2325,7 +2313,7 @@ def ftp_LIST(self, path):
23252313
self.fs.lstat(path) # raise exc in case of problems
23262314
iterator = self.fs.format_list(basedir, [filename])
23272315
except (OSError, FilesystemError) as err:
2328-
why = _strerror(err)
2316+
why = strerror(err)
23292317
self.respond(f"550 {why}.")
23302318
else:
23312319
producer = BufferedIteratorProducer(iterator)
@@ -2345,7 +2333,7 @@ def ftp_NLST(self, path):
23452333
self.fs.lstat(path) # raise exc in case of problems
23462334
listing = [os.path.basename(path)]
23472335
except (OSError, FilesystemError) as err:
2348-
self.respond(f"550 {_strerror(err)}.")
2336+
self.respond(f"550 {strerror(err)}.")
23492337
else:
23502338
data = ""
23512339
if listing:
@@ -2382,7 +2370,7 @@ def ftp_MLST(self, path):
23822370
)
23832371
data = b"".join(iterator)
23842372
except (OSError, FilesystemError) as err:
2385-
self.respond(f"550 {_strerror(err)}.")
2373+
self.respond(f"550 {strerror(err)}.")
23862374
else:
23872375
data = data.decode(self.encoding, self.unicode_errors)
23882376
# since TVFS is supported (see RFC-3659 chapter 6), a fully
@@ -2407,7 +2395,7 @@ def ftp_MLSD(self, path):
24072395
try:
24082396
listing = self.run_as_current_user(self.fs.listdir, path)
24092397
except (OSError, FilesystemError) as err:
2410-
why = _strerror(err)
2398+
why = strerror(err)
24112399
self.respond(f"550 {why}.")
24122400
else:
24132401
perms = self.authorizer.get_perms(self.username)
@@ -2427,7 +2415,7 @@ def ftp_RETR(self, file):
24272415
try:
24282416
fd = self.run_as_current_user(self.fs.open, file, "rb")
24292417
except (OSError, FilesystemError) as err:
2430-
why = _strerror(err)
2418+
why = strerror(err)
24312419
self.respond(f"550 {why}.")
24322420
return
24332421

@@ -2448,7 +2436,7 @@ def ftp_RETR(self, file):
24482436
except ValueError:
24492437
why = f"REST position ({rest_pos}) > file size ({fsize})"
24502438
except (OSError, FilesystemError) as err:
2451-
why = _strerror(err)
2439+
why = strerror(err)
24522440
if not ok:
24532441
fd.close()
24542442
self.respond(f"554 {why}")
@@ -2477,7 +2465,7 @@ def ftp_STOR(self, file, mode="w"):
24772465
try:
24782466
fd = self.run_as_current_user(self.fs.open, file, mode + "b")
24792467
except (OSError, FilesystemError) as err:
2480-
why = _strerror(err)
2468+
why = strerror(err)
24812469
self.respond(f"550 {why}.")
24822470
return
24832471

@@ -2498,7 +2486,7 @@ def ftp_STOR(self, file, mode="w"):
24982486
except ValueError:
24992487
why = f"REST position ({rest_pos}) > file size ({fsize})"
25002488
except (OSError, FilesystemError) as err:
2501-
why = _strerror(err)
2489+
why = strerror(err)
25022490
if not ok:
25032491
fd.close()
25042492
self.respond(f"554 {why}")
@@ -2554,7 +2542,7 @@ def ftp_STOU(self, line):
25542542
why = "No usable unique file name found"
25552543
# something else happened
25562544
else:
2557-
why = _strerror(err)
2545+
why = strerror(err)
25582546
self.respond(f"450 {why}.")
25592547
return
25602548

@@ -2776,7 +2764,7 @@ def ftp_CWD(self, path):
27762764
try:
27772765
self.run_as_current_user(self.fs.chdir, path)
27782766
except (OSError, FilesystemError) as err:
2779-
why = _strerror(err)
2767+
why = strerror(err)
27802768
self.respond(f"550 {why}.")
27812769
else:
27822770
cwd = self.fs.cwd
@@ -2821,7 +2809,7 @@ def ftp_SIZE(self, path):
28212809
try:
28222810
size = self.run_as_current_user(self.fs.getsize, path)
28232811
except (OSError, FilesystemError) as err:
2824-
why = _strerror(err)
2812+
why = strerror(err)
28252813
self.respond(f"550 {why}.")
28262814
else:
28272815
self.respond(f"213 {size}")
@@ -2845,7 +2833,7 @@ def ftp_MDTM(self, path):
28452833
# happens to be too old (prior to year 1900)
28462834
why = "Can't determine file's last modification time"
28472835
else:
2848-
why = _strerror(err)
2836+
why = strerror(err)
28492837
self.respond(f"550 {why}.")
28502838
else:
28512839
self.respond(f"213 {lmt}")
@@ -2892,7 +2880,7 @@ def ftp_MFMT(self, path, timeval):
28922880
# happens to be too old (prior to year 1900)
28932881
why = "Can't determine file's last modification time"
28942882
else:
2895-
why = _strerror(err)
2883+
why = strerror(err)
28962884
self.respond(f"550 {why}.")
28972885
else:
28982886
self.respond(f"213 Modify={lmt}; {line}.")
@@ -2906,7 +2894,7 @@ def ftp_MKD(self, path):
29062894
try:
29072895
self.run_as_current_user(self.fs.mkdir, path)
29082896
except (OSError, FilesystemError) as err:
2909-
why = _strerror(err)
2897+
why = strerror(err)
29102898
self.respond(f"550 {why}.")
29112899
else:
29122900
# The 257 response is supposed to include the directory
@@ -2928,7 +2916,7 @@ def ftp_RMD(self, path):
29282916
try:
29292917
self.run_as_current_user(self.fs.rmdir, path)
29302918
except (OSError, FilesystemError) as err:
2931-
why = _strerror(err)
2919+
why = strerror(err)
29322920
self.respond(f"550 {why}.")
29332921
else:
29342922
self.respond("250 Directory removed.")
@@ -2940,7 +2928,7 @@ def ftp_DELE(self, path):
29402928
try:
29412929
self.run_as_current_user(self.fs.remove, path)
29422930
except (OSError, FilesystemError) as err:
2943-
why = _strerror(err)
2931+
why = strerror(err)
29442932
self.respond(f"550 {why}.")
29452933
else:
29462934
self.respond("250 File removed.")
@@ -2970,7 +2958,7 @@ def ftp_RNTO(self, path):
29702958
try:
29712959
self.run_as_current_user(self.fs.rename, src, path)
29722960
except (OSError, FilesystemError) as err:
2973-
why = _strerror(err)
2961+
why = strerror(err)
29742962
self.respond(f"550 {why}.")
29752963
else:
29762964
self.respond("250 Renaming ok.")
@@ -3082,7 +3070,7 @@ def ftp_STAT(self, path):
30823070
self.fs.lstat(path) # raise exc in case of problems
30833071
iterator = self.fs.format_list(basedir, [filename])
30843072
except (OSError, FilesystemError) as err:
3085-
why = _strerror(err)
3073+
why = strerror(err)
30863074
self.respond(f"550 {why}.")
30873075
else:
30883076
self.push(f'213-Status of "{line}":\r\n')
@@ -3206,7 +3194,7 @@ def ftp_SITE_CHMOD(self, path, mode):
32063194
try:
32073195
self.run_as_current_user(self.fs.chmod, path, mode)
32083196
except (OSError, FilesystemError) as err:
3209-
why = _strerror(err)
3197+
why = strerror(err)
32103198
self.respond(f"550 {why}.")
32113199
else:
32123200
self.respond("200 SITE CHMOD successful.")

pyftpdlib/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,9 @@ def hilite(s, color=None, bold=False): # pragma: no cover
6767
if bold:
6868
attr.append("1")
6969
return f"\x1b[{';'.join(attr)}m{s}\x1b[0m"
70+
71+
72+
def strerror(err):
73+
if isinstance(err, OSError):
74+
return os.strerror(err.errno)
75+
return str(err)

0 commit comments

Comments
 (0)