summaryrefslogtreecommitdiff
path: root/Objects/funcobject.c
AgeCommit message (Collapse)AuthorLines
2022-10-06[3.10] gh-97943: PyFunction_GetAnnotations should return a borrowed ↵Miss Islington (bot)-2/+5
reference. (GH-97949) (GH-97989) gh-97943: PyFunction_GetAnnotations should return a borrowed reference. (GH-97949) (cherry picked from commit 6bfb0be80486c614cd60dce44c9fe7b3e6c76e3b) Co-authored-by: larryhastings <larry@hastings.org>
2022-01-05bpo-46236: Fix PyFunction_GetAnnotations() returned tuple. (GH-30409)Miss Islington (bot)-22/+33
Automerge-Triggered-By: GH:pablogsal (cherry picked from commit 46e4c257e7c26c813620232135781e6c53fe8d4d) Co-authored-by: Inada Naoki <songofacandy@gmail.com>
2021-07-15bpo-42073: allow classmethod to wrap other classmethod-like descriptors ↵Miss Islington (bot)-1/+1
(GH-27115) (GH-27162) Patch by Erik Welch. bpo-19072 (GH-8405) allows `classmethod` to wrap other descriptors, but this does not work when the wrapped descriptor mimics classmethod. The current PR fixes this. In Python 3.8 and before, one could create a callable descriptor such that this works as expected (see Lib/test/test_decorators.py for examples): ```python class A: @myclassmethod def f1(cls): return cls @classmethod @myclassmethod def f2(cls): return cls ``` In Python 3.8 and before, `A.f2()` return `A`. Currently in Python 3.9, it returns `type(A)`. This PR make `A.f2()` return `A` again. As of GH-8405, classmethod calls `obj.__get__(type)` if `obj` has `__get__`. This allows one to chain `@classmethod` and `@property` together. When using classmethod-like descriptors, it's the second argument to `__get__`--the owner or the type--that is important, but this argument is currently missing. Since it is None, the "owner" argument is assumed to be the type of the first argument, which, in this case, is wrong (we want `A`, not `type(A)`). This PR updates classmethod to call `obj.__get__(type, type)` if `obj` has `__get__`. Co-authored-by: Erik Welch <erik.n.welch@gmail.com> (cherry picked from commit b83861f0265e07207a6ae2c49c40fa8f447893f2)
2021-04-12bpo-43682: Make staticmethod objects callable (GH-25117)Victor Stinner-1/+8
Static methods (@staticmethod) are now callable as regular functions.
2021-04-09bpo-43682: @staticmethod inherits attributes (GH-25268)Victor Stinner-9/+71
Static methods (@staticmethod) and class methods (@classmethod) now inherit the method attributes (__module__, __name__, __qualname__, __doc__, __annotations__) and have a new __wrapped__ attribute. Changes: * Add a repr() method to staticmethod and classmethod types. * Add tests on the @classmethod decorator.
2021-03-18bpo-43541: Fix PyEval_EvalCodeEx() regression (GH-24918)Victor Stinner-1/+1
* Remove an assertion which required CO_NEWLOCALS and CO_OPTIMIZED code flags. It is ok to call this function on a code with these flags set. * Fix reference counting on builtins: remove Py_DECREF(). Fix regression introduced in the commit 46496f9d12582bf11f4911ad0f23315d6f277907. Add also a comment to document that _PyEval_BuiltinsFromGlobals() returns a borrowed reference.
2021-02-20bpo-42990: Functions inherit current builtins (GH-24564)Victor Stinner-2/+6
The types.FunctionType constructor now inherits the current builtins if the globals dictionary has no "__builtins__" key, rather than using {"None": None} as builtins: same behavior as eval() and exec() functions. Defining a function with "def function(...): ..." in Python is not affected, globals cannot be overriden with this syntax: it also inherits the current builtins. PyFrame_New(), PyEval_EvalCode(), PyEval_EvalCodeEx(), PyFunction_New() and PyFunction_NewWithQualName() now inherits the current builtins namespace if the globals dictionary has no "__builtins__" key. * Add _PyEval_GetBuiltins() function. * _PyEval_BuiltinsFromGlobals() now uses _PyEval_GetBuiltins() if builtins cannot be found in globals. * Add tstate parameter to _PyEval_BuiltinsFromGlobals().
2021-02-18bpo-42990: Refactor _PyFrame_New_NoTrack() (GH-24566)Victor Stinner-52/+59
* Refactor _PyFrame_New_NoTrack() and PyFunction_NewWithQualName() code. * PyFrame_New() checks for _PyEval_BuiltinsFromGlobals() failure. * Fix a ref leak in _PyEval_BuiltinsFromGlobals() error path. * Complete PyFunction_GetModule() documentation: it returns a borrowed reference and it can return NULL. * Move _PyEval_BuiltinsFromGlobals() definition to the internal C API. * PyFunction_NewWithQualName() uses _Py_IDENTIFIER() API for the "__name__" string to make it compatible with subinterpreters.
2021-02-18bpo-42990: Add __builtins__ attribute to functions (GH-24559)Victor Stinner-0/+1
Expose the new PyFunctionObject.func_builtins member in Python as a new __builtins__ attribute on functions. Document also the behavior change in What's New in Python 3.10.
2021-02-01bpo-42990: Further refactoring of PyEval_ functions. (GH-24368)Mark Shannon-2/+2
* Further refactoring of PyEval_EvalCode and friends. Break into make-frame, and eval-frame parts. * Simplify function vector call using new _PyEval_Vector. * Remove unused internal functions: _PyEval_EvalCodeWithName and _PyEval_EvalCode. * Don't use legacy function PyEval_EvalCodeEx.
2021-01-29bpo-42990: Introduce 'frame constructor' struct to simplify API for ↵Mark Shannon-2/+11
PyEval_CodeEval and friends (GH-24298) * Introduce 'frame constructor' to simplify API for frame creation * Embed struct using a macro to conform to PEP 7
2020-11-25bpo-42202: Store func annotations as a tuple (GH-23316)Yurii Karabas-0/+19
Reduce memory footprint and improve performance of loading modules having many func annotations. >>> sys.getsizeof({"a":"int","b":"int","return":"int"}) 232 >>> sys.getsizeof(("a","int","b","int","return","int")) 88 The tuple is converted into dict on the fly when `func.__annotations__` is accessed first. Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Inada Naoki <songofacandy@gmail.com>
2020-10-29bpo-42143: Ensure PyFunction_NewWithQualName() can't fail after creating the ↵Yonatan Goldschmidt-13/+16
func object (GH-22953) func_dealloc() does not handle partially-created objects. Best not to give it any.
2020-06-22bpo-41078: Rename pycore_tupleobject.h to pycore_tuple.h (GH-21056)Victor Stinner-1/+0
2020-04-15bpo-40268: Remove unused pycore_pymem.h includes (GH-19531)Victor Stinner-1/+0
2020-04-15bpo-40268: Remove unused structmember.h includes (GH-19530)Victor Stinner-1/+1
If only offsetof() is needed: include stddef.h instead. When structmember.h is used, add a comment explaining that PyMemberDef is used.
2020-04-14bpo-40268: Remove a few pycore_pystate.h includes (GH-19510)Victor Stinner-1/+0
2020-02-18bpo-36347: stop using RESTRICTED constants (GH-12684)Jeroen Demeyer-6/+4
The constants `RESTRICTED` and `PY_WRITE_RESTRICTED` no longer have a meaning in Python 3. Therefore, CPython should not use them. CC @matrixise https://bugs.python.org/issue36347
2020-02-11bpo-39245: Switch to public API for Vectorcall (GH-18460)Petr Viktorin-1/+1
The bulk of this patch was generated automatically with: for name in \ PyObject_Vectorcall \ Py_TPFLAGS_HAVE_VECTORCALL \ PyObject_VectorcallMethod \ PyVectorcall_Function \ PyObject_CallOneArg \ PyObject_CallMethodNoArgs \ PyObject_CallMethodOneArg \ ; do echo $name git grep -lwz _$name | xargs -0 sed -i "s/\b_$name\b/$name/g" done old=_PyObject_FastCallDict new=PyObject_VectorcallDict git grep -lwz $old | xargs -0 sed -i "s/\b$old\b/$new/g" and then cleaned up: - Revert changes to in docs & news - Revert changes to backcompat defines in headers - Nudge misaligned comments
2020-02-07bpo-39573: Use Py_TYPE() macro in Objects directory (GH-18392)Victor Stinner-2/+2
Replace direct access to PyObject.ob_type with Py_TYPE().
2019-08-24bpo-19072: Make @classmethod support chained decorators (GH-8405)Berker Peksag-0/+4
2019-06-18bpo-37151: remove _PyFunction_FastCallDict (GH-13864)Jeroen Demeyer-12/+1
2019-05-30bpo-36974: tp_print -> tp_vectorcall_offset and tp_reserved -> tp_as_async ↵Jeroen Demeyer-5/+5
(GH-13464) Automatically replace tp_print -> tp_vectorcall_offset tp_compare -> tp_as_async tp_reserved -> tp_as_async
2019-05-30bpo-36974: rename _FastCallKeywords -> _Vectorcall (GH-13653)Jeroen Demeyer-1/+1
2019-05-29bpo-36974: implement PEP 590 (GH-13185)Jeroen Demeyer-1/+3
Co-authored-by: Jeroen Demeyer <J.Demeyer@UGent.be> Co-authored-by: Mark Shannon <mark@hotpy.org>
2019-05-28bpo-36922: implement PEP-590 Py_TPFLAGS_METHOD_DESCRIPTOR (GH-13338)Jeroen Demeyer-1/+2
Co-authored-by: Mark Shannon <mark@hotpy.org>
2019-05-23bpo-36842: Implement PEP 578 (GH-12613)Steve Dower-0/+40
Adds sys.audit, sys.addaudithook, io.open_code, and associated C APIs.
2019-02-25bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). (GH-11112)Serhiy Storchaka-1/+5
2018-12-23Clarify the behavior of the staticmethod builtin (GH-4362)Jess Shapiro-2/+4
2018-11-27bpo-33029: Fix signatures of getter and setter functions. (GH-10746)Serhiy Storchaka-12/+12
Fix also return type for few other functions (clear, releasebuffer).
2018-11-25bpo-35081: Add Include/internal/pycore_tupleobject.h (GH-10705)Victor Stinner-0/+1
Move _PyTuple_ITEMS() to a new header file: Include/internal/pycore_tupleobject.h
2018-11-21bpo-35081: Add Include/internal/pycore_object.h (GH-10640)Victor Stinner-0/+1
Move _PyObject_GC_TRACK() and _PyObject_GC_UNTRACK() from Include/objimpl.h to Include/internal/pycore_object.h.
2018-11-12bpo-35081: Rename internal headers (GH-10275)Victor Stinner-2/+2
Rename Include/internal/ headers: * pycore_hash.h -> pycore_pyhash.h * pycore_lifecycle.h -> pycore_pylifecycle.h * pycore_mem.h -> pycore_pymem.h * pycore_state.h -> pycore_pystate.h Add missing headers to Makefile.pre.in and PCbuild: * pycore_condvar.h. * pycore_hamt.h * pycore_pyhash.h
2018-11-09bpo-35199: Add an internal _PyTuple_ITEMS() macro (GH-10434)Victor Stinner-1/+1
* _PyTuple_ITEMS() gives access to the tuple->ob_item field and cast the first argument to PyTupleObject*. This internal macro is only usable if Py_BUILD_CORE is defined. * Replace &PyTuple_GET_ITEM(ob, 0) with _PyTuple_ITEMS(ob). * Replace PyTuple_GET_ITEM(op, 1) with &_PyTuple_ITEMS(ob)[1].
2018-11-01bpo-35081: Add pycore_ prefix to internal header files (GH-10263)Victor Stinner-2/+2
* Rename Include/internal/ header files: * pyatomic.h -> pycore_atomic.h * ceval.h -> pycore_ceval.h * condvar.h -> pycore_condvar.h * context.h -> pycore_context.h * pygetopt.h -> pycore_getopt.h * gil.h -> pycore_gil.h * hamt.h -> pycore_hamt.h * hash.h -> pycore_hash.h * mem.h -> pycore_mem.h * pystate.h -> pycore_state.h * warnings.h -> pycore_warnings.h * PCbuild project, Makefile.pre.in, Modules/Setup: add the Include/internal/ directory to the search paths of header files. * Update includes. For example, replace #include "internal/mem.h" with #include "pycore_mem.h".
2018-07-04bpo-33418: Add tp_clear for function object (GH-8058)INADA Naoki-13/+21
Without tp_clear, GC can't break cyclic reference. It will cause memory leak when cyclic reference is created intentionally.
2018-02-13bpo-31787: Prevent refleaks when calling __init__() more than once (GH-3995)Oren Milman-2/+2
2017-09-07bpo-30860: Consolidate stateful runtime globals. (#3397)Eric Snow-0/+2
* group the (stateful) runtime globals into various topical structs * consolidate the topical structs under a single top-level _PyRuntimeState struct * add a check-c-globals.py script that helps identify runtime globals Other globals are excluded (see globals.txt and check-c-globals.py).
2017-07-09bpo-30878: Fix error message when keyword arguments are passed (#2635)Sylvain-4/+4
to staticmethod() and classmethod().
2017-03-19bpo-29793: Convert some builtin types constructors to Argument Clinic. (#615)Serhiy Storchaka-25/+30
2017-01-23Issue #28999: Use Py_RETURN_NONE, Py_RETURN_TRUE and Py_RETURN_FALSE whereverSerhiy Storchaka-4/+2
possible. Patch is writen with Coccinelle.
2017-01-03Issue #28839: Optimize function_call()Victor Stinner-47/+6
function_call() now simply calls _PyFunction_FastCallDict(). _PyFunction_FastCallDict() is more efficient: it contains fast paths for the common case (optimized code object and no keyword argument).
2016-12-16Issue #28959: Added private macro PyDict_GET_SIZE for retrieving the size of ↵Serhiy Storchaka-1/+1
dict.
2016-09-17Issue #28139: Merge indentation fixes from 3.5 into 3.6Martin Panter-8/+12
2016-09-17Issue #28139: Fix messed up indentationMartin Panter-8/+12
Also update the classmethod and staticmethod doc strings and comments to match the RST documentation.
2016-04-10Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREFSerhiy Storchaka-1/+1
in places where Py_DECREF was used.
2016-04-10Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREFSerhiy Storchaka-1/+1
in places where Py_DECREF was used.
2016-04-06Issue #22570: Renamed Py_SETREF to Py_XSETREF.Serhiy Storchaka-11/+11
2016-04-06Issue #22570: Renamed Py_SETREF to Py_XSETREF.Serhiy Storchaka-5/+5
2016-01-05Issue #20440: Cleaning up the code by using Py_SETREF.Serhiy Storchaka-29/+6