Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f96482e

Browse files
committed
as per discussion with antoine revert changes made in 83708 as the user useing ftplib's readline methods is supposed to always use a binary file
1 parent 226e945 commit f96482e

4 files changed

Lines changed: 12 additions & 52 deletions

File tree

Lib/ftplib.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,9 @@ def storlines(self, cmd, fp, callback=None):
493493
while 1:
494494
buf = fp.readline()
495495
if not buf: break
496-
if isinstance(buf, str):
497-
if not buf.endswith(CRLF):
498-
if buf[-1] in CRLF: buf = buf[:-1]
499-
buf = buf + CRLF
500-
buf = bytes(buf, self.encoding)
501-
else:
502-
if not buf.endswith(B_CRLF):
503-
if buf[-1:] in B_CRLF: buf = buf[:-1]
504-
buf = buf + B_CRLF
496+
if buf[-2:] != B_CRLF:
497+
if buf[-1] in B_CRLF: buf = buf[:-1]
498+
buf = buf + B_CRLF
505499
conn.sendall(buf)
506500
if callback: callback(buf)
507501
conn.close()
@@ -777,15 +771,9 @@ def storlines(self, cmd, fp, callback=None):
777771
while 1:
778772
buf = fp.readline()
779773
if not buf: break
780-
if isinstance(buf, str):
781-
if not buf.endswith(CRLF):
782-
if buf[-1] in CRLF: buf = buf[:-1]
783-
buf = buf + CRLF
784-
buf = bytes(buf, self.encoding)
785-
else:
786-
if not buf.endswith(B_CRLF):
787-
if buf[-1:] in B_CRLF: buf = buf[:-1]
788-
buf = buf + B_CRLF
774+
if buf[-2:] != B_CRLF:
775+
if buf[-1] in B_CRLF: buf = buf[:-1]
776+
buf = buf + B_CRLF
789777
conn.sendall(buf)
790778
if callback: callback(buf)
791779
# shutdown ssl layer
@@ -795,7 +783,6 @@ def storlines(self, cmd, fp, callback=None):
795783
conn.close()
796784
return self.voidresp()
797785

798-
799786
__all__.append('FTP_TLS')
800787
all_errors = (Error, IOError, EOFError, ssl.SSLError)
801788

Lib/test/test_ftplib.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
# the dummy data returned by server over the data channel when
2525
# RETR, LIST and NLST commands are issued
2626
RETR_DATA = 'abcde12345\r\n' * 1000
27-
RETR_TEXT = 'abcd\xe912345\r\n' * 1000
2827
LIST_DATA = 'foo\r\nbar\r\n'
2928
NLST_DATA = 'foo\r\nbar\r\n'
3029

@@ -38,7 +37,7 @@ def __init__(self, conn, baseclass):
3837
self.baseclass.last_received_data = ''
3938

4039
def handle_read(self):
41-
self.baseclass.last_received_data += self.recv(1024).decode('latin-1')
40+
self.baseclass.last_received_data += self.recv(1024).decode('ascii')
4241

4342
def handle_close(self):
4443
# XXX: this method can be called many times in a row for a single
@@ -50,7 +49,7 @@ def handle_close(self):
5049
self.dtp_conn_closed = True
5150

5251
def push(self, what):
53-
super(DummyDTPHandler, self).push(what.encode('latin-1'))
52+
super(DummyDTPHandler, self).push(what.encode('ascii'))
5453

5554
def handle_error(self):
5655
raise
@@ -69,7 +68,6 @@ def __init__(self, conn):
6968
self.last_received_data = ''
7069
self.next_response = ''
7170
self.rest = None
72-
self.current_type = 'a'
7371
self.push('220 welcome')
7472

7573
def collect_incoming_data(self, data):
@@ -177,16 +175,7 @@ def cmd_pwd(self, arg):
177175
self.push('257 "pwd ok"')
178176

179177
def cmd_type(self, arg):
180-
# ASCII type
181-
if arg.lower() == 'a':
182-
self.current_type = 'a'
183-
self.push('200 type ok')
184-
# Binary type
185-
elif arg.lower() == 'i':
186-
self.current_type = 'i'
187-
self.push('200 type ok')
188-
else:
189-
self.push('504 unsupported type')
178+
self.push('200 type ok')
190179

191180
def cmd_quit(self, arg):
192181
self.push('221 quit ok')
@@ -205,10 +194,7 @@ def cmd_retr(self, arg):
205194
offset = int(self.rest)
206195
else:
207196
offset = 0
208-
if self.current_type == 'i':
209-
self.dtp.push(RETR_DATA[offset:])
210-
else:
211-
self.dtp.push(RETR_TEXT[offset:])
197+
self.dtp.push(RETR_DATA[offset:])
212198
self.dtp.close_when_done()
213199
self.rest = None
214200

@@ -525,7 +511,7 @@ def callback(data):
525511
def test_retrlines(self):
526512
received = []
527513
self.client.retrlines('retr', received.append)
528-
self.assertEqual(''.join(received), RETR_TEXT.replace('\r\n', ''))
514+
self.assertEqual(''.join(received), RETR_DATA.replace('\r\n', ''))
529515

530516
def test_storbinary(self):
531517
f = io.BytesIO(RETR_DATA.encode('ascii'))
@@ -544,7 +530,7 @@ def test_storbinary_rest(self):
544530
self.client.storbinary('stor', f, rest=r)
545531
self.assertEqual(self.server.handler_instance.rest, str(r))
546532

547-
def test_storlines_bytes(self):
533+
def test_storlines(self):
548534
f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii'))
549535
self.client.storlines('stor', f)
550536
self.assertEqual(self.server.handler_instance.last_received_data, RETR_DATA)
@@ -554,16 +540,6 @@ def test_storlines_bytes(self):
554540
self.client.storlines('stor foo', f, callback=lambda x: flag.append(None))
555541
self.assertTrue(flag)
556542

557-
def test_storlines_str(self):
558-
f = io.StringIO(RETR_TEXT.replace('\r\n', '\n'))
559-
self.client.storlines('stor', f)
560-
self.assertEqual(self.server.handler_instance.last_received_data, RETR_TEXT)
561-
# test new callback arg
562-
flag = []
563-
f.seek(0)
564-
self.client.storlines('stor foo', f, callback=lambda x: flag.append(None))
565-
self.assertTrue(flag)
566-
567543
def test_nlst(self):
568544
self.client.nlst()
569545
self.assertEqual(self.client.nlst(), NLST_DATA.split('\r\n')[:-1])

Misc/ACKS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,4 +896,3 @@ Uwe Zessin
896896
Tarek Ziadé
897897
Peter Åstrand
898898
Alexander Shigin
899-
Robert DeVaughn

Misc/NEWS

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ Extensions
3737
Library
3838
-------
3939

40-
- Issue #6822: ftplib's storlines method doesn't work with text files.
41-
4240
- Issue #2944: asyncore doesn't handle connection refused correctly.
4341

4442
- Issue #4184: Private attributes on smtpd.SMTPChannel made public and

0 commit comments

Comments
 (0)