irc.libera.chat
.
Release done for pip reasons.
Release done for pip reasons.
CPython 3 on Windows: we again try to compile with
Py_LIMITED_API
by default. This flag is not added if you run the compilation with CPython 3.4, as it only works with CPython >= 3.5, but by now this version of Python is quite old (and we no longer distribute cffi wheels for it).
This may require that you upgrade
virtualenv
(requires version 16 or newer) or at least copy manually
python3.dll
into your existing virtualenvs. For distributing wheels with your cffi modules, you may also need to upgrade
wheel
to the just-released version 0.35.
You can manually disable
Py_LIMITED_API
by calling
ffi.set_source(..., py_limited_api=False)
.
typedef int my_array_t[...];
with an explicit
dot-dot-dot in API mode (
issue #453
)
ffi.dlopen()
can now be called with a handle (as a
void *
) to an already-opened C library.
CPython only: fixed a stack overflow issue for calls like
lib.myfunc([large list])
. If the function is declared as taking a
float *
argument, for example, then the array is temporarily converted into a C array of floats—however, the code used to use
alloca()
for this temporary storage, no matter how large. This is now fixed.
The fix concerns all modes: in-line/out-of-line API/ABI. Also note that your API-mode C extension modules need to be regenerated with cffi 1.14 in order to get the fix; i.e. for API mode, the fix is in the generated C sources. (The C sources generated from cffi 1.14 should also work when running in a different environment in which we have an older version of cffi. Also, this change makes no difference on PyPy.)
As a workaround that works on all versions of cffi, you can write
lib.myfunc(ffi.new("float[]", [large list]))
, which is equivalent but explicity builds the intermediate array as a regular Python object on the heap.
fixed a memory leak inside
ffi.getwinerror()
on CPython 3.x.
cdef()
a global variable with only
void *foo;
. You should always use a storage class, like
extern void
*foo;
or maybe
static void *foo;
. These are all equivalent for
the purposes of
cdef()
, but the reason for deprecating the bare version
is that (as far as I know) it would always be mistake in a real C header.
RuntimeError: found a situation in which we try
to build a type recursively
(
issue #429
).
ffi.from_buffer("type *", ..)
is now supported, in addition to
"type[]"
. You can then write
p.field
to access the items, instead
of only
p[0].field
. Be careful that no bounds checking is performed, so
p[n]
might access data out of bounds.
int : 1;
.
ffi.cdef("""struct X { void(*fnptr)(struct X); };""")
U
and
L
characters at the end of integer
constants in
ffi.cdef()
(thanks Guillaume).
CPython 3 on Windows: we again no longer compile with
Py_LIMITED_API
by default because such modules
still
cannot be used with virtualenv. The problem is that it doesn’t work in CPython <= 3.4, and for technical reason we can’t enable this flag automatically based on the version of Python.
Like before,
Issue #350
mentions a workaround if you still want the
Py_LIMITED_API
标志和
either
you are not concerned about virtualenv
or
you are sure your module will not be used on CPython <= 3.4: pass
define_macros=[("Py_LIMITED_API", None)]
as a keyword to the
ffibuilder.set_source()
调用。
ffi.from_buffer()
takes a new optional
第一
argument that gives
the array type of the result. It also takes an optional keyword argument
require_writable
to refuse read-only Python buffers.
ffi.new()
,
ffi.gc()
or
ffi.from_buffer()
cdata objects
can now be released at known times, either by using the
with
keyword or by calling the new
ffi.release()
.
python3.dll
again. This makes them independant on the exact CPython version,
like they are on other platforms.
It requires virtualenv 16.0.0.
ffi.new("int[4]", p)
if
p
is itself
another cdata
int[4]
.
ffi.dlopen()
failed with non-ascii file names on Posix
ffi.cdef(..., pack=N)
where N is a power of two.
Means to emulate
#pragma pack(N)
on MSVC. Also, the default on
Windows is now
pack=8
, like on MSVC. This might make a difference
in corner cases, although I can’t think of one in the context of CFFI.
The old way
ffi.cdef(..., packed=True)
remains and is equivalent
to
pack=1
(saying e.g. that fields like
int
should be aligned
to 1 byte instead of 4).
ffi.emit_python_code()
which generated a buggy
Python file if you are using a
struct
with an anonymous
union
field or vice-versa.
ffi.dlopen()
should now handle unicode filenames.
ffi.dlclose()
for the in-line case (it used
to be present only in the out-of-line case).
setup.py install --record=xx --root=yy
with an out-of-line ABI module. Also fixed
Issue #345
.
setup.py
.
Py_InitializeEx()
). Sadly, recent
changes to CPython make that solution needed on CPython 2 too.
Py_LIMITED_API
by default because such modules cannot be used with virtualenv.
Issue #350
mentions a workaround if you still want that and are not
concerned about virtualenv: pass
define_macros=[("Py_LIMITED_API",
None)]
as a keyword to the
ffibuilder.set_source()
调用。
python3.dll
,因为
virtualenv does not make this DLL available to virtual environments
for now. See
Issue #355
. On Windows only, the C extension
modules created by cffi follow for now the standard naming scheme
foo.cp36-win32.pyd
, to make it clear that they are regular
CPython modules depending on
python36.dll
.
__loader__
or
__spec__
from the cffi-generated lib modules gave a buggy
SystemError. (These attributes are always None, and provided only to
help compatibility with tools that expect them in all modules.)
ffi.embedding_init_code(large_string)
); and an issue with
Py_LIMITED_API
linking with
python35.dll/python36.dll
代替
of
python3.dll
.
char16_t
and
char32_t
.
These work like
wchar_t
: they represent one unicode character, or
when used as
charN_t *
or
charN_t[]
they represent a unicode
string. The difference with
wchar_t
is that they have a known,
fixed size. They should work at all places that used to work with
wchar_t
(please report an issue if I missed something). Note
that with
set_source()
, you need to make sure that these types are
actually defined by the C source you provide (if used in
cdef()
).
float _Complex
and
double _Complex
.
Note that libffi doesn’t support them, which means that in the ABI
mode you still cannot call C functions that take complex numbers
directly as arguments or return type.
FFI
instances
from multiple threads. (Note that you aren’t meant to create many
FFI
instances: in inline mode, you should write
ffi =
cffi.FFI()
at module level just after
import cffi
; and in
out-of-line mode you don’t instantiate
FFI
explicitly at all.)
@ffi.def_extern(onerror=...)
and send the error logs where it
makes sense for your application, or record them in log files, and so
on.) So what is new in CFFI is that now, on Windows CFFI will try to
open a non-modal MessageBox (in addition to sending raw messages to
stderr). The MessageBox is only visible if the process stays alive:
typically, console applications that crash close immediately, but that
is also the situation where stderr should be visible anyway.
(only released inside PyPy 5.8.0)
cdef()
errors.
Also, I just noticed, but pycparser always supported the preprocessor
directive
# 42 "foo.h"
to mean “from the next line, we’re in file
foo.h starting from line 42”, which it puts in the error messages.
ffi.new(large-array)
where most of the time
you never touch most of the array.
ffi.from_buffer
. Now all
buffer/memoryview objects can be passed. The one remaining check is
against passing unicode strings in Python 2. (They support the buffer
interface, but that gives the raw bytes behind the UTF16/UCS4 storage,
which is most of the times not what you expect. In Python 3 this has
been fixed and the unicode strings don’t support the memoryview
interface any more.)
_Bool
or
bool
now converts to a Python boolean
when reading, instead of the content of the byte as an integer. The
potential incompatibility here is what occurs if the byte contains a
value different from 0 and 1. Previously, it would just return it;
with this change, CFFI raises an exception in this case. But this
case means “undefined behavior” in C; if you really have to interface
with a library relying on this, don’t use
bool
in the CFFI side.
Also, it is still valid to use a byte string as initializer for a
bool[]
, but now it must only contain
\x00
or
\x01
. As an
aside,
ffi.string()
no longer works on
bool[]
(but it never
made much sense, as this function stops at the first zero).
ffi.buffer
is now the name of cffi’s buffer type, and
ffi.buffer()
works like before but is the constructor of that type.
ffi.addressof(lib, "name")
now works also in in-line mode, not
only in out-of-line mode. This is useful for taking the address of
global variables.
cdata
objects of a primitive type (integers, floats,
char) are now compared and ordered by value. For example,
<cdata
'int' 42>
compares equal to
42
and
<cdata 'char' b'A'>
compares equal to
b'A'
. Unlike C,
<cdata 'int' -1>
does not
compare equal to
ffi.cast("unsigned int", -1)
: it compares
smaller, because
-1 < 4294967295
.
ffi.new()
and
ffi.new_allocator()()
did not record
“memory pressure”, causing the GC to run too infrequently if you call
ffi.new()
very often and/or with large arrays. Fixed in PyPy 5.7.
ffi.cdef()
for numeric expressions with
+
or
-
. Assumes that there is no overflow; it should be fixed first
before we add more general support for arbitrary arithmetic on
常量。
ffi.new()
is called, just like we
always tracked the length of
ffi.new("int[]", 42)
. This lets us
detect out-of-range accesses to array items. This also lets us
display a better
repr()
, and have the total size returned by
ffi.sizeof()
and
ffi.buffer()
. Previously both functions
would return a result based on the size of the declared structure
type, with an assumed empty array. (Thanks andrew for starting this
refactoring.)
cdef()/set_source()
for unspecified-length arrays
in typedefs:
typedef int foo_t[...];
. It was already supported
for global variables or structure fields.
cffi/model.py
into an error:
'enum xxx' has no values explicitly defined: refusing to guess which
integer type it is meant to be (unsigned/signed, int/long)
. Now I’m
turning it back to a warning again; it seems that guessing that the
enum has size
int
is a 99%-safe bet. (But not 100%, so it stays
as a warning.)
FILE *
arguments. In CPython 3
there is a remaining issue that is hard to fix: if you pass a Python
file object to a
FILE *
argument, then
os.dup()
is used and
the new file descriptor is only closed when the GC reclaims the Python
file object—and not at the earlier time when you call
close()
,
which only closes the original file descriptor. If this is an issue,
you should avoid this automatic convertion of Python file objects:
instead, explicitly manipulate file descriptors and call
fdopen()
from C (…via cffi).
void *
argument to a function with a different
pointer type, or vice-versa, the cast occurs automatically, like in C.
The same occurs for initialization with
ffi.new()
and a few other
places. However, I thought that
char *
had the same
property—but I was mistaken. In C you get the usual warning if you
try to give a
char *
到
char **
argument, for example.
Sorry about the confusion. This has been fixed in CFFI by giving for
now a warning, too. It will turn into an error in a future version.
ffi.new()
on structures/unions with nested
anonymous structures/unions, when there is at least one union in
the mix. When initialized with a list or a dict, it should now
behave more closely like the
{ }
syntax does in GCC.
NAME.abi3.so
, or use the very
recent setuptools 26.
ffi.compile(debug=...)
,类似
python setup.py build
--debug
but defaulting to True if we are running a debugging
version of Python itself.
ffi.from_buffer()
cannot be used on
byte strings. Now you can get a
char *
out of a byte string,
which is valid as long as the string object is kept alive. (But
don’t use it to
修改
the string object! If you need this, use
bytearray
or other official techniques.)
char *
argument (in older versions, a copy would be made). This used to be
a CPython-only optimization.
ffi.gc(p, None)
removes the destructor on an object previously
created by another call to
ffi.gc()
bool(ffi.cast("primitive type", x))
now returns False if the
value is zero (including
-0.0
), and True otherwise. Previously
this would only return False for cdata objects of a pointer type when
the pointer is NULL.
ffi.from_buffer(bytearray-object)
现在支持。
(The reason it was not supported was that it was hard to do in PyPy,
but it works since PyPy 5.3.) To call a C function with a
char *
argument from a buffer object—now including bytearrays—you write
lib.foo(ffi.from_buffer(x))
. Additionally, this is now supported:
p[0:length] = bytearray-object
. The problem with this was that a
iterating over bytearrays gives
numbers
而不是
characters
.
(Now it is implemented with just a memcpy, of course, not actually
iterating over the characters.)
bool
type (because that is rendered
as the C
_Bool
type, which doesn’t exist in C++).
help(lib)
and
help(lib.myfunc)
now give useful information,
及
dir(p)
where
p
is a struct or pointer-to-struct.
lib.foo.__doc__
contains the C signature now. On
CPython you can say
help(lib.foo)
, but for some reason
help(lib)
(或
help(lib.foo)
on PyPy) is still useless; I
haven’t yet figured out the hacks needed to convince
pydoc
to
show more. (You can use
dir(lib)
but it is not most helpful.)
ffi.def_extern()
against
CPython’s interpreter shutdown logic.
__stdcall
was never generated for
extern "Python"
函数
Nothing changed from v1.4.1.
cdef()
of which callbacks are needed. This is
more C-like, in that you have to structure your code around the idea
that you get a fixed number of function pointers, instead of
creating them on-the-fly.
ffi.compile()
now takes an optional
verbose
argument. When
True
, distutils prints the calls to the compiler.
ffi.compile()
used to fail if given
sources
with a path that
包括
".."
. Fixed.
ffi.init_once()
added. See
docs
.
dir(lib)
now works on libs returned by
ffi.dlopen()
也。
demo
subdirectory
in the sources (thanks matti!).
ffi.new_handle()
is now guaranteed to return unique
void *
values, even if called twice on the same object. Previously, in
that case, CPython would return two
cdata
objects with the same
void *
value. This change is useful to add and remove handles
from a global dict (or set) without worrying about duplicates.
It already used to work like that on PyPy.
This change can break code that used to work on CPython by relying
on the object to be kept alive by other means than keeping the
result of ffi.new_handle() alive.
(The corresponding
warning in
the docs
of
ffi.new_handle()
has been here since v0.8!)
bool
,
FILE
and all Windows types) were
not always available from out-of-line FFI objects.
unsigned int
.
Please report if you get a reasonable use case for them.
volatile
is passed along like
const
and
restrict
. Also, older versions of pycparser
mis-parse some pointer-to-pointer types like
char * const *
: the
“const” ends up at the wrong place. Added a workaround.
typedef float... foo_t;
. This only
works if
foo_t
is a float or a double, not
long double
.
set_source()
or
verify()
,
the
const
and
restrict
keywords are copied from the cdef
to the generated C code; this fixes warnings by the C compiler.
It also fixes corner cases like
typedef const int T; T a;
which would previously not consider
a
as a constant. (The
cdata objects themselves are never
const
)。
__stdcall
. For callbacks and function
pointers; regular C functions still don’t need to have their
调用
convention
declared.
import setuptools
first, which patches distutils…
Nothing changed from v1.2.0.
int a[][...];
can be used to declare a structure
field or global variable which is, simultaneously, of total length
unknown to the C compiler (the
a[]
part) and each element is
itself an array of N integers, where the value of N
is
known to the
C compiler (the
int
and
[...]
parts around it). Similarly,
int a[5][...];
is supported (but probably less useful: remember
that in C it means
int (a[5])[...];
).
lib.some_function
objects were missing the attributes
__name__
,
__module__
and
__doc__
that are expected e.g. by
some decorators-management functions from
functools
.
from _example.lib import x
to import the name
x
from
_example.lib
, even though the
lib
object is not a standard module object. (Also works in
from
_example.lib
import
*
, but this is even more of a hack and will fail
if
lib
happens to declare a name called
__all__
。注意,
*
excludes the global variables; only the functions and constants
make sense to import like this.)
lib.__dict__
works again and gives you a copy of the
dict—assuming that
lib
has got no symbol called precisely
__dict__
. (In general, it is safer to use
dir(lib)
)。
errno
)
or
__thread
-local variables. (This change might also tighten
the C compiler’s check on the variables’ type.)
ffi.callback(...,
onerror=...)
. If the main callback function raises an exception
and
onerror
is provided, then
onerror(exception, exc_value,
traceback)
is called. This is similar to writing a
try:
except:
in the main callback function, but in some cases (e.g. a
signal) an exception can occur at the very start of the callback
function—before it had time to enter the
try: except:
块。
ffi.new_allocator()
, which officializes
support for
alternative allocators
.
ffi.gc()
: fixed a race condition in multithreaded programs
introduced in 1.1.1
ffi.string()
,
ffi.buffer()
and
ffi.getwinerror()
didn’t accept their arguments as keyword
arguments, unlike their in-line mode equivalent. (It worked in PyPy.)
ffi.dlopen()
when compared to the in-line mode.
ffi.gc()
: when called several times with equal pointers, it was
accidentally registering only the last destructor, or even none at
all depending on details. (It was correctly registering all of them
only in PyPy, and only with the out-of-line FFIs.)
typedef int... foo_t;
. The exact size and signedness of
foo_t
is figured out by the compiler.
int n[...][...]
. Before, only the
outermost dimension would support the
...
句法。
extern const sometype somename;
.
ffi.addressof(lib, "func_name")
now returns a regular cdata object
of type “pointer to function”. You can use it on any function from a
library in API mode (in ABI mode, all functions are already regular
cdata objects). To support this, you need to recompile your cffi
模块。
struct
type, what you saw from lib.CONSTANT was corrupted.
ffi.set_source("package._ffi", None)
would
incorrectly generate the Python source to
package._ffi.py
代替
of
package/_ffi.py
. Also fixed: in some cases, if the C file was
in
build/foo.c
, the .o file would be put in
build/build/foo.o
.
ffi.set_source()
crashed if passed a
sources=[..]
自变量。
Fixed by chrippa on pull request #60.
ffi.dlopen()
ffi.verify()
, now deprecated