Skip to content

Commit

Permalink
Merge pull request #56 from perrinjerome/fix/func_code_py2
Browse files Browse the repository at this point in the history
Tests for zope2 -> zope4 migration on python2
  • Loading branch information
perrinjerome committed Jun 9, 2022
2 parents 1d6f889 + c1e1fba commit d3227aa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include tox.ini
recursive-include src *.py
include buildout4.cfg
include src/Products/PythonScripts/www/default_content
recursive-include src *.pkl
recursive-include src *.ps
recursive-include src *.dtml
recursive-include src *.zcml
64 changes: 64 additions & 0 deletions src/Products/PythonScripts/tests/testPythonScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import contextlib
import io
import os
import pickle
import sys
import unittest
import warnings
Expand Down Expand Up @@ -383,6 +384,23 @@ def test_factory(self):
self.assertEqual(container.testing.body(), 'return 1\n')
self.assertEqual(container.testing.params(), '')

def testCodeIntrospection(self):
script = self._newPS('##parameters=a="b"')

self.assertEqual(script.__code__.co_argcount, 1)
self.assertEqual(
script.__code__.co_varnames,
('a',))
if six.PY2:
self.assertEqual(script.func_code.co_argcount, 1)
self.assertEqual(
script.func_code.co_varnames,
('a',))

self.assertEqual(script.__defaults__, ('b',))
if six.PY2:
self.assertEqual(script.func_defaults, ('b',))


class TestPythonScriptErrors(PythonScriptTestBase):

Expand Down Expand Up @@ -606,3 +624,49 @@ def test_PythonScript_proxyroles_nonmanager(self):

# Cleanup
noSecurityManager()


class PythonScriptPickle(unittest.TestCase):

@unittest.skipIf(
six.PY3,
'Pickles from python2 can not be loaded on python3')
def test_load_old_zope_2_pickle_recompiles(self):
with open(os.path.join(HERE, 'tscripts', 'ps_2.13.2.pkl'), 'rb') as f:
script = pickle.load(f)
self.assertIsInstance(script, PythonScript)

self.assertEqual(
script.document_src(),
'## Script (Python) "test"\n'
'##bind container=container\n'
'##bind context=context\n'
'##bind namespace=\n'
'##bind script=script\n'
'##bind subpath=traverse_subpath\n'
'##parameters=Products_PythonScripts_version=\'2.13.2\'\n'
'##title=\n'
'##\n'
'return """This is an old script created with\n'
'Products.PythonScripts version %s\n'
'and Zope version 2.13.30 (on python 2.7)\n'
'""" % Products_PythonScripts_version\n')

self.assertEqual(script.__code__.co_argcount, 1)
self.assertEqual(
script.__code__.co_varnames,
('Products_PythonScripts_version',))
self.assertEqual(script.func_code.co_argcount, 1)
self.assertEqual(
script.func_code.co_varnames,
('Products_PythonScripts_version',))
self.assertEqual(script.__defaults__, ('2.13.2',))
self.assertEqual(script.func_defaults, ('2.13.2',))

container = makerequest(DummyFolder('container'))
container.REQUEST.form = {}
self.assertEqual(
script.__of__(container)(),
'This is an old script created with\n'
'Products.PythonScripts version 2.13.2\n'
'and Zope version 2.13.30 (on python 2.7)\n')
Binary file not shown.

0 comments on commit d3227aa

Please sign in to comment.