[Python-checkins] r61752 - in python/branches/trunk-bytearray: Include/Python.h Include/pydebug.h Include/pyerrors.h Include/pythonrun.h Lib/test/string_tests.py Lib/test/test_bytes.py Makefile.pre.in Objects/bytesobject.c Objects/exceptions.c Objects/object.c Objects/stringobject.c Python/bltinmodule.c Python/pythonrun.c

christian.heimes python-checkins at python.org
Sat Mar 22 20:53:20 CET 2008


Author: christian.heimes
Date: Sat Mar 22 20:53:20 2008
New Revision: 61752

Modified:
   python/branches/trunk-bytearray/Include/Python.h
   python/branches/trunk-bytearray/Include/pydebug.h
   python/branches/trunk-bytearray/Include/pyerrors.h
   python/branches/trunk-bytearray/Include/pythonrun.h
   python/branches/trunk-bytearray/Lib/test/string_tests.py
   python/branches/trunk-bytearray/Lib/test/test_bytes.py
   python/branches/trunk-bytearray/Makefile.pre.in
   python/branches/trunk-bytearray/Objects/bytesobject.c
   python/branches/trunk-bytearray/Objects/exceptions.c
   python/branches/trunk-bytearray/Objects/object.c
   python/branches/trunk-bytearray/Objects/stringobject.c
   python/branches/trunk-bytearray/Python/bltinmodule.c
   python/branches/trunk-bytearray/Python/pythonrun.c
Log:
Take One
* Added initialization code, warnings, flags etc. to the appropriate places
* Added new buffer interface to string type
* Modified tests
* Modified Makefile.pre.in to compile the new files
* Added bytesobject.c to Python.h


Modified: python/branches/trunk-bytearray/Include/Python.h
==============================================================================
--- python/branches/trunk-bytearray/Include/Python.h	(original)
+++ python/branches/trunk-bytearray/Include/Python.h	Sat Mar 22 20:53:20 2008
@@ -92,6 +92,7 @@
 #include "stringobject.h"
 /* #include "memoryobject.h" */
 #include "bufferobject.h"
+#include "bytesobject.h"
 #include "tupleobject.h"
 #include "listobject.h"
 #include "dictobject.h"

Modified: python/branches/trunk-bytearray/Include/pydebug.h
==============================================================================
--- python/branches/trunk-bytearray/Include/pydebug.h	(original)
+++ python/branches/trunk-bytearray/Include/pydebug.h	Sat Mar 22 20:53:20 2008
@@ -11,6 +11,7 @@
 PyAPI_DATA(int) Py_InspectFlag;
 PyAPI_DATA(int) Py_OptimizeFlag;
 PyAPI_DATA(int) Py_NoSiteFlag;
+PyAPI_DATA(int) Py_BytesWarningFlag;
 PyAPI_DATA(int) Py_UseClassExceptionsFlag;
 PyAPI_DATA(int) Py_FrozenFlag;
 PyAPI_DATA(int) Py_TabcheckFlag;

Modified: python/branches/trunk-bytearray/Include/pyerrors.h
==============================================================================
--- python/branches/trunk-bytearray/Include/pyerrors.h	(original)
+++ python/branches/trunk-bytearray/Include/pyerrors.h	Sat Mar 22 20:53:20 2008
@@ -175,6 +175,7 @@
 PyAPI_DATA(PyObject *) PyExc_FutureWarning;
 PyAPI_DATA(PyObject *) PyExc_ImportWarning;
 PyAPI_DATA(PyObject *) PyExc_UnicodeWarning;
+PyAPI_DATA(PyObject *) PyExc_BytesWarning;
 
 
 /* Convenience functions */

Modified: python/branches/trunk-bytearray/Include/pythonrun.h
==============================================================================
--- python/branches/trunk-bytearray/Include/pythonrun.h	(original)
+++ python/branches/trunk-bytearray/Include/pythonrun.h	Sat Mar 22 20:53:20 2008
@@ -123,6 +123,7 @@
 PyAPI_FUNC(int) _PyFrame_Init(void);
 PyAPI_FUNC(int) _PyInt_Init(void);
 PyAPI_FUNC(void) _PyFloat_Init(void);
+PyAPI_FUNC(int) PyBytes_Init(void);
 
 /* Various internal finalizers */
 PyAPI_FUNC(void) _PyExc_Fini(void);
@@ -138,6 +139,7 @@
 PyAPI_FUNC(void) PyInt_Fini(void);
 PyAPI_FUNC(void) PyFloat_Fini(void);
 PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
+PyAPI_FUNC(void) PyBytes_Fini(void);
 
 /* Stuff with no proper home (yet) */
 PyAPI_FUNC(char *) PyOS_Readline(FILE *, FILE *, char *);

Modified: python/branches/trunk-bytearray/Lib/test/string_tests.py
==============================================================================
--- python/branches/trunk-bytearray/Lib/test/string_tests.py	(original)
+++ python/branches/trunk-bytearray/Lib/test/string_tests.py	Sat Mar 22 20:53:20 2008
@@ -727,6 +727,9 @@
 
         self.checkraises(TypeError, '123', 'zfill')
 
+# XXX alias for py3k forward compatibility
+BaseTest = CommonTest
+
 class MixinStrUnicodeUserStringTest:
     # additional tests that only work for
     # stringlike objects, i.e. str, unicode, UserString

Modified: python/branches/trunk-bytearray/Lib/test/test_bytes.py
==============================================================================
--- python/branches/trunk-bytearray/Lib/test/test_bytes.py	(original)
+++ python/branches/trunk-bytearray/Lib/test/test_bytes.py	Sat Mar 22 20:53:20 2008
@@ -448,9 +448,6 @@
                          [0, 65, 127, 128, 255])
 
 
-class BytesTest(BaseBytesTest):
-    type2test = bytes
-
 class ByteArrayTest(BaseBytesTest):
     type2test = bytearray
 
@@ -886,9 +883,6 @@
 class ByteArrayAsStringTest(FixedStringTest):
     type2test = bytearray
 
-class BytesAsStringTest(FixedStringTest):
-    type2test = bytes
-
 
 class ByteArraySubclass(bytearray):
     pass
@@ -969,10 +963,10 @@
 
 
 def test_main():
-    test.test_support.run_unittest(BytesTest)
+    #test.test_support.run_unittest(BytesTest)
     test.test_support.run_unittest(ByteArrayTest)
-    test.test_support.run_unittest(AssortedBytesTest)
-    test.test_support.run_unittest(BytesAsStringTest)
+    #test.test_support.run_unittest(AssortedBytesTest)
+    #test.test_support.run_unittest(BytesAsStringTest)
     test.test_support.run_unittest(ByteArrayAsStringTest)
     test.test_support.run_unittest(ByteArraySubclassTest)
     test.test_support.run_unittest(BytearrayPEP3137Test)

Modified: python/branches/trunk-bytearray/Makefile.pre.in
==============================================================================
--- python/branches/trunk-bytearray/Makefile.pre.in	(original)
+++ python/branches/trunk-bytearray/Makefile.pre.in	Sat Mar 22 20:53:20 2008
@@ -295,6 +295,8 @@
 		Objects/abstract.o \
 		Objects/boolobject.o \
 		Objects/bufferobject.o \
+		Objects/bytes_methods.o \
+		Objects/bytesobject.o \
 		Objects/cellobject.o \
 		Objects/classobject.o \
 		Objects/cobject.o \
@@ -518,13 +520,16 @@
 				$(srcdir)/Objects/unicodetype_db.h
 
 STRINGLIB_HEADERS= \
+		$(srcdir)/Include/bytes_methods.h \
 		$(srcdir)/Objects/stringlib/count.h \
+		$(srcdir)/Objects/stringlib/ctype.h \
 		$(srcdir)/Objects/stringlib/fastsearch.h \
 		$(srcdir)/Objects/stringlib/find.h \
 		$(srcdir)/Objects/stringlib/formatter.h \
 		$(srcdir)/Objects/stringlib/partition.h \
 		$(srcdir)/Objects/stringlib/stringdefs.h \
 		$(srcdir)/Objects/stringlib/string_format.h \
+		$(srcdir)/Objects/stringlib/transmogrify.h \
 		$(srcdir)/Objects/stringlib/unicodedefs.h
 
 Objects/unicodeobject.o: $(srcdir)/Objects/unicodeobject.c \
@@ -532,6 +537,8 @@
 
 Objects/stringobject.o: $(srcdir)/Objects/stringobject.c \
 				$(STRINGLIB_HEADERS)
+Objects/bytesobject.o: $(srcdir)/Objects/bytesobject.c \
+				$(STRINGLIB_HEADERS)
 
 Python/formatter_unicode.o: $(srcdir)/Python/formatter_unicode.c \
 				$(STRINGLIB_HEADERS)
@@ -550,6 +557,8 @@
 		Include/ast.h \
 		Include/bitset.h \
 		Include/boolobject.h \
+		Include/bytes_methods.h \
+		Include/bytesobject.h \
 		Include/bufferobject.h \
 		Include/cellobject.h \
 		Include/ceval.h \

Modified: python/branches/trunk-bytearray/Objects/bytesobject.c
==============================================================================
--- python/branches/trunk-bytearray/Objects/bytesobject.c	(original)
+++ python/branches/trunk-bytearray/Objects/bytesobject.c	Sat Mar 22 20:53:20 2008
@@ -713,12 +713,26 @@
         return 0;
     }
 
+    if (PyString_Check(arg)) {
+        PyObject *new;
+        if (encoding != NULL) {
+            PyErr_SetString(PyExc_TypeError,
+                            "str argument with an encoding");
+            return -1;
+        }
+        new = bytes_iconcat(self, arg);
+        if (new == NULL)
+            return -1;
+        Py_DECREF(new);
+        return 0;
+    }
+
     if (PyUnicode_Check(arg)) {
         /* Encode via the codec registry */
         PyObject *encoded, *new;
         if (encoding == NULL) {
             PyErr_SetString(PyExc_TypeError,
-                            "string argument without an encoding");
+                            "unicode argument without an encoding");
             return -1;
         }
         encoded = PyCodec_Encode(arg, encoding, errors);
@@ -3038,6 +3052,10 @@
 };
 
 static PyBufferProcs bytes_as_buffer = {
+    0,
+    0,
+    0,
+    0,
     (getbufferproc)bytes_getbuffer,
     (releasebufferproc)bytes_releasebuffer,
 };

Modified: python/branches/trunk-bytearray/Objects/exceptions.c
==============================================================================
--- python/branches/trunk-bytearray/Objects/exceptions.c	(original)
+++ python/branches/trunk-bytearray/Objects/exceptions.c	Sat Mar 22 20:53:20 2008
@@ -1923,6 +1923,12 @@
     "Base class for warnings about Unicode related problems, mostly\n"
     "related to conversion problems.");
 
+/*
+ *    BytesWarning extends Warning
+ */
+SimpleExtendsException(PyExc_Warning, BytesWarning,
+    "Base class for warnings about bytes and buffer related problems, mostly\n"
+    "related to conversion from str or comparing to str.");
 
 /* Pre-computed MemoryError instance.  Best to create this as early as
  * possible and not wait until a MemoryError is actually raised!
@@ -2031,6 +2037,7 @@
     PRE_INIT(FutureWarning)
     PRE_INIT(ImportWarning)
     PRE_INIT(UnicodeWarning)
+    PRE_INIT(BytesWarning)
 
     m = Py_InitModule4("exceptions", functions, exceptions_doc,
         (PyObject *)NULL, PYTHON_API_VERSION);
@@ -2097,6 +2104,7 @@
     POST_INIT(FutureWarning)
     POST_INIT(ImportWarning)
     POST_INIT(UnicodeWarning)
+    POST_INIT(BytesWarning)
 
     PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
     if (!PyExc_MemoryErrorInst)

Modified: python/branches/trunk-bytearray/Objects/object.c
==============================================================================
--- python/branches/trunk-bytearray/Objects/object.c	(original)
+++ python/branches/trunk-bytearray/Objects/object.c	Sat Mar 22 20:53:20 2008
@@ -1985,6 +1985,9 @@
 	if (PyType_Ready(&PyString_Type) < 0)
 		Py_FatalError("Can't initialize 'str'");
 
+	if (PyType_Ready(&PyBytes_Type) < 0)
+		Py_FatalError("Can't initialize 'bytes'");
+
 	if (PyType_Ready(&PyList_Type) < 0)
 		Py_FatalError("Can't initialize 'list'");
 

Modified: python/branches/trunk-bytearray/Objects/stringobject.c
==============================================================================
--- python/branches/trunk-bytearray/Objects/stringobject.c	(original)
+++ python/branches/trunk-bytearray/Objects/stringobject.c	Sat Mar 22 20:53:20 2008
@@ -1303,6 +1303,13 @@
 	return Py_SIZE(self);
 }
 
+static int
+string_buffer_getbuffer(PyStringObject *self, Py_buffer *view, int flags)
+{
+	return PyBuffer_FillInfo(view, (void *)self->ob_sval, Py_SIZE(self),
+				 0, flags);
+}
+
 static PySequenceMethods string_as_sequence = {
 	(lenfunc)string_length, /*sq_length*/
 	(binaryfunc)string_concat, /*sq_concat*/
@@ -1325,6 +1332,7 @@
 	(writebufferproc)string_buffer_getwritebuf,
 	(segcountproc)string_buffer_getsegcount,
 	(charbufferproc)string_buffer_getcharbuf,
+	(getbufferproc)string_buffer_getbuffer,
 };
 
 

Modified: python/branches/trunk-bytearray/Python/bltinmodule.c
==============================================================================
--- python/branches/trunk-bytearray/Python/bltinmodule.c	(original)
+++ python/branches/trunk-bytearray/Python/bltinmodule.c	Sat Mar 22 20:53:20 2008
@@ -2548,6 +2548,7 @@
 	SETBUILTIN("basestring",	&PyBaseString_Type);
 	SETBUILTIN("bool",		&PyBool_Type);
 	/*	SETBUILTIN("memoryview",        &PyMemoryView_Type); */
+	SETBUILTIN("bytearray",		&PyBytes_Type);
 	SETBUILTIN("bytes",		&PyString_Type);
 	SETBUILTIN("buffer",		&PyBuffer_Type);
 	SETBUILTIN("classmethod",	&PyClassMethod_Type);

Modified: python/branches/trunk-bytearray/Python/pythonrun.c
==============================================================================
--- python/branches/trunk-bytearray/Python/pythonrun.c	(original)
+++ python/branches/trunk-bytearray/Python/pythonrun.c	Sat Mar 22 20:53:20 2008
@@ -72,6 +72,7 @@
 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
 int Py_NoSiteFlag; /* Suppress 'import site' */
+int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
 int Py_FrozenFlag; /* Needed by getpath.c */
@@ -193,6 +194,9 @@
 	if (!_PyInt_Init())
 		Py_FatalError("Py_Initialize: can't init ints");
 
+	if (!PyBytes_Init())
+		Py_FatalError("Py_Initialize: can't init bytearray");
+
 	_PyFloat_Init();
 
 	interp->modules = PyDict_New();
@@ -251,8 +255,30 @@
 #endif /* WITH_THREAD */
 
 	warnings_module = PyImport_ImportModule("warnings");
-	if (!warnings_module)
+	if (!warnings_module) {
 		PyErr_Clear();
+	}
+#if 0
+	else {
+		PyObject *o;
+		char *action[8];
+
+		if (Py_BytesWarningFlag > 1)
+			*action = "error";
+		else if (Py_BytesWarningFlag)
+			*action = "default";
+		else
+			*action = "ignore";
+
+		o = PyObject_CallMethod(warnings_module,
+					"simplefilter", "sO",
+					*action, PyExc_BytesWarning);
+		if (o == NULL)
+			Py_FatalError("Py_Initialize: can't initialize"
+				      "warning filter for BytesWarning.");
+		Py_DECREF(o);
+        }
+#endif
 
 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
 	/* On Unix, set the file system encoding according to the
@@ -471,6 +497,7 @@
 	PyList_Fini();
 	PySet_Fini();
 	PyString_Fini();
+	PyBytes_Fini();
 	PyInt_Fini();
 	PyFloat_Fini();
 	PyDict_Fini();


More information about the Python-checkins mailing list