Skip to content

Commit f15a29f

Browse files
committed
More coding by random modification.
Encoding now return bytes instead of str8. eval(), exec(), compile() now accept unicode or bytes.
1 parent bae5ced commit f15a29f

12 files changed

Lines changed: 185 additions & 155 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ def XXX_test_cmp(self):
208208

209209
def test_compile(self):
210210
compile('print(1)\n', '', 'exec')
211-
bom = '\xef\xbb\xbf'
212-
compile((bom + 'print(1)\n').encode("latin-1"), '', 'exec')
211+
## bom = b'\xef\xbb\xbf'
212+
## compile(bom + b'print(1)\n', '', 'exec')
213213
compile(source='pass', filename='?', mode='exec')
214214
compile(dont_inherit=0, filename='tmp', source='0', mode='eval')
215215
compile('pass', '?', dont_inherit=1, mode='exec')
@@ -220,7 +220,7 @@ def test_compile(self):
220220
self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
221221
mode='eval', source='0', filename='tmp')
222222
if have_unicode:
223-
compile(str(b'print(u"\xc3\xa5")\n', 'utf8'), '', 'exec')
223+
compile('print(u"\xe5")\n', '', 'exec')
224224
self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
225225
self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad')
226226

@@ -338,10 +338,9 @@ def test_eval(self):
338338
self.assertEqual(eval(str('a'), globals, locals), 1)
339339
self.assertEqual(eval(str('b'), globals, locals), 200)
340340
self.assertEqual(eval(str('c'), globals, locals), 300)
341-
bom = '\xef\xbb\xbf'
342-
self.assertEqual(eval((bom + 'a').encode("latin-1"), globals, locals), 1)
343-
self.assertEqual(eval(str(b'u"\xc3\xa5"', 'utf8'), globals),
344-
str(b'\xc3\xa5', 'utf8'))
341+
## bom = b'\xef\xbb\xbf'
342+
## self.assertEqual(eval(bom + b'a', globals, locals), 1)
343+
self.assertEqual(eval('u"\xe5"', globals), u"\xe5")
345344
self.assertRaises(TypeError, eval)
346345
self.assertRaises(TypeError, eval, ())
347346

@@ -675,16 +674,14 @@ def test_getattr(self):
675674
self.assertRaises(TypeError, getattr, sys, 1)
676675
self.assertRaises(TypeError, getattr, sys, 1, "foo")
677676
self.assertRaises(TypeError, getattr)
678-
if have_unicode:
679-
self.assertRaises(UnicodeError, getattr, sys, chr(sys.maxunicode))
677+
self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode))
680678

681679
def test_hasattr(self):
682680
import sys
683681
self.assert_(hasattr(sys, 'stdout'))
684682
self.assertRaises(TypeError, hasattr, sys, 1)
685683
self.assertRaises(TypeError, hasattr)
686-
if have_unicode:
687-
self.assertRaises(UnicodeError, hasattr, sys, chr(sys.maxunicode))
684+
self.assertEqual(False, hasattr(sys, chr(sys.maxunicode)))
688685

689686
def test_hash(self):
690687
hash(None)

Objects/bytesobject.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,31 @@ PyObject *
7979
PyBytes_FromStringAndSize(const char *bytes, Py_ssize_t size)
8080
{
8181
PyBytesObject *new;
82+
int alloc;
8283

8384
assert(size >= 0);
8485

8586
new = PyObject_New(PyBytesObject, &PyBytes_Type);
8687
if (new == NULL)
8788
return NULL;
8889

89-
if (size == 0)
90+
if (size == 0) {
9091
new->ob_bytes = NULL;
92+
alloc = 0;
93+
}
9194
else {
92-
new->ob_bytes = PyMem_Malloc(size);
95+
alloc = size + 1;
96+
new->ob_bytes = PyMem_Malloc(alloc);
9397
if (new->ob_bytes == NULL) {
9498
Py_DECREF(new);
9599
return NULL;
96100
}
97101
if (bytes != NULL)
98102
memcpy(new->ob_bytes, bytes, size);
103+
new->ob_bytes[size] = '\0'; /* Trailing null byte */
99104
}
100-
new->ob_size = new->ob_alloc = size;
105+
new->ob_size = size;
106+
new->ob_alloc = alloc;
101107

102108
return (PyObject *)new;
103109
}
@@ -134,7 +140,7 @@ PyBytes_Resize(PyObject *self, Py_ssize_t size)
134140
/* Major downsize; resize down to exact size */
135141
alloc = size;
136142
}
137-
else if (size <= alloc) {
143+
else if (size < alloc) {
138144
/* Within allocated size; quick exit */
139145
((PyBytesObject *)self)->ob_size = size;
140146
return 0;
@@ -147,6 +153,8 @@ PyBytes_Resize(PyObject *self, Py_ssize_t size)
147153
/* Major upsize; resize up to exact size */
148154
alloc = size;
149155
}
156+
if (alloc <= size)
157+
alloc = size + 1;
150158

151159
sval = PyMem_Realloc(((PyBytesObject *)self)->ob_bytes, alloc);
152160
if (sval == NULL) {
@@ -158,6 +166,8 @@ PyBytes_Resize(PyObject *self, Py_ssize_t size)
158166
((PyBytesObject *)self)->ob_size = size;
159167
((PyBytesObject *)self)->ob_alloc = alloc;
160168

169+
((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */
170+
161171
return 0;
162172
}
163173

@@ -221,7 +231,7 @@ bytes_iconcat(PyBytesObject *self, PyObject *other)
221231
size = mysize + osize;
222232
if (size < 0)
223233
return PyErr_NoMemory();
224-
if (size <= self->ob_alloc)
234+
if (size < self->ob_alloc)
225235
self->ob_size = size;
226236
else if (PyBytes_Resize((PyObject *)self, size) < 0)
227237
return NULL;
@@ -243,7 +253,7 @@ bytes_repeat(PyBytesObject *self, Py_ssize_t count)
243253
size = mysize * count;
244254
if (count != 0 && size / count != mysize)
245255
return PyErr_NoMemory();
246-
result = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, size);
256+
result = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, size);
247257
if (result != NULL && size != 0) {
248258
if (mysize == 1)
249259
memset(result->ob_bytes, self->ob_bytes[0], size);
@@ -268,7 +278,7 @@ bytes_irepeat(PyBytesObject *self, Py_ssize_t count)
268278
size = mysize * count;
269279
if (count != 0 && size / count != mysize)
270280
return PyErr_NoMemory();
271-
if (size <= self->ob_alloc)
281+
if (size < self->ob_alloc)
272282
self->ob_size = size;
273283
else if (PyBytes_Resize((PyObject *)self, size) < 0)
274284
return NULL;
@@ -703,7 +713,7 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
703713
}
704714
bytes = PyString_AS_STRING(encoded);
705715
size = PyString_GET_SIZE(encoded);
706-
if (size <= self->ob_alloc)
716+
if (size < self->ob_alloc)
707717
self->ob_size = size;
708718
else if (PyBytes_Resize((PyObject *)self, size) < 0) {
709719
Py_DECREF(encoded);

Objects/moduleobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ PyModule_GetName(PyObject *m)
7272
PyErr_SetString(PyExc_SystemError, "nameless module");
7373
return NULL;
7474
}
75-
if (PyUnicode_Check(nameobj))
76-
nameobj = _PyUnicode_AsDefaultEncodedString(nameobj, "replace");
75+
if (PyUnicode_Check(nameobj)) {
76+
nameobj = _PyUnicode_AsDefaultEncodedString(nameobj, NULL);
77+
if (nameobj == NULL)
78+
return NULL;
79+
}
7780
return PyString_AsString(nameobj);
7881
}
7982

Objects/object.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ PyObject_Str(PyObject *v)
422422
return NULL;
423423
if (PyUnicode_Check(res)) {
424424
PyObject* str;
425-
str = PyUnicode_AsEncodedString(res, NULL, NULL);
425+
str = _PyUnicode_AsDefaultEncodedString(res, NULL);
426+
Py_XINCREF(str);
426427
Py_DECREF(res);
427428
if (str)
428429
res = str;
@@ -929,12 +930,12 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
929930
PyTypeObject *tp = v->ob_type;
930931
int err;
931932

932-
if (!PyString_Check(name)){
933+
if (!PyString_Check(name)) {
933934
/* The Unicode to string conversion is done here because the
934935
existing tp_setattro slots expect a string object as name
935936
and we wouldn't want to break those. */
936937
if (PyUnicode_Check(name)) {
937-
name = PyUnicode_AsEncodedString(name, NULL, NULL);
938+
name = _PyUnicode_AsDefaultEncodedString(name, NULL);
938939
if (name == NULL)
939940
return -1;
940941
}
@@ -946,8 +947,7 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
946947
return -1;
947948
}
948949
}
949-
else
950-
Py_INCREF(name);
950+
Py_INCREF(name);
951951

952952
PyString_InternInPlace(&name);
953953
if (tp->tp_setattro != NULL) {
@@ -961,6 +961,7 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
961961
return err;
962962
}
963963
Py_DECREF(name);
964+
assert(name->ob_refcnt >= 1);
964965
if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
965966
PyErr_Format(PyExc_TypeError,
966967
"'%.100s' object has no attributes "

Objects/stringobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,9 +3181,9 @@ string_encode(PyStringObject *self, PyObject *args)
31813181
v = PyString_AsEncodedObject((PyObject *)self, encoding, errors);
31823182
if (v == NULL)
31833183
goto onError;
3184-
if (!PyString_Check(v) && !PyUnicode_Check(v)) {
3184+
if (!PyBytes_Check(v)) {
31853185
PyErr_Format(PyExc_TypeError,
3186-
"encoder did not return a string/unicode object "
3186+
"[str8] encoder did not return a bytes object "
31873187
"(type=%.400s)",
31883188
v->ob_type->tp_name);
31893189
Py_DECREF(v);

0 commit comments

Comments
 (0)