summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJeroen Demeyer <jeroen.k.demeyer@gmail.com>2019-11-05 15:48:04 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-11-05 15:48:04 (GMT)
commitbf17d41826a8bb4bc1e34ba6345da98aac779e41 (patch)
tree1c256d14c23ffdcbbb5ae54efa546cf718a8f892 /Lib
parentb3966639d28313809774ca3859a347b9007be8d2 (diff)
downloadcpython-bf17d41826a8bb4bc1e34ba6345da98aac779e41.zip
cpython-bf17d41826a8bb4bc1e34ba6345da98aac779e41.tar.gz
cpython-bf17d41826a8bb4bc1e34ba6345da98aac779e41.tar.bz2
bpo-37645: add new function _PyObject_FunctionStr() (GH-14890)
Additional note: the `method_check_args` function in `Objects/descrobject.c` is written in such a way that it applies to all kinds of descriptors. In particular, a future re-implementation of `wrapper_descriptor` could use that code. CC @vstinner @encukou https://bugs.python.org/issue37645 Automerge-Triggered-By: @encukou
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_call.py10
-rw-r--r--Lib/test/test_descr.py2
-rw-r--r--Lib/test/test_extcall.py38
-rw-r--r--Lib/test/test_unpack_ex.py10
4 files changed, 30 insertions, 30 deletions
diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py
index c233ba1..d178aa4 100644
--- a/Lib/test/test_call.py
+++ b/Lib/test/test_call.py
@@ -74,7 +74,7 @@ class CFunctionCallsErrorMessages(unittest.TestCase):
self.assertRaisesRegex(TypeError, msg, bool, x=2)
def test_varargs4_kw(self):
- msg = r"^index\(\) takes no keyword arguments$"
+ msg = r"^list[.]index\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, [].index, x=2)
def test_varargs5_kw(self):
@@ -90,19 +90,19 @@ class CFunctionCallsErrorMessages(unittest.TestCase):
self.assertRaisesRegex(TypeError, msg, next, x=2)
def test_varargs8_kw(self):
- msg = r"^pack\(\) takes no keyword arguments$"
+ msg = r"^_struct[.]pack\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, struct.pack, x=2)
def test_varargs9_kw(self):
- msg = r"^pack_into\(\) takes no keyword arguments$"
+ msg = r"^_struct[.]pack_into\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, struct.pack_into, x=2)
def test_varargs10_kw(self):
- msg = r"^index\(\) takes no keyword arguments$"
+ msg = r"^deque[.]index\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, collections.deque().index, x=2)
def test_varargs11_kw(self):
- msg = r"^pack\(\) takes no keyword arguments$"
+ msg = r"^Struct[.]pack\(\) takes no keyword arguments$"
self.assertRaisesRegex(TypeError, msg, struct.Struct.pack, struct.Struct(""), x=2)
def test_varargs12_kw(self):
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 796e60a..d2e1218 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1967,7 +1967,7 @@ order (MRO) for bases """
# different error messages.
set_add = set.add
- expected_errmsg = "descriptor 'add' of 'set' object needs an argument"
+ expected_errmsg = "unbound method set.add() needs an argument"
with self.assertRaises(TypeError) as cm:
set_add()
diff --git a/Lib/test/test_extcall.py b/Lib/test/test_extcall.py
index d9dcb70..4edb668 100644
--- a/Lib/test/test_extcall.py
+++ b/Lib/test/test_extcall.py
@@ -52,15 +52,15 @@ Here we add keyword arguments
>>> f(1, 2, **{'a': -1, 'b': 5}, **{'a': 4, 'c': 6})
Traceback (most recent call last):
...
- TypeError: f() got multiple values for keyword argument 'a'
+ TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
>>> f(1, 2, **{'a': -1, 'b': 5}, a=4, c=6)
Traceback (most recent call last):
...
- TypeError: f() got multiple values for keyword argument 'a'
+ TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
>>> f(1, 2, a=3, **{'a': 4}, **{'a': 5})
Traceback (most recent call last):
...
- TypeError: f() got multiple values for keyword argument 'a'
+ TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
>>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
>>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
@@ -118,7 +118,7 @@ Verify clearing of SF bug #733667
>>> g(*Nothing())
Traceback (most recent call last):
...
- TypeError: g() argument after * must be an iterable, not Nothing
+ TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing
>>> class Nothing:
... def __len__(self): return 5
@@ -127,7 +127,7 @@ Verify clearing of SF bug #733667
>>> g(*Nothing())
Traceback (most recent call last):
...
- TypeError: g() argument after * must be an iterable, not Nothing
+ TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing
>>> class Nothing():
... def __len__(self): return 5
@@ -247,17 +247,17 @@ What about willful misconduct?
>>> h(*h)
Traceback (most recent call last):
...
- TypeError: h() argument after * must be an iterable, not function
+ TypeError: test.test_extcall.h() argument after * must be an iterable, not function
>>> h(1, *h)
Traceback (most recent call last):
...
- TypeError: h() argument after * must be an iterable, not function
+ TypeError: test.test_extcall.h() argument after * must be an iterable, not function
>>> h(*[1], *h)
Traceback (most recent call last):
...
- TypeError: h() argument after * must be an iterable, not function
+ TypeError: test.test_extcall.h() argument after * must be an iterable, not function
>>> dir(*h)
Traceback (most recent call last):
@@ -268,38 +268,38 @@ What about willful misconduct?
>>> nothing(*h)
Traceback (most recent call last):
...
- TypeError: NoneType object argument after * must be an iterable, \
+ TypeError: None argument after * must be an iterable, \
not function
>>> h(**h)
Traceback (most recent call last):
...
- TypeError: h() argument after ** must be a mapping, not function
+ TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
>>> h(**[])
Traceback (most recent call last):
...
- TypeError: h() argument after ** must be a mapping, not list
+ TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
>>> h(a=1, **h)
Traceback (most recent call last):
...
- TypeError: h() argument after ** must be a mapping, not function
+ TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
>>> h(a=1, **[])
Traceback (most recent call last):
...
- TypeError: h() argument after ** must be a mapping, not list
+ TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
>>> h(**{'a': 1}, **h)
Traceback (most recent call last):
...
- TypeError: h() argument after ** must be a mapping, not function
+ TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
>>> h(**{'a': 1}, **[])
Traceback (most recent call last):
...
- TypeError: h() argument after ** must be a mapping, not list
+ TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
>>> dir(**h)
Traceback (most recent call last):
@@ -309,7 +309,7 @@ not function
>>> nothing(**h)
Traceback (most recent call last):
...
- TypeError: NoneType object argument after ** must be a mapping, \
+ TypeError: None argument after ** must be a mapping, \
not function
>>> dir(b=1, **{'b': 1})
@@ -351,17 +351,17 @@ Test a kwargs mapping with duplicated keys.
>>> g(**MultiDict([('x', 1), ('x', 2)]))
Traceback (most recent call last):
...
- TypeError: g() got multiple values for keyword argument 'x'
+ TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
>>> g(a=3, **MultiDict([('x', 1), ('x', 2)]))
Traceback (most recent call last):
...
- TypeError: g() got multiple values for keyword argument 'x'
+ TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
>>> g(**MultiDict([('a', 3)]), **MultiDict([('x', 1), ('x', 2)]))
Traceback (most recent call last):
...
- TypeError: g() got multiple values for keyword argument 'x'
+ TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
Another helper function
diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py
index 87fea59..46f70c2 100644
--- a/Lib/test/test_unpack_ex.py
+++ b/Lib/test/test_unpack_ex.py
@@ -236,27 +236,27 @@ Overridden parameters
>>> f(x=5, **{'x': 3}, y=2)
Traceback (most recent call last):
...
- TypeError: f() got multiple values for keyword argument 'x'
+ TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'
>>> f(**{'x': 3}, x=5, y=2)
Traceback (most recent call last):
...
- TypeError: f() got multiple values for keyword argument 'x'
+ TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'
>>> f(**{'x': 3}, **{'x': 5}, y=2)
Traceback (most recent call last):
...
- TypeError: f() got multiple values for keyword argument 'x'
+ TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'
>>> f(x=5, **{'x': 3}, **{'x': 2})
Traceback (most recent call last):
...
- TypeError: f() got multiple values for keyword argument 'x'
+ TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'
>>> f(**{1: 3}, **{1: 5})
Traceback (most recent call last):
...
- TypeError: f() got multiple values for keyword argument '1'
+ TypeError: test.test_unpack_ex.f() got multiple values for keyword argument '1'
Unpacking non-sequence
iled code 1] _PyEval_EvalFrame _PyEval_Vector _PyFunction_Vectorcall PyObject_Vectorcall call_function ... Py_RunMain When we generate every unique copy of the trampoline (what here we called "[Jit compiled code N]") we write the relationship between the compiled code and the Python function that is associated with it. Every profiler requires this information in a different format. For example, the Linux "perf" profiler requires a file in "/tmp/perf-PID.map" (name and location not configurable) with the following format: <compiled code address> <compiled code size> <name of the compiled code> If this file is available when "perf" generates reports, it will automatically associate every trampoline with the Python function that it is associated with allowing it to generate reports that include Python information. These reports then can also be filtered in a way that *only* Python information appears. Notice that for this to work, there must be a unique copied of the trampoline per Python code object even if the code in the trampoline is the same. To achieve this we have a assembly template in Objects/asm_trampiline.S that is compiled into the Python executable/shared library. This template generates a symbol that maps the start of the assembly code and another that marks the end of the assembly code for the trampoline. Then, every time we need a unique trampoline for a Python code object, we copy the assembly code into a mmaped area that has executable permissions and we return the start of that area as our trampoline function. Asking for a mmap-ed memory area for trampoline is very wasteful so we allocate big arenas of memory in a single mmap call, we populate the entire arena with copies of the trampoline (this allows us to now have to invalidate the icache for the instructions in the page) and then we return the next available chunk every time someone asks for a new trampoline. We keep a linked list of arenas in case the current memory arena is exhausted and another one is needed. For the best results, Python should be compiled with CFLAGS="-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer" as this allows profilers to unwind using only the frame pointer and not on DWARF debug information (note that as trampilines are dynamically generated there won't be any DWARF information available for them). */ #include "Python.h" #include "pycore_ceval.h" #include "pycore_frame.h" #include "pycore_interp.h" #ifdef PY_HAVE_PERF_TRAMPOLINE #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/types.h> #include <unistd.h> #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) #define PY_HAVE_INVALIDATE_ICACHE #if defined(__clang__) || defined(__GNUC__) extern void __clear_cache(void *, void*); #endif static void invalidate_icache(char* begin, char*end) { #if defined(__clang__) || defined(__GNUC__) return __clear_cache(begin, end); #else return; #endif } #endif /* The function pointer is passed as last argument. The other three arguments * are passed in the same order as the function requires. This results in * shorter, more efficient ASM code for trampoline. */ typedef PyObject *(*py_evaluator)(PyThreadState *, _PyInterpreterFrame *, int throwflag); typedef PyObject *(*py_trampoline)(PyThreadState *, _PyInterpreterFrame *, int, py_evaluator); extern void *_Py_trampoline_func_start; // Start of the template of the // assembly trampoline extern void * _Py_trampoline_func_end; // End of the template of the assembly trampoline struct code_arena_st { char *start_addr; // Start of the memory arena char *current_addr; // Address of the current trampoline within the arena size_t size; // Size of the memory arena size_t size_left; // Remaining size of the memory arena size_t code_size; // Size of the code of every trampoline in the arena struct code_arena_st *prev; // Pointer to the arena or NULL if this is the first arena. }; typedef struct code_arena_st code_arena_t; typedef struct trampoline_api_st trampoline_api_t; #define perf_status _PyRuntime.ceval.perf.status #define extra_code_index _PyRuntime.ceval.perf.extra_code_index #define perf_code_arena _PyRuntime.ceval.perf.code_arena #define trampoline_api _PyRuntime.ceval.perf.trampoline_api #define perf_map_file _PyRuntime.ceval.perf.map_file static void perf_map_write_entry(void *state, const void *code_addr, unsigned int code_size, PyCodeObject *co) { const char *entry = ""; if (co->co_qualname != NULL) { entry = PyUnicode_AsUTF8(co->co_qualname); } const char *filename = ""; if (co->co_filename != NULL) { filename = PyUnicode_AsUTF8(co->co_filename); } size_t perf_map_entry_size = snprintf(NULL, 0, "py::%s:%s", entry, filename) + 1; char* perf_map_entry = (char*) PyMem_RawMalloc(perf_map_entry_size); if (perf_map_entry == NULL) { return; } snprintf(perf_map_entry, perf_map_entry_size, "py::%s:%s", entry, filename); PyUnstable_WritePerfMapEntry(code_addr, code_size, perf_map_entry); PyMem_RawFree(perf_map_entry); } _PyPerf_Callbacks _Py_perfmap_callbacks = { NULL, &perf_map_write_entry, NULL, }; static int new_code_arena(void) { // non-trivial programs typically need 64 to 256 kiB. size_t mem_size = 4096 * 16; assert(mem_size % sysconf(_SC_PAGESIZE) == 0); char *memory = mmap(NULL, // address mem_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, // fd (not used here) 0); // offset (not used here) if (!memory) { PyErr_SetFromErrno(PyExc_OSError); _PyErr_WriteUnraisableMsg( "Failed to create new mmap for perf trampoline", NULL); perf_status = PERF_STATUS_FAILED; return -1; } void *start = &_Py_trampoline_func_start; void *end = &_Py_trampoline_func_end; size_t code_size = end - start; // TODO: Check the effect of alignment of the code chunks. Initial investigation // showed that this has no effect on performance in x86-64 or aarch64 and the current // version has the advantage that the unwinder in GDB can unwind across JIT-ed code. // // We should check the values in the future and see if there is a // measurable performance improvement by rounding trampolines up to 32-bit // or 64-bit alignment. size_t n_copies = mem_size / code_size; for (size_t i = 0; i < n_copies; i++) { memcpy(memory + i * code_size, start, code_size * sizeof(char)); } // Some systems may prevent us from creating executable code on the fly. int res = mprotect(memory, mem_size, PROT_READ | PROT_EXEC); if (res == -1) { PyErr_SetFromErrno(PyExc_OSError); munmap(memory, mem_size); _PyErr_WriteUnraisableMsg( "Failed to set mmap for perf trampoline to PROT_READ | PROT_EXEC", NULL); return -1; } #ifdef PY_HAVE_INVALIDATE_ICACHE // Before the JIT can run a block of code that has been emitted it must invalidate // the instruction cache on some platforms like arm and aarch64. invalidate_icache(memory, memory + mem_size); #endif code_arena_t *new_arena = PyMem_RawCalloc(1, sizeof(code_arena_t)); if (new_arena == NULL) { PyErr_NoMemory(); munmap(memory, mem_size); _PyErr_WriteUnraisableMsg("Failed to allocate new code arena struct", NULL); return -1; } new_arena->start_addr = memory; new_arena->current_addr = memory; new_arena->size = mem_size; new_arena->size_left = mem_size; new_arena->code_size = code_size; new_arena->prev = perf_code_arena; perf_code_arena = new_arena; return 0; } static void free_code_arenas(void) { code_arena_t *cur = perf_code_arena; code_arena_t *prev; perf_code_arena = NULL; // invalid static pointer while (cur) { munmap(cur->start_addr, cur->size); prev = cur->prev; PyMem_RawFree(cur); cur = prev; } } static inline py_trampoline code_arena_new_code(code_arena_t *code_arena) { py_trampoline trampoline = (py_trampoline)code_arena->current_addr; code_arena->size_left -= code_arena->code_size; code_arena->current_addr += code_arena->code_size; return trampoline; } static inline py_trampoline compile_trampoline(void) { if ((perf_code_arena == NULL) || (perf_code_arena->size_left <= perf_code_arena->code_size)) { if (new_code_arena() < 0) { return NULL; } } assert(perf_code_arena->size_left <= perf_code_arena->size); return code_arena_new_code(perf_code_arena); } static PyObject * py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame, int throw) { if (perf_status == PERF_STATUS_FAILED || perf_status == PERF_STATUS_NO_INIT) { goto default_eval; } PyCodeObject *co = frame->f_code; py_trampoline f = NULL; assert(extra_code_index != -1); int ret = _PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f); if (ret != 0 || f == NULL) { // This is the first time we see this code object so we need // to compile a trampoline for it. py_trampoline new_trampoline = compile_trampoline(); if (new_trampoline == NULL) { goto default_eval; } trampoline_api.write_state(trampoline_api.state, new_trampoline, perf_code_arena->code_size, co); _PyCode_SetExtra((PyObject *)co, extra_code_index, (void *)new_trampoline); f = new_trampoline; } assert(f != NULL); return f(ts, frame, throw, _PyEval_EvalFrameDefault); default_eval: // Something failed, fall back to the default evaluator. return _PyEval_EvalFrameDefault(ts, frame, throw); } #endif // PY_HAVE_PERF_TRAMPOLINE int _PyIsPerfTrampolineActive(void) { #ifdef PY_HAVE_PERF_TRAMPOLINE PyThreadState *tstate = _PyThreadState_GET(); return tstate->interp->eval_frame == py_trampoline_evaluator; #endif return 0; } void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *callbacks) { if (callbacks == NULL) { return; } #ifdef PY_HAVE_PERF_TRAMPOLINE callbacks->init_state = trampoline_api.init_state; callbacks->write_state = trampoline_api.write_state; callbacks->free_state = trampoline_api.free_state; #endif return; } int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *callbacks) { if (callbacks == NULL) { return -1; } #ifdef PY_HAVE_PERF_TRAMPOLINE if (trampoline_api.state) { _PyPerfTrampoline_Fini(); } trampoline_api.init_state = callbacks->init_state; trampoline_api.write_state = callbacks->write_state; trampoline_api.free_state = callbacks->free_state; trampoline_api.state = NULL; perf_status = PERF_STATUS_OK; #endif return 0; } int _PyPerfTrampoline_Init(int activate) { #ifdef PY_HAVE_PERF_TRAMPOLINE PyThreadState *tstate = _PyThreadState_GET(); if (tstate->interp->eval_frame && tstate->interp->eval_frame != py_trampoline_evaluator) { PyErr_SetString(PyExc_RuntimeError, "Trampoline cannot be initialized as a custom eval " "frame is already present"); return -1; } if (!activate) { tstate->interp->eval_frame = NULL; } else { tstate->interp->eval_frame = py_trampoline_evaluator; if (new_code_arena() < 0) { return -1; } extra_code_index = _PyEval_RequestCodeExtraIndex(NULL); if (extra_code_index == -1) { return -1; } perf_status = PERF_STATUS_OK; } #endif return 0; } int _PyPerfTrampoline_Fini(void) { #ifdef PY_HAVE_PERF_TRAMPOLINE PyThreadState *tstate = _PyThreadState_GET(); if (tstate->interp->eval_frame == py_trampoline_evaluator) { tstate->interp->eval_frame = NULL; } free_code_arenas(); extra_code_index = -1; #endif return 0; } PyStatus _PyPerfTrampoline_AfterFork_Child(void) { #ifdef PY_HAVE_PERF_TRAMPOLINE // Restart trampoline in file in child. int was_active = _PyIsPerfTrampolineActive(); _PyPerfTrampoline_Fini(); PyUnstable_PerfMapState_Fini(); if (was_active) { _PyPerfTrampoline_Init(1); } #endif return PyStatus_Ok(); }