From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14022 invoked by alias); 20 Jan 2017 16:49:02 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 13909 invoked by uid 89); 20 Jan 2017 16:48:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-HELO: simark.ca Received: from simark.ca (HELO simark.ca) (158.69.221.121) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 20 Jan 2017 16:48:57 +0000 Received: by simark.ca (Postfix, from userid 33) id EDE541E7DE; Fri, 20 Jan 2017 11:48:55 -0500 (EST) To: Pedro Alves Subject: Re: [PATCH] Fix python-interactive with Python 3.6 X-PHP-Originating-Script: 33:rcube.php MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Fri, 20 Jan 2017 16:49:00 -0000 From: Simon Marchi Cc: Simon Marchi , gdb-patches@sourceware.org In-Reply-To: <1b7bea3d-3044-a643-d7b9-3b11db14758f@redhat.com> References: <20170120151550.24928-1-simon.marchi@ericsson.com> <1b7bea3d-3044-a643-d7b9-3b11db14758f@redhat.com> Message-ID: <5989286b709d1361c8bc9062567ffedf@polymtl.ca> X-Sender: simon.marchi@polymtl.ca User-Agent: Roundcube Webmail/1.2.3 X-IsSubscribed: yes X-SW-Source: 2017-01/txt/msg00424.txt.bz2 On 2017-01-20 11:37, Pedro Alves wrote: > On 01/20/2017 03:15 PM, Simon Marchi wrote: >> Since Python 3.4, the callback installed in >> PyOS_ReadlineFunctionPointer >> should return a value allocated with PyMem_RawMalloc instead of >> PyMem_Malloc. The reason is that PyMem_Malloc must be called with the >> Python Global Interpreter Lock (GIL) held, which is not the case in >> the >> context where this function is called. PyMem_RawMalloc was introduced >> for cases like this. >> > >> +/* Starting from Python 3.4, the result of the >> PyOS_ReadlineFunctionPointer >> + callback must be allocated with PyMem_RawMalloc rather than >> PyMem_Malloc. */ >> + >> +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4 > > This will break with PY_MAJOR_VERSION 4 (at some point :-) ). Yeah, I thought about that and concluded it would be a problem for my grandchildren ;). > Write: > > #if PY_MAJOR_VERSION > 3 > || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4) > > ? Ok. > Or add some macro that makes it a bit easier to write, > like "#if HAVE_PY_AT_LEAST(3, 4)" or > "#if GDB_PY_VERSION >= 3004" (like GCC_VERSION). > Or use Python's PY_VERSION_HEX, like > "#if PY_VERSION_HEX >= 0x03040000". I think that's better. >> +#define PyOS_ReadlineFunctionPointer_Malloc PyMem_RawMalloc >> +#else >> +#define PyOS_ReadlineFunctionPointer_Malloc PyMem_Malloc >> +#endif > > It sounds like we'll find other cases that will need > to call PyMem_RawMalloc going forward? How about handling > this similarly to how we handle fixing up other missing > Python bits, in python-internal.h. Like: For now it looks like this is the only place where the Python API requires using PyMem_RawMalloc. In the other cases, we are holding the GIL, thanks to gdbpy_enter, so using the regular PyMem functions is ok. It doesn't mean there won't be other cases added in the future though... > #if python < 3.4 > > static inline void * > gdb_PyMem_RawMalloc (size_t n) > { > return gdb_PyMem_Malloc (n); > } > #define PyMem_RawMalloc(n) gdb_PyMem_RawMalloc (n) > > #endif ... and this is cleaner anyway. But why not just #if python < 3.4 #define PyMem_RawMalloc PyMem_Malloc #endif ? >> + >> /* Readline function suitable for PyOS_ReadlineFunctionPointer, which >> is used for Python's interactive parser and raw_input. In both >> cases, sys_stdin and sys_stdout are always stdin and stdout >> @@ -63,7 +73,7 @@ gdbpy_readline_wrapper (FILE *sys_stdin, FILE >> *sys_stdout, >> /* Detect EOF (Ctrl-D). */ >> if (p == NULL) >> { >> - q = (char *) PyMem_Malloc (1); >> + q = (char *) PyOS_ReadlineFunctionPointer_Malloc (1); > > and then write PyMem_RawMalloc here. > > Thanks, > Pedro Alves