summaryrefslogtreecommitdiffstats
path: root/Objects/funcobject.c
Commit message (Collapse)AuthorAgeFilesLines
* Extend function() to support an optional closure argument.Jeremy Hylton2002-07-111-12/+65
| | | | Also, simplify some ref counting for other optional arguments.
* SF patch 568629 by Oren Tirosh: types made callable.Guido van Rossum2002-06-141-1/+50
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | These built-in functions are replaced by their (now callable) type: slice() buffer() and these types can also be called (but have no built-in named function named after them) classobj (type name used to be "class") code function instance instancemethod (type name used to be "instance method") The module "new" has been replaced with a small backward compatibility placeholder in Python. A large portion of the patch simply removes the new module from various platform-specific build recipes. The following binary Mac project files still have references to it: Mac/Build/PythonCore.mcp Mac/Build/PythonStandSmall.mcp Mac/Build/PythonStandalone.mcp [I've tweaked the code layout and the doc strings here and there, and added a comment to types.py about StringTypes vs. basestring. --Guido]
* Patch #568124: Add doc string macros.Martin v. Löwis2002-06-131-4/+4
|
* PyObject_Del can now be used as a function designator.Neil Schemenauer2002-04-121-2/+2
|
* Fix the names of the classmethod and staticmethod constructors as passed toFred Drake2002-04-031-2/+2
| | | | PyArg_ParseTuple() as part of the format string.
* Fix for SF bug 528132 (Armin Rigo): classmethod().__get__() segfaultGuido van Rossum2002-03-181-0/+2
| | | | | | | | | | | The proper fix is not quite what was submitted; it's really better to take the class of the object passed rather than calling PyMethod_New with NULL pointer args, because that can then cause other core dumps later. I also added a testcase for the fix to classmethods() in test_descr.py. I've already applied this to the 2.2 branch.
* Portability fix: Not every compiler implements the extension ofSjoerd Mullender2001-12-171-1/+1
| | | | unescaped newlines in strings.
* SF patch #493452: docstrings for staticmethod/classmethod (SkipGuido van Rossum2001-12-171-2/+41
| | | | | | Montanaro) (With minor adjustments.)
* function_call(): Remove a bogus (and I mean *really* bogus) call toGuido van Rossum2001-12-031-1/+0
| | | | | Py_DECREF(arg) after the PyErr_NoMemory() call. (Armin Rigo, SF bug #488477.)
* Be smarter about clearing the weakref lists for instances, instance methods,Fred Drake2001-10-261-1/+2
| | | | | | and functions: we only need to call PyObject_ClearWeakRefs() if the weakref list is non-NULL. Since these objects are common but weakrefs are still unusual, saving the call at deallocation time makes a lot of sense.
* Enable GC for new-style instances. This touches lots of files, sinceGuido van Rossum2001-10-051-2/+4
| | | | | | | | | | | | | | | | | | | | | | many types were subclassable but had a xxx_dealloc function that called PyObject_DEL(self) directly instead of deferring to self->ob_type->tp_free(self). It is permissible to set tp_free in the type object directly to _PyObject_Del, for non-GC types, or to _PyObject_GC_Del, for GC types. Still, PyObject_DEL was a tad faster, so I'm fearing that our pystone rating is going down again. I'm not sure if doing something like void xxx_dealloc(PyObject *self) { if (PyXxxCheckExact(self)) PyObject_DEL(self); else self->ob_type->tp_free(self); } is any faster than always calling the else branch, so I haven't attempted that -- however those types whose own dealloc is fancier (int, float, unicode) do use this pattern.
* Add optional docstrings to getset descriptors. Fortunately, there'sGuido van Rossum2001-09-201-1/+1
| | | | | | | | | | no backwards compatibility to worry about, so I just pushed the 'closure' struct member to the back -- it's never used in the current code base (I may eliminate it, but that's more work because the getter and setter signatures would have to change.) As examples, I added actual docstrings to the getset attributes of a few types: file.closed, xxsubtype.spamdict.state.
* Add optional docstrings to member descriptors. For backwardsGuido van Rossum2001-09-201-1/+1
| | | | | | | | | | | | | | | compatibility, this required all places where an array of "struct memberlist" structures was declared that is referenced from a type's tp_members slot to change the type of the structure to PyMemberDef; "struct memberlist" is now only used by old code that still calls PyMember_Get/Set. The code in PyObject_GenericGetAttr/SetAttr now calls the new APIs PyMember_GetOne/SetOne, which take a PyMemberDef argument. As examples, I added actual docstrings to the attributes of a few types: file, complex, instance method, super, and xxsubtype.spamlist. Also converted the symtable to new style getattr.
* Rewrite function attributes to use the generic routines properly.Guido van Rossum2001-09-171-72/+118
| | | | | This uses the new "restricted" feature of structmember, and getset descriptors for some of the type checks.
* Use new GC API.Neil Schemenauer2001-08-291-7/+6
|
* repr's converted to using PyString_FromFormat() instead of sprintf'ingBarry Warsaw2001-08-241-7/+4
| | | | | | into a hardcoded char* buffer. Closes patch #454743.
* func_getattro(), func_setattro(): Implement the new semantics forBarry Warsaw2001-08-141-8/+22
| | | | | | | | | | setting and deleting a function's __dict__ attribute. Deleting it, or setting it to a non-dictionary result in a TypeError. Note that getting it the first time magically initializes it to an empty dict so that func.__dict__ will always appear to be a dictionary (never None). Closes SF bug #446645.
* Merge of descr-branch back into trunk.Tim Peters2001-08-021-62/+318
|
* Since Py_TPFLAGS_HAVE_WEAKREFS is set in Py_TPFLAGS_DEFAULT, it does notFred Drake2001-05-031-20/+20
| | | | | | | need to be specified in the type structures independently. The flag exists only for binary compatibility. This is a "source cleanliness" issue and introduces no behavioral changes.
* Add support for weak references to the function and method types.Fred Drake2001-03-231-2/+7
|
* Visit the closure during traversal and XDECREF it on during deallocation.Jeremy Hylton2001-03-011-0/+6
|
* Fix SF buf 404774 submitted by Gregory H. BallJeremy Hylton2001-02-281-1/+1
| | | | | A user program could delete a function's func_closure, which would cause it to crash when called.
* The one thing I love more then writing code is deleting code.Moshe Zadka2001-01-291-33/+2
| | | | | | | | | * Removed func_hash and func_compare, so they can be treated as immutable content-less objects (address hash and comparison) * Added tests to that affect to test_funcattrs (also testing func_code is writable) * Reverse meaning of tests in test_opcodes which checked identical code gets identical functions
* PEP 227 implementationJeremy Hylton2001-01-251-0/+33
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The majority of the changes are in the compiler. The mainloop changes primarily to implement the new opcodes and to pass a function's closure to eval_code2(). Frames and functions got new slots to hold the closure. Include/compile.h Add co_freevars and co_cellvars slots to code objects. Update PyCode_New() to take freevars and cellvars as arguments Include/funcobject.h Add func_closure slot to function objects. Add GetClosure()/SetClosure() functions (and corresponding macros) for getting at the closure. Include/frameobject.h PyFrame_New() now takes a closure. Include/opcode.h Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF. Remove comment about old requirement for opcodes to fit in 7 bits. compile.c Implement changes to code objects for co_freevars and co_cellvars. Modify symbol table to use st_cur_name (string object for the name of the current scope) and st_cur_children (list of nested blocks). Also define st_nested, which might more properly be called st_cur_nested. Add several DEF_XXX flags to track def-use information for free variables. New or modified functions of note: com_make_closure(struct compiling *, PyCodeObject *) Emit LOAD_CLOSURE opcodes as needed to pass cells for free variables into nested scope. com_addop_varname(struct compiling *, int, char *) Emits opcodes for LOAD_DEREF and STORE_DEREF. get_ref_type(struct compiling *, char *name) Return NAME_CLOSURE if ref type is FREE or CELL symtable_load_symbols(struct compiling *) Decides what variables are cell or free based on def-use info. Can now raise SyntaxError if nested scopes are mixed with exec or from blah import *. make_scope_info(PyObject *, PyObject *, int, int) Helper functions for symtable scope stack. symtable_update_free_vars(struct symtable *) After a code block has been analyzed, it must check each of its children for free variables that are not defined in the block. If a variable is free in a child and not defined in the parent, then it is defined by block the enclosing the current one or it is a global. This does the right logic. symtable_add_use() is now a macro for symtable_add_def() symtable_assign(struct symtable *, node *) Use goto instead of for (;;) Fixed bug in symtable where name of keyword argument in function call was treated as assignment in the scope of the call site. Ex: def f(): g(a=2) # a was considered a local of f ceval.c eval_code2() now take one more argument, a closure. Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE> Also: When name error occurs for global variable, report that the name was global in the error mesage. Objects/frameobject.c Initialize f_closure to be a tuple containing space for cellvars and freevars. f_closure is NULL if neither are present. Objects/funcobject.c Add support for func_closure. Python/import.c Change the magic number. Python/marshal.c Track changes to code objects.
* Application and elaboration of patch #103305 to fix core dumps whenBarry Warsaw2001-01-191-30/+40
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | del'ing func.func_dict. I took the opportunity to also clean up some other nits with the code, namely core dumps when del'ing func_defaults and KeyError instead of AttributeError when del'ing a non-existant function attribute. Specifically, func_memberlist: Move func_dict and __dict__ into here instead of special casing them in the setattro and getattro methods. I don't remember why I took them out of here before I first uploaded the PEP 232 patch. :/ func_getattro(): No need to special case __dict__/func_dict since their now in the func_memberlist and PyMember_Get() should Do The Right Thing (i.e. transforms NULL values into Py_None). func_setattro(): Document the intended behavior of del'ing or setting to None one of the special func_* attributes. I.e.: func_code - can only be set to a code object. It can't be del'd or set to None. func_defaults - can be del'd. Can only be set to None or a tuple. func_dict - can be del'd. Can only be set to None or a dictionary. Fix core dumps and incorrect exceptions as described above. Also, if we're del'ing an arbitrary function attribute but func_dict is NULL, don't create func_dict before discovering that we'll get an AttributeError anyway.
* Committing PEP 232, function attribute feature, approved by Guido.Barry Warsaw2001-01-151-11/+75
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Closes SF patch #103123. funcobject.h: PyFunctionObject: add the func_dict slot. funcobject.c: PyFunction_New(): Initialize the func_dict slot to NULL. func_getattr(): Rename to func_getattro() and change the signature. It's more efficient to use attro methods and dig the C string out than it is to re-convert a C string to a PyString. Also, add support for getting the __dict__ (a.k.a. func_dict) attribute, and for getting an arbitrary function attribute. func_setattr(): Rename to func_setattro() and change the signature for the same reason. Also add support for setting __dict__ (a.k.a. func_dict) and any arbitrary function attribute. func_dealloc(): Be sure to DECREF the func_dict slot. func_traverse(): Be sure to traverse func_dict too. PyFunction_Type: make the necessary func_?etattro() changes. classobject.c: instancemethod_memberlist: Add __dict__ instancemethod_setattro(): New method to set arbitrary attributes on methods (really the underlying im_func). Raise TypeError when the instance is bound or when you're trying to set one of the reserved im_* attributes. instancemethod_getattr(): Renamed to instancemethod_getattro() since that's what it really is. Also, added support fo getting arbitrary attributes through the im_func. PyMethod_Type: Do the ?etattr{,o} dance.
* REMOVED all CWI, CNRI and BeOpen copyright markings.Guido van Rossum2000-09-011-9/+0
| | | | This should match the situation in the 1.6b1 tree.
* ANSI-fication of the sources.Fred Drake2000-07-091-27/+11
|
* Neil Schemenauer: small fixes for GCGuido van Rossum2000-07-011-0/+1
|
* Change copyright notice - 2nd try.Guido van Rossum2000-06-301-6/+0
|
* Change copyright notice.Guido van Rossum2000-06-301-21/+6
|
* Trent Mick <trentm@activestate.com>:Fred Drake2000-06-301-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | The common technique for printing out a pointer has been to cast to a long and use the "%lx" printf modifier. This is incorrect on Win64 where casting to a long truncates the pointer. The "%p" formatter should be used instead. The problem as stated by Tim: > Unfortunately, the C committee refused to define what %p conversion "looks > like" -- they explicitly allowed it to be implementation-defined. Older > versions of Microsoft C even stuck a colon in the middle of the address (in > the days of segment+offset addressing)! The result is that the hex value of a pointer will maybe/maybe not have a 0x prepended to it. Notes on the patch: There are two main classes of changes: - in the various repr() functions that print out pointers - debugging printf's in the various thread_*.h files (these are why the patch is large) Closes SourceForge patch #100505.
* final patches from Neil Schemenauer for garbage collectionJeremy Hylton2000-06-301-1/+3
|
* This patch addresses two main issues: (1) There exist some non-fatalFred Drake2000-06-291-2/+4
| | | | | | | | | | | | | | | | | | | | errors in some of the hash algorithms. For exmaple, in float_hash and complex_hash a certain part of the value is not included in the hash calculation. See Tim's, Guido's, and my discussion of this on python-dev in May under the title "fix float_hash and complex_hash for 64-bit *nix" (2) The hash algorithms that use pointers (e.g. func_hash, code_hash) are universally not correct on Win64 (they assume that sizeof(long) == sizeof(void*)) As well, this patch significantly cleans up the hash code. It adds the two function _Py_HashDouble and _PyHash_VoidPtr that the various hashing routine are changed to use. These help maintain the hash function invariant: (a==b) => (hash(a)==hash(b))) I have added Lib/test/test_hash.py and Lib/test/output/test_hash to test this for some cases.
* part 2 of Neil Schemenauer's GC patches:Jeremy Hylton2000-06-231-2/+2
| | | | | | | | This patch modifies the type structures of objects that participate in GC. The object's tp_basicsize is increased when GC is enabled. GC information is prefixed to the object to maintain binary compatibility. GC objects also define the tp_flag Py_TPFLAGS_GC.
* Round 1 of Neil Schemenauer's GC patches:Jeremy Hylton2000-06-231-0/+40
| | | | | This patch adds the type methods traverse and clear necessary for GC implementation.
* Vladimir Marangozov's long-awaited malloc restructuring.Guido van Rossum2000-05-031-1/+1
| | | | | | | | | | For more comments, read the patches@python.org archives. For documentation read the comments in mymalloc.h and objimpl.h. (This is not exactly what Vladimir posted to the patches list; I've made a few changes, and Vladimir sent me a fix in private email for a problem that only occurs in debug mode. I'm also holding back on his change to main.c, which seems unnecessary to me.)
* Marc-Andre Lemburg:Guido van Rossum2000-04-271-1/+1
| | | | Doc strings can now be given as Unicode strings.
* Make function objects somewhat mutable -- the members func_code,Guido van Rossum1998-05-221-3/+35
| | | | | | func_defaults and func_doc (alias __doc__) may be assigned to. For the first two, there's a type restriction to code object and tuple, respectively.
* Make new gcc -Wall happyGuido van Rossum1998-04-101-1/+2
|
* Fix bug in comparing function objects detected by Sjoerd:Guido van Rossum1997-08-051-3/+9
| | | | | | SystemError: bad argument to internal function caused by comparing NULL pointer default args.
* Quickly renamed the last directory.Guido van Rossum1997-05-021-73/+74
|
* New permission notice, includes CNRI.Guido van Rossum1996-10-251-13/+20
|
* changes for keyword arguments and fast function call; added abstract.cGuido van Rossum1995-07-181-25/+15
|
* fix leak in func_dealloc (forgot to decref name)Guido van Rossum1995-01-201-0/+1
|
* add restrictions in restricted modeGuido van Rossum1995-01-101-0/+6
|
* Added __doc__ attribute (alias func_doc), initialized from firstGuido van Rossum1995-01-071-1/+16
| | | | constant in code object if it is a string, else None
* Added 1995 to copyright message.Guido van Rossum1995-01-041-2/+2
| | | | | floatobject.c: fix hash(). methodobject.c: support METH_FREENAME flag bit.
* Merge back to main trunkGuido van Rossum1994-08-301-6/+56
|
* * import.c (get_module): pass .py filename to parse_file, not .pyc filename!Guido van Rossum1993-11-301-3/+6
| | | | | | | | | | | * funcobject.c (func_repr): don't call getstringvalue(None) for anonymous functions. * bltinmodule.c: removed lambda (which is now a built-in function); removed implied lambda for string arg to filter/map/reduce. * Grammar, graminit.[ch], compile.[ch]: replaced lambda as built-in function by lambda as grammar entity: instead of "lambda('x: x+1')" you write "lambda x: x+1". * Xtmodule.c (checkargdict): return 0, not NULL, for error.