summaryrefslogtreecommitdiff
path: root/Python/ceval.c
AgeCommit message (Collapse)AuthorLines
2001-09-04Move call_trace(..., PyTrace_CALL, ...) call to top of eval_frame. ThatNeil Schemenauer-35/+35
way it's called each time a generator is resumed. The tracing of normal functions should be unaffected by this change.
2001-08-30Do the int inlining only if the type is really an int, not wheneverGuido van Rossum-6/+9
PyInt_Check() succeeds. That returns true for subtypes of int, which may override __add__ or __sub__.
2001-08-30Removed some unreachable break statements to silence SGI compiler.Sjoerd Mullender-3/+0
2001-08-23When an inlined operation on two small ints causes overflow, don'tGuido van Rossum-32/+24
raise the exception here -- call the generic function (which may convert the arguments to long and try again).
2001-08-18Fix SF bug #443600:Guido van Rossum-15/+46
Change to get/set/del slice operations so that if the object doesn't support slicing, *or* if either of the slice arguments is not an int or long, we construct a slice object and call the get/set/del item operation instead. This makes it possible to design classes that support slice arguments of non-integral types.
2001-08-17ceval, PyEval_MergeCompilerFlags: wasn't merging in theTim Peters-6/+3
CO_FUTURE_DIVISION flag. Redid this to use Jeremy's PyCF_MASK #define instead, so we dont have to remember to fiddle individual feature names here again. pythonrun.h: Also #define a PyCF_MASK_OBSOLETE mask. This isn't used yet, but will be as part of the PEP 264 implementation (compile() mustn't raise an error just because old code uses a flag name that's become obsolete; a warning may be appropriate, but not an error; so compile() has to know about obsolete flags too, but nobody is going to remember to update compile() with individual obsolete flag names across releases either -- i.e., this is the flip side of PyEval_MergeCompilerFlags's oversight).
2001-08-16Patch #427190: Implement and use METH_NOARGS and METH_O.Martin v. Löwis-22/+49
2001-08-12Remove much dead code from ceval.cJeremy Hylton-220/+18
The descr changes moved the dispatch for calling objects from call_object() in ceval.c to PyObject_Call() in abstract.c. call_object() and the many functions it used in ceval.c were no longer used, but were not removed. Rename meth_call() as PyCFunction_Call() so that it can be called by the CALL_FUNCTION opcode in ceval.c. Also, fix error message that referred to PyEval_EvalCodeEx() by its old name eval_code2(). (I'll probably refer to it by its old name, too.)
2001-08-10Refactor future feature handlingJeremy Hylton-2/+2
Replace uses of PyCF_xxx with CO_xxx. Replace individual feature slots in PyFutureFeatures with single bitmask ff_features. When flags must be transfered among the three parts of the interpreter that care about them -- the pythonrun layer, the compiler, and the future feature parser -- can simply or (|) the definitions.
2001-08-08Implement PEP 238 in its (almost) full glory.Guido van Rossum-0/+40
This introduces: - A new operator // that means floor division (the kind of division where 1/2 is 0). - The "future division" statement ("from __future__ import division) which changes the meaning of the / operator to implement "true division" (where 1/2 is 0.5). - New overloadable operators __truediv__ and __floordiv__. - New slots in the PyNumberMethods struct for true and floor division, new abstract APIs for them, new opcodes, and so on. I emphasize that without the future division statement, the semantics of / will remain unchanged until Python 3.0. Not yet implemented are warnings (default off) when / is used with int or long arguments. This has been on display since 7/31 as SF patch #443474. Flames to /dev/null.
2001-08-02Merge of descr-branch back into trunk.Tim Peters-109/+54
2001-07-16Part way to allowing "from __future__ import generators" to communicateTim Peters-13/+24
that info to code dynamically compiled *by* code compiled with generators enabled. Doesn't yet work because there's still no way to tell the parser that "yield" is OK (unlike nested_scopes, the parser has its fingers in this too). Replaced PyEval_GetNestedScopes by a more-general PyEval_MergeCompilerFlags. Perhaps I should not have? I doubted it was *intended* to be part of the public API, so just did.
2001-07-12GC for generator objects.Neil Schemenauer-4/+12
2001-07-03This change adjusts the profiling/tracing support so that the commonFred Drake-47/+65
path (with no profile/trace function) through eval_code2() and eval_frame() avoids several checks. In the common cases of calls, returns, and exception propogation, eval_code2() and eval_frame() used to test two values in the thread-state: the profiling function and the tracing function. With this change, a flag is set in the thread-state if either of these is active, allowing a single check to suffice when both are NULL. This also simplifies the code needed when either function is in use but is already active (to avoid profiling/tracing the profiler/tracer); the flag is set to 0 when the profile/trace code is entered, allowing the same check to suffice for "already in the tracer" for call/return/ exception events.
2001-06-27Revise the interface to the profiling and tracing support for theFred Drake-133/+64
Python interpreter. This change adds two new C-level APIs: PyEval_SetProfile() and PyEval_SetTrace(). These can be used to install profile and trace functions implemented in C, which can operate at much higher speeds than Python-based functions. The overhead for calling a C-based profile function is a very small fraction of a percent of the overhead involved in calling a Python-based function. The machinery required to call a Python-based profile or trace function been moved to sysmodule.c, where sys.setprofile() and sys.setprofile() simply become users of the new interface. As a side effect, SF bug #436058 is fixed; there is no longer a _PyTrace_Init() function to declare.
2001-06-26gen_getattr: make the gi_running and gi_frame members discoverable (butTim Peters-4/+17
not writable -- too dangerous!) from Python code.
2001-06-26Add "gi_" (generator-iterator) prefix to names of genobject members.Tim Peters-9/+13
Makes it much easier to find references via dumb editor search (former "frame" in particular was near-hopeless).
2001-06-23Change the semantics of "return" in generators, as discussed on theTim Peters-0/+7
Iterators list and Python-Dev; e.g., these all pass now: def g1(): try: return except: yield 1 assert list(g1()) == [] def g2(): try: return finally: yield 1 assert list(g2()) == [1] def g3(): for i in range(3): yield None yield None assert list(g3()) == [None] * 4 compile.c: compile_funcdef and com_return_stmt: Just van Rossum's patch to compile the same code for "return" regardless of function type (this goes back to the previous scheme of returning Py_None). ceval.c: gen_iternext: take a return (but not a yield) of Py_None as meaning the generator is exhausted.
2001-06-23gen_iternext(): Don't assume that the current thread state's frame isTim Peters-2/+2
not NULL. I don't think it can be NULL from Python code, but if using generators via the C API I expect a NULL frame is possible.
2001-06-23PyFrameObject: rename f_stackbottom to f_stacktop, since it points toTim Peters-5/+5
the next free valuestack slot, not to the base (in America, stacks push and pop at the top -- they mutate at the bottom in Australia <winK>). eval_frame(): assert that f_stacktop isn't NULL upon entry. frame_delloc(): avoid ordered pointer comparisons involving f_stacktop when f_stacktop is NULL.
2001-06-21Teach the UNPACK_SEQUENCE opcode how to tease an iterable object intoTim Peters-32/+38
giving up the goods. NEEDS DOC CHANGES
2001-06-21Try to avoid creating reference cycles involving generators. Only keep aNeil Schemenauer-14/+27
reference to f_back when its really needed. Do a little whitespace normalization as well. This whole file is a big war between tabs and spaces but now is probably not the time to reindent everything.
2001-06-20gen_iternext(): repair subtle refcount problem.Tim Peters-0/+5
NeilS, please check! This came from staring at your genbug.py, but I'm not sure it plugs all possible holes. Without this, I caught a frameobject refcount going negative, and it was also the cause (in debug build) of _Py_ForgetReference's attempt to forget an object with already- NULL _ob_prev and _ob_next pointers -- although I'm still not entirely sure how! Part of the difficulty is that frameobjects are stored on a free list that gets recycled very quickly, so if there's a stray pointer to one of them it never looks like an insane frameobject (never goes trough the free() mangling MS debug forces, etc).
2001-06-20Remove unused code.Neil Schemenauer-9/+0
2001-06-18Merging the gen-branch into the main line, at Guido's direction. Yay!Tim Peters-257/+419
Bugfix candidate in inspect.py: it was referencing "self" outside of a method.
2001-06-16Instead of initializing & interning the strings passed to the profileFred Drake-26/+42
and trace functions lazily, which incurs extra argument pushing and checks in the C overhead for profiling/tracing, create the strings semi-lazily when the Python code first registers a profile or trace function. This simplifies the trampoline into the profile/trace functions.
2001-06-16SF bug 433228: repr(list) woes when len(list) bigTim Peters-2/+3
call_object: If the object isn't callable, display its type in the error msg rather than its repr. Bugfix candidate.
2001-06-08call_trace(): Add an additional parameter -- pointer to a PyObject*Fred Drake-13/+36
that should be used to cache an interned version of the event string passed to the profile/trace function. call_trace() will create interned strings and cache them in using the storage specified by this additional parameter, avoiding a lot of string object creation at runtime when using the profiling or tracing functions. All call sites are modified to pass the additional parameter, and four static PyObject* variables are allocated to cache the interned string objects. This closes SF patch #431257.
2001-05-29Fix bug reported by Tim Peters on python-dev:Jeremy Hylton-6/+5
Keyword arguments passed to builtin functions that don't take them are ignored. >>> {}.clear(x=2) >>> instead of >>> {}.clear(x=2) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: clear() takes no keyword arguments
2001-05-18Add a second special case to the inline function call code in eval_code2().Jeremy Hylton-1/+7
If we have a PyCFunction (builtin) and it is METH_VARARGS only, load the args and dispatch to call_cfunction() directly. This provides a small speedup for perhaps the most common function calls -- builtins.
2001-05-05Make PyIter_Next() a little smarter (wrt its knowledge of iteratorTim Peters-5/+3
internals) so clients can be a lot dumber (wrt their knowledge).
2001-04-27improved error message-- names the type of the unexpected objectJeremy Hylton-2/+3
2001-04-23Mondo changes to the iterator stuff, without changing how Python codeGuido van Rossum-27/+16
sees it (test_iter.py is unchanged). - Added a tp_iternext slot, which calls the iterator's next() method; this is much faster for built-in iterators over built-in types such as lists and dicts, speeding up pybench's ForLoop with about 25% compared to Python 2.1. (Now there's a good argument for iterators. ;-) - Renamed the built-in sequence iterator SeqIter, affecting the C API functions for it. (This frees up the PyIter prefix for generic iterator operations.) - Added PyIter_Check(obj), which checks that obj's type has a tp_iternext slot and that the proper feature flag is set. - Added PyIter_Next(obj) which calls the tp_iternext slot. It has a somewhat complex return condition due to the need for speed: when it returns NULL, it may not have set an exception condition, meaning the iterator is exhausted; when the exception StopIteration is set (or a derived exception class), it means the same thing; any other exception means some other error occurred.
2001-04-21SF but #417587: compiler warnings compiling 2.1.Tim Peters-1/+0
Repaired *some* of the SGI compiler warnings Sjoerd Mullender reported.
2001-04-20Iterators phase 1. This comprises:Guido van Rossum-0/+41
new slot tp_iter in type object, plus new flag Py_TPFLAGS_HAVE_ITER new C API PyObject_GetIter(), calls tp_iter new builtin iter(), with two forms: iter(obj), and iter(function, sentinel) new internal object types iterobject and calliterobject new exception StopIteration new opcodes for "for" loops, GET_ITER and FOR_ITER (also supported by dis.py) new magic number for .pyc files new special method for instances: __iter__() returns an iterator iteration over dictionaries: "for x in dict" iterates over the keys iteration over files: "for x in file" iterates over lines TODO: documentation test suite decide whether to use a different way to spell iter(function, sentinal) decide whether "for key in dict" is a good idea use iterators in map/filter/reduce, min/max, and elsewhere (in/not in?) speed tuning (make next() a slot tp_next???)
2001-04-13Change error message raised when free variable is not yet bound. ItJeremy Hylton-10/+16
now raises NameError instead of UnboundLocalError, because the var in question is definitely not local. (This affects test_scope.py) Also update the recent fix by Ping using get_func_name(). Replace tests of get_func_name() return value with call to get_func_desc() to match all the other uses.
2001-04-13Patch by Ping (SF bug 415879, Exception.__init__() causes segfault):Guido van Rossum-3/+2
Calling an unbound method on a C extension class without providing an instance can yield a segfault. Try "Exception.__init__()" or "ValueError.__init__()". This is a simple fix. The error-reporting bits in call_method mistakenly treat the misleadingly-named variable "func" as a function, when in fact it is a method. If we let get_func_name take care of the work, all is fine.
2001-04-11Fix exception handling for non-PyFunction objects, SF bug 414743.Jeremy Hylton-16/+54
Fix based on patch #414750 by Michael Hudson. New functions get_func_name() and get_func_desc() return reasonable names and descriptions for all objects. XXX Even objects that aren't actually callable.
2001-03-22Extend support for from __future__ import nested_scopesJeremy Hylton-2/+16
If a module has a future statement enabling nested scopes, they are also enable for the exec statement and the functions compile() and execfile() if they occur in the module. If Python is run with the -i option, which enters interactive mode after executing a script, and the script it runs enables nested scopes, they are also enabled in interactive mode. XXX The use of -i with -c "from __future__ import nested_scopes" is not supported. What's the point? To support these changes, many function variants have been added to pythonrun.c. All the variants names end with Flags and they take an extra PyCompilerFlags * argument. It is possible that this complexity will be eliminated in a future version of the interpreter in which nested scopes are not optional.
2001-03-22If a code object is compiled with nested scopes, define the CO_NESTED flag.Jeremy Hylton-1/+9
Add PyEval_GetNestedScopes() which returns a non-zero value if the code for the current interpreter frame has CO_NESTED defined.
2001-03-21Use PyObject_IsInstance() to check whether the first argument to anGuido van Rossum-13/+19
unbound method is of the right type. Hopefully this solves SF patch #409355 (Meta-class inheritance problem); I have no easy way to test.
2001-03-21Fix PyFrame_FastToLocals() and counterpart to deal with cells andJeremy Hylton-4/+45
frees. Note there doesn't seem to be any way to test LocalsToFast(), because the instructions that trigger it are illegal in nested scopes with free variables. Fix allocation strategy for cells that are also formal parameters. Instead of emitting LOAD_FAST / STORE_DEREF pairs for each parameter, have the argument handling code in eval_code2() do the right thing. A side-effect of this change is that cell variables that are also arguments are listed at the front of co_cellvars in the order they appear in the argument list.
2001-03-13Variety of small INC/DECREF patches that fix reported memory leaksJeremy Hylton-4/+7
with free variables. Thanks to Martin v. Loewis for finding two of the problems. This fixes SF buf 405583. There is also a C API change: PyFrame_New() is reverting to its pre-2.1 signature. The change introduced by nested scopes was a mistake. XXX Is this okay between beta releases? cell_clear(), the GC helper, must decref its reference to break cycles. frame_dealloc() must dealloc all cell vars and free vars in addition to locals. eval_code2() setup code must INCREF cells it copies out of the closure. The STORE_DEREF opcode implementation must DECREF the object it passes to PyCell_Set().
2001-02-16Remove trailing comma from 'why_code' enum, which was introduced by theThomas Wouters-1/+1
continue-inside-try patch. Partly fixes SF bug #132597.
2001-02-09When calling a PyCFunction that has METH_KEYWORDS defined, don'tJeremy Hylton-10/+0
create an empty dictionary if it is called without keyword args. Just pass NULL. XXX I had believed that this caused weird errors, but the test suite runs cleanly.
2001-02-05SF patch 103596 by Nick Mathewson: rause UnboundLocalError forJeremy Hylton-0/+16
uninitialized free variables
2001-02-01Allow 'continue' inside 'try' clauseJeremy Hylton-4/+24
SF patch 102989 by Thomas Wouters
2001-02-01Undo recent change that banned using import to bind a global, as perJeremy Hylton-5/+3
discussion on python-dev. 'from mod import *' is still banned except at the module level. Fix value for special NOOPT entry in symtable. Initialze to 0 instead of None, so that later uses of PyInt_AS_LONG() are valid. (Bug reported by Donn Cave.) replace local REPR macros with PyObject_REPR in object.h
2001-01-31SF bug #130532: newest CVS won't build on AIX.Tim Peters-2/+0
Removed illegal redefinition of REPR macro; kept the one with the argument name that isn't too easy to confuse with zero <wink>.
2001-01-29Remove f_closure slot of frameobject and use f_localsplus instead.Jeremy Hylton-4/+16
This change eliminates an extra malloc/free when a frame with free variables is created. Any cell vars or free vars are stored in f_localsplus after the locals and before the stack. eval_code2() fills in the appropriate values after handling initialization of locals. To track the size the frame has an f_size member that tracks the total size of f_localsplus. It used to be implicitly f_nlocals + f_stacksize.