[issue1303] adapt str8 constructor to bytes constructor

2007-10-24 Thread Georg Brandl

Georg Brandl added the comment:

Okay, committed in r58640.

Gregory wrote this in test_bytes:
# I've started porting some of those into buffer_tests.py, we should port
# the rest that make sense (the code can be cleaned up to use modern
# unittest methods at the same time).

--
status: open -> closed

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-24 Thread Guido van Rossum

Guido van Rossum added the comment:

Georg, can you check this in even with test_str failing?

And who is working on that?  If I don't get a name I'll tackle it myself.

--
assignee: gvanrossum -> georg.brandl

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-23 Thread Georg Brandl

Georg Brandl added the comment:

Okay, somebody seems to have started this, so I won't interfere for now.

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-22 Thread Georg Brandl

Georg Brandl added the comment:

I'll perhaps find some time today to try and make the tests useful and
consistent again.

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-22 Thread Brett Cannon

Brett Cannon added the comment:

Attached is an updated patch.  I didn't touch Georg's code short of
tossing in some comments on whether we wanted to sneak around some APIs
for performance or not.  Anything I added was to make all but one test pass.

And that one test is test_str.  There is now this odd mixture of
test_unicode, test_str, and test_bytes.  But that doesn't really fit
anymore since unicode is now str, so that file should probably get
renamed.  And str8 is becoming more like bytes.  That means that
string_tests is really just tests for what is now str and we need a
bytes_tests thing for str8 and bytes.

Assigning back to Guido since he seems to be assigned everything that is
holding up 3.0a2.  =)  If I am lucky I will be able to help with this
testing reorg, but that is iffy.

--
assignee: brett.cannon -> gvanrossum
Added file: http://bugs.python.org/file8594/str_constructor_updated.diff

__
Tracker <[EMAIL PROTECTED]>

__

str_constructor_updated.diff
Description: Binary data
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-22 Thread Christian Heimes

Changes by Christian Heimes:


--
nosy: +tiran

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-22 Thread Brett Cannon

Brett Cannon added the comment:

The changes to str8 look fine.

Now it's time to find out how badly tests break since Georg's patch was
generated.  =)

--
assignee: gvanrossum -> brett.cannon

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-19 Thread Brett Cannon

Changes by Brett Cannon:


--
nosy: +brett.cannon

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-19 Thread Brett Cannon

Changes by Brett Cannon:


--
keywords: +patch

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1303] adapt str8 constructor to bytes constructor

2007-10-19 Thread Georg Brandl

New submission from Georg Brandl:

This makes the str8 constructor accept the same kinds of types as the
bytes constructor. I had to fix instances of str8("abc") to str8(b"abc")
to make tests pass again. The only remaining failure should be test_str
-- the string test suite must be thoroughly redesigned to fit all three
string-like types.

--
assignee: gvanrossum
components: Interpreter Core
files: str_constructor.diff
keywords: py3k
messages: 56572
nosy: georg.brandl, gvanrossum
severity: normal
status: open
title: adapt str8 constructor to bytes constructor
versions: Python 3.0
Added file: http://bugs.python.org/file8570/str_constructor.diff

__
Tracker <[EMAIL PROTECTED]>

__Index: Objects/stringobject.c
===
--- Objects/stringobject.c  (Revision 58552)
+++ Objects/stringobject.c  (Arbeitskopie)
@@ -3020,16 +3020,149 @@
 static PyObject *
 string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-   PyObject *x = NULL;
-   static char *kwlist[] = {"object", 0};
+   PyObject *x = NULL, *it;
+   PyObject *(*iternext)(PyObject *);
+   const char *encoding = NULL;
+   const char *errors = NULL;
+   PyObject *new = NULL;
+   Py_ssize_t i, size;
+   static char *kwlist[] = {"object", "encoding", "errors", 0};
 
if (type != &PyString_Type)
return str_subtype_new(type, args, kwds);
-   if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str8", kwlist, &x))
+   if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str8", kwlist, &x,
+&encoding, &errors))
return NULL;
-   if (x == NULL)
+   if (x == NULL) {
+   if (encoding != NULL || errors != NULL) {
+   PyErr_SetString(PyExc_TypeError,
+   "encoding or errors without sequence "
+   "argument");
+   return NULL;
+   }
return PyString_FromString("");
-   return PyObject_Str(x);
+   }
+
+   if (PyUnicode_Check(x)) {
+   /* Encode via the codec registry */
+   if (encoding == NULL) {
+   PyErr_SetString(PyExc_TypeError,
+   "string argument without an encoding");
+   return NULL;
+   }
+   new = PyCodec_Encode(x, encoding, errors);
+   if (new == NULL)
+   return NULL;
+   /* XXX(gb): must accept bytes here since codecs output bytes
+  at the moment */
+   if (PyBytes_Check(new)) {
+   PyObject *str;
+   str = PyString_FromString(PyBytes_AsString(new));
+   Py_DECREF(new);
+   if (!str)
+   return NULL;
+   return str;
+   }
+   if (!PyString_Check(new)) {
+   PyErr_Format(PyExc_TypeError,
+"encoder did not return a str8 "
+"object (type=%.400s)",
+Py_Type(new)->tp_name);
+   Py_DECREF(new);
+   return NULL;
+   }
+   return new;
+   }
+
+   /* If it's not unicode, there can't be encoding or errors */
+   if (encoding != NULL || errors != NULL) {
+   PyErr_SetString(PyExc_TypeError,
+   "encoding or errors without a string argument");
+   return NULL;
+   }
+
+   /* Use the modern buffer interface */
+   if (PyObject_CheckBuffer(x)) {
+   Py_buffer view;
+   if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0)
+   return NULL;
+   new = PyString_FromStringAndSize(NULL, view.len);
+   if (!new)
+   goto fail;
+   if (PyBuffer_ToContiguous(((PyStringObject *)new)->ob_sval,
+ &view, view.len, 'C') < 0)
+   goto fail;
+   PyObject_ReleaseBuffer(x, &view);
+   return new;
+ fail:
+   Py_XDECREF(new);
+   PyObject_ReleaseBuffer(x, &view);
+   return NULL;
+   }
+
+   /* For the iterator version, create a string object and resize as 
needed. */
+   /* XXX(gb): is 64 a good value? also, optimize this if length is known 
*/
+   size = 64;
+   new = PyString_FromStringAndSize(NULL, size);
+   if (new == NULL)
+   return NULL;
+
+   /* XXX Optimize this if the arguments is a list, tuple */
+
+   /* Get the iterator */
+   it = PyObj