[mod_python] Is this related to not having support to upload a file?

Graham Dumpleton grahamd at dscpl.com.au
Mon Apr 3 22:34:54 EDT 2006


Alberto Ruiz wrote ..
> Like I mentioned in earlier threads, this code used to work in a Debian
> system, but now it doesn't work on FreeBSD with modpython 2.7.11.
> I'm getting the following error.
> 
> Mod_python error: "PythonHandler mod_python.publisher"
> 
> Traceback (most recent call last):
> 
>   File "/usr/local/lib/python2.4/site-packages/mod_python/apache.py",
> line 193, in Dispatch
>     result = object(req)
> 
>   File "/usr/local/lib/python2.4/site-packages/mod_python/publisher.py",
> line 106, in handler
>     val = File(field)
> 
>   File "/usr/local/lib/python2.4/site-packages/mod_python/publisher.py",
> line 322, in __init__
>     for m in field.file.__methods__:
> 
> AttributeError: 'file' object has no attribute '__methods__'
> 
> 
> 
> It is hard to pin point the problem, since it doesn't show where in my
> code is happening. But my guess is that it is somewhere in this code
> snippet:
> 
> for p in photo:
> phpath='/data/webroot/propman/propphotos/'
> F= open(phpath+str("tempprop"+id+"-"+p.name[-1:]+p.filename[-4:]),'wb')
> F.write(p.file.read())
> F.flush()
> os.system("convert "+phpath+str("tempprop"+id
> +"-"+p.name[-1:]+p.filename[-4:])+" -resize 400x300 "+phpath
> +""+str("prop"+id+"-"+p.name[-1:]+p.filename[-4:]))
> com="convert "+phpath+str("tempprop"+id
> +"-"+p.name[-1:]+p.filename[-4:])+" -resize 80x60 "+phpath
> +"thm"+str("prop"+id+"-"+p.name[-1:]+p.filename[-4:])
> R.write("\n\n"+com+"\n\n")
> os.system(com)
> if p.name[-1:]=="1":
> com="convert "+phpath+str("tempprop"+id
> +"-"+p.name[-1:]+p.filename[-4:])+" -resize 220x165\! "+phpath
> +"bthm"+str("prop"+id+"-"+p.name[-1:]+p.filename[-4:])
> R.write("\n\n"+com+"\n\n")
> os.system(com)
> 
> Sorry about the indentation, it got missed up while pasting in the
> email.
> 
> My guess is in 'p.file.read()
> 
> How do I implemented for 2.7.11? or do I need to configure my Apache
> conf file.

The problem is nothing to do with your code and there is nothing
you can do in your code to get around it.

The problem is that in mod_python.publisher it uses:

class File:
    """ Like a file, but also has headers and filename
    """

    def __init__(self, field):
                 
        # steal all the file-like methods
        for m in field.file.__methods__:
            self.__dict__[m] = getattr(field.file, m)

__methods__ is from Python 1.5 and therefore will no longer work if you
are using a newer version of Python.

  __methods__
    List of the methods of many built-in object types, e.g., [].__methods__
    yields ['append', 'count', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'].

You might be able to modify mod_python 2.7.X source code to do it
differently. You might be able to use something like:

        # steal all the file-like methods
        #for m in field.file.__methods__:
        #    self.__dict__[m] = getattr(field.file, m)

        for k in field.file.__dict__:
            if k[:2] != '__':
                v = getattr(field.file, k)
                if callable(v):
                    self.__dict__[k] = v

The file in mod_python source code that needs to be modified is:

 lib/python/mod_python/publisher.py

Graham





More information about the Mod_python mailing list