mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	gen_getattr: make the gi_running and gi_frame members discoverable (but
not writable -- too dangerous!) from Python code.
This commit is contained in:
		
							parent
							
								
									0ba70cc3c8
								
							
						
					
					
						commit
						e77f2e2798
					
				
					 2 changed files with 39 additions and 6 deletions
				
			
		|  | @ -368,7 +368,7 @@ | ||||||
|     [1, 2, 3, 4] |     [1, 2, 3, 4] | ||||||
| 5-combs of [1, 2, 3, 4]: | 5-combs of [1, 2, 3, 4]: | ||||||
| 
 | 
 | ||||||
| # From the Iterators list, about the types of these things. | From the Iterators list, about the types of these things. | ||||||
| 
 | 
 | ||||||
| >>> def g(): | >>> def g(): | ||||||
| ...     yield 1 | ...     yield 1 | ||||||
|  | @ -379,7 +379,7 @@ | ||||||
| >>> type(i) | >>> type(i) | ||||||
| <type 'generator'> | <type 'generator'> | ||||||
| >>> dir(i) | >>> dir(i) | ||||||
| ['next'] | ['gi_frame', 'gi_running', 'next'] | ||||||
| >>> print i.next.__doc__ | >>> print i.next.__doc__ | ||||||
| next() -- get the next value, or raise StopIteration | next() -- get the next value, or raise StopIteration | ||||||
| >>> iter(i) is i | >>> iter(i) is i | ||||||
|  | @ -387,6 +387,26 @@ | ||||||
| >>> import types | >>> import types | ||||||
| >>> isinstance(i, types.GeneratorType) | >>> isinstance(i, types.GeneratorType) | ||||||
| 1 | 1 | ||||||
|  | 
 | ||||||
|  | And more, added later. | ||||||
|  | 
 | ||||||
|  | >>> i.gi_running | ||||||
|  | 0 | ||||||
|  | >>> type(i.gi_frame) | ||||||
|  | <type 'frame'> | ||||||
|  | >>> i.gi_running = 42 | ||||||
|  | Traceback (most recent call last): | ||||||
|  |   ... | ||||||
|  | TypeError: object has read-only attributes | ||||||
|  | >>> def g(): | ||||||
|  | ...     yield me.gi_running | ||||||
|  | >>> me = g() | ||||||
|  | >>> me.gi_running | ||||||
|  | 0 | ||||||
|  | >>> me.next() | ||||||
|  | 1 | ||||||
|  | >>> me.gi_running | ||||||
|  | 0 | ||||||
| """ | """ | ||||||
| 
 | 
 | ||||||
| # Fun tests (for sufficiently warped notions of "fun"). | # Fun tests (for sufficiently warped notions of "fun"). | ||||||
|  |  | ||||||
|  | @ -115,8 +115,8 @@ typedef struct { | ||||||
| 
 | 
 | ||||||
| 	PyFrameObject *gi_frame; | 	PyFrameObject *gi_frame; | ||||||
| 
 | 
 | ||||||
|         /* True if generator is being executed. */  | 	/* True if generator is being executed. */  | ||||||
|         int gi_running; | 	int gi_running; | ||||||
| } genobject; | } genobject; | ||||||
| 
 | 
 | ||||||
| static PyObject * | static PyObject * | ||||||
|  | @ -207,14 +207,27 @@ gen_getiter(PyObject *gen) | ||||||
| 
 | 
 | ||||||
| static struct PyMethodDef gen_methods[] = { | static struct PyMethodDef gen_methods[] = { | ||||||
| 	{"next",     (PyCFunction)gen_next, METH_VARARGS, | 	{"next",     (PyCFunction)gen_next, METH_VARARGS, | ||||||
| 	 "next() -- get the next value, or raise StopIteration"}, | 	 	"next() -- get the next value, or raise StopIteration"}, | ||||||
| 	{NULL,          NULL}   /* Sentinel */ | 	{NULL,          NULL}   /* Sentinel */ | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| static PyObject * | static PyObject * | ||||||
| gen_getattr(genobject *gen, char *name) | gen_getattr(genobject *gen, char *name) | ||||||
| { | { | ||||||
| 	return Py_FindMethod(gen_methods, (PyObject *)gen, name); | 	PyObject *result; | ||||||
|  | 
 | ||||||
|  | 	if (strcmp(name, "gi_frame") == 0) { | ||||||
|  | 		result = (PyObject *)gen->gi_frame; | ||||||
|  | 		assert(result != NULL); | ||||||
|  | 		Py_INCREF(result); | ||||||
|  | 	} | ||||||
|  | 	else if (strcmp(name, "gi_running") == 0) | ||||||
|  | 		result = (PyObject *)PyInt_FromLong((long)gen->gi_running); | ||||||
|  | 	else if (strcmp(name, "__members__") == 0) | ||||||
|  | 		result = Py_BuildValue("[ss]", "gi_frame", "gi_running"); | ||||||
|  | 	else | ||||||
|  |  		result = Py_FindMethod(gen_methods, (PyObject *)gen, name); | ||||||
|  |  	return result; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| statichere PyTypeObject gentype = { | statichere PyTypeObject gentype = { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Tim Peters
						Tim Peters