Description
Chad Lowe wrote:
Everything is working great except for attaching files.
When I attempt to attach a file I get the following back in the web browser:
request.print_exception handler Traceback (most recent call last): File "C:\Python23\lib\site-packages\MoinMoin\request.py", line 681, in run self.args = self.setup_args() File "C:\Python23\lib\site-packages\MoinMoin\request.py", line 1583, in setup_args i = wikiutil.decodeUserInput(i, self.decode_charsets) File "C:\Python23\Lib\site-packages\MoinMoin\wikiutil.py", line 70, in decodeUserInput raise UnicodeError('The string "%s" cannot be decoded.' % s) UnicodeError: The string "��ࡱ� *** The rest of the file (a Word document) displays in the plain text.
The same happens wether I try to attach a text file, pdf file, etc.
Previously I was running the same MoinMoin instance using the standalone server and file attaching worked fine.
Also, files that I had previously attached work display correctly when viewed on the wiki.
Steps to reproduce
See above.
Details
Apache 2.0.52 on WinXP (sp1) and MoinMoin 1.3.0 using mod_python.
Workaround
Do not use mod_python.
Discussion
--FransVanNieuwenhoven 2005-01-11 07:22:00:
Had the same error and spend some time debugging. The problem is located in the util.py file of mod_python and caused by a Python library bug(I think). On evaluating the fields in a form, mod_python tests for type file input fields by using following syntax:
1 if isinstance(item.file, FileType):
The item.file variable turns out to be an instance of tempfile.TemporaryFile(), and the test listed above fails (I think this is a Python bug and have reported it). Because of the failing test a file input is treated as a text input, resulting in the decode error.
To fix it, the test has to be extended. I'll list the complete source to of 2 methods of the FieldStorage class in the util.py file after the fix:
1 def __getitem__(self, key):
2 """Dictionary style indexing."""
3 if self.list is None:
4 raise TypeError, "not indexable"
5 found = []
6 for item in self.list:
7 if item.name == key:
8 if isinstance(item.file, FileType) or isinstance(item.file, tempfile._TemporaryFileWrapper):
9 found.append(item)
10 else:
11 found.append(StringField(item.value))
12 if not found:
13 raise KeyError, key
14 if len(found) == 1:
15 return found[0]
16 else:
17 return found
18
19 def getfirst(self, key, default=None):
20 """ return the first value received """
21 for item in self.list:
22 if item.name == key:
23 if isinstance(item.file, FileType) or isinstance(item.file, tempfile._TemporaryFileWrapper):
24 return item
25 else:
26 return StringField(item.value)
27 return default
note that only the lines 8 and 23 are different from the original
Fix above works in the following browsers: FireFox 1.0, Mozilla 1.7.3 and IE 6 (all on Windows). - Chad
Nir cannot reproduce this on Mac OS X with Apache 1.3 and mod python 2.7.10 or Apache 2 and mod python 3.1.3.
It seems to be a mod python bug on Windows (I don't see any report for other system here). To check what is the problem, please run Python in a terminal and type the following lines:
>>> import tempfile >>> import types >>> isinstance(tempfile.TemporaryFile('w+b'), types.FileType) True
If you don't get True - then mod_python code is broken on your platform, as one of the reporters suggested.
I see False as well. So this fails because mod_py does not use duck typing. -- AlexanderSchremmer 2005-03-11 19:41:47
I also see 'False. -- EricDavis 2005-03-11 19:43:00
We can add a temporary fix until mod_python will fix this, I checked Python tempfile module, and the code suggested by the reporter is correct. On posix, Python TemporaryFile returns an unlinked file, which has no name, but can be used to read and write data. On non-posix or cygwin, TemporaryFile returns a _TemporaryFileWrapper, which is a simple wrapper around a regular file, that try to delete the file when its not used any more.
Our patch should replace all lines checking for:
if isinstance(item.file, FileType):
With:
if isinstance(item.file, (FileType, tempfile._TemporaryFileWrapper)):
Or maybe use hasattr to check the object, and not use private data of tempfile that might change in the future.
Check with current code in tla. I found and fixed an encoding error of attachments names in mod python code.
No problems seen here for a long time! (moin 1.3.4, mod_python 3.2.0 dev, Apache 2.0.54 on XP) Probably this bug can be marked as fixed. -- DavidLinke
See also MoinMoinBugs/ModPyPngImageUploadGivesUnicodeError.
Plan
- Priority:
- Assigned to:
Status: Mod Python (no real moin bug) Windows bug, reopened, cf. MoinMoinBugs/UploadBinaryUnicodeErrorWin32ModPython
Ancient, appears fixed, not moin anyway. -- JohannesBerg