Newbies: Re: Returning none

Michael Hudson mwh21 at cam.ac.uk
Fri Sep 3 11:51:42 EDT 1999


Got bored.

On Fri, 3 Sep 1999, Evan Simpson wrote:

> I have the vague idea that someone else already suggested this, but I'm not
> going to let that stop me:
> 
> Could we have a new singleton, perhaps called "Nothing", which is passed
> back by empty "return"s and at the end of functions (and explicitly, if one
> likes).  Have the code for function calls raise an exception if (1)
> "Nothing" is being returned and (2) the next opcode is not "POP_TOP".

I also check for PRINT_EXPR, otherwise it would be really annoying.

> Make all builtins and standard library functions which act like "[].sort"
> and "print_traceback" return "Nothing". 

Haven't done this (obviously; I'm at work, I'm supposed to be working,
even if it is my last day).

> Now any attempt to use a
> "procedure" except as a simple statement will raise an exception, and
> functions can be explicit about whether or not they return a usable value.

with my hacked python:

>>> def f():
...  pass
>>> f()
>>> a=f()
=> exception

> Heck, a single function could even act as a "procedure" in some
> circumstances and not in others.

Ooh, a whole slew of new good programming practices!

> Of course, adding "Nothing" means we have to wait for 2.0, right?

I'd imagine so. I haven't updated the magic number, so .pyc won't get
recompiled so old code won't break immediately.

I-don't-actually-think-this-is-a-good-idea-ly y'rs
Michael

Index: Include/object.h
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Include/object.h,v
retrieving revision 2.47
diff -u -r2.47 object.h
--- object.h	1998/12/04 18:48:12	2.47
+++ object.h	1999/09/03 15:25:27
@@ -431,6 +431,9 @@
 Don't forget to apply Py_INCREF() when returning this value!!!
 */
 
+extern DL_IMPORT(PyObject) _Py_NothingStruct; /* Don't use this directly */
+
+#define Py_Nothing (&_Py_NothingStruct)
 extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
 
 #define Py_None (&_Py_NoneStruct)
Index: Objects/object.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/object.c,v
retrieving revision 2.60
diff -u -r2.60 object.c
--- object.c	1998/07/21 21:56:41	2.60
+++ object.c	1999/09/03 15:25:27
@@ -618,6 +618,9 @@
 PyObject _Py_NoneStruct = {
 	PyObject_HEAD_INIT(&PyNothing_Type)
 };
+PyObject _Py_NothingStruct = {
+	PyObject_HEAD_INIT(&PyNothing_Type)
+};
 
 
 #ifdef Py_TRACE_REFS
Index: Python/bltinmodule.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.145
diff -u -r2.145 bltinmodule.c
--- bltinmodule.c	1999/07/19 15:21:16	2.145
+++ bltinmodule.c	1999/09/03 15:25:27
@@ -2489,6 +2489,8 @@
 		return NULL;
 	dict = PyModule_GetDict(mod);
 	initerrors(dict);
+	if (PyDict_SetItemString(dict, "Nothing", Py_Nothing) < 0)
+		return NULL;
 	if (PyDict_SetItemString(dict, "None", Py_None) < 0)
 		return NULL;
 	if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
Index: Python/ceval.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/ceval.c,v
retrieving revision 2.162
diff -u -r2.162 ceval.c
--- ceval.c	1999/06/22 14:47:32	2.162
+++ ceval.c	1999/09/03 15:25:28
@@ -1014,6 +1014,7 @@
 			/* After printing, also assign to '_' */
 			/* Before, set '_' to None to avoid recursion */
 			if (v != Py_None &&
+				v != Py_Nothing &&
 			    (err = PyDict_SetItemString(
 				    f->f_builtins, "_", Py_None)) == 0) {
 				err = Py_FlushLine();
@@ -1662,6 +1663,14 @@
 				Py_DECREF(w);
 			}
 			PUSH(x);
+			if (x == Py_Nothing 
+				&& *next_instr != POP_TOP 
+				&& *next_instr != PRINT_EXPR) {
+				x = NULL;
+				PyErr_SetString(
+					PyExc_TypeError,
+	   "Nothing used! (chew on that error message, wimps)");
+			}
 			if (x != NULL) continue;
 			break;
 		}
Index: Python/compile.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/compile.c,v
retrieving revision 2.97
diff -u -r2.97 compile.c
--- compile.c	1999/01/28 15:08:09	2.97
+++ compile.c	1999/09/03 15:25:28
@@ -3158,7 +3158,7 @@
 	c->c_infunction = 1;
 	com_node(c, CHILD(n, 4));
 	c->c_infunction = 0;
-	com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
+	com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Nothing));
 	com_push(c, 1);
 	com_addbyte(c, RETURN_VALUE);
 	com_pop(c, 1);







More information about the Python-list mailing list