Skip to content

Commit

Permalink
Merge pull request #44 from zopefoundation/issue_43
Browse files Browse the repository at this point in the history
Fix updating an existing script from a file
  • Loading branch information
dataflake committed Jun 3, 2020
2 parents bcc0ce1 + cda53eb commit 2c534bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -3,6 +3,8 @@ Changelog

4.12 (unreleased)
-----------------
- fix ``TypeError`` when updating an existing script from a file
(`#43 <https://github.com/zopefoundation/Products.PythonScripts/issues/43>`_)


4.11 (2020-02-11)
Expand Down
10 changes: 6 additions & 4 deletions src/Products/PythonScripts/PythonScript.py
Expand Up @@ -48,6 +48,7 @@
from Shared.DC.Scripts.Script import Script
from Shared.DC.Scripts.Script import defaultBindings
from zExceptions import Forbidden
from ZPublisher.HTTPRequest import default_encoding


try:
Expand Down Expand Up @@ -416,10 +417,6 @@ def PUT(self, REQUEST, RESPONSE):
self.dav__init(REQUEST, RESPONSE)
self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1)
new_body = REQUEST.get('BODY', '')
if six.PY3 and isinstance(new_body, bytes):
new_body = new_body.decode('UTF-8')
elif six.PY2 and not isinstance(new_body, bytes):
new_body = new_body.encode('UTF-8')
self.write(new_body)
RESPONSE.setStatus(204)
return RESPONSE
Expand All @@ -434,6 +431,11 @@ def write(self, text):
bindmap = self.getBindingAssignments().getAssignedNames()
bup = 0

if six.PY3 and isinstance(text, bytes):
text = text.decode(default_encoding)
elif six.PY2 and not isinstance(text, bytes):
text = text.encode(default_encoding)

st = 0
try:
while 1:
Expand Down
17 changes: 17 additions & 0 deletions src/Products/PythonScripts/tests/testPythonScript.py
Expand Up @@ -295,6 +295,15 @@ def test_PUT_bytes(self):
'return \xc3\xa4\xc3\xa9\xc3\xae\xc3\xb6\xc3\xbc\n')
self.assertEqual(ps.params(), 'oops')

def test_write(self):
ps = self._newPS('')

ps.write(b'return 1')
self.assertEqual(ps.body(), 'return 1\n')

ps.write(u'return 1')
self.assertEqual(ps.body(), 'return 1\n')


class TestPythonScriptErrors(PythonScriptTestBase):

Expand Down Expand Up @@ -430,3 +439,11 @@ def test_ZPythonScriptHTML_upload__no_file(self):
"""It renders an error message if no file is uploaded."""
self.browser.getControl('Upload File').click()
self.assertIn('No file specified', self.browser.contents)

def test_ZPythonScriptHTML_upload__with_file(self):
file_contents = b'print("hello")'
self.browser.getControl('file').add_file(
file_contents, 'text/plain', 'script.py')
self.browser.getControl('Upload File').click()

assert 'Saved changes.' in self.browser.contents

0 comments on commit 2c534bc

Please sign in to comment.