[py3k] ensure Binary objects are initialized with bytes
authorRémi Cardona <remi.cardona@logilab.fr>
Tue, 22 Sep 2015 14:47:04 +0200
changeset 10704 73367a56ee41
parent 10703 3cb87b61b067
child 10705 e7eafadbeb15
[py3k] ensure Binary objects are initialized with bytes
devtools/fill.py
server/test/unittest_storage.py
test/unittest_entity.py
test/unittest_predicates.py
web/test/unittest_views_basecontrollers.py
web/test/unittest_viewselector.py
--- a/devtools/fill.py	Thu Sep 17 12:02:40 2015 +0200
+++ b/devtools/fill.py	Tue Sep 22 14:47:04 2015 +0200
@@ -177,7 +177,7 @@
     generate_tztime = generate_time # XXX implementation should add a timezone
 
     def generate_bytes(self, entity, attrname, index, format=None):
-        fakefile = Binary("%s%s" % (attrname, index))
+        fakefile = Binary(("%s%s" % (attrname, index)).encode('ascii'))
         fakefile.filename = u"file_%s" % attrname
         return fakefile
 
--- a/server/test/unittest_storage.py	Thu Sep 17 12:02:40 2015 +0200
+++ b/server/test/unittest_storage.py	Tue Sep 22 14:47:04 2015 +0200
@@ -66,7 +66,7 @@
         shutil.rmtree(self.tempdir)
 
 
-    def create_file(self, cnx, content='the-data'):
+    def create_file(self, cnx, content=b'the-data'):
         return cnx.create_entity('File', data=Binary(content),
                                  data_format=u'text/plain',
                                  data_name=u'foo.pdf')
@@ -109,7 +109,7 @@
 
             # add f1 back to the entity cache with req as _cw
             f1 = req.entity_from_eid(f1.eid)
-            f1.cw_set(data=Binary('the new data'))
+            f1.cw_set(data=Binary(b'the new data'))
             cnx.rollback()
             self.assertEqual(open(expected_filepath).read(), 'the-data')
             f1.cw_delete()
@@ -132,7 +132,7 @@
         with self.admin_access.repo_cnx() as cnx:
             cnx.transaction_data['fs_importing'] = True
             filepath = osp.abspath(__file__)
-            f1 = cnx.create_entity('File', data=Binary(filepath),
+            f1 = cnx.create_entity('File', data=Binary(filepath.encode(sys.getfilesystemencoding())),
                                    data_format=u'text/plain', data_name=u'foo')
             self.assertEqual(self.fspath(cnx, f1), filepath)
 
@@ -212,7 +212,7 @@
         with self.admin_access.repo_cnx() as cnx:
             cnx.transaction_data['fs_importing'] = True
             filepath = osp.abspath(__file__)
-            f1 = cnx.create_entity('File', data=Binary(filepath),
+            f1 = cnx.create_entity('File', data=Binary(filepath.encode(sys.getfilesystemencoding())),
                                    data_format=u'text/plain', data_name=u'foo')
             cw_value = f1.data.getvalue()
             fs_value = open(filepath).read()
@@ -222,12 +222,12 @@
     @tag('update')
     def test_bfss_update_with_existing_data(self):
         with self.admin_access.repo_cnx() as cnx:
-            f1 = cnx.create_entity('File', data=Binary('some data'),
+            f1 = cnx.create_entity('File', data=Binary(b'some data'),
                                    data_format=u'text/plain', data_name=u'foo')
             # NOTE: do not use cw_set() which would automatically
             #       update f1's local dict. We want the pure rql version to work
             cnx.execute('SET F data %(d)s WHERE F eid %(f)s',
-                         {'d': Binary('some other data'), 'f': f1.eid})
+                         {'d': Binary(b'some other data'), 'f': f1.eid})
             self.assertEqual(f1.data.getvalue(), 'some other data')
             cnx.commit()
             f2 = cnx.execute('Any F WHERE F eid %(f)s, F is File', {'f': f1.eid}).get_entity(0, 0)
@@ -236,7 +236,7 @@
     @tag('update', 'extension', 'commit')
     def test_bfss_update_with_different_extension_commited(self):
         with self.admin_access.repo_cnx() as cnx:
-            f1 = cnx.create_entity('File', data=Binary('some data'),
+            f1 = cnx.create_entity('File', data=Binary(b'some data'),
                                    data_format=u'text/plain', data_name=u'foo.txt')
             # NOTE: do not use cw_set() which would automatically
             #       update f1's local dict. We want the pure rql version to work
@@ -246,7 +246,7 @@
             self.assertEqual(osp.splitext(old_path)[1], '.txt')
             cnx.execute('SET F data %(d)s, F data_name %(dn)s, '
                          'F data_format %(df)s WHERE F eid %(f)s',
-                         {'d': Binary('some other data'), 'f': f1.eid,
+                         {'d': Binary(b'some other data'), 'f': f1.eid,
                           'dn': u'bar.jpg', 'df': u'image/jpeg'})
             cnx.commit()
             # the new file exists with correct extension
@@ -260,7 +260,7 @@
     @tag('update', 'extension', 'rollback')
     def test_bfss_update_with_different_extension_rolled_back(self):
         with self.admin_access.repo_cnx() as cnx:
-            f1 = cnx.create_entity('File', data=Binary('some data'),
+            f1 = cnx.create_entity('File', data=Binary(b'some data'),
                                    data_format=u'text/plain', data_name=u'foo.txt')
             # NOTE: do not use cw_set() which would automatically
             #       update f1's local dict. We want the pure rql version to work
@@ -271,7 +271,7 @@
             self.assertEqual(osp.splitext(old_path)[1], '.txt')
             cnx.execute('SET F data %(d)s, F data_name %(dn)s, '
                          'F data_format %(df)s WHERE F eid %(f)s',
-                         {'d': Binary('some other data'),
+                         {'d': Binary(b'some other data'),
                           'f': f1.eid,
                           'dn': u'bar.jpg',
                           'df': u'image/jpeg'})
@@ -290,7 +290,7 @@
     @tag('update', 'NULL')
     def test_bfss_update_to_None(self):
         with self.admin_access.repo_cnx() as cnx:
-            f = cnx.create_entity('Affaire', opt_attr=Binary('toto'))
+            f = cnx.create_entity('Affaire', opt_attr=Binary(b'toto'))
             cnx.commit()
             f.cw_set(opt_attr=None)
             cnx.commit()
@@ -298,7 +298,7 @@
     @tag('fs_importing', 'update')
     def test_bfss_update_with_fs_importing(self):
         with self.admin_access.repo_cnx() as cnx:
-            f1 = cnx.create_entity('File', data=Binary('some data'),
+            f1 = cnx.create_entity('File', data=Binary(b'some data'),
                                    data_format=u'text/plain',
                                    data_name=u'foo')
             old_fspath = self.fspath(cnx, f1)
@@ -306,7 +306,7 @@
             new_fspath = osp.join(self.tempdir, 'newfile.txt')
             open(new_fspath, 'w').write('the new data')
             cnx.execute('SET F data %(d)s WHERE F eid %(f)s',
-                         {'d': Binary(new_fspath), 'f': f1.eid})
+                         {'d': Binary(new_fspath.encode(sys.getfilesystemencoding())), 'f': f1.eid})
             cnx.commit()
             self.assertEqual(f1.data.getvalue(), 'the new data')
             self.assertEqual(self.fspath(cnx, f1), new_fspath)
--- a/test/unittest_entity.py	Thu Sep 17 12:02:40 2015 +0200
+++ b/test/unittest_entity.py	Tue Sep 22 14:47:04 2015 +0200
@@ -644,7 +644,7 @@
 
     def test_printable_value_bytes(self):
         with self.admin_access.web_request() as req:
-            e = req.create_entity('FakeFile', data=Binary('lambda x: 1'), data_format=u'text/x-python',
+            e = req.create_entity('FakeFile', data=Binary(b'lambda x: 1'), data_format=u'text/x-python',
                                   data_encoding=u'ascii', data_name=u'toto.py')
             from cubicweb import mttransforms
             if mttransforms.HAS_PYGMENTS_TRANSFORMS:
@@ -663,8 +663,10 @@
     <span style="color: #C00000;">lambda</span> <span style="color: #000000;">x</span><span style="color: #0000C0;">:</span> <span style="color: #0080C0;">1</span>
 </pre>''')
 
-            e = req.create_entity('FakeFile', data=Binary('*héhéhé*'), data_format=u'text/rest',
-                                data_encoding=u'utf-8', data_name=u'toto.txt')
+            e = req.create_entity('FakeFile',
+                                  data=Binary(u'*héhéhé*'.encode('utf-8')),
+                                  data_format=u'text/rest',
+                                  data_encoding=u'utf-8', data_name=u'toto.txt')
             self.assertEqual(e.printable_value('data'),
                               u'<p><em>héhéhé</em></p>')
 
@@ -717,7 +719,7 @@
             e = self.vreg['etypes'].etype_class('FakeFile')(req)
             e.cw_attr_cache['description'] = 'du <em>html</em>'
             e.cw_attr_cache['description_format'] = 'text/html'
-            e.cw_attr_cache['data'] = Binary('some <em>data</em>')
+            e.cw_attr_cache['data'] = Binary(b'some <em>data</em>')
             e.cw_attr_cache['data_name'] = 'an html file'
             e.cw_attr_cache['data_format'] = 'text/html'
             e.cw_attr_cache['data_encoding'] = 'ascii'
--- a/test/unittest_predicates.py	Thu Sep 17 12:02:40 2015 +0200
+++ b/test/unittest_predicates.py	Tue Sep 22 14:47:04 2015 +0200
@@ -37,7 +37,7 @@
 class ImplementsTC(CubicWebTC):
     def test_etype_priority(self):
         with self.admin_access.web_request() as req:
-            f = req.create_entity('FakeFile', data_name=u'hop.txt', data=Binary('hop'),
+            f = req.create_entity('FakeFile', data_name=u'hop.txt', data=Binary(b'hop'),
                                   data_format=u'text/plain')
             rset = f.as_rset()
             anyscore = is_instance('Any')(f.__class__, req, rset=rset)
--- a/web/test/unittest_views_basecontrollers.py	Thu Sep 17 12:02:40 2015 +0200
+++ b/web/test/unittest_views_basecontrollers.py	Tue Sep 22 14:47:04 2015 +0200
@@ -226,7 +226,7 @@
 
                         '__type:Y': 'File',
                         '_cw_entity_fields:Y': 'data-subject,described_by_test-object',
-                        'data-subject:Y': (u'coucou.txt', Binary('coucou')),
+                        'data-subject:Y': (u'coucou.txt', Binary(b'coucou')),
                         'described_by_test-object:Y': 'X',
                         }
             path, _params = self.expect_redirect_handle_request(req, 'edit')
@@ -251,7 +251,7 @@
 
                         '__type:Y': 'File',
                         '_cw_entity_fields:Y': 'data-subject',
-                        'data-subject:Y': (u'coucou.txt', Binary('coucou')),
+                        'data-subject:Y': (u'coucou.txt', Binary(b'coucou')),
                         }
             path, _params = self.expect_redirect_handle_request(req, 'edit')
             self.assertTrue(path.startswith('salesterm/'), path)
@@ -349,7 +349,7 @@
     def test_interval_bound_constraint_success(self):
         with self.admin_access.repo_cnx() as cnx:
             feid = cnx.execute('INSERT File X: X data_name "toto.txt", X data %(data)s',
-                               {'data': Binary('yo')})[0][0]
+                               {'data': Binary(b'yo')})[0][0]
             cnx.commit()
 
         with self.admin_access.web_request(rollbackfirst=True) as req:
@@ -395,7 +395,7 @@
         constrained attributes"""
         with self.admin_access.repo_cnx() as cnx:
             feid = cnx.execute('INSERT File X: X data_name "toto.txt", X data %(data)s',
-                               {'data': Binary('yo')})[0][0]
+                               {'data': Binary(b'yo')})[0][0]
             seid = cnx.create_entity('Salesterm', amount=0, described_by_test=feid).eid
             cnx.commit()
 
@@ -732,7 +732,7 @@
 
                             '__type:Y': 'File',
                             '_cw_entity_fields:Y': 'data-subject',
-                            'data-subject:Y': (u'coucou.txt', Binary('coucou')),
+                            'data-subject:Y': (u'coucou.txt', Binary(b'coucou')),
                             }
                 values_by_eid = dict((eid, req.extract_entity_params(eid, minparams=2))
                                      for eid in req.edited_eids())
--- a/web/test/unittest_viewselector.py	Thu Sep 17 12:02:40 2015 +0200
+++ b/web/test/unittest_viewselector.py	Tue Sep 22 14:47:04 2015 +0200
@@ -422,7 +422,7 @@
 
     def test_interface_selector(self):
         with self.admin_access.web_request() as req:
-            req.create_entity('File', data_name=u'bim.png', data=Binary('bim'))
+            req.create_entity('File', data_name=u'bim.png', data=Binary(b'bim'))
             # image primary view priority
             rset = req.execute('File X WHERE X data_name "bim.png"')
             self.assertIsInstance(self.vreg['views'].select('primary', req, rset=rset),
@@ -431,21 +431,21 @@
 
     def test_score_entity_selector(self):
         with self.admin_access.web_request() as req:
-            req.create_entity('File', data_name=u'bim.png', data=Binary('bim'))
+            req.create_entity('File', data_name=u'bim.png', data=Binary(b'bim'))
             # image/ehtml primary view priority
             rset = req.execute('File X WHERE X data_name "bim.png"')
             self.assertIsInstance(self.vreg['views'].select('image', req, rset=rset),
                                   idownloadable.ImageView)
             self.assertRaises(NoSelectableObject, self.vreg['views'].select, 'ehtml', req, rset=rset)
 
-            fileobj = req.create_entity('File', data_name=u'bim.html', data=Binary('<html>bam</html'))
+            fileobj = req.create_entity('File', data_name=u'bim.html', data=Binary(b'<html>bam</html'))
             # image/ehtml primary view priority
             rset = req.execute('File X WHERE X data_name "bim.html"')
             self.assertIsInstance(self.vreg['views'].select('ehtml', req, rset=rset),
                                   idownloadable.EHTMLView)
             self.assertRaises(NoSelectableObject, self.vreg['views'].select, 'image', req, rset=rset)
 
-            fileobj = req.create_entity('File', data_name=u'bim.txt', data=Binary('boum'))
+            fileobj = req.create_entity('File', data_name=u'bim.txt', data=Binary(b'boum'))
             # image/ehtml primary view priority
             rset = req.execute('File X WHERE X data_name "bim.txt"')
             self.assertRaises(NoSelectableObject, self.vreg['views'].select, 'image', req, rset=rset)