[pypy-commit] pypy missing-tp_new: add passing test, refactor len(s)
mattip
pypy.commits at gmail.com
Thu Jan 5 00:18:56 EST 2017
Author: Matti Picus <matti.picus at gmail.com>
Branch: missing-tp_new
Changeset: r89371:ac830f4f76a0
Date: 2017-01-03 22:41 +0200
http://bitbucket.org/pypy/pypy/changeset/ac830f4f76a0/
Log: add passing test, refactor len(s)
diff --git a/pypy/module/cpyext/bytesobject.py b/pypy/module/cpyext/bytesobject.py
--- a/pypy/module/cpyext/bytesobject.py
+++ b/pypy/module/cpyext/bytesobject.py
@@ -80,13 +80,14 @@
"""
py_str = rffi.cast(PyBytesObject, py_obj)
s = space.str_w(w_obj)
- if py_str.c_ob_size < len(s):
+ len_s = len(s)
+ if py_str.c_ob_size < len_s:
raise oefmt(space.w_ValueError,
"bytes_attach called on object with ob_size %d but trying to store %d",
- py_str.c_ob_size, len(s))
+ py_str.c_ob_size, len_s)
with rffi.scoped_nonmovingbuffer(s) as s_ptr:
- rffi.c_memcpy(py_str.c_ob_sval, s_ptr, len(s))
- py_str.c_ob_sval[len(s)] = '\0'
+ rffi.c_memcpy(py_str.c_ob_sval, s_ptr, len_s)
+ py_str.c_ob_sval[len_s] = '\0'
py_str.c_ob_shash = space.hash_w(w_obj)
py_str.c_ob_sstate = rffi.cast(rffi.INT, 1) # SSTATE_INTERNED_MORTAL
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
@@ -353,11 +353,15 @@
data = PyString_AS_STRING(args);
len = PyString_GET_SIZE(args);
- if (data == NULL || len < 1)
+ if (data == NULL)
Py_RETURN_NONE;
obj = PyArray_Scalar(data, len);
return obj;
"""),
+ ("get_len", "METH_O",
+ """
+ return PyLong_FromLong(PyObject_Size(args));
+ """),
], prologue="""
#include <Python.h>
PyTypeObject PyStringArrType_Type = {
@@ -439,6 +443,9 @@
a = module.newsubstr('abc')
assert type(a).__name__ == 'string_'
assert a == 'abc'
+ assert 3 == module.get_len(a)
+ b = module.newsubstr('')
+ assert 0 == module.get_len(b)
class TestBytes(BaseApiTest):
def test_bytes_resize(self, space, api):
More information about the pypy-commit
mailing list