Edgewall Software

Ticket #4311 (closed defect: fixed)

Opened 2 years ago

Last modified 8 months ago

AttributeError: 'cStringIO.StringO' object has no attribute 'len'

Reported by: anonymous Owned by: mgood
Priority: high Milestone: 0.10.4
Component: general Version: 0.10.2
Severity: normal Keywords: python25
Cc:

Description

Attaching files doesn't seem to work correctly anymore. This is Trac 0.10.2 and Python 2.5.

Python Traceback

Traceback (most recent call last):
  File "/usr/lib64/python2.5/site-packages/trac/web/main.py", line 387, in dispatch_request
    dispatcher.dispatch(req)
  File "/usr/lib64/python2.5/site-packages/trac/web/main.py", line 238, in dispatch
    resp = chosen_handler.process_request(req)
  File "/usr/lib64/python2.5/site-packages/trac/attachment.py", line 361, in process_request
    self._do_save(req, attachment)
  File "/usr/lib64/python2.5/site-packages/trac/attachment.py", line 449, in _do_save
    size = upload.file.len
AttributeError: 'cStringIO.StringO' object has no attribute 'len'

Attachments

Change History

  Changed 2 years ago by ThurnerRupert

what platform are you on?

  Changed 2 years ago by anonymous

Linux 2.6.18, x86_64, SuSE.

in reply to: ↑ description   Changed 2 years ago by eblot

Replying to anonymous:

Attaching files doesn't seem to work correctly anymore. This is Trac 0.10.2 and Python 2.5.

I don't think this is related to this very issue, but Trac 0.10 is known to have issues when run with Python 2.5, due to issues w/ ClearSilver.

  Changed 2 years ago by mgood

  • keywords needinfo added

Which web frontend are you using? FastCGI, mod_python, CGI, etc.? Which web server?

  Changed 2 years ago by stepan@…

No fastcgi afaik

apache2-2.2.3-20 apache2-mod_python-3.2.10-27 python-2.5-19

what else do you need to know?

  Changed 2 years ago by mgood

  • keywords needinfo removed
  • milestone set to 0.10.3

Hmm, Python 2.5 changed the cgi module to use cStringIO for files if it's available, which unlike the StringIO object doesn't support reading the length. I guess we need to find another way to check the size of the uploaded file.

  Changed 2 years ago by Stefan Reinauer <stepan@…>

My oh my.. I am not becoming a python fan. After wasting about 5h with wild guesses and restarting apache I found the following workaround seems to do it for my small test file. Don't know if it is worth anything.

if hasattr(upload.file, 'fileno'):

size = os.fstat(upload.file.fileno())[6]

else:

fstr = StringIO.StringIO(upload.file) size = fstr.len

  Changed 2 years ago by anonymous

I found the same error and observed that the error comes up, if the size of the uploaded file is smaller than 1kByte (approx.) Maybe this helps to find the reason.

  Changed 2 years ago by cboos

  • keywords python25 added
  • owner changed from jonas to cmlenz
  • component changed from ticket system to mod_python frontend

  Changed 2 years ago by mgood

  • owner changed from cmlenz to jonas
  • component changed from mod_python frontend to general

This is not specific to mod_python. The problem is due to the change in the Python cgi module which is used by Trac for parsing POSTs on all frontends.

follow-up: ↓ 12   Changed 2 years ago by thatch

I think using the file-like interface ought to work since StringIO and cStringIO both provide it. This is constant time for both modules.

Tested on Python 2.4 and it works. Analysis of 2.5's source for cStringIO shows it should work too.

  • trac/attachment.py

     
    448448        if hasattr(upload.file, 'fileno'): 
    449449            size = os.fstat(upload.file.fileno())[6] 
    450450        else: 
    451             size = upload.file.len 
     451            upload.file.seek(0, 2) 
     452            size = upload.file.tell() 
     453            upload.file.seek(0) 
    452454        if size == 0: 
    453455            raise TracError("Can't upload empty file") 

in reply to: ↑ 11   Changed 2 years ago by mgood

  • owner changed from jonas to mgood
  • status changed from new to assigned

Replying to thatch:

I think using the file-like interface ought to work since StringIO and cStringIO both provide it. This is constant time for both modules.

Aha, yes I figured there should be some other way to check the size, but I missed that seek() supported seeking relative to the end of the file. That should do the trick.

  Changed 2 years ago by mgood

  • status changed from assigned to closed
  • resolution set to fixed

Fixed in r4459 and r4460. Thanks for the patch.

follow-up: ↓ 15   Changed 18 months ago by demarco@…

This bug is triggered by attempting to upload a vCard / vcf. These keywords should help others avoid submitting duplicate bugs.

in reply to: ↑ 14   Changed 18 months ago by mgood

Replying to demarco@maya.com:

This bug is triggered by attempting to upload a vCard / vcf. These keywords should help others avoid submitting duplicate bugs.

The problem is not dependent on the file type. It was an issue with an internal API that changed in Python 2.5.

  Changed 14 months ago by kap4020@…

Yup, I have this problem too:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=7.04
DISTRIB_CODENAME=feisty
DISTRIB_DESCRIPTION="Ubuntu 7.04"

But, the patch fixed things for me. =)

  Changed 13 months ago by Didactylos

  • status changed from closed to reopened
  • resolution fixed deleted

I am experiencing this bug in Trac 0.10.4 when trying to attach a file to a template.

Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/trac/web/main.py", line 406, in dispatch_request
    dispatcher.dispatch(req)
  File "/usr/lib/python2.5/site-packages/trac/web/main.py", line 237, in dispatch
    resp = chosen_handler.process_request(req)
  File "build/bdist.linux-i686/egg/WikiTemplates/attachment.py", line 363, in process_request
    self._do_save(req, attachment)
  File "build/bdist.linux-i686/egg/WikiTemplates/attachment.py", line 455, in _do_save
    size = upload.file.len
AttributeError: 'cStringIO.StringO' object has no attribute 'len'

  Changed 13 months ago by cboos

  • status changed from reopened to closed
  • resolution set to fixed

Well, as you could see by following the link to r4459, that size = upload.file.len line has been replaced for 0.10.4. So if you're having that line in your backtrace, you're not running 0.10.4 (and installing Trac as an egg is not supported for versions before 0.11dev).

  Changed 13 months ago by Didactylos

  • status changed from closed to reopened
  • resolution fixed deleted

Look again: this is when attaching a file to a template. I'm a Trac user, not an administrator - so I'm just reporting the bug. I know nothing of eggs. If the bug needs to go elsewhere, or is a separate issue, then please do what needs doing. I'm reporting it here because it is an identical bug to the previously fixed bug - just in a different place.

  Changed 13 months ago by cboos

  • status changed from reopened to closed
  • resolution set to fixed

Ok, my bad, but that bug should be reported on http://wikitemplates.ufsoft.org/, where the WikiTemplates? plugin is developed.

Closing again the ticket, as the problem is fixed in Trac itself.

  Changed 13 months ago by Alexo

You should also patch this line

size = f.len

with

f.seek(0, 2) size = f.tell() f.seek(0)

For newer cStringIO.

Cheers.

Alexo

  Changed 8 months ago by anonymous

Well, -as you could see by following the link to r4459, that size = upload.file.len line has been replaced for 0.10.4. So if you're having that line in your backtrace, you're not running 0.10.4 (and installing Trac as an egg is not supported for versions before 0.11dev).

Add/Change #4311 (AttributeError: 'cStringIO.StringO' object has no attribute 'len')

Author



Change Properties
<Author field>
Action
as closed
Next status will be 'reopened'
to The owner will change from mgood. Next status will be 'closed'
 
Note: See TracTickets for help on using tickets.