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
Xavier Thompson
cython
Commits
14387ec8
Commit
14387ec8
authored
Sep 25, 2018
by
mattip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ENH: add check_size option to ctypedef class for external classes
parent
b6509bf7
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
140 additions
and
24 deletions
+140
-24
Cython/Compiler/ModuleNode.py
Cython/Compiler/ModuleNode.py
+15
-4
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+4
-0
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+8
-2
Cython/Compiler/PyrexTypes.py
Cython/Compiler/PyrexTypes.py
+3
-2
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+4
-2
Cython/Utility/ImportExport.c
Cython/Utility/ImportExport.c
+27
-10
tests/run/check_size.srctree
tests/run/check_size.srctree
+79
-4
No files found.
Cython/Compiler/ModuleNode.py
View file @
14387ec8
...
@@ -3059,10 +3059,21 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
...
@@ -3059,10 +3059,21 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
else
:
else
:
code
.
put
(
'sizeof(%s), '
%
objstruct
)
code
.
put
(
'sizeof(%s), '
%
objstruct
)
code
.
putln
(
'%i); if (!%s) %s'
%
(
# check_size
not
type
.
is_external
or
type
.
is_subclassed
,
if
not
type
.
is_external
or
type
.
is_subclassed
:
type
.
typeptr_cname
,
cs
=
'__PYX_CHECKSIZE_STRICT'
error_code
))
elif
type
.
check_size
==
b'min'
:
cs
=
'__PYX_CHECKSIZE_MIN'
elif
type
.
check_size
is
True
:
cs
=
'__PYX_CHECKSIZE_STRICT'
elif
type
.
check_size
is
False
:
cs
=
'__PYX_CHECKSIZE_LOOSE'
else
:
raise
AttributeError
(
"invalid value for check_size '%r' when compiling "
"%s.%s"
%
(
type
.
check_size
,
module_name
,
type
.
name
))
code
.
putln
(
'%s);'
%
cs
)
code
.
putln
(
' if (!%s) %s'
%
(
type
.
typeptr_cname
,
error_code
))
def
generate_type_ready_code
(
self
,
entry
,
code
):
def
generate_type_ready_code
(
self
,
entry
,
code
):
Nodes
.
CClassDefNode
.
generate_type_ready_code
(
entry
,
code
)
Nodes
.
CClassDefNode
.
generate_type_ready_code
(
entry
,
code
)
...
...
Cython/Compiler/Nodes.py
View file @
14387ec8
...
@@ -4629,6 +4629,7 @@ class CClassDefNode(ClassDefNode):
...
@@ -4629,6 +4629,7 @@ class CClassDefNode(ClassDefNode):
# bases TupleNode Base class(es)
# bases TupleNode Base class(es)
# objstruct_name string or None Specified C name of object struct
# objstruct_name string or None Specified C name of object struct
# typeobj_name string or None Specified C name of type object
# typeobj_name string or None Specified C name of type object
# check_size b'min' or boolean Issue warning if tp_basicsize does not match
# in_pxd boolean Is in a .pxd file
# in_pxd boolean Is in a .pxd file
# decorators [DecoratorNode] list of decorators or None
# decorators [DecoratorNode] list of decorators or None
# doc string or None
# doc string or None
...
@@ -4645,6 +4646,7 @@ class CClassDefNode(ClassDefNode):
...
@@ -4645,6 +4646,7 @@ class CClassDefNode(ClassDefNode):
api
=
False
api
=
False
objstruct_name
=
None
objstruct_name
=
None
typeobj_name
=
None
typeobj_name
=
None
check_size
=
b'min'
decorators
=
None
decorators
=
None
shadow
=
False
shadow
=
False
...
@@ -4680,6 +4682,7 @@ class CClassDefNode(ClassDefNode):
...
@@ -4680,6 +4682,7 @@ class CClassDefNode(ClassDefNode):
typeobj_cname
=
self
.
typeobj_name
,
typeobj_cname
=
self
.
typeobj_name
,
visibility
=
self
.
visibility
,
visibility
=
self
.
visibility
,
typedef_flag
=
self
.
typedef_flag
,
typedef_flag
=
self
.
typedef_flag
,
check_size
=
self
.
check_size
,
api
=
self
.
api
,
api
=
self
.
api
,
buffer_defaults
=
self
.
buffer_defaults
(
env
),
buffer_defaults
=
self
.
buffer_defaults
(
env
),
shadow
=
self
.
shadow
)
shadow
=
self
.
shadow
)
...
@@ -4765,6 +4768,7 @@ class CClassDefNode(ClassDefNode):
...
@@ -4765,6 +4768,7 @@ class CClassDefNode(ClassDefNode):
base_type
=
self
.
base_type
,
base_type
=
self
.
base_type
,
objstruct_cname
=
self
.
objstruct_name
,
objstruct_cname
=
self
.
objstruct_name
,
typeobj_cname
=
self
.
typeobj_name
,
typeobj_cname
=
self
.
typeobj_name
,
check_size
=
self
.
check_size
,
visibility
=
self
.
visibility
,
visibility
=
self
.
visibility
,
typedef_flag
=
self
.
typedef_flag
,
typedef_flag
=
self
.
typedef_flag
,
api
=
self
.
api
,
api
=
self
.
api
,
...
...
Cython/Compiler/Parsing.py
View file @
14387ec8
...
@@ -3471,6 +3471,7 @@ def p_c_class_definition(s, pos, ctx):
...
@@ -3471,6 +3471,7 @@ def p_c_class_definition(s, pos, ctx):
objstruct_name
=
None
objstruct_name
=
None
typeobj_name
=
None
typeobj_name
=
None
bases
=
None
bases
=
None
check_size
=
b'min'
if
s
.
sy
==
'('
:
if
s
.
sy
==
'('
:
positional_args
,
keyword_args
=
p_call_parse_args
(
s
,
allow_genexp
=
False
)
positional_args
,
keyword_args
=
p_call_parse_args
(
s
,
allow_genexp
=
False
)
if
keyword_args
:
if
keyword_args
:
...
@@ -3482,7 +3483,7 @@ def p_c_class_definition(s, pos, ctx):
...
@@ -3482,7 +3483,7 @@ def p_c_class_definition(s, pos, ctx):
if
s
.
sy
==
'['
:
if
s
.
sy
==
'['
:
if
ctx
.
visibility
not
in
(
'public'
,
'extern'
)
and
not
ctx
.
api
:
if
ctx
.
visibility
not
in
(
'public'
,
'extern'
)
and
not
ctx
.
api
:
error
(
s
.
position
(),
"Name options only allowed for 'public', 'api', or 'extern' C class"
)
error
(
s
.
position
(),
"Name options only allowed for 'public', 'api', or 'extern' C class"
)
objstruct_name
,
typeobj_name
=
p_c_class_options
(
s
)
objstruct_name
,
typeobj_name
,
check_size
=
p_c_class_options
(
s
)
if
s
.
sy
==
':'
:
if
s
.
sy
==
':'
:
if
ctx
.
level
==
'module_pxd'
:
if
ctx
.
level
==
'module_pxd'
:
body_level
=
'c_class_pxd'
body_level
=
'c_class_pxd'
...
@@ -3521,6 +3522,7 @@ def p_c_class_definition(s, pos, ctx):
...
@@ -3521,6 +3522,7 @@ def p_c_class_definition(s, pos, ctx):
bases
=
bases
,
bases
=
bases
,
objstruct_name
=
objstruct_name
,
objstruct_name
=
objstruct_name
,
typeobj_name
=
typeobj_name
,
typeobj_name
=
typeobj_name
,
check_size
=
check_size
,
in_pxd
=
ctx
.
level
==
'module_pxd'
,
in_pxd
=
ctx
.
level
==
'module_pxd'
,
doc
=
doc
,
doc
=
doc
,
body
=
body
)
body
=
body
)
...
@@ -3528,6 +3530,7 @@ def p_c_class_definition(s, pos, ctx):
...
@@ -3528,6 +3530,7 @@ def p_c_class_definition(s, pos, ctx):
def
p_c_class_options
(
s
):
def
p_c_class_options
(
s
):
objstruct_name
=
None
objstruct_name
=
None
typeobj_name
=
None
typeobj_name
=
None
check_size
=
b'min'
s
.
expect
(
'['
)
s
.
expect
(
'['
)
while
1
:
while
1
:
if
s
.
sy
!=
'IDENT'
:
if
s
.
sy
!=
'IDENT'
:
...
@@ -3538,11 +3541,14 @@ def p_c_class_options(s):
...
@@ -3538,11 +3541,14 @@ def p_c_class_options(s):
elif
s
.
systring
==
'type'
:
elif
s
.
systring
==
'type'
:
s
.
next
()
s
.
next
()
typeobj_name
=
p_ident
(
s
)
typeobj_name
=
p_ident
(
s
)
elif
s
.
systring
==
'check_size'
:
s
.
next
()
check_size
=
p_atom
(
s
).
value
if
s
.
sy
!=
','
:
if
s
.
sy
!=
','
:
break
break
s
.
next
()
s
.
next
()
s
.
expect
(
']'
,
"Expected 'object' or 'type'"
)
s
.
expect
(
']'
,
"Expected 'object' or 'type'"
)
return
objstruct_name
,
typeobj_name
return
objstruct_name
,
typeobj_name
,
check_size
def
p_property_decl
(
s
):
def
p_property_decl
(
s
):
...
...
Cython/Compiler/PyrexTypes.py
View file @
14387ec8
...
@@ -1345,14 +1345,14 @@ class PyExtensionType(PyObjectType):
...
@@ -1345,14 +1345,14 @@ class PyExtensionType(PyObjectType):
# vtable_cname string Name of C method table definition
# vtable_cname string Name of C method table definition
# early_init boolean Whether to initialize early (as opposed to during module execution).
# early_init boolean Whether to initialize early (as opposed to during module execution).
# defered_declarations [thunk] Used to declare class hierarchies in order
# defered_declarations [thunk] Used to declare class hierarchies in order
# check_size b'min' or boolean should tp_basicsize match sizeof(obstruct_cname)
is_extension_type
=
1
is_extension_type
=
1
has_attributes
=
1
has_attributes
=
1
early_init
=
1
early_init
=
1
objtypedef_cname
=
None
objtypedef_cname
=
None
def
__init__
(
self
,
name
,
typedef_flag
,
base_type
,
is_external
=
0
):
def
__init__
(
self
,
name
,
typedef_flag
,
base_type
,
is_external
=
0
,
check_size
=
b'min'
):
self
.
name
=
name
self
.
name
=
name
self
.
scope
=
None
self
.
scope
=
None
self
.
typedef_flag
=
typedef_flag
self
.
typedef_flag
=
typedef_flag
...
@@ -1368,6 +1368,7 @@ class PyExtensionType(PyObjectType):
...
@@ -1368,6 +1368,7 @@ class PyExtensionType(PyObjectType):
self
.
vtabptr_cname
=
None
self
.
vtabptr_cname
=
None
self
.
vtable_cname
=
None
self
.
vtable_cname
=
None
self
.
is_external
=
is_external
self
.
is_external
=
is_external
self
.
check_size
=
check_size
self
.
defered_declarations
=
[]
self
.
defered_declarations
=
[]
def
set_scope
(
self
,
scope
):
def
set_scope
(
self
,
scope
):
...
...
Cython/Compiler/Symtab.py
View file @
14387ec8
...
@@ -1475,7 +1475,8 @@ class ModuleScope(Scope):
...
@@ -1475,7 +1475,8 @@ class ModuleScope(Scope):
def
declare_c_class
(
self
,
name
,
pos
,
defining
=
0
,
implementing
=
0
,
def
declare_c_class
(
self
,
name
,
pos
,
defining
=
0
,
implementing
=
0
,
module_name
=
None
,
base_type
=
None
,
objstruct_cname
=
None
,
module_name
=
None
,
base_type
=
None
,
objstruct_cname
=
None
,
typeobj_cname
=
None
,
typeptr_cname
=
None
,
visibility
=
'private'
,
typedef_flag
=
0
,
api
=
0
,
typeobj_cname
=
None
,
typeptr_cname
=
None
,
visibility
=
'private'
,
typedef_flag
=
0
,
api
=
0
,
check_size
=
b'min'
,
buffer_defaults
=
None
,
shadow
=
0
):
buffer_defaults
=
None
,
shadow
=
0
):
# If this is a non-extern typedef class, expose the typedef, but use
# If this is a non-extern typedef class, expose the typedef, but use
# the non-typedef struct internally to avoid needing forward
# the non-typedef struct internally to avoid needing forward
...
@@ -1508,7 +1509,8 @@ class ModuleScope(Scope):
...
@@ -1508,7 +1509,8 @@ class ModuleScope(Scope):
# Make a new entry if needed
# Make a new entry if needed
#
#
if
not
entry
or
shadow
:
if
not
entry
or
shadow
:
type
=
PyrexTypes
.
PyExtensionType
(
name
,
typedef_flag
,
base_type
,
visibility
==
'extern'
)
type
=
PyrexTypes
.
PyExtensionType
(
name
,
typedef_flag
,
base_type
,
visibility
==
'extern'
,
check_size
=
check_size
)
type
.
pos
=
pos
type
.
pos
=
pos
type
.
buffer_defaults
=
buffer_defaults
type
.
buffer_defaults
=
buffer_defaults
if
objtypedef_cname
is
not
None
:
if
objtypedef_cname
is
not
None
:
...
...
Cython/Utility/ImportExport.c
View file @
14387ec8
...
@@ -308,14 +308,21 @@ set_path:
...
@@ -308,14 +308,21 @@ set_path:
/////////////// TypeImport.proto ///////////////
/////////////// TypeImport.proto ///////////////
static
PyTypeObject
*
__Pyx_ImportType
(
PyObject
*
module
,
const
char
*
module_name
,
const
char
*
class_name
,
size_t
size
,
int
strict
);
/*proto*/
typedef
enum
{
/* What to do if tp_basicsize is different from size? */
__PYX_CHECKSIZE_STRICT
,
/* Error */
__PYX_CHECKSIZE_MIN
,
/* Error if tp_basicsize is smaller, warn if larger */
__PYX_CHECKSIZE_LOOSE
,
/* Error if tp_basicsize is smaller */
}
__pyx_CheckSizeState
;
static
PyTypeObject
*
__Pyx_ImportType
(
PyObject
*
module
,
const
char
*
module_name
,
const
char
*
class_name
,
size_t
size
,
__pyx_CheckSizeState
check_size
);
/*proto*/
/////////////// TypeImport ///////////////
/////////////// TypeImport ///////////////
#ifndef __PYX_HAVE_RT_ImportType
#ifndef __PYX_HAVE_RT_ImportType
#define __PYX_HAVE_RT_ImportType
#define __PYX_HAVE_RT_ImportType
static
PyTypeObject
*
__Pyx_ImportType
(
PyObject
*
module
,
const
char
*
module_name
,
const
char
*
class_name
,
static
PyTypeObject
*
__Pyx_ImportType
(
PyObject
*
module
,
const
char
*
module_name
,
const
char
*
class_name
,
size_t
size
,
int
strict
)
size_t
size
,
__pyx_CheckSizeState
check_size
)
{
{
PyObject
*
result
=
0
;
PyObject
*
result
=
0
;
char
warning
[
200
];
char
warning
[
200
];
...
@@ -345,18 +352,28 @@ static PyTypeObject *__Pyx_ImportType(PyObject *module, const char *module_name,
...
@@ -345,18 +352,28 @@ static PyTypeObject *__Pyx_ImportType(PyObject *module, const char *module_name,
if
(
basicsize
==
(
Py_ssize_t
)
-
1
&&
PyErr_Occurred
())
if
(
basicsize
==
(
Py_ssize_t
)
-
1
&&
PyErr_Occurred
())
goto
bad
;
goto
bad
;
#endif
#endif
if
(
!
strict
&&
(
size_t
)
basicsize
>
size
)
{
if
((
size_t
)
basicsize
<
size
)
{
PyOS_snprintf
(
warning
,
sizeof
(
warning
),
PyErr_Format
(
PyExc_ValueError
,
"%s.%s size changed, may indicate binary incompatibility. Expected %zd, got %zd"
,
"%.200s.%.200s size changed, may indicate binary incompatibility. "
module_name
,
class_name
,
basicsize
,
size
);
"Expected %zd from C header, got %zd from PyObject"
,
if
(
PyErr_WarnEx
(
NULL
,
warning
,
0
)
<
0
)
goto
bad
;
module_name
,
class_name
,
size
,
basicsize
);
goto
bad
;
}
}
else
if
(
(
size_t
)
basicsize
!=
size
)
{
if
(
check_size
==
__PYX_CHECKSIZE_STRICT
&&
(
size_t
)
basicsize
!=
size
)
{
PyErr_Format
(
PyExc_ValueError
,
PyErr_Format
(
PyExc_ValueError
,
"%.200s.%.200s has the wrong size, try recompiling. Expected %zd, got %zd"
,
"%.200s.%.200s size changed, may indicate binary incompatibility. "
module_name
,
class_name
,
basicsize
,
size
);
"Expected %zd from C header, got %zd from PyObject"
,
module_name
,
class_name
,
size
,
basicsize
);
goto
bad
;
goto
bad
;
}
}
else
if
(
check_size
==
__PYX_CHECKSIZE_MIN
&&
(
size_t
)
basicsize
>
size
)
{
PyOS_snprintf
(
warning
,
sizeof
(
warning
),
"%s.%s size changed, may indicate binary incompatibility. "
"Expected %zd from C header, got %zd from PyObject"
,
module_name
,
class_name
,
size
,
basicsize
);
if
(
PyErr_WarnEx
(
NULL
,
warning
,
0
)
<
0
)
goto
bad
;
}
/* __PYX_CHECKSIZE_LOOSE does not warn nor error */
return
(
PyTypeObject
*
)
result
;
return
(
PyTypeObject
*
)
result
;
bad:
bad:
Py_XDECREF
(
result
);
Py_XDECREF
(
result
);
...
...
tests/run/check_size.srctree
View file @
14387ec8
...
@@ -11,6 +11,12 @@ setup(ext_modules= cythonize("check_size.pyx"))
...
@@ -11,6 +11,12 @@ setup(ext_modules= cythonize("check_size.pyx"))
setup(ext_modules = cythonize("_check_size*.pyx"))
setup(ext_modules = cythonize("_check_size*.pyx"))
try:
setup(ext_modules= cythonize("check_size6.pyx"))
assert False
except AttributeError as e:
assert 'max' in str(e)
######## check_size_nominal.h ########
######## check_size_nominal.h ########
#include <Python.h>
#include <Python.h>
...
@@ -116,6 +122,58 @@ cdef extern from "check_size_smaller.h":
...
@@ -116,6 +122,58 @@ cdef extern from "check_size_smaller.h":
int f9
int f9
cpdef public int testme(Foo f) except -1:
return f.f9
######## _check_size3.pyx ########
cdef extern from "check_size_smaller.h":
# make sure missing check_size is equivalent to 'min'
ctypedef class check_size.Foo [object FooStructSmall, check_size 'min']:
cdef:
int f9
cpdef public int testme(Foo f) except -1:
return f.f9
######## _check_size4.pyx ########
cdef extern from "check_size_smaller.h":
# Disable size check
ctypedef class check_size.Foo [object FooStructSmall, check_size False]:
cdef:
int f9
cpdef public int testme(Foo f) except -1:
return f.f9
######## _check_size5.pyx ########
cdef extern from "check_size_smaller.h":
# Strict checking, will raise an error
ctypedef class check_size.Foo [object FooStructSmall, check_size True]:
cdef:
int f9
cpdef public int testme(Foo f) except -1:
return f.f9
######## check_size6.pyx ########
cdef extern from "check_size_smaller.h":
# Raise AttributeError when using bad value
ctypedef class check_size.Foo [object FooStructSmall, check_size 'max']:
cdef:
int f9
cpdef public int testme(Foo f) except -1:
cpdef public int testme(Foo f) except -1:
return f.f9
return f.f9
...
@@ -137,19 +195,36 @@ try:
...
@@ -137,19 +195,36 @@ try:
import _check_size1
import _check_size1
assert False
assert False
except ValueError as e:
except ValueError as e:
assert str(e).startswith('check_size.Foo
has the wrong size, try recompiling
')
assert str(e).startswith('check_size.Foo
size changed
')
# Warining since check_size.Foo's tp_basicsize is larger than what is needed
# Warining since check_size.Foo's tp_basicsize is larger than what is needed
# for FooStructSmall. There is "spare", accessing FooStructSmall's fields will
# for FooStructSmall. There is "spare", accessing FooStructSmall's fields will
# never access invalid memory. This can happen, for instance, when using old
# never access invalid memory. This can happen, for instance, when using old
# headers with a newer runtime, or when using an old _check_size
2
with a newer
# headers with a newer runtime, or when using an old _check_size
{2,3}
with a newer
# check_size, where the developers of check_size are careful to be backward
# check_size, where the developers of check_size are careful to be backward
# compatible.
# compatible.
with warnings.catch_warnings(record=True) as w:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
import _check_size2
import _check_size2
assert len(w) == 1, 'expected one warning, got %d' % len(w)
import _check_size3
assert str(w[-1].message).startswith('check_size.Foo size changed')
assert len(w) == 2, 'expected two warnings, got %d' % len(w)
assert str(w[0].message).startswith('check_size.Foo size changed')
assert str(w[1].message).startswith('check_size.Foo size changed')
ret = _check_size2.testme(foo)
ret = _check_size2.testme(foo)
assert ret == 23
assert ret == 23
ret = _check_size3.testme(foo)
assert ret == 23
with warnings.catch_warnings(record=True) as w:
# No warning, runtime vendor must provide backward compatibility
import _check_size4
assert len(w) == 0
try:
# Enforce strict checking
import _check_size5
assert False
except ValueError as e:
assert str(e).startswith('check_size.Foo size changed')
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