[Python-checkins] r51679 - in python/branches/bcannon-objcap: Lib/compiler/pyassem.py Lib/test/output/test_new Lib/test/test_new.py Lib/test/test_objcap.py Modules/objcapmodule.c Objects/codeobject.c securing_python.txt

brett.cannon python-checkins at python.org
Sat Sep 2 00:42:52 CEST 2006


Author: brett.cannon
Date: Sat Sep 2 00:42:51 2006
New Revision: 51679
Modified:
 python/branches/bcannon-objcap/Lib/compiler/pyassem.py
 python/branches/bcannon-objcap/Lib/test/output/test_new
 python/branches/bcannon-objcap/Lib/test/test_new.py
 python/branches/bcannon-objcap/Lib/test/test_objcap.py
 python/branches/bcannon-objcap/Modules/objcapmodule.c
 python/branches/bcannon-objcap/Objects/codeobject.c
 python/branches/bcannon-objcap/securing_python.txt
Log:
Remove the code type's constructor. Now the only way to create a code instance
in Python code is through the factory function objcap.code_new().
Modified: python/branches/bcannon-objcap/Lib/compiler/pyassem.py
==============================================================================
--- python/branches/bcannon-objcap/Lib/compiler/pyassem.py	(original)
+++ python/branches/bcannon-objcap/Lib/compiler/pyassem.py	Sat Sep 2 00:42:51 2006
@@ -1,8 +1,8 @@
 """A flow graph representation for Python bytecode"""
 
 import dis
-import new
 import sys
+import objcap
 
 from compiler import misc
 from compiler.consts \
@@ -595,7 +595,7 @@
 argcount = self.argcount
 if self.flags & CO_VARKEYWORDS:
 argcount = argcount - 1
- return new.code(argcount, nlocals, self.stacksize, self.flags,
+ return objcap.code_new(argcount, nlocals, self.stacksize, self.flags,
 self.lnotab.getCode(), self.getConsts(),
 tuple(self.names), tuple(self.varnames),
 self.filename, self.name, self.lnotab.firstline,
Modified: python/branches/bcannon-objcap/Lib/test/output/test_new
==============================================================================
--- python/branches/bcannon-objcap/Lib/test/output/test_new	(original)
+++ python/branches/bcannon-objcap/Lib/test/output/test_new	Sat Sep 2 00:42:51 2006
@@ -4,4 +4,3 @@
 new.instance()
 new.instancemethod()
 new.function()
-new.code()
Modified: python/branches/bcannon-objcap/Lib/test/test_new.py
==============================================================================
--- python/branches/bcannon-objcap/Lib/test/test_new.py	(original)
+++ python/branches/bcannon-objcap/Lib/test/test_new.py	Sat Sep 2 00:42:51 2006
@@ -105,71 +105,3 @@
 test_closure(g, (1, 1), ValueError) # closure is wrong size
 test_closure(f, g.func_closure, ValueError) # no closure needed
 
-print 'new.code()'
-# bogus test of new.code()
-# Note: Jython will never have new.code()
-if hasattr(new, 'code'):
- def f(a): pass
-
- c = f.func_code
- argcount = c.co_argcount
- nlocals = c.co_nlocals
- stacksize = c.co_stacksize
- flags = c.co_flags
- codestring = c.co_code
- constants = c.co_consts
- names = c.co_names
- varnames = c.co_varnames
- filename = c.co_filename
- name = c.co_name
- firstlineno = c.co_firstlineno
- lnotab = c.co_lnotab
- freevars = c.co_freevars
- cellvars = c.co_cellvars
-
- d = new.code(argcount, nlocals, stacksize, flags, codestring,
- constants, names, varnames, filename, name,
- firstlineno, lnotab, freevars, cellvars)
-
- # test backwards-compatibility version with no freevars or cellvars
- d = new.code(argcount, nlocals, stacksize, flags, codestring,
- constants, names, varnames, filename, name,
- firstlineno, lnotab)
-
- try: # this used to trigger a SystemError
- d = new.code(-argcount, nlocals, stacksize, flags, codestring,
- constants, names, varnames, filename, name,
- firstlineno, lnotab)
- except ValueError:
- pass
- else:
- raise TestFailed, "negative co_argcount didn't trigger an exception"
-
- try: # this used to trigger a SystemError
- d = new.code(argcount, -nlocals, stacksize, flags, codestring,
- constants, names, varnames, filename, name,
- firstlineno, lnotab)
- except ValueError:
- pass
- else:
- raise TestFailed, "negative co_nlocals didn't trigger an exception"
-
- try: # this used to trigger a Py_FatalError!
- d = new.code(argcount, nlocals, stacksize, flags, codestring,
- constants, (5,), varnames, filename, name,
- firstlineno, lnotab)
- except TypeError:
- pass
- else:
- raise TestFailed, "non-string co_name didn't trigger an exception"
-
- # new.code used to be a way to mutate a tuple...
- class S(str): pass
- t = (S("ab"),)
- d = new.code(argcount, nlocals, stacksize, flags, codestring,
- constants, t, varnames, filename, name,
- firstlineno, lnotab)
- verify(type(t[0]) is S, "eek, tuple changed under us!")
-
- if verbose:
- print d
Modified: python/branches/bcannon-objcap/Lib/test/test_objcap.py
==============================================================================
--- python/branches/bcannon-objcap/Lib/test/test_objcap.py	(original)
+++ python/branches/bcannon-objcap/Lib/test/test_objcap.py	Sat Sep 2 00:42:51 2006
@@ -60,10 +60,84 @@
 ins.close()
 
 
+class CodeNewTests(unittest.TestCase):
+
+ """Test code_new()."""
+
+ def test_all(self):
+ # Originally taken from test_new.py .
+
+ def f(a): pass
+
+ c = f.func_code
+ argcount = c.co_argcount
+ nlocals = c.co_nlocals
+ stacksize = c.co_stacksize
+ flags = c.co_flags
+ codestring = c.co_code
+ constants = c.co_consts
+ names = c.co_names
+ varnames = c.co_varnames
+ filename = c.co_filename
+ name = c.co_name
+ firstlineno = c.co_firstlineno
+ lnotab = c.co_lnotab
+ freevars = c.co_freevars
+ cellvars = c.co_cellvars
+
+ d = objcap.code_new(argcount, nlocals, stacksize, flags, codestring,
+ constants, names, varnames, filename, name,
+ firstlineno, lnotab, freevars, cellvars)
+
+ # test backwards-compatibility version with no freevars or cellvars
+ d = objcap.code_new(argcount, nlocals, stacksize, flags, codestring,
+ constants, names, varnames, filename, name,
+ firstlineno, lnotab)
+
+ try: # this used to trigger a SystemError
+ d = objcap.code_new(-argcount, nlocals, stacksize, flags, codestring,
+ constants, names, varnames, filename, name,
+ firstlineno, lnotab)
+ except ValueError:
+ pass
+ else:
+ raise test_support.TestFailed(
+ "negative co_argcount didn't trigger an exception")
+
+ try: # this used to trigger a SystemError
+ d = objcap.code_new(argcount, -nlocals, stacksize, flags, codestring,
+ constants, names, varnames, filename, name,
+ firstlineno, lnotab)
+ except ValueError:
+ pass
+ else:
+ raise test_support.TestFailed(
+ "negative co_nlocals didn't trigger an exception")
+
+ try: # this used to trigger a Py_FatalError!
+ d = objcap.code_new(argcount, nlocals, stacksize, flags, codestring,
+ constants, (5,), varnames, filename, name,
+ firstlineno, lnotab)
+ except TypeError:
+ pass
+ else:
+ raise TestFailed, "non-string co_name didn't trigger an exception"
+
+ # new.code used to be a way to mutate a tuple...
+ class S(str): pass
+ t = (S("ab"),)
+ d = objcap.code_new(argcount, nlocals, stacksize, flags, codestring,
+ constants, t, varnames, filename, name,
+ firstlineno, lnotab)
+ self.failUnless(type(t[0]) is S, "eek, tuple changed under us!")
+
+
+
 def test_main():
 test_support.run_unittest(
 ObjectSubclasses,
 FileInitTests,
+ CodeNewTests,
 )
 
 
Modified: python/branches/bcannon-objcap/Modules/objcapmodule.c
==============================================================================
--- python/branches/bcannon-objcap/Modules/objcapmodule.c	(original)
+++ python/branches/bcannon-objcap/Modules/objcapmodule.c	Sat Sep 2 00:42:51 2006
@@ -132,11 +132,139 @@
 );
 
 
+/* Helper for code_new: return a shallow copy of a tuple that is
+ guaranteed to contain exact strings, by converting string subclasses
+ to exact strings and complaining if a non-string is found. */
+static PyObject*
+validate_and_copy_tuple(PyObject *tup)
+{
+	PyObject *newtuple;
+	PyObject *item;
+	Py_ssize_t i, len;
+
+	len = PyTuple_GET_SIZE(tup);
+	newtuple = PyTuple_New(len);
+	if (newtuple == NULL)
+		return NULL;
+
+	for (i = 0; i < len; i++) {
+		item = PyTuple_GET_ITEM(tup, i);
+		if (PyString_CheckExact(item)) {
+			Py_INCREF(item);
+		}
+		else if (!PyString_Check(item)) {
+			PyErr_Format(
+				PyExc_TypeError,
+				"name tuples must contain only "
+				"strings, not '%.500s'",
+				item->ob_type->tp_name);
+			Py_DECREF(newtuple);
+			return NULL;
+		}
+		else {
+			item = PyString_FromStringAndSize(
+				PyString_AS_STRING(item),
+				PyString_GET_SIZE(item));
+			if (item == NULL) {
+				Py_DECREF(newtuple);
+				return NULL;
+			}
+		}
+		PyTuple_SET_ITEM(newtuple, i, item);
+	}
+
+	return newtuple;
+}
+
+PyDoc_STRVAR(code_doc,
+"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
+ varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
+\n\
+Create a code object. Not for the faint of heart.");
+
+static PyObject *
+code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
+{
+	int argcount;
+	int nlocals;
+	int stacksize;
+	int flags;
+	PyObject *co = NULL;
+	PyObject *code;
+	PyObject *consts;
+	PyObject *names, *ournames = NULL;
+	PyObject *varnames, *ourvarnames = NULL;
+	PyObject *freevars = NULL, *ourfreevars = NULL;
+	PyObject *cellvars = NULL, *ourcellvars = NULL;
+	PyObject *filename;
+	PyObject *name;
+	int firstlineno;
+	PyObject *lnotab;
+
+	if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
+			 &argcount, &nlocals, &stacksize, &flags,
+			 &code,
+			 &PyTuple_Type, &consts,
+			 &PyTuple_Type, &names,
+			 &PyTuple_Type, &varnames,
+			 &filename, &name,
+			 &firstlineno, &lnotab,
+			 &PyTuple_Type, &freevars,
+			 &PyTuple_Type, &cellvars))
+		return NULL;
+
+	if (argcount < 0) {
+		PyErr_SetString(
+			PyExc_ValueError,
+			"code: argcount must not be negative");
+		goto cleanup;
+	}
+
+	if (nlocals < 0) {
+		PyErr_SetString(
+			PyExc_ValueError,
+			"code: nlocals must not be negative");
+		goto cleanup;
+	}
+
+	ournames = validate_and_copy_tuple(names);
+	if (ournames == NULL)
+		goto cleanup;
+	ourvarnames = validate_and_copy_tuple(varnames);
+	if (ourvarnames == NULL)
+		goto cleanup;
+	if (freevars)
+		ourfreevars = validate_and_copy_tuple(freevars);
+	else
+		ourfreevars = PyTuple_New(0);
+	if (ourfreevars == NULL)
+		goto cleanup;
+	if (cellvars)
+		ourcellvars = validate_and_copy_tuple(cellvars);
+	else
+		ourcellvars = PyTuple_New(0);
+	if (ourcellvars == NULL)
+		goto cleanup;
+
+	co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
+				 code, consts, ournames, ourvarnames,
+				 ourfreevars, ourcellvars, filename,
+				 name, firstlineno, lnotab);
+ cleanup:
+	Py_XDECREF(ournames);
+	Py_XDECREF(ourvarnames);
+	Py_XDECREF(ourfreevars);
+	Py_XDECREF(ourcellvars);
+	return co;
+}
+
+
 
 static PyMethodDef module_methods[] = {
 {"subclasses", (PyCFunction)object_subclasses, METH_O, object_subclass_doc},
 {"file_init", (PyCFunction)file_init, METH_VARARGS | METH_KEYWORDS,
 	file_init_doc},
+ {"code_new", (PyCFunction)code_new, METH_VARARGS, code_doc},
 {NULL, NULL}
 };
 
Modified: python/branches/bcannon-objcap/Objects/codeobject.c
==============================================================================
--- python/branches/bcannon-objcap/Objects/codeobject.c	(original)
+++ python/branches/bcannon-objcap/Objects/codeobject.c	Sat Sep 2 00:42:51 2006
@@ -128,131 +128,10 @@
 	{NULL}	/* Sentinel */
 };
 
-/* Helper for code_new: return a shallow copy of a tuple that is
- guaranteed to contain exact strings, by converting string subclasses
- to exact strings and complaining if a non-string is found. */
-static PyObject*
-validate_and_copy_tuple(PyObject *tup)
-{
-	PyObject *newtuple;
-	PyObject *item;
-	Py_ssize_t i, len;
-
-	len = PyTuple_GET_SIZE(tup);
-	newtuple = PyTuple_New(len);
-	if (newtuple == NULL)
-		return NULL;
-
-	for (i = 0; i < len; i++) {
-		item = PyTuple_GET_ITEM(tup, i);
-		if (PyString_CheckExact(item)) {
-			Py_INCREF(item);
-		}
-		else if (!PyString_Check(item)) {
-			PyErr_Format(
-				PyExc_TypeError,
-				"name tuples must contain only "
-				"strings, not '%.500s'",
-				item->ob_type->tp_name);
-			Py_DECREF(newtuple);
-			return NULL;
-		}
-		else {
-			item = PyString_FromStringAndSize(
-				PyString_AS_STRING(item),
-				PyString_GET_SIZE(item));
-			if (item == NULL) {
-				Py_DECREF(newtuple);
-				return NULL;
-			}
-		}
-		PyTuple_SET_ITEM(newtuple, i, item);
-	}
-
-	return newtuple;
-}
 
 PyDoc_STRVAR(code_doc,
-"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
- varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
-\n\
-Create a code object. Not for the faint of heart.");
+"Code type.");
 
-static PyObject *
-code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
-{
-	int argcount;
-	int nlocals;
-	int stacksize;
-	int flags;
-	PyObject *co = NULL;
-	PyObject *code;
-	PyObject *consts;
-	PyObject *names, *ournames = NULL;
-	PyObject *varnames, *ourvarnames = NULL;
-	PyObject *freevars = NULL, *ourfreevars = NULL;
-	PyObject *cellvars = NULL, *ourcellvars = NULL;
-	PyObject *filename;
-	PyObject *name;
-	int firstlineno;
-	PyObject *lnotab;
-
-	if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
-			 &argcount, &nlocals, &stacksize, &flags,
-			 &code,
-			 &PyTuple_Type, &consts,
-			 &PyTuple_Type, &names,
-			 &PyTuple_Type, &varnames,
-			 &filename, &name,
-			 &firstlineno, &lnotab,
-			 &PyTuple_Type, &freevars,
-			 &PyTuple_Type, &cellvars))
-		return NULL;
-
-	if (argcount < 0) {
-		PyErr_SetString(
-			PyExc_ValueError,
-			"code: argcount must not be negative");
-		goto cleanup;
-	}
-
-	if (nlocals < 0) {
-		PyErr_SetString(
-			PyExc_ValueError,
-			"code: nlocals must not be negative");
-		goto cleanup;
-	}
-
-	ournames = validate_and_copy_tuple(names);
-	if (ournames == NULL)
-		goto cleanup;
-	ourvarnames = validate_and_copy_tuple(varnames);
-	if (ourvarnames == NULL)
-		goto cleanup;
-	if (freevars)
-		ourfreevars = validate_and_copy_tuple(freevars);
-	else
-		ourfreevars = PyTuple_New(0);
-	if (ourfreevars == NULL)
-		goto cleanup;
-	if (cellvars)
-		ourcellvars = validate_and_copy_tuple(cellvars);
-	else
-		ourcellvars = PyTuple_New(0);
-	if (ourcellvars == NULL)
-		goto cleanup;
-
-	co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
-				 code, consts, ournames, ourvarnames,
-				 ourfreevars, ourcellvars, filename,
-				 name, firstlineno, lnotab);
- cleanup:
-	Py_XDECREF(ournames);
-	Py_XDECREF(ourvarnames);
-	Py_XDECREF(ourfreevars);
-	Py_XDECREF(ourcellvars);
-	return co;
-}
 
 static void
 code_dealloc(PyCodeObject *co)
@@ -392,7 +271,7 @@
 	0,				/* tp_dictoffset */
 	0,				/* tp_init */
 	0,				/* tp_alloc */
-	code_new,			/* tp_new */
+	0,				/* tp_new */
 };
 
 /* All about c_lnotab.
Modified: python/branches/bcannon-objcap/securing_python.txt
==============================================================================
--- python/branches/bcannon-objcap/securing_python.txt	(original)
+++ python/branches/bcannon-objcap/securing_python.txt	Sat Sep 2 00:42:51 2006
@@ -24,7 +24,8 @@
 makes less of a performance-critical operation.
 + Might need to add some C code for easily accessing
 built-in objects.
- - code
+ - code [done]
+ * Add objcap.code_new() function [done]
 - ??? <go through Objects/*>
 + Sandboxed versions of built-ins (`Sanitizing Built-In Types`_)
 - open()


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /