[issue25933] Unhandled exception (TypeError) with ftplib in function retrbinary/retrlines causes inoperable behavior without crashing

2015-12-23 Thread Sam Adams

New submission from Sam Adams:

Hello,

I believe that I found a bug in ftplib.py in version 3.3.6.

Whenever a user opens a file for writing in nonbinary mode and then proceeds to 
call the retrbinary function, or opens the file in binary mode and proceeds to 
call the retrlines fuction, then attempts to write to the file using the 
callback function, an unhandled TypeError exception is raised that does not 
cause the program to crash, but rather cause the last message from the server 
to not be "read" by the readline() function, thus causing cascading errors 
until the function readline() is called on its own or the connection with the 
server is reset.

Code to replicate:
from ftplib import FTP
ftp = FTP('your.server.here')
ftp.login()
fileTest = open('filename','w') < not binary
ftp.retrbinary('retr randomfilehere',fileTest.write)

Unhandled exception raised: 

File "/usr/local/lib/python3.3/ftplib.py", line 434, in retrbinary
callback(data)
TypeError: must be str, not bytes

Likewise, if 

ftp.retrlines('retr randomfilehere', fileTest.write) 

is called when fileTest is opened for writing with binary option the following 
error is raised:

File "/usr/local/lib/python3.3/ftplib.py", line 464, in retrlines
callback(line)
TypeError: 'str' does not support the buffer interface

Calling ftp.getline() clears the error and resumes normal operation


Possible Solution:

add exception handling in lines 434/464 for TypeError

--
components: Library (Lib)
messages: 256927
nosy: Sam Adams
priority: normal
severity: normal
status: open
title: Unhandled exception (TypeError) with ftplib in function 
retrbinary/retrlines causes inoperable behavior without crashing
versions: Python 3.3

___
Python tracker 
<http://bugs.python.org/issue25933>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25933] Unhandled exception (TypeError) with ftplib in function retrbinary/retrlines causes inoperable behavior without crashing

2015-12-23 Thread Sam Adams

Changes by Sam Adams :


--
type:  -> behavior

___
Python tracker 
<http://bugs.python.org/issue25933>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25933] Unhandled exception (TypeError) with ftplib in function retrbinary/retrlines causes inoperable behavior without crashing

2015-12-23 Thread Sam Adams

Sam Adams added the comment:

I don't have access to 3.5 where I am now. I can try later on, but it appears 
after a quick glance that the code for this function between 3.3 and 3.5 is the 
same for calling the callback function.

--

___
Python tracker 
<http://bugs.python.org/issue25933>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25933] Unhandled exception (TypeError) with ftplib in function retrbinary/retrlines causes inoperable behavior without crashing

2015-12-23 Thread Sam Adams

Sam Adams added the comment:

Silent: The issue that i see is how the error is handled. I can trap the 
TypeError easily, however, if I keep the socket open, the behavior of ftplib 
will not be as intended. For example:

fileTest = open('filename1', 'wb')

ftp.retrlines('RETR README.html', fileTest.write)

This will give a TypeError, as intended

However, what happens next is, I believe, not intended --

If i send a command after this trapped error, say, ftp.sendcmd('NOOP')

The result will not be 
200 NOOP command successful

but, instead,

226 Transfer Complete

As the previous status was not read from the file created by the socket, so if 
i want to try and do anything for the user after, the message received, and 
subsequently parsed by the getresp function, will not be the message that is 
expected.

For a noop command this is ok, but for a transfer command, it will set the mode 
to either A or I and return a 200 status when a different status was expected. 
This will raise an error. 

The only way to fix this is to either run the internal function getline() or 
reset the connection, thus clearing all messages from the file.

I have attached a file that has a fix, Im not sure if this is helpful or not.

I modified the retrbinary function on lines:
440,449-450,454-455

I also modified the retrlines function on lines:
470,490-491,496-497

--
Added file: http://bugs.python.org/file41400/ftplib.py

___
Python tracker 
<http://bugs.python.org/issue25933>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com