zopefoundation / Products.ExternalEditor

Other
2 stars 6 forks source link

TypeError: 'Acquisition.ImplicitAcquisitionWrapper' object is not an iterator #23

Open openlegis-br opened 3 weeks ago

openlegis-br commented 3 weeks ago

BUG/PROBLEM REPORT / FEATURE REQUEST

2024-08-19 12:03:19,269 ERROR   [waitress:435][waitress-0] Exception while serving /sagl/cadastros/materia/sapl_documentos/materia_odt/externalEdit_/72109_texto_integral.odt.zem
Traceback (most recent call last):
  File "/var/openlegis/SAGL5/eggs/waitress-2.1.2-py3.10.egg/waitress/channel.py", line 428, in service
    task.service()
  File "/var/openlegis/SAGL5/eggs/waitress-2.1.2-py3.10.egg/waitress/task.py", line 168, in service
    self.execute()
  File "/var/openlegis/SAGL5/eggs/waitress-2.1.2-py3.10.egg/waitress/task.py", line 434, in execute
    app_iter = self.channel.server.application(environ, start_response)
  File "/var/openlegis/SAGL5/eggs/Paste-3.7.1-py3.10.egg/paste/translogger.py", line 77, in __call__
    return self.application(environ, replacement_start_response)
  File "/var/openlegis/SAGL5/eggs/Zope-5.10-py3.10.egg/ZPublisher/httpexceptions.py", line 30, in __call__
    return self.application(environ, start_response)
  File "/var/openlegis/SAGL5/eggs/Zope-5.10-py3.10.egg/ZPublisher/WSGIPublisher.py", line 391, in publish_module
    response = _publish(request, new_mod_info)
  File "/var/openlegis/SAGL5/eggs/Zope-5.10-py3.10.egg/ZPublisher/WSGIPublisher.py", line 285, in publish
    result = mapply(obj,
  File "/var/openlegis/SAGL5/eggs/Zope-5.10-py3.10.egg/ZPublisher/mapply.py", line 98, in mapply
    return debug(object, args, context)
  File "/var/openlegis/SAGL5/eggs/Zope-5.10-py3.10.egg/ZPublisher/WSGIPublisher.py", line 68, in call_object
    return obj(*args)
  File "/var/openlegis/SAGL5/eggs/Products.ExternalEditor-4.0-py3.10.egg/Products/ExternalEditor/ExternalEditor.py", line 224, in index_html
    for data in body:
  File "/var/openlegis/SAGL5/eggs/Products.ExternalEditor-4.0-py3.10.egg/Products/ExternalEditor/ExternalEditor.py", line 52, in __next__
    self.data = next(self.data)
TypeError: 'Acquisition.ImplicitAcquisitionWrapper' object is not an iterator

What version of Python and Zope/Addons I am using:

Python 3.10 Zope 5.10

d-maurer commented 3 weeks ago

OpenLegis wrote at 2024-8-19 08:08 -0700:

BUG/PROBLEM REPORT / FEATURE REQUEST

2024-08-19 12:03:19,269 ERROR [waitress:435][waitress-0] Exception while serving /sagl/cadastros/materia/sapl_documentos/materiaodt/externalEdit/72109_texto_integral.odt.zem Traceback (most recent call last): ... File "/var/openlegis/SAGL5/eggs/Products.ExternalEditor-4.0-py3.10.egg/Products/ExternalEditor/ExternalEditor.py", line 224, in index_html for data in body: File "/var/openlegis/SAGL5/eggs/Products.ExternalEditor-4.0-py3.10.egg/Products/ExternalEditor/ExternalEditor.py", line 52, in next self.data = next(self.data) TypeError: 'Acquisition.ImplicitAcquisitionWrapper' object is not an iterator

This looks more like a data error in your ZODB than a bug in ExternalEditor: "AquicisitonWrapper"'s delegate most operations (such as __next__) to the wrapped object; thus the error your observe likely results from the wrapped object not supporting __next__.

I suggest you analyse the context of the TypeError with a debugger (my favorite way is to use Products.PDBDebugMode which enters the Python debugger in case of an unhandled exception (provided Zope runs in debug/development mode)). Unwrap self.deta (self.data.aq_base) and look at the result type.

openlegis-br commented 13 hours ago

The solution that worked for me was to change line 52 of the ExternalEditor.py file, from: self.data = next(self.data) to: self.data = self.data.next

d-maurer commented 12 hours ago

OpenLegis wrote at 2024-9-14 08:13 -0700:

The solution that worked for me was to change line 52 of the ExternalEditor.py file, from: self.data = next(self.data) to: self.data = self.data.next

I am still convinced that the problem is elsewhere.

Have you found out what type self.data has? Apparently, there is a Python 2/3 incompatibility there: Python 2 uses the next, Python 3 __next__ for the iterator protocol. Apparently, the above type forgets to define __next__.

openlegis-br commented 12 hours ago

I was unable to debug with Products.PDBDebugMode.

But I ran several tests and found the following:

1) Not all ODT files produce the error when trying to edit, it depends on their content (it seems to be something related to binary content, such as an image or link, for example), and there is no way to identify what exactly inside the file generates the error.

2) Even files with an error when trying to edit can be downloaded normally, so they are not corrupted in ZoDB

3) Based on this, it can be concluded that the problem is not directly related to ZPublisher, Acquisition, ZoDB or waitress.

I tested the solution in Python 3.10 and 3.12. So far, the solution found has worked well.

Thank you for your time.

d-maurer commented 8 hours ago

@.*** wrote at 2024-9-14 17:49 +0200:

OpenLegis wrote at 2024-9-14 08:13 -0700:

The solution that worked for me was to change line 52 of the ExternalEditor.py file, from: self.data = next(self.data) to: self.data = self.data.next

I am still convinced that the problem is elsewhere.

Likely, I have been wrong: The code above belongs to the class PDataStreamIterator. The name suggests that it should implement an iterator for OFS.Image.Pdata instances, but it does not fit the Pdata definition. Your fix is the right one.

Your observation that the error happens only for some files likely comes from the fact, that Pdata elements are only used for sufficiently large files; smaller files have the content in a bytes (rather than Pdata) object.

Would you like to become a zopefoundation (Zope) contributor? You would then be able to fix this problem yourself.

openlegis-br commented 6 hours ago

I would be happy to make this small contribution to the community.

d-maurer commented 8 minutes ago

OpenLegis wrote at 2024-9-14 14:40 -0700:

I would be happy to make this small contribution to the community.

You would need to sign the "Zope contributor agreement" and then create a github pull request (short "PR"). The PR would contain your correction and an entry in CHANGES.rst to describe your modification. After a review, you will be able to merge your PR.

I could help you with the PR, if necessary.