Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
50a63dfd
Commit
50a63dfd
authored
Sep 22, 2018
by
mattip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP: TEST: add test for 'binary incompatibility' warning, error
parent
c4c9683e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
155 additions
and
0 deletions
+155
-0
tests/run/check_size.srctree
tests/run/check_size.srctree
+155
-0
No files found.
tests/run/check_size.srctree
0 → 100644
View file @
50a63dfd
PYTHON setup.py build_ext --inplace
PYTHON -c "import runner"
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
# force the build order
setup(ext_modules= cythonize("check_size.pyx"))
setup(ext_modules = cythonize("_check_size*.pyx"))
######## check_size_nominal.h ########
#include <Python.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
PyObject_HEAD
int f0;
int f1;
int f2;
} FooStructNominal;
#ifdef __cplusplus
}
#endif
######## check_size_bigger.h ########
#include <Python.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
PyObject_HEAD
int f0;
int f1;
int f2;
int f3;
int f4;
} FooStructBig;
#ifdef __cplusplus
}
#endif
######## check_size_smaller.h ########
#include <Python.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
PyObject_HEAD
int f9;
} FooStructSmall;
#ifdef __cplusplus
}
#endif
######## check_size.pyx ########
cdef class Foo:
cdef public int field0, field1, field2;
def __init__(self, f0, f1, f2):
self.field0 = f0
self.field1 = f1
self.field2 = f2
######## _check_size0.pyx ########
cdef extern from "check_size_nominal.h":
ctypedef class check_size.Foo [object FooStructNominal]:
cdef:
int f0
int f1
cpdef public int testme(Foo f) except -1:
return f.f0 + f.f1
######## _check_size1.pyx ########
cdef extern from "check_size_bigger.h":
ctypedef class check_size.Foo [object FooStructBig]:
cdef:
int f0
int f1
int f2
cpdef public int testme(Foo f, int f2) except -1:
f.f2 = f2
return f.f0 + f.f1 + f.f2
######## _check_size2.pyx ########
cdef extern from "check_size_smaller.h":
ctypedef class check_size.Foo [object FooStructSmall]:
cdef:
int f9
cpdef public int testme(Foo f) except -1:
return f.f9
######## runner.py ########
import check_size, _check_size0, warnings
foo = check_size.Foo(23, 123, 1023)
assert foo.field0 == 23
assert foo.field1 == 123
ret = _check_size0.testme(foo)
assert ret == 23 + 123
# ValueError since check_size.Foo's tp_basicsize is smaller than what is needed
# for FooStructBig. Messing with f2 will access memory outside the struct!
try:
import _check_size1
assert False
except ValueError as e:
assert str(e).startswith('check_size.Foo has the wrong size, try recompiling')
# Warining since check_size.Foo's tp_basicsize is larger than what is needed
# for FooStructSmall. There is "spare", accessing FooStructSmall's fields will
# never access invalid memory. This can happen, for instance, when using old
# headers with a newer runtime, or when using an old _check_size2 with a newer
# check_size, where the developers of check_size are careful to be backward
# compatible.
with warnings.catch_warnings(record=True) as w:
import _check_size2
assert len(w) == 1, 'expected one warning, got %d' % len(w)
assert str(w[-1].message).startswith('check_size.Foo size changed')
ret = _check_size2.testme(foo)
assert ret == 23
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment