cfbolz changed the topic of #pypy to: PyPy, the flexible snake (IRC logs: https://quodlibet.duckdns.org/irc/pypy/latest.log.html#irc-end ) | use cffi for calling C | if a pep adds a mere 25-30 [C-API] functions or so, it's a drop in the ocean (cough) - Armin
adamholmberg has joined #pypy
jcea has quit [Quit: jcea]
adamholmberg has quit [Remote host closed the connection]
_whitelogger has joined #pypy
MithrasX has joined #pypy
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
xcm has quit [Remote host closed the connection]
mattip has quit [Ping timeout: 256 seconds]
xcm has joined #pypy
mattip has joined #pypy
_whitelogger has joined #pypy
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
adamholmberg has joined #pypy
adamholmberg has quit [Ping timeout: 240 seconds]
xcm has quit [Ping timeout: 250 seconds]
xcm has joined #pypy
dmalcolm has quit [Remote host closed the connection]
jvesely has quit [Quit: jvesely]
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
dddddd has quit [Ping timeout: 250 seconds]
MithrasX1 has joined #pypy
MithrasX has quit [Ping timeout: 250 seconds]
MithrasX2 has joined #pypy
MithrasX1 has quit [Ping timeout: 264 seconds]
<bbot2> Started: http://buildbot.pypy.org/builders/pypy-c-jit-win-x86-32/builds/5192 [mattip: force build, win32-cppyy]
MithrasX2 has quit [Ping timeout: 264 seconds]
xcm has quit [Read error: Connection reset by peer]
xcm has joined #pypy
ekaologik has joined #pypy
xcm has quit [Remote host closed the connection]
BPL has joined #pypy
xcm has joined #pypy
MithrasX has joined #pypy
MithrasX1 has joined #pypy
MithrasX has quit [Ping timeout: 240 seconds]
nimaje has quit [Quit: WeeChat 2.7.1]
nimaje has joined #pypy
xcm has quit [Remote host closed the connection]
rubdos has quit [Quit: WeeChat 2.7.1]
MithrasX2 has joined #pypy
xcm has joined #pypy
rubdos has joined #pypy
MithrasX1 has quit [Ping timeout: 240 seconds]
rubdos_ has joined #pypy
rubdos has quit [Ping timeout: 240 seconds]
rubdos has joined #pypy
rubdos_ has quit [Ping timeout: 250 seconds]
rubdos has quit [Ping timeout: 265 seconds]
<bbot2> Failure: http://buildbot.pypy.org/builders/pypy-c-jit-win-x86-32/builds/5192 [mattip: force build, win32-cppyy]
<rjarry> guys, I'm having troubles with a function cdef that contains a time_t pointer: http://paste.debian.net/1135711/
<rjarry> I guess it is because it doesn't know what is a time_t
<rjarry> however, since #includes are not supported, how am I supposed to provide the correct type?
<wleslie> I search the included file myself, usually
rubdos has joined #pypy
<mattip> it depends if you need the contents of the struct in your cdef
<mattip> if not I think you can do "typedef struct time_t;" ??
<rjarry> it is not a struct, it is a long
<rjarry> found in /usr/include/bits/typesizes.h
<rjarry> #define __TIME_T_TYPE __SYSCALL_SLONG_TYPE
<mattip> +1
FullTiltProspect has joined #pypy
<rjarry> I can do typedef time_t long; in my cdef
<rjarry> but it may be incorrect depending on where the cffi wrapper is compiled
<wleslie> the example I gave is a working example
<rjarry> wleslie: basically, just typedef struct time_t time_t; ?
<wleslie> in my cdefs call, yes
<rjarry> but how does the compiler know that it is a long ?
<rjarry> sorry, I'm not sure I understand what I should do :)
rubdos has quit [Ping timeout: 264 seconds]
<wleslie> sorry I mean you should typedef it with the correct type (:
<rjarry> ah
<rjarry> that should be tricky...
<wleslie> typedef long time_t;
<rjarry> yeah that's the easy/dirty way to fix the problem
<rjarry> <rjarry> but it may be incorrect depending on where the cffi wrapper is compiled
<rjarry> for example, on 32bit or exotic archs
<wleslie> ah, so it's not defined by your library, it's a system include?
<rjarry> yes
<rjarry> <rjarry> found in /usr/include/bits/typesizes.h
<rjarry> <rjarry> #define __TIME_T_TYPE __SYSCALL_SLONG_TYPE
<rjarry> in the library I am writing a cffi wrapper for, there is #include <time.h>
<rjarry> which provides time_t
rubdos has joined #pypy
<wleslie> ok, I'm not sure, I don't have any of these in my library
jcea has joined #pypy
adamholmberg has joined #pypy
dddddd has joined #pypy
<arigato> rjarry: you can often use "typedef int... time_t;"
<arigato> (literally)
<rjarry> arigato: oh, is that a special syntax?
<rjarry> thanks
<arigato> yes
<rjarry> arigato: when facing with a library function such as: int sr_connect(int options, sr_connection_t **);
lritter has joined #pypy
<rjarry> in C, the use is: on the stack, sr_connection *conn = NULL;
<rjarry> then, sr_connect(opts, &conn);
<rjarry> however, with cffi, it seems more complex
<rjarry> as addressof seems not to cut it
<arigato> the cffi version is similar to what you could also write in C as:
<arigato> conn = malloc()
<arigato> sr_connect(opts, conn);
<rjarry> the malloc is additional :)
<arigato> now with sr_connection **conn;
MithrasX2 has quit [Ping timeout: 240 seconds]
<arigato> yes, so maybe let's call it "sr_connection **pconn = malloc(); sr_connect(opts, pconn);"
<arigato> and then that translates to cffi: pconn = ffi.new("sr_connection **"); sr_connect(opts, pconn)
<rjarry> that's what I have come up with until now
<rjarry> but I need to explicitly access to pconn[0]
<arigato> yes
<rjarry> for other functions that require sr_connection * args
<arigato> the problem with addressof() is that while it could potentially work in this case, in general it can't, if say we're talking about a function "foo(int *arg)": because it doesn't make sense to take ffi.addressof(n) if n is a Python integer object
<rjarry> should I always keep a reference to pconn or only to pconn[0] ?
<rjarry> as all other functions want a 'sr_connection *' value, not 'sr_connection **'
<arigato> it doesn't matter. if sr_connect() writes a result to pconn[0], then just after the call you can do "conn = pconn[0]" and forget pconn
<rjarry> ok, I do not risk pconn freeing too much then
<rjarry> when it goes out of scope
<arigato> no, pconn is only holding the "sr_connection *", i.e. the pointer itself
<arigato> not a complete "sr_connection"
<rjarry> got it
<rjarry> I had similar a problem with functions that allocate 'char *'
<rjarry> I had to do pbuf = ffi.new('char **')
<rjarry> call the fuinction with pbuff
<rjarry> pbuf
<rjarry> and the do not forget to explicitly call lib.free(pbuf[0])
<rjarry> is there a cleaner way to do this?
<arigato> yes, that's correct, but also you can say "buf = pbuf[0]" after the call to the function, and only use "buf" and forget pbuf
<arigato> (including lib.free(buf))
<rjarry> does that properly handle subtleties such as 'const char *' != 'char *' ?
<arigato> no, cffi completely ignores the "const"
<rjarry> that's what I feared :)
<arigato> but that shouldn't be a problem
<rjarry> well, I wouldn't want cffi to free something it should not
<arigato> as long as you don't actually try to write into the "const char *" array, at least
<arigato> ah, well, "const" is not related to "the caller is supposed to free this" in C
<rjarry> let me find a more pertinent example
<arigato> there's no way to double-check automatically that you're correctly freeing stuff (or freeing something you shouldn't), in C
<rjarry> here it is
<rjarry> I have: lib.lys_path() that returns a 'char *'
<rjarry> which must be freed with free()
<rjarry> explicitly
<arigato> OK. that's right. maybe better (on pypy, where ffi.gc() doesn't free immediately when doing out of scope) would be to use more explicitly:
<arigato> p = lib.lys_path()
<arigato> try:
<arigato> return c2str(p)
<arigato> finally:
<arigato> lib.free(p)
<rjarry> better than ffi.gc ?
<arigato> yes, because it calls lib.free(p) here, as opposed to "some unknown time later"
<arigato> you can also write it like this if you prefer:
<arigato> with lib.gc(lib.lys_path(), lib.free) as p:
<arigato> return c2str(p)
<rjarry> I see
<arigato> it's equivalent (shorter, but uses more concepts)
MithrasX has joined #pypy
<rjarry> but it requires that the libc free() function is declared somewhere in the cffi object
<arigato> yes
<rjarry> there is no "builtin" way to achieve this?
<arigato> that part is correct and unavoidable: you're calling some API that requires you to call free() afterwards
<arigato> no
<rjarry> ok
<arigato> msotly because, why? it's just one line to add, like you did, to the cdef()
<rjarry> that's easily overlooked and leads to memory leaks
<arigato> yes :-( welcome to C
<rjarry> hehe, I am used to it, thankfully
<rjarry> thanks for all the clarifications, arigato :)
<arigato> if you're calling the same function lys_path() from many places, then you should turn it into a helper function so you don't need to worry about memory management outside
<arigato> welcome!
dmalcolm has joined #pypy
<arigato> it's good practice in general to put all this memory-management functions into their own module
<arigato> but maybe this libyang/schema.py is already a thin later around the lib functions in C, so that's enough
<rjarry> coming back to sr_connect(), with that pconn
<rjarry> pconn = ffi.new('sr_connection_t **')
<rjarry> sr_connect(options, pconn)
<rjarry> conn = pconn[0]
<rjarry> is there a clean way to call sr_disconnect(conn) when conn goes out of scope?
jvesely has joined #pypy
<arigato> we've seen them all: if you want to call sr_disconnect() at a known point, use try: finally: or "with lib.gc()". Otherwise, just say "conn = lib.gc(pconn[0], lib.sr_disconnect)" and it will call sr_disconnect at some not-very-well-specified point later
<arigato> (not-well-specified but after the last reference to 'conn' goes away)
svanheulen has joined #pypy
MithrasX1 has joined #pypy
MithrasX has quit [Ping timeout: 250 seconds]
mattip has quit [Quit: ZNC 1.6.6+deb1ubuntu0.2 - http://znc.in]
mattip has joined #pypy
<mattip> arigato: if you have a second, the win32 extra_test/test/test_semlock is hanging
<mattip> it errors and does not kill the test
MithrasX2 has joined #pypy
<mattip> on default
MithrasX1 has quit [Ping timeout: 250 seconds]
<mattip> if I reduce the number of threads to 500 from 1000, then the test runs to completion but fails, len(result) == 294 not 500
<arigato> OK let me see
<arigato> it's failing in this way since forever, right?
jacob22 has quit [Quit: Konversation terminated!]
jacob22 has joined #pypy
<arigato> I think that's just because timeouts occur in the test and the test continues in a bogus state
<arigato> OK, no, if I increase the "timeout" from 5 to 50 then it sleeps for 50 seconds and then fails
<mattip> note this is -A tests, I didn’t try untranslated
adamholmberg has quit [Remote host closed the connection]
adamholmberg has joined #pypy
adamholmberg has quit [Remote host closed the connection]
adamholmberg has joined #pypy
MithrasX2 has quit [Ping timeout: 240 seconds]
<arigato> uh OK, may have found it?
<arigato> 263ac72641a2
<arigato> ronan fixed the very same problem on Posix
svanheulen has quit [Quit: WeeChat 2.7.1]
<arigato> and on Windows he made a typo, "releasegil=True" instead of "releasegil=False"
<arigato> (gah I keep making typos *here*. above I meant to type "uh OK, maybe I have found it?"
MithrasX2 has joined #pypy
<bbot2> Started: http://buildbot.pypy.org/builders/pypy-c-jit-win-x86-32/builds/5193 [arigo: testing 5c458d5d5ce5]
vstinner has joined #pypy
<vstinner> arigato: hello. i proposed https://github.com/python/cpython/pull/19094 to remove _PyThreadState_GetFrame. you added it in 2002 for psyco! https://hg.python.org/lookup/019a78e76d3542d4d56a08015e6980f8c8aeaba1
<vstinner> arigato: i understood that the psyco project is dead (superseded by PyPy obviously). but just in case, can you confirm that you don't use _PyThreadState_GetFrame anymore?
MithrasX2 has quit [Ping timeout: 240 seconds]
jacob22 has quit [Quit: Konversation terminated!]
<arigato> no, we don't. And yes, psyco is python <= 2.6
<vstinner> "python <= 2.6" oh wow. hello time travelers :)
<vstinner> arigato: FYI i discovered (!) _PyThreadState_GetFrame while attempting to make the PyThreadState structure opaque in CPython
<vstinner> if anyone is curious, i'm working on https://bugs.python.org/issue39947 "Make the PyThreadState structure opaque (move it to the internal C API)" and on https://bugs.python.org/issue39573 "Make PyObject an opaque structure in the limited C API"
<simpson> Nice.
<vstinner> i don't think that making them opaque is doable in Python 3.9, but I made some changes to prepare C extensions for that
<vstinner> see https://docs.python.org/dev/whatsnew/3.9.html#build-and-c-api-changes for the summary of these changes
<vstinner> especially the "Changes in the limited C API" part
<arigato> cool. maybe one day you'll have made it opaque and generalized a little bit refcounting, and then we'll be able to remove the H in HPy_Xxx functions and be done :-)
<vstinner> "remove the H in HPy_Xxx functions and be done" that would be nice, yep ;)
<vstinner> arigato: the work i'm doing in CPython is the prepare the C API to move it towards HPy. hide as much implementation details as I can
<vstinner> many implementation details are only exposed "by mistake" (aka "for efficiency") and can easily be hidden without touching to the C API
<vstinner> the usual fix is to replace a macro (or a static inline function) with a regular “opaque” function call
<vstinner> arigato: HPy and "make the C API opaque" projects are complementary and have the same goals ;)
adamholmberg has quit [Remote host closed the connection]
adamholmberg has joined #pypy
WGH has joined #pypy
pyusr has joined #pypy
<pyusr> cffi should add support for PyUnicode_Decode at the cffi.unpack level
<pyusr> so a char* could be directly transformed to a string, without an .decode() step
vstinner has left #pypy [#pypy]
<bbot2> Started: http://buildbot.pypy.org/builders/own-linux-x86-64/builds/8075 [ronan: force build, py3.6]
FullTiltProspect has left #pypy [#pypy]
<arigato> pyusr: yes, I see the point
<pyusr> +1
<arigato> but in the other direction? ffi.new("char *", unicode_string, encoding) ?
<pyusr> arigato: I tried to port hiredis-py (a C-api extension) to use cffi and it ended up 10 times slower
<arigato> that's a lot
<pyusr> arigato: I wanted the char* to python string directly actually
<arigato> yes, but that's still not close to a 10x difference
<pyusr> ahh, it's not related, it's something else
<pyusr> let me show you the verbatim code change +-:
<arigato> (I guess the char*-to-bytestring thing is a relic of python 2 times, I should update my view on that topic)
<bbot2> Failure: http://buildbot.pypy.org/builders/own-linux-x86-64/builds/8075 [ronan: force build, py3.6]
<pyusr> no, char* to bytestring is important as well, both options should be possible
<arigato> yes, sure
<arigato> I meant the fact that you *have* to go via bytestring
<arigato> uuh, this works? I see that 'function_table' goes out of scope and is deallocated after __init__
<arigato> ah, if lib.redisReaderCreateWithFunctions copies the whole table, then yes
<pyusr> again the *10 slowdown is general cffi vs. c-api, not related to the bytes->string one
ekaologik has quit [Quit: https://quassel-irc.org - Komfortabler Chat. Überall.]
<arigato> yes, I see now
<arigato> this implementation of createIntegerPython() is horribly slow (but unclear how to do better in cffi)
<pyusr> is there any idom i'm missing, or for this type of code, this is what it is
<arigato> no, I can't think of anything better
<arigato> this is certainly the recommended idiom
<pyusr> k, guess it's better to write a pure python parser for pypy
<bbot2> Failure: http://buildbot.pypy.org/builders/pypy-c-jit-win-x86-32/builds/5193 [arigo: testing 5c458d5d5ce5]
<arigato> yes, or if you have to use the library and can't reasonably write everything in Python, you could try to use a bit more C code (inside ffibuilder.set_source())
<arigato> some two-steps process where all the functions in the function_table are from C, and build a C data structure,
<arigato> which is then transformed (lazily or not lazily) into Python afterwards
<arigato> if you're going for PyPy then the fastest would be to transform lazily
<arigato> it would be a bit more involved to write than what you have now, but not too much
<arigato> at least if you're comfortable writing C, with some malloc() and stuff
<pyusr> it's ok, there are pure python parsers (the protocol is easy), but soon there will be a new vresion of protocol
<pyusr> so I wanted to supported it via the C parser that is already written, but no biggie
<arigato> OK
<arigato> then yes, you can rewrite the parser in Python---and the same applies actually, if you can, make it lazy for better performance
<pyusr> the reason I wanted to modify hiredis-py is to make it possible to set per read the encoding / bytes
<pyusr> and not per object, so I thought let's try cffi
<pyusr> it was a nice learnign experience
<pyusr> arigato: I am actually trying a new model in python, where it's much more lazy than normal
<arigato> cool!
<pyusr> i.e. I'm reading the data from the I/O stream, as an memory-backed pool of small bytearrays
<arigato> nice
<pyusr> and then I parse that as a non-contingous memory with normal operations of read, readuntil, etc...
<pyusr> (I also support recv_into both sync socket, and async socket)
<pyusr> so minimizing allocations and copying
<arigato> messy but nice
<pyusr> yep, I'm trying to write now a benchmark to show how good or bad the idea is :)
<arigato> just to be clear, the "lazy" I had in mind was in the returned data structures, e.g. you wouldn't build a list with all the objects at once (but maybe it doesn't make a difference if individual messages are not large)
<pyusr> ahh :)
<pyusr> this is the benchmarkign frameework i'm building now https://bpaste.net/56NQ
<pyusr> surprised osmething like this does not exist
Ai9zO5AP has joined #pypy
adamholmberg has quit [Remote host closed the connection]
<mattip> arigato: the test still hangs
<arigato> thanks
<arigato> I'll look further
<arigato> I suspect it passes now if it doesn't hang (with less threads), but I'll check
xcm has quit [Read error: Connection reset by peer]
xcm has joined #pypy
adamholmberg has joined #pypy
adamholmberg has quit [Remote host closed the connection]
adamholmberg has joined #pypy
adamholmberg has quit [Remote host closed the connection]
adamholmberg has joined #pypy
adamholmberg has quit [Read error: Connection reset by peer]
adamholmberg has joined #pypy
adamholmberg has quit [Remote host closed the connection]
jacob22 has joined #pypy
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
BPL has quit [Quit: Leaving]
Ai9zO5AP has quit [Quit: WeeChat 2.7.1]
adamholmberg has joined #pypy
adamholmberg has quit [Remote host closed the connection]
lritter has quit [Quit: Leaving]
dansan_ has joined #pypy
starlord has quit [Ping timeout: 246 seconds]
wleslie has quit [Ping timeout: 246 seconds]
altendky has quit [Ping timeout: 246 seconds]
dansan has quit [Ping timeout: 246 seconds]
wleslie has joined #pypy
altendky has joined #pypy
starlord has joined #pypy
Ai9zO5AP has joined #pypy
starlord has quit [Ping timeout: 246 seconds]
epony has quit [Quit: reconf]
starlord has joined #pypy
epony has joined #pypy