[pypy-commit] pypy default: Reduce diff with py3k

rlamy pypy.commits at gmail.com
Mon Jun 27 14:41:46 EDT 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: 
Changeset: r85415:65fe40eb8c0a
Date: 2016-06-27 19:41 +0100
http://bitbucket.org/pypy/pypy/changeset/65fe40eb8c0a/

Log:	Reduce diff with py3k

diff --git a/pypy/module/cpyext/test/test_bytearrayobject.py b/pypy/module/cpyext/test/test_bytearrayobject.py
--- a/pypy/module/cpyext/test/test_bytearrayobject.py
+++ b/pypy/module/cpyext/test/test_bytearrayobject.py
@@ -28,10 +28,10 @@
              """
                 return PyBool_FromLong(PyByteArray_Check(PyTuple_GetItem(args, 0)));
              """)], prologue='#include <stdlib.h>')
-        assert module.get_hello1() == 'Hello world'
-        assert module.get_hello2() == 'Hello world\x00'
+        assert module.get_hello1() == b'Hello world'
+        assert module.get_hello2() == b'Hello world\x00'
         assert module.test_Size()
-        assert module.test_is_bytearray(bytearray(""))
+        assert module.test_is_bytearray(bytearray(b""))
         assert not module.test_is_bytearray(())
 
     def test_bytearray_buffer_init(self):
@@ -63,7 +63,7 @@
             ])
         s = module.getbytearray()
         assert len(s) == 4
-        assert s == 'ab\x00c'
+        assert s == b'ab\x00c'
 
     def test_bytearray_mutable(self):
         module = self.import_extension('foo', [
@@ -79,9 +79,9 @@
              """),
             ])
         s = module.mutable()
-        if s == '\x00' * 10:
+        if s == b'\x00' * 10:
             assert False, "no RW access to bytearray"
-        assert s[:6] == 'works\x00'
+        assert s[:6] == b'works\x00'
 
     def test_AsByteArray(self):
         module = self.import_extension('foo', [
@@ -97,18 +97,18 @@
              """),
             ])
         s = module.getbytearray()
-        assert s == 'test'
+        assert s == b'test'
 
     def test_manipulations(self):
         import sys
         module = self.import_extension('foo', [
-            ("bytearray_from_string", "METH_VARARGS",
+            ("bytearray_from_bytes", "METH_VARARGS",
              '''
-             return PyByteArray_FromStringAndSize(PyString_AsString(
+             return PyByteArray_FromStringAndSize(PyBytes_AsString(
                        PyTuple_GetItem(args, 0)), 4);
              '''
             ),
-            ("str_from_bytearray", "METH_VARARGS",
+            ("bytes_from_bytearray", "METH_VARARGS",
              '''
                 char * buf;
                 int n;
@@ -121,7 +121,7 @@
                     return NULL;
                 }
                 n = PyByteArray_Size(obj);
-                return PyString_FromStringAndSize(buf, n);
+                return PyBytes_FromStringAndSize(buf, n);
              '''
             ),
             ("concat", "METH_VARARGS",
@@ -129,7 +129,7 @@
                 PyObject * ret, *right, *left;
                 PyObject *ba1, *ba2;
                 if (!PyArg_ParseTuple(args, "OO", &left, &right)) {
-                    return PyString_FromString("parse failed");
+                    return PyUnicode_FromString("parse failed");
                 }
                 ba1 = PyByteArray_FromObject(left);
                 ba2 = PyByteArray_FromObject(right);
@@ -141,16 +141,16 @@
                 ret = PyByteArray_Concat(ba1, ba2);
                 return ret;
              """)])
-        assert module.bytearray_from_string("huheduwe") == "huhe"
-        assert module.str_from_bytearray(bytearray('abc')) == 'abc'
+        assert module.bytearray_from_bytes(b"huheduwe") == b"huhe"
+        assert module.bytes_from_bytearray(bytearray(b'abc')) == b'abc'
         if '__pypy__' in sys.builtin_module_names:
             # CPython only makes an assert.
-            raises(ValueError, module.str_from_bytearray, 4.0)
-        ret = module.concat('abc', 'def')
-        assert ret == 'abcdef'
+            raises(ValueError, module.bytes_from_bytearray, 4.0)
+        ret = module.concat(b'abc', b'def')
+        assert ret == b'abcdef'
         assert not isinstance(ret, str)
         assert isinstance(ret, bytearray)
-        raises(TypeError, module.concat, 'abc', u'def')
+        raises(TypeError, module.concat, b'abc', u'def')
 
     def test_bytearray_resize(self):
         module = self.import_extension('foo', [
@@ -159,7 +159,7 @@
              PyObject *obj, *ba;
              int newsize, oldsize, ret;
              if (!PyArg_ParseTuple(args, "Oi", &obj, &newsize)) {
-                 return PyString_FromString("parse failed");
+                 return PyUnicode_FromString("parse failed");
              }
 
              ba = PyByteArray_FromObject(obj);
@@ -168,7 +168,7 @@
              oldsize = PyByteArray_Size(ba);
              if (oldsize == 0)
              {
-                  return PyString_FromString("oldsize is 0");
+                  return PyUnicode_FromString("oldsize is 0");
              }
              ret = PyByteArray_Resize(ba, newsize);
              if (ret != 0)
@@ -179,10 +179,9 @@
              return ba;
              '''
             )])
-        ret = module.bytearray_resize('abc', 6)
+        ret = module.bytearray_resize(b'abc', 6)
         assert len(ret) == 6,"%s, len=%d" % (ret, len(ret))
-        assert ret == 'abc\x00\x00\x00'
-        ret = module.bytearray_resize('abcdefghi', 4)
+        assert ret == b'abc\x00\x00\x00'
+        ret = module.bytearray_resize(b'abcdefghi', 4)
         assert len(ret) == 4,"%s, len=%d" % (ret, len(ret))
-        assert ret == 'abcd'
-
+        assert ret == b'abcd'
diff --git a/pypy/module/cpyext/test/test_bytesobject.py b/pypy/module/cpyext/test/test_bytesobject.py
--- a/pypy/module/cpyext/test/test_bytesobject.py
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -10,22 +10,22 @@
 import py
 import sys
 
-class AppTestStringObject(AppTestCpythonExtensionBase):
-    def test_stringobject(self):
+class AppTestBytesObject(AppTestCpythonExtensionBase):
+    def test_bytesobject(self):
         module = self.import_extension('foo', [
             ("get_hello1", "METH_NOARGS",
              """
-                 return PyString_FromStringAndSize(
+                 return PyBytes_FromStringAndSize(
                      "Hello world<should not be included>", 11);
              """),
             ("get_hello2", "METH_NOARGS",
              """
-                 return PyString_FromString("Hello world");
+                 return PyBytes_FromString("Hello world");
              """),
             ("test_Size", "METH_NOARGS",
              """
-                 PyObject* s = PyString_FromString("Hello world");
-                 int result = PyString_Size(s);
+                 PyObject* s = PyBytes_FromString("Hello world");
+                 int result = PyBytes_Size(s);
 
                  Py_DECREF(s);
                  return PyLong_FromLong(result);
@@ -33,38 +33,38 @@
             ("test_Size_exception", "METH_NOARGS",
              """
                  PyObject* f = PyFloat_FromDouble(1.0);
-                 PyString_Size(f);
+                 PyBytes_Size(f);
 
                  Py_DECREF(f);
                  return NULL;
              """),
-             ("test_is_string", "METH_VARARGS",
+             ("test_is_bytes", "METH_VARARGS",
              """
-                return PyBool_FromLong(PyString_Check(PyTuple_GetItem(args, 0)));
+                return PyBool_FromLong(PyBytes_Check(PyTuple_GetItem(args, 0)));
              """)], prologue='#include <stdlib.h>')
-        assert module.get_hello1() == 'Hello world'
-        assert module.get_hello2() == 'Hello world'
+        assert module.get_hello1() == b'Hello world'
+        assert module.get_hello2() == b'Hello world'
         assert module.test_Size() == 11
         raises(TypeError, module.test_Size_exception)
 
-        assert module.test_is_string("")
-        assert not module.test_is_string(())
+        assert module.test_is_bytes(b"")
+        assert not module.test_is_bytes(())
 
-    def test_string_buffer_init(self):
+    def test_bytes_buffer_init(self):
         module = self.import_extension('foo', [
-            ("getstring", "METH_NOARGS",
+            ("getbytes", "METH_NOARGS",
              """
                  PyObject *s, *t;
                  char* c;
 
-                 s = PyString_FromStringAndSize(NULL, 4);
+                 s = PyBytes_FromStringAndSize(NULL, 4);
                  if (s == NULL)
                     return NULL;
-                 t = PyString_FromStringAndSize(NULL, 3);
+                 t = PyBytes_FromStringAndSize(NULL, 3);
                  if (t == NULL)
                     return NULL;
                  Py_DECREF(t);
-                 c = PyString_AS_STRING(s);
+                 c = PyBytes_AS_STRING(s);
                  c[0] = 'a';
                  c[1] = 'b';
                  c[2] = 0;
@@ -72,62 +72,62 @@
                  return s;
              """),
             ])
-        s = module.getstring()
+        s = module.getbytes()
         assert len(s) == 4
-        assert s == 'ab\x00c'
+        assert s == b'ab\x00c'
 
-    def test_string_tp_alloc(self):
+    def test_bytes_tp_alloc(self):
         module = self.import_extension('foo', [
             ("tpalloc", "METH_NOARGS",
              """
                 PyObject *base;
                 PyTypeObject * type;
                 PyStringObject *obj;
-                base = PyString_FromString("test");
-                if (PyString_GET_SIZE(base) != 4)
-                    return PyLong_FromLong(-PyString_GET_SIZE(base));
+                base = PyBytes_FromString("test");
+                if (PyBytes_GET_SIZE(base) != 4)
+                    return PyLong_FromLong(-PyBytes_GET_SIZE(base));
                 type = base->ob_type;
                 if (type->tp_itemsize != 1)
                     return PyLong_FromLong(type->tp_itemsize);
                 obj = (PyStringObject*)type->tp_alloc(type, 10);
-                if (PyString_GET_SIZE(obj) != 10)
-                    return PyLong_FromLong(PyString_GET_SIZE(obj));
-                /* cannot work, there is only RO access */
-                memcpy(PyString_AS_STRING(obj), "works", 6);
+                if (PyBytes_GET_SIZE(obj) != 10)
+                    return PyLong_FromLong(PyBytes_GET_SIZE(obj));
+                /* cannot work, there is only RO access
+                memcpy(PyBytes_AS_STRING(obj), "works", 6); */
                 Py_INCREF(obj);
                 return (PyObject*)obj;
              """),
             ('alloc_rw', "METH_NOARGS",
              '''
-                PyObject *obj = _PyObject_NewVar(&PyString_Type, 10);
-                memcpy(PyString_AS_STRING(obj), "works", 6);
+                PyObject *obj = _PyObject_NewVar(&PyBytes_Type, 10);
+                memcpy(PyBytes_AS_STRING(obj), "works", 6);
                 return (PyObject*)obj;
              '''),
             ])
         s = module.alloc_rw()
-        assert s == 'works' + '\x00' * 5
+        assert s == b'works' + b'\x00' * 5
         s = module.tpalloc()
-        assert s == 'works' + '\x00' * 5
+        assert s == b'\x00' * 10
 
     def test_AsString(self):
         module = self.import_extension('foo', [
-            ("getstring", "METH_NOARGS",
+            ("getbytes", "METH_NOARGS",
              """
-                 PyObject* s1 = PyString_FromStringAndSize("test", 4);
-                 char* c = PyString_AsString(s1);
-                 PyObject* s2 = PyString_FromStringAndSize(c, 4);
+                 PyObject* s1 = PyBytes_FromStringAndSize("test", 4);
+                 char* c = PyBytes_AsString(s1);
+                 PyObject* s2 = PyBytes_FromStringAndSize(c, 4);
                  Py_DECREF(s1);
                  return s2;
              """),
             ])
-        s = module.getstring()
-        assert s == 'test'
+        s = module.getbytes()
+        assert s == b'test'
 
     def test_manipulations(self):
         module = self.import_extension('foo', [
-            ("string_as_string", "METH_VARARGS",
+            ("bytes_as_string", "METH_VARARGS",
              '''
-             return PyString_FromStringAndSize(PyString_AsString(
+             return PyBytes_FromStringAndSize(PyBytes_AsString(
                        PyTuple_GetItem(args, 0)), 4);
              '''
             ),
@@ -137,22 +137,22 @@
                 PyObject * left = PyTuple_GetItem(args, 0);
                 Py_INCREF(left);    /* the reference will be stolen! */
                 v = &left;
-                PyString_Concat(v, PyTuple_GetItem(args, 1));
+                PyBytes_Concat(v, PyTuple_GetItem(args, 1));
                 return *v;
              """)])
-        assert module.string_as_string("huheduwe") == "huhe"
-        ret = module.concat('abc', 'def')
-        assert ret == 'abcdef'
+        assert module.bytes_as_string(b"huheduwe") == b"huhe"
+        ret = module.concat(b'abc', b'def')
+        assert ret == b'abcdef'
         ret = module.concat('abc', u'def')
         assert not isinstance(ret, str)
         assert isinstance(ret, unicode)
         assert ret == 'abcdef'
 
-    def test_py_string_as_string_None(self):
+    def test_py_bytes_as_string_None(self):
         module = self.import_extension('foo', [
             ("string_None", "METH_VARARGS",
              '''
-             if (PyString_AsString(Py_None)) {
+             if (PyBytes_AsString(Py_None)) {
                 Py_RETURN_NONE;
              }
              return NULL;
@@ -162,18 +162,18 @@
 
     def test_AsStringAndSize(self):
         module = self.import_extension('foo', [
-            ("getstring", "METH_NOARGS",
+            ("getbytes", "METH_NOARGS",
              """
-                 PyObject* s1 = PyString_FromStringAndSize("te\\0st", 5);
+                 PyObject* s1 = PyBytes_FromStringAndSize("te\\0st", 5);
                  char *buf;
                  Py_ssize_t len;
-                 if (PyString_AsStringAndSize(s1, &buf, &len) < 0)
+                 if (PyBytes_AsStringAndSize(s1, &buf, &len) < 0)
                      return NULL;
                  if (len != 5) {
                      PyErr_SetString(PyExc_AssertionError, "Bad Length");
                      return NULL;
                  }
-                 if (PyString_AsStringAndSize(s1, &buf, NULL) >= 0) {
+                 if (PyBytes_AsStringAndSize(s1, &buf, NULL) >= 0) {
                      PyErr_SetString(PyExc_AssertionError, "Should Have failed");
                      return NULL;
                  }
@@ -183,7 +183,7 @@
                  return Py_None;
              """),
             ])
-        module.getstring()
+        module.getbytes()
 
     def test_py_string_as_string_Unicode(self):
         module = self.import_extension('foo', [
@@ -420,8 +420,8 @@
         assert type(a).__name__ == 'string_'
         assert a == 'abc'
 
-class TestString(BaseApiTest):
-    def test_string_resize(self, space, api):
+class TestBytes(BaseApiTest):
+    def test_bytes_resize(self, space, api):
         py_str = new_empty_str(space, 10)
         ar = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
         py_str.c_ob_sval[0] = 'a'


More information about the pypy-commit mailing list