zopefoundation / Products.ExternalEditor

Other
2 stars 6 forks source link

TypeError when click the pecil #20

Closed n1k9 closed 1 year ago

n1k9 commented 1 year ago

When try to use ExternalEditor the server return Internal Server Error (500) but in response there is base64 encode file

Schermata 2022-09-21 alle 16 04 57

and in event.log there is this

2022-09-21 13:46:28 ERROR [Zope.SiteErrorLog:252][waitress-2] 1663767988.39756420.7942048012268971 http://10.xxx.yyy.zzz:8080/externalEdit_/index_html
Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 167, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 376, in publish_module
  Module ZPublisher.WSGIPublisher, line 271, in publish
  Module ZPublisher.mapply, line 85, in mapply
  Module ZPublisher.WSGIPublisher, line 68, in call_object
  Module Products.ExternalEditor.ExternalEditor, line 238, in index_html
TypeError: sequence item 1: expected a bytes-like object, str found

Python 3.9.7 / Zope 5.5.1 / ExternalEditor 3.1.0

icemac commented 1 year ago

Thank you for writing this issue. It seems to be an edge case as it happens in a very commonly used function and this seems to be the first report of this issue.

Could you please use a debugger to find out why the body variable is a str object instead of bytes?

d-maurer commented 1 year ago

Could you please use a debugger to find out why the body variable is a str object instead of bytes?

Apparently, this happens for DTMLMethod objects (maybe only for DTMLMethod's which have been migrated from Python 2 to Python 3). For a DTMLMethod, https://github.com/zopefoundation/Products.ExternalEditor/blob/ecce6f48b69bf0bb91af26b30eed7912661f4b33/src/Products/ExternalEditor/ExternalEditor.py#L213 , i.e, document_src, is responsible to determine the editable data. In my instance (migrated from Python 2 to Python 3), it returns str (not bytes) for the DTMLMethod standard_error_message.

icemac commented 1 year ago

@d-maurer So you think this is a bug in DTMLMethod and should be fixed there?

dataflake commented 1 year ago

From my quick reading over the source code the DTML objects expect the source code to be a native string. This did not change after the move to Python 3. document_src returns that source code unchanged. Under Python 2 this would have been bytes, but now it's str. IMHO ExternalEditor needs to be fixed to account for that.

d-maurer commented 1 year ago

Michael Howitz wrote at 2022-10-12 23:26 -0700: @.*** So you think this is a bug in DTMLMethod and should be fixed there?

I am not sure but my feeling is: document_src is likely used for other purposes as well (e.g. editing the object, indexing the object) where it is expected to be str.

fixader commented 1 year ago

For those of us who meanwhile needs to get this to work, i just typecasted the body object in the ExternalEditor.py on line 238 as indicated by the error message. I just wrapped the "body" into a "bytes(body,'utf-8')", so the full line reads:

return b'\n'.join((metadata,bytes(body,'utf-8')))

This works, and i have had no detrimental effects whatsoever. I am using Zope 5.8.3, but it was originally tested and installed on Zope 5.3. the typecast seem to me as the safest way as a typecast of an object that already is a bytes object will do nothing, while strings gets the correct format.

dataflake commented 1 year ago

I have created a PR at https://github.com/zopefoundation/Products.ExternalEditor/pull/22 for fixing the issue. Please try it out.