Commit 6ed229d1 authored by Robert Bradshaw's avatar Robert Bradshaw

stupid merge

parents f4064312 00dd94db
...@@ -242,9 +242,7 @@ def put_acquire_arg_buffer(entry, code, pos): ...@@ -242,9 +242,7 @@ def put_acquire_arg_buffer(entry, code, pos):
# entry.buffer_aux.buffer_info_var.cname)) # entry.buffer_aux.buffer_info_var.cname))
def get_release_buffer_code(entry): def get_release_buffer_code(entry):
return "__Pyx_SafeReleaseBuffer((PyObject*)%s, &%s)" % ( return "__Pyx_SafeReleaseBuffer(&%s)" % entry.buffer_aux.buffer_info_var.cname
entry.cname,
entry.buffer_aux.buffer_info_var.cname)
def put_assign_to_buffer(lhs_cname, rhs_cname, buffer_aux, buffer_type, def put_assign_to_buffer(lhs_cname, rhs_cname, buffer_aux, buffer_type,
is_initialized, pos, code): is_initialized, pos, code):
...@@ -274,8 +272,7 @@ def put_assign_to_buffer(lhs_cname, rhs_cname, buffer_aux, buffer_type, ...@@ -274,8 +272,7 @@ def put_assign_to_buffer(lhs_cname, rhs_cname, buffer_aux, buffer_type,
if is_initialized: if is_initialized:
# Release any existing buffer # Release any existing buffer
code.putln('__Pyx_SafeReleaseBuffer((PyObject*)%s, &%s);' % ( code.putln('__Pyx_SafeReleaseBuffer(&%s);' % bufstruct)
lhs_cname, bufstruct))
# Acquire # Acquire
retcode_cname = code.funcstate.allocate_temp(PyrexTypes.c_int_type) retcode_cname = code.funcstate.allocate_temp(PyrexTypes.c_int_type)
code.putln("%s = %s;" % (retcode_cname, getbuffer % rhs_cname)) code.putln("%s = %s;" % (retcode_cname, getbuffer % rhs_cname))
...@@ -537,12 +534,11 @@ def get_getbuffer_code(dtype, code): ...@@ -537,12 +534,11 @@ def get_getbuffer_code(dtype, code):
} }
ts = buf->format; ts = buf->format;
ts = __Pyx_ConsumeWhitespace(ts); ts = __Pyx_ConsumeWhitespace(ts);
ts = __Pyx_BufferTypestringCheckEndian(ts);
if (!ts) goto fail; if (!ts) goto fail;
ts = __Pyx_ConsumeWhitespace(ts);
ts = %(itemchecker)s(ts); ts = %(itemchecker)s(ts);
if (!ts) goto fail; if (!ts) goto fail;
ts = __Pyx_ConsumeWhitespace(ts); ts = __Pyx_ConsumeWhitespace(ts);
if (!ts) goto fail;
if (*ts != 0) { if (*ts != 0) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"Expected non-struct buffer data type (expected end, got '%%s')", ts); "Expected non-struct buffer data type (expected end, got '%%s')", ts);
...@@ -569,6 +565,9 @@ def buffer_type_checker(dtype, code): ...@@ -569,6 +565,9 @@ def buffer_type_checker(dtype, code):
return funcname return funcname
def use_py2_buffer_functions(env): def use_py2_buffer_functions(env):
# Emulation of PyObject_GetBuffer and PyBuffer_Release for Python 2.
# For >= 2.6 we do double mode -- use the new buffer interface on objects
# which has the right tp_flags set, but emulation otherwise.
codename = "PyObject_GetBuffer" # just a representative unique key codename = "PyObject_GetBuffer" # just a representative unique key
# Search all types for __getbuffer__ overloads # Search all types for __getbuffer__ overloads
...@@ -589,8 +588,12 @@ def use_py2_buffer_functions(env): ...@@ -589,8 +588,12 @@ def use_py2_buffer_functions(env):
find_buffer_types(env) find_buffer_types(env)
code = dedent(""" code = dedent("""
#if (PY_MAJOR_VERSION < 3) && !(Py_TPFLAGS_DEFAULT & Py_TPFLAGS_HAVE_NEWBUFFER) #if PY_MAJOR_VERSION < 3
static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags) { static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags) {
#if PY_VERSION_HEX >= 0x02060000
if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)
return PyObject_GetBuffer(obj, view, flags);
#endif
""") """)
if len(types) > 0: if len(types) > 0:
clause = "if" clause = "if"
...@@ -606,7 +609,9 @@ def use_py2_buffer_functions(env): ...@@ -606,7 +609,9 @@ def use_py2_buffer_functions(env):
code += dedent(""" code += dedent("""
} }
static void __Pyx_ReleaseBuffer(PyObject *obj, Py_buffer *view) { static void __Pyx_ReleaseBuffer(Py_buffer *view) {
PyObject* obj = view->obj;
if (obj) {
""") """)
if len(types) > 0: if len(types) > 0:
clause = "if" clause = "if"
...@@ -615,18 +620,21 @@ def use_py2_buffer_functions(env): ...@@ -615,18 +620,21 @@ def use_py2_buffer_functions(env):
code += "%s (PyObject_TypeCheck(obj, %s)) %s(obj, view);" % (clause, t, release) code += "%s (PyObject_TypeCheck(obj, %s)) %s(obj, view);" % (clause, t, release)
clause = "else if" clause = "else if"
code += dedent(""" code += dedent("""
Py_DECREF(obj);
view->obj = NULL;
}
} }
#endif #endif
""") """)
env.use_utility_code([dedent("""\ env.use_utility_code([dedent("""\
#if (PY_MAJOR_VERSION < 3) && !(Py_TPFLAGS_DEFAULT & Py_TPFLAGS_HAVE_NEWBUFFER) #if PY_MAJOR_VERSION < 3
static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags); static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags);
static void __Pyx_ReleaseBuffer(PyObject *obj, Py_buffer *view); static void __Pyx_ReleaseBuffer(Py_buffer *view);
#else #else
#define __Pyx_GetBuffer PyObject_GetBuffer #define __Pyx_GetBuffer PyObject_GetBuffer
#define __Pyx_ReleaseBuffer PyObject_ReleaseBuffer #define __Pyx_ReleaseBuffer PyBuffer_Release
#endif #endif
"""), code], codename) """), code], codename)
...@@ -655,20 +663,20 @@ static void __Pyx_RaiseBufferIndexError(int axis) { ...@@ -655,20 +663,20 @@ static void __Pyx_RaiseBufferIndexError(int axis) {
# exporter. # exporter.
# #
acquire_utility_code = ["""\ acquire_utility_code = ["""\
static INLINE void __Pyx_SafeReleaseBuffer(PyObject* obj, Py_buffer* info); static INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info);
static INLINE void __Pyx_ZeroBuffer(Py_buffer* buf); /*proto*/ static INLINE void __Pyx_ZeroBuffer(Py_buffer* buf); /*proto*/
static INLINE const char* __Pyx_ConsumeWhitespace(const char* ts); /*proto*/ static INLINE const char* __Pyx_ConsumeWhitespace(const char* ts); /*proto*/
static INLINE const char* __Pyx_BufferTypestringCheckEndian(const char* ts); /*proto*/
static void __Pyx_BufferNdimError(Py_buffer* buffer, int expected_ndim); /*proto*/ static void __Pyx_BufferNdimError(Py_buffer* buffer, int expected_ndim); /*proto*/
""", """ """, """
static INLINE void __Pyx_SafeReleaseBuffer(PyObject* obj, Py_buffer* info) { static INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info) {
if (info->buf == NULL) return; if (info->buf == NULL) return;
if (info->suboffsets == __Pyx_minusones) info->suboffsets = NULL; if (info->suboffsets == __Pyx_minusones) info->suboffsets = NULL;
__Pyx_ReleaseBuffer(obj, info); __Pyx_ReleaseBuffer(info);
} }
static INLINE void __Pyx_ZeroBuffer(Py_buffer* buf) { static INLINE void __Pyx_ZeroBuffer(Py_buffer* buf) {
buf->buf = NULL; buf->buf = NULL;
buf->obj = NULL;
buf->strides = __Pyx_zeros; buf->strides = __Pyx_zeros;
buf->shape = __Pyx_zeros; buf->shape = __Pyx_zeros;
buf->suboffsets = __Pyx_minusones; buf->suboffsets = __Pyx_minusones;
...@@ -677,41 +685,24 @@ static INLINE void __Pyx_ZeroBuffer(Py_buffer* buf) { ...@@ -677,41 +685,24 @@ static INLINE void __Pyx_ZeroBuffer(Py_buffer* buf) {
static INLINE const char* __Pyx_ConsumeWhitespace(const char* ts) { static INLINE const char* __Pyx_ConsumeWhitespace(const char* ts) {
while (1) { while (1) {
switch (*ts) { switch (*ts) {
case '@':
case 10: case 10:
case 13: case 13:
case ' ': case ' ':
++ts; ++ts;
break;
case '=':
case '<':
case '>':
case '!':
PyErr_SetString(PyExc_ValueError, "Buffer acquisition error: Only native byte order, size and alignment supported.");
return NULL;
default: default:
return ts; return ts;
} }
} }
} }
static INLINE const char* __Pyx_BufferTypestringCheckEndian(const char* ts) {
int num = 1;
int little_endian = ((char*)&num)[0];
int ok = 1;
switch (*ts) {
case '@':
case '=':
++ts; break;
case '<':
if (little_endian) ++ts;
else ok = 0;
break;
case '>':
case '!':
if (!little_endian) ++ts;
else ok = 0;
break;
}
if (!ok) {
PyErr_Format(PyExc_ValueError, "Buffer has wrong endianness (rejecting on '%s')", ts);
return NULL;
}
return ts;
}
static void __Pyx_BufferNdimError(Py_buffer* buffer, int expected_ndim) { static void __Pyx_BufferNdimError(Py_buffer* buffer, int expected_ndim) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"Buffer has wrong number of dimensions (expected %d, got %d)", "Buffer has wrong number of dimensions (expected %d, got %d)",
......
...@@ -104,14 +104,15 @@ builtin_types_table = [ ...@@ -104,14 +104,15 @@ builtin_types_table = [
builtin_structs_table = [ builtin_structs_table = [
('Py_buffer', 'Py_buffer', ('Py_buffer', 'Py_buffer',
[("buf", PyrexTypes.c_void_ptr_type), [("buf", PyrexTypes.c_void_ptr_type),
("obj", PyrexTypes.py_object_type),
("len", PyrexTypes.c_py_ssize_t_type), ("len", PyrexTypes.c_py_ssize_t_type),
("itemsize", PyrexTypes.c_py_ssize_t_type),
("readonly", PyrexTypes.c_bint_type), ("readonly", PyrexTypes.c_bint_type),
("format", PyrexTypes.c_char_ptr_type),
("ndim", PyrexTypes.c_int_type), ("ndim", PyrexTypes.c_int_type),
("format", PyrexTypes.c_char_ptr_type),
("shape", PyrexTypes.c_py_ssize_t_ptr_type), ("shape", PyrexTypes.c_py_ssize_t_ptr_type),
("strides", PyrexTypes.c_py_ssize_t_ptr_type), ("strides", PyrexTypes.c_py_ssize_t_ptr_type),
("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type), ("suboffsets", PyrexTypes.c_py_ssize_t_ptr_type),
("itemsize", PyrexTypes.c_py_ssize_t_type),
("internal", PyrexTypes.c_void_ptr_type), ("internal", PyrexTypes.c_void_ptr_type),
]) ])
] ]
......
...@@ -428,14 +428,15 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -428,14 +428,15 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln("") code.putln("")
code.putln(" typedef struct {") code.putln(" typedef struct {")
code.putln(" void *buf;") code.putln(" void *buf;")
code.putln(" PyObject *obj;")
code.putln(" Py_ssize_t len;") code.putln(" Py_ssize_t len;")
code.putln(" Py_ssize_t itemsize;")
code.putln(" int readonly;") code.putln(" int readonly;")
code.putln(" const char *format;")
code.putln(" int ndim;") code.putln(" int ndim;")
code.putln(" char *format;")
code.putln(" Py_ssize_t *shape;") code.putln(" Py_ssize_t *shape;")
code.putln(" Py_ssize_t *strides;") code.putln(" Py_ssize_t *strides;")
code.putln(" Py_ssize_t *suboffsets;") code.putln(" Py_ssize_t *suboffsets;")
code.putln(" Py_ssize_t itemsize;")
code.putln(" void *internal;") code.putln(" void *internal;")
code.putln(" } Py_buffer;") code.putln(" } Py_buffer;")
code.putln("") code.putln("")
...@@ -459,6 +460,10 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -459,6 +460,10 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln(" #define Py_TPFLAGS_HAVE_INDEX 0") code.putln(" #define Py_TPFLAGS_HAVE_INDEX 0")
code.putln("#endif") code.putln("#endif")
code.putln("#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)")
code.putln(" #define Py_TPFLAGS_HAVE_NEWBUFFER 0")
code.putln("#endif")
code.putln("#if PY_MAJOR_VERSION >= 3") code.putln("#if PY_MAJOR_VERSION >= 3")
code.putln(" #define PyBaseString_Type PyUnicode_Type") code.putln(" #define PyBaseString_Type PyUnicode_Type")
code.putln(" #define PyString_Type PyBytes_Type") code.putln(" #define PyString_Type PyBytes_Type")
...@@ -757,10 +762,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -757,10 +762,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
dll_linkage = dll_linkage) dll_linkage = dll_linkage)
if entry.visibility == 'private': if entry.visibility == 'private':
storage_class = "static " storage_class = "static "
elif entry.visibility == 'extern':
storage_class = "%s " % Naming.extern_c_macro
else: else:
storage_class = "" storage_class = "%s " % Naming.extern_c_macro
code.putln("%s%s; /*proto*/" % ( code.putln("%s%s; /*proto*/" % (
storage_class, storage_class,
header)) header))
...@@ -868,7 +871,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -868,7 +871,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
else: else:
code.put_init_var_to_py_none(entry, "p->%s") code.put_init_var_to_py_none(entry, "p->%s")
entry = scope.lookup_here("__new__") entry = scope.lookup_here("__new__")
if entry: if entry and entry.is_special:
if entry.trivial_signature: if entry.trivial_signature:
cinit_args = "o, %s, NULL" % Naming.empty_tuple cinit_args = "o, %s, NULL" % Naming.empty_tuple
else: else:
...@@ -1954,11 +1957,11 @@ builtin_module_name_utility_code = [ ...@@ -1954,11 +1957,11 @@ builtin_module_name_utility_code = [
import_module_utility_code = [ import_module_utility_code = [
""" """
static PyObject *__Pyx_ImportModule(char *name); /*proto*/ static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
""",""" ""","""
#ifndef __PYX_HAVE_RT_ImportModule #ifndef __PYX_HAVE_RT_ImportModule
#define __PYX_HAVE_RT_ImportModule #define __PYX_HAVE_RT_ImportModule
static PyObject *__Pyx_ImportModule(char *name) { static PyObject *__Pyx_ImportModule(const char *name) {
PyObject *py_name = 0; PyObject *py_name = 0;
PyObject *py_module = 0; PyObject *py_module = 0;
...@@ -1983,29 +1986,32 @@ bad: ...@@ -1983,29 +1986,32 @@ bad:
type_import_utility_code = [ type_import_utility_code = [
""" """
static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, long size); /*proto*/ static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, long size); /*proto*/
""",""" ""","""
#ifndef __PYX_HAVE_RT_ImportType #ifndef __PYX_HAVE_RT_ImportType
#define __PYX_HAVE_RT_ImportType #define __PYX_HAVE_RT_ImportType
static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
long size) long size)
{ {
PyObject *py_module = 0; PyObject *py_module = 0;
PyObject *result = 0; PyObject *result = 0;
PyObject *py_name = 0; PyObject *py_name = 0;
py_module = __Pyx_ImportModule(module_name);
if (!py_module)
goto bad;
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
py_name = PyString_FromString(module_name); py_name = PyString_FromString(class_name);
#else #else
py_name = PyUnicode_FromString(module_name); py_name = PyUnicode_FromString(class_name);
#endif #endif
if (!py_name) if (!py_name)
goto bad; goto bad;
result = PyObject_GetAttr(py_module, py_name);
py_module = __Pyx_ImportModule(module_name); Py_DECREF(py_name);
if (!py_module) py_name = 0;
goto bad; Py_DECREF(py_module);
result = PyObject_GetAttrString(py_module, class_name); py_module = 0;
if (!result) if (!result)
goto bad; goto bad;
if (!PyType_Check(result)) { if (!PyType_Check(result)) {
...@@ -2022,7 +2028,7 @@ static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, ...@@ -2022,7 +2028,7 @@ static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name,
} }
return (PyTypeObject *)result; return (PyTypeObject *)result;
bad: bad:
Py_XDECREF(py_name); Py_XDECREF(py_module);
Py_XDECREF(result); Py_XDECREF(result);
return 0; return 0;
} }
......
...@@ -44,7 +44,7 @@ vtabstruct_prefix = pyrex_prefix + "vtabstruct_" ...@@ -44,7 +44,7 @@ vtabstruct_prefix = pyrex_prefix + "vtabstruct_"
opt_arg_prefix = pyrex_prefix + "opt_args_" opt_arg_prefix = pyrex_prefix + "opt_args_"
args_cname = pyrex_prefix + "args" args_cname = pyrex_prefix + "args"
kwdlist_cname = pyrex_prefix + "argnames" pykwdlist_cname = pyrex_prefix + "pyargnames"
obj_base_cname = pyrex_prefix + "base" obj_base_cname = pyrex_prefix + "base"
builtins_cname = pyrex_prefix + "b" builtins_cname = pyrex_prefix + "b"
preimport_cname = pyrex_prefix + "i" preimport_cname = pyrex_prefix + "i"
...@@ -95,6 +95,10 @@ exc_lineno_name = pyrex_prefix + "exc_lineno" ...@@ -95,6 +95,10 @@ exc_lineno_name = pyrex_prefix + "exc_lineno"
exc_vars = (exc_type_name, exc_value_name, exc_tb_name) exc_vars = (exc_type_name, exc_value_name, exc_tb_name)
exc_save_vars = (pyrex_prefix + 'save_exc_type',
pyrex_prefix + 'save_exc_value',
pyrex_prefix + 'save_exc_tb')
api_name = pyrex_prefix + "capi__" api_name = pyrex_prefix + "capi__"
h_guard_prefix = "__PYX_HAVE__" h_guard_prefix = "__PYX_HAVE__"
......
This diff is collapsed.
...@@ -56,9 +56,8 @@ class SwitchTransform(Visitor.VisitorTransform): ...@@ -56,9 +56,8 @@ class SwitchTransform(Visitor.VisitorTransform):
def visit_IfStatNode(self, node): def visit_IfStatNode(self, node):
self.visitchildren(node) self.visitchildren(node)
if len(node.if_clauses) < 3:
return node
common_var = None common_var = None
case_count = 0
cases = [] cases = []
for if_clause in node.if_clauses: for if_clause in node.if_clauses:
var, conditions = self.extract_conditions(if_clause.condition) var, conditions = self.extract_conditions(if_clause.condition)
...@@ -70,9 +69,12 @@ class SwitchTransform(Visitor.VisitorTransform): ...@@ -70,9 +69,12 @@ class SwitchTransform(Visitor.VisitorTransform):
return node return node
else: else:
common_var = var common_var = var
case_count += len(conditions)
cases.append(Nodes.SwitchCaseNode(pos = if_clause.pos, cases.append(Nodes.SwitchCaseNode(pos = if_clause.pos,
conditions = conditions, conditions = conditions,
body = if_clause.body)) body = if_clause.body))
if case_count < 2:
return node
common_var = unwrap_node(common_var) common_var = unwrap_node(common_var)
return Nodes.SwitchStatNode(pos = node.pos, return Nodes.SwitchStatNode(pos = node.pos,
......
...@@ -297,32 +297,37 @@ def p_call(s, function): ...@@ -297,32 +297,37 @@ def p_call(s, function):
keyword_args = [] keyword_args = []
star_arg = None star_arg = None
starstar_arg = None starstar_arg = None
while s.sy not in ('*', '**', ')'): while s.sy not in ('**', ')'):
arg = p_simple_expr(s) if s.sy == '*':
if s.sy == '=': if star_arg:
s.error("only one star-arg parameter allowed",
pos = s.position())
s.next() s.next()
if not arg.is_name: star_arg = p_simple_expr(s)
s.error("Expected an identifier before '='",
pos = arg.pos)
encoded_name = EncodedString(arg.name)
keyword = ExprNodes.IdentifierStringNode(arg.pos,
value = encoded_name)
arg = p_simple_expr(s)
keyword_args.append((keyword, arg))
else: else:
if keyword_args: arg = p_simple_expr(s)
s.error("Non-keyword arg following keyword arg", if s.sy == '=':
pos = arg.pos) s.next()
positional_args.append(arg) if not arg.is_name:
s.error("Expected an identifier before '='",
pos = arg.pos)
encoded_name = EncodedString(arg.name)
keyword = ExprNodes.IdentifierStringNode(arg.pos,
value = encoded_name)
arg = p_simple_expr(s)
keyword_args.append((keyword, arg))
else:
if keyword_args:
s.error("Non-keyword arg following keyword arg",
pos = arg.pos)
if star_arg:
s.error("Non-keyword arg following star-arg",
pos = arg.pos)
positional_args.append(arg)
if s.sy != ',': if s.sy != ',':
break break
s.next() s.next()
if s.sy == '*':
s.next()
star_arg = p_simple_expr(s)
if s.sy == ',':
s.next()
if s.sy == '**': if s.sy == '**':
s.next() s.next()
starstar_arg = p_simple_expr(s) starstar_arg = p_simple_expr(s)
...@@ -1738,7 +1743,7 @@ def p_c_declarator(s, ctx = Ctx(), empty = 0, is_type = 0, cmethod_flag = 0, ...@@ -1738,7 +1743,7 @@ def p_c_declarator(s, ctx = Ctx(), empty = 0, is_type = 0, cmethod_flag = 0,
if s.sy == '(': if s.sy == '(':
s.next() s.next()
if s.sy == ')' or looking_at_type(s): if s.sy == ')' or looking_at_type(s):
base = Nodes.CNameDeclaratorNode(pos, name = "", cname = None) base = Nodes.CNameDeclaratorNode(pos, name = EncodedString(u""), cname = None)
result = p_c_func_declarator(s, pos, ctx, base, cmethod_flag) result = p_c_func_declarator(s, pos, ctx, base, cmethod_flag)
else: else:
result = p_c_declarator(s, ctx, empty = empty, is_type = is_type, result = p_c_declarator(s, ctx, empty = empty, is_type = is_type,
...@@ -1808,7 +1813,7 @@ def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag, ...@@ -1808,7 +1813,7 @@ def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
else: else:
rhs = None rhs = None
if s.sy == 'IDENT': if s.sy == 'IDENT':
name = s.systring name = EncodedString(s.systring)
if is_type: if is_type:
s.add_type_name(name) s.add_type_name(name)
if empty: if empty:
...@@ -2171,7 +2176,7 @@ def p_def_statement(s, decorators=None): ...@@ -2171,7 +2176,7 @@ def p_def_statement(s, decorators=None):
# s.sy == 'def' # s.sy == 'def'
pos = s.position() pos = s.position()
s.next() s.next()
name = p_ident(s) name = EncodedString( p_ident(s) )
#args = [] #args = []
s.expect('('); s.expect('(');
args = p_c_arg_list(s, in_pyfunc = 1, nonempty_declarators = 1) args = p_c_arg_list(s, in_pyfunc = 1, nonempty_declarators = 1)
......
...@@ -127,13 +127,15 @@ class SlotDescriptor: ...@@ -127,13 +127,15 @@ class SlotDescriptor:
# flag Py_TPFLAGS_XXX value indicating presence of slot # flag Py_TPFLAGS_XXX value indicating presence of slot
# py3k Indicates presence of slot in Python 3 # py3k Indicates presence of slot in Python 3
# py2 Indicates presence of slot in Python 2 # py2 Indicates presence of slot in Python 2
# ifdef Full #ifdef string that slot is wrapped in. Using this causes py3k, py2 and flags to be ignored.)
def __init__(self, slot_name, dynamic = 0, flag = None, py3k = True, py2 = True): def __init__(self, slot_name, dynamic = 0, flag = None, py3k = True, py2 = True, ifdef = None):
self.slot_name = slot_name self.slot_name = slot_name
self.is_initialised_dynamically = dynamic self.is_initialised_dynamically = dynamic
self.flag = flag self.flag = flag
self.py3k = py3k self.py3k = py3k
self.py2 = py2 self.py2 = py2
self.ifdef = ifdef
def generate(self, scope, code): def generate(self, scope, code):
if self.is_initialised_dynamically: if self.is_initialised_dynamically:
...@@ -143,16 +145,17 @@ class SlotDescriptor: ...@@ -143,16 +145,17 @@ class SlotDescriptor:
flag = self.flag flag = self.flag
py3k = self.py3k py3k = self.py3k
py2 = self.py2 py2 = self.py2
if not py3k: if self.ifdef:
code.putln("#if PY_MAJOR_VERSION < 3") code.putln("#if %s" % self.ifdef)
elif not py2: else:
code.putln("#if PY_MAJOR_VERSION >= 3") if not py3k:
if flag: code.putln("#if PY_MAJOR_VERSION < 3")
code.putln("#if (PY_MAJOR_VERSION >= 3) || (Py_TPFLAGS_DEFAULT & %s)" % flag) elif not py2:
code.putln("#if PY_MAJOR_VERSION >= 3")
if flag:
code.putln("#if (PY_MAJOR_VERSION >= 3) || (Py_TPFLAGS_DEFAULT & %s)" % flag)
code.putln("%s, /*%s*/" % (value, self.slot_name)) code.putln("%s, /*%s*/" % (value, self.slot_name))
if flag: if flag or (not py3k or not py2) or self.ifdef:
code.putln("#endif")
if not py3k or not py2:
code.putln("#endif") code.putln("#endif")
# Some C implementations have trouble statically # Some C implementations have trouble statically
...@@ -199,8 +202,8 @@ class MethodSlot(SlotDescriptor): ...@@ -199,8 +202,8 @@ class MethodSlot(SlotDescriptor):
# method_name string The __xxx__ name of the method # method_name string The __xxx__ name of the method
# default string or None Default value of the slot # default string or None Default value of the slot
def __init__(self, signature, slot_name, method_name, default = None, flag = None, py3k=True, py2=True): def __init__(self, signature, slot_name, method_name, default = None, flag = None, py3k=True, py2=True, ifdef=None):
SlotDescriptor.__init__(self, slot_name, flag = flag, py3k = py3k, py2=py2) SlotDescriptor.__init__(self, slot_name, flag = flag, py3k = py3k, py2=py2, ifdef=ifdef)
self.signature = signature self.signature = signature
self.slot_name = slot_name self.slot_name = slot_name
self.method_name = method_name self.method_name = method_name
...@@ -296,7 +299,7 @@ class TypeFlagsSlot(SlotDescriptor): ...@@ -296,7 +299,7 @@ class TypeFlagsSlot(SlotDescriptor):
# Descriptor for the type flags slot. # Descriptor for the type flags slot.
def slot_code(self, scope): def slot_code(self, scope):
value = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_BASETYPE" value = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_NEWBUFFER"
if scope.needs_gc(): if scope.needs_gc():
value += "|Py_TPFLAGS_HAVE_GC" value += "|Py_TPFLAGS_HAVE_GC"
return value return value
...@@ -609,8 +612,8 @@ PyBufferProcs = ( ...@@ -609,8 +612,8 @@ PyBufferProcs = (
MethodSlot(getsegcountproc, "bf_getsegcount", "__getsegcount__", py3k = False), MethodSlot(getsegcountproc, "bf_getsegcount", "__getsegcount__", py3k = False),
MethodSlot(getcharbufferproc, "bf_getcharbuffer", "__getcharbuffer__", py3k = False), MethodSlot(getcharbufferproc, "bf_getcharbuffer", "__getcharbuffer__", py3k = False),
MethodSlot(getbufferproc, "bf_getbuffer", "__getbuffer__", flag = "Py_TPFLAGS_HAVE_NEWBUFFER"), MethodSlot(getbufferproc, "bf_getbuffer", "__getbuffer__", ifdef = "PY_VERSION_HEX >= 0x02060000"),
MethodSlot(releasebufferproc, "bf_releasebuffer", "__releasebuffer__", flag = "Py_TPFLAGS_HAVE_NEWBUFFER"), MethodSlot(releasebufferproc, "bf_releasebuffer", "__releasebuffer__", ifdef = "PY_VERSION_HEX >= 0x02060000")
) )
#------------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------------
......
...@@ -43,6 +43,7 @@ cdef extern from "numpy/arrayobject.h": ...@@ -43,6 +43,7 @@ cdef extern from "numpy/arrayobject.h":
raise RuntimeError("Py_intptr_t and Py_ssize_t differs in size, numpy.pxd does not support this") raise RuntimeError("Py_intptr_t and Py_ssize_t differs in size, numpy.pxd does not support this")
info.buf = PyArray_DATA(self) info.buf = PyArray_DATA(self)
# info.obj = None # this is automatic
info.ndim = PyArray_NDIM(self) info.ndim = PyArray_NDIM(self)
info.strides = <Py_ssize_t*>PyArray_STRIDES(self) info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
info.shape = <Py_ssize_t*>PyArray_DIMS(self) info.shape = <Py_ssize_t*>PyArray_DIMS(self)
......
##################################################################### #####################################################################
# #
# These are the "SageX" pxi files for (most of) the Python/C API. # These are the Cython pxd files for (most of) the Python/C API.
#
# SageX = SAGE Pyrex, which is a fork of Pyrex for use in SAGE.
# #
# REFERENCE COUNTING: # REFERENCE COUNTING:
# #
# JUST TO SCARE YOU: # JUST TO SCARE YOU:
# If you are going to use any of the Python/C API in your SageX # If you are going to use any of the Python/C API in your Cython
# program, you might be responsible for doing reference counting. # program, you might be responsible for doing reference counting.
# Read http://docs.python.org/api/refcounts.html which is so # Read http://docs.python.org/api/refcounts.html which is so
# important I've copied it below. # important I've copied it below.
...@@ -15,10 +13,10 @@ ...@@ -15,10 +13,10 @@
# For all the declaration below, whenver the Py_ function returns # For all the declaration below, whenver the Py_ function returns
# a *new reference* to a PyObject*, the return type is "object". # a *new reference* to a PyObject*, the return type is "object".
# When the function returns a borrowed reference, the return # When the function returns a borrowed reference, the return
# type is PyObject*. When SageX sees "object" as a return type # type is PyObject*. When Cython sees "object" as a return type
# it doesn't increment the reference count. When it sees PyObject* # it doesn't increment the reference count. When it sees PyObject*
# in order to use the result you must explicitly cast to <object>, # in order to use the result you must explicitly cast to <object>,
# and when you do that SageX increments the reference count wether # and when you do that Cython increments the reference count wether
# you want it to or not, forcing you to an explicit DECREF (or leak memory). # you want it to or not, forcing you to an explicit DECREF (or leak memory).
# To avoid this we make the above convention. Note, you can # To avoid this we make the above convention. Note, you can
# always locally override this convention by putting something like # always locally override this convention by putting something like
...@@ -26,10 +24,10 @@ ...@@ -26,10 +24,10 @@
# cdef extern from "Python.h": # cdef extern from "Python.h":
# PyObject* PyNumber_Add(PyObject *o1, PyObject *o2) # PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
# #
# in your file after any .pxi includes. SageX will use the latest # in your file after any .pxi includes. Cython will use the latest
# declaration. # declaration.
# #
# SageX takes care of this automatically for anything of type object. # Cython takes care of this automatically for anything of type object.
## More precisely, I think the correct convention for ## More precisely, I think the correct convention for
## using the Python/C API from Pyrex is as follows. ## using the Python/C API from Pyrex is as follows.
## ##
...@@ -119,7 +117,7 @@ ...@@ -119,7 +117,7 @@
# #
################################################################# #################################################################
from python_version cimport *
from python_ref cimport * from python_ref cimport *
from python_exc cimport * from python_exc cimport *
from python_module cimport * from python_module cimport *
...@@ -138,6 +136,7 @@ from python_long cimport * ...@@ -138,6 +136,7 @@ from python_long cimport *
from python_float cimport * from python_float cimport *
from python_complex cimport * from python_complex cimport *
from python_string cimport * from python_string cimport *
from python_unicode cimport *
from python_dict cimport * from python_dict cimport *
from python_instance cimport * from python_instance cimport *
from python_function cimport * from python_function cimport *
......
This diff is collapsed.
# Python version constants
#
# It's better to evaluate these at runtime (i.e. C compile time) using
#
# if PY_MAJOR_VERSION >= 3:
# do_stuff_in_Py3_0_and_later()
# if PY_VERSION_HEX >= 0x02050000:
# do_stuff_in_Py2_5_and_later()
#
# than using the IF/DEF statements, which are evaluated at Cython
# compile time. This will keep your C code portable.
cdef extern from *:
# the complete version, e.g. 0x010502B2 == 1.5.2b2
int PY_VERSION_HEX
# the individual sections as plain numbers
int PY_MAJOR_VERSION
int PY_MINOR_VERSION
int PY_MICRO_VERSION
int PY_RELEASE_LEVEL
int PY_RELEASE_SERIAL
# Note: PY_RELEASE_LEVEL is one of
# 0xA (alpha)
# 0xB (beta)
# 0xC (release candidate)
# 0xF (final)
char[] PY_VERSION
char[] PY_PATCHLEVEL_REVISION
This diff is collapsed.
...@@ -10,11 +10,11 @@ cdef extern from "longintrepr.h": ...@@ -10,11 +10,11 @@ cdef extern from "longintrepr.h":
cdef struct _longobject: cdef struct _longobject:
int ob_refcnt int ob_refcnt
PyTypeObject *ob_type PyTypeObject *ob_type
int ob_size # int ob_size # not in Py3k
unsigned int *ob_digit unsigned int *ob_digit
def test(temp = long(0)): def test(temp = long(0)):
cdef _longobject *l cdef _longobject *l
l = <_longobject *> temp l = <_longobject *> temp
print sizeof(l.ob_size) #print sizeof(l.ob_size) # not in Py3k
print sizeof(l.ob_digit[0]) print sizeof(l.ob_digit[0])
def f(*args, **kwargs):
pass
args = (1,2,3)
kwargs = {u"test" : "toast"}
def test():
f(*args, 1, 2, 3)
f(**kwargs, 1, 2, c=3)
f(*args, **kwargs, *args)
f(1, 2, c=3, *args, **kwargs, *args)
f(1, 2, c=3, *args, d=5, **kwargs, **kwargs)
f(1, 2, c=3, *args, d=5, **kwargs, x=6)
f(1=2)
# too bad we don't get more errors here ...
_ERRORS = u"""
8:13: Non-keyword arg following star-arg
"""
cdef class Test:
cdef __cinit__(self):
pass
cdef __len__(self):
pass
_ERRORS = u"""
3:9: Special methods must be declared with 'def', not 'cdef'
6:9: Special methods must be declared with 'def', not 'cdef'
"""
try:
raise KeyError
except KeyError:
pass
except:
pass
except:
pass
except AttributeError:
pass
_ERRORS = u"""
8:0: default 'except:' must be last
10:0: default 'except:' must be last
"""
...@@ -329,6 +329,35 @@ def explicitly_release_buffer(): ...@@ -329,6 +329,35 @@ def explicitly_release_buffer():
x = None x = None
print "After release" print "After release"
#
# Format strings
#
@testcase
def alignment_string(object[int] buf):
"""
>>> alignment_string(IntMockBuffer(None, [1,2], format="@i"))
2
>>> alignment_string(IntMockBuffer(None, [1,2], format="@i@@"))
2
>>> alignment_string(IntMockBuffer(None, [1,2], format=">i"))
Traceback (most recent call last):
...
ValueError: Buffer acquisition error: Only native byte order, size and alignment supported.
>>> alignment_string(IntMockBuffer(None, [1,2], format="<i"))
Traceback (most recent call last):
...
ValueError: Buffer acquisition error: Only native byte order, size and alignment supported.
>>> alignment_string(IntMockBuffer(None, [1,2], format="=i"))
Traceback (most recent call last):
...
ValueError: Buffer acquisition error: Only native byte order, size and alignment supported.
>>> alignment_string(IntMockBuffer(None, [1,2], format="!i"))
Traceback (most recent call last):
...
ValueError: Buffer acquisition error: Only native byte order, size and alignment supported.
"""
print buf[1]
# #
# Getting items and index bounds checking # Getting items and index bounds checking
# #
...@@ -949,10 +978,6 @@ cdef class MockBuffer: ...@@ -949,10 +978,6 @@ cdef class MockBuffer:
def __getbuffer__(MockBuffer self, Py_buffer* buffer, int flags): def __getbuffer__(MockBuffer self, Py_buffer* buffer, int flags):
if self.fail: if self.fail:
raise ValueError("Failing on purpose") raise ValueError("Failing on purpose")
if buffer is NULL:
print u"locking!"
return
self.recieved_flags = [] self.recieved_flags = []
cdef int value cdef int value
...@@ -961,6 +986,7 @@ cdef class MockBuffer: ...@@ -961,6 +986,7 @@ cdef class MockBuffer:
self.recieved_flags.append(name) self.recieved_flags.append(name)
buffer.buf = <void*>(<char*>self.buffer + (<int>self.offset * self.itemsize)) buffer.buf = <void*>(<char*>self.buffer + (<int>self.offset * self.itemsize))
buffer.obj = self
buffer.len = self.len buffer.len = self.len
buffer.readonly = 0 buffer.readonly = 0
buffer.format = <char*>self.format buffer.format = <char*>self.format
...@@ -1000,7 +1026,7 @@ cdef class IntMockBuffer(MockBuffer): ...@@ -1000,7 +1026,7 @@ cdef class IntMockBuffer(MockBuffer):
(<int*>buf)[0] = <int>value (<int*>buf)[0] = <int>value
return 0 return 0
cdef get_itemsize(self): return sizeof(int) cdef get_itemsize(self): return sizeof(int)
cdef get_default_format(self): return b"=i" cdef get_default_format(self): return b"@i"
cdef class ShortMockBuffer(MockBuffer): cdef class ShortMockBuffer(MockBuffer):
cdef int write(self, char* buf, object value) except -1: cdef int write(self, char* buf, object value) except -1:
...@@ -1014,7 +1040,7 @@ cdef class UnsignedShortMockBuffer(MockBuffer): ...@@ -1014,7 +1040,7 @@ cdef class UnsignedShortMockBuffer(MockBuffer):
(<unsigned short*>buf)[0] = <unsigned short>value (<unsigned short*>buf)[0] = <unsigned short>value
return 0 return 0
cdef get_itemsize(self): return sizeof(unsigned short) cdef get_itemsize(self): return sizeof(unsigned short)
cdef get_default_format(self): return b"=1H" # Try with repeat count cdef get_default_format(self): return b"@1H" # Try with repeat count
cdef class FloatMockBuffer(MockBuffer): cdef class FloatMockBuffer(MockBuffer):
cdef int write(self, char* buf, object value) except -1: cdef int write(self, char* buf, object value) except -1:
...@@ -1040,7 +1066,7 @@ cdef class ObjectMockBuffer(MockBuffer): ...@@ -1040,7 +1066,7 @@ cdef class ObjectMockBuffer(MockBuffer):
return 0 return 0
cdef get_itemsize(self): return sizeof(void*) cdef get_itemsize(self): return sizeof(void*)
cdef get_default_format(self): return b"=O" cdef get_default_format(self): return b"@O"
cdef class IntStridedMockBuffer(IntMockBuffer): cdef class IntStridedMockBuffer(IntMockBuffer):
...@@ -1052,10 +1078,10 @@ cdef class ErrorBuffer: ...@@ -1052,10 +1078,10 @@ cdef class ErrorBuffer:
def __init__(self, label): def __init__(self, label):
self.label = label self.label = label
def __getbuffer__(MockBuffer self, Py_buffer* buffer, int flags): def __getbuffer__(ErrorBuffer self, Py_buffer* buffer, int flags):
raise Exception("acquiring %s" % self.label) raise Exception("acquiring %s" % self.label)
def __releasebuffer__(MockBuffer self, Py_buffer* buffer): def __releasebuffer__(ErrorBuffer self, Py_buffer* buffer):
raise Exception("releasing %s" % self.label) raise Exception("releasing %s" % self.label)
# #
......
...@@ -8,18 +8,16 @@ if sys.version_info[0] >= 3: ...@@ -8,18 +8,16 @@ if sys.version_info[0] >= 3:
__doc__ += u""" __doc__ += u"""
>>> ms = memoryview(s) >>> ms = memoryview(s)
>>> ms.tobytes() >>> ms.tobytes()
bytearray(b'abcdefg') b'abcdefg'
>>> m1 = memoryview(b1) >>> m1 = memoryview(b1)
>>> m1.tobytes() >>> m1.tobytes()
locking! b'abcdefg'
bytearray(b'abcdefg')
>>> m2 = memoryview(b2) >>> m2 = memoryview(b2)
>>> m2.tobytes() >>> m2.tobytes()
locking! releasing!
unlocking! b'abcdefg'
bytearray(b'abcdefg')
>>> del m1 >>> del m1
>>> del m2 >>> del m2
...@@ -30,10 +28,8 @@ s = "abcdefg" ...@@ -30,10 +28,8 @@ s = "abcdefg"
cdef class TestBuffer: cdef class TestBuffer:
def __getbuffer__(self, Py_buffer* buffer, int flags): def __getbuffer__(self, Py_buffer* buffer, int flags):
if buffer is NULL:
print u"locking!"
return
buffer.buf = <char*>s buffer.buf = <char*>s
buffer.obj = self
buffer.len = len(s) buffer.len = len(s)
buffer.readonly = 0 buffer.readonly = 0
buffer.format = "B" buffer.format = "B"
...@@ -46,7 +42,4 @@ cdef class TestBuffer: ...@@ -46,7 +42,4 @@ cdef class TestBuffer:
cdef class TestBufferRelease(TestBuffer): cdef class TestBufferRelease(TestBuffer):
def __releasebuffer__(self, Py_buffer* buffer): def __releasebuffer__(self, Py_buffer* buffer):
if buffer is NULL: print u"releasing!"
print u"unlocking!"
else:
print u"releasing!"
__doc__ = u"""
>>> test_pos_args(h)
1 2 3 * 0 0
1 2 9 * 2 0
1 2 7 * 2 0
9 8 7 * 0 0
7 8 9 * 0 0
>>> test_kw_args(h)
1 2 3 * 0 0
1 2 9 * 2 1
1 2 7 * 2 1
1 2 9 * 2 2
1 2 9 * 2 2
1 2 9 * 2 3
>>> test_kw_args(e)
2 1
5 1
5 1
5 2
5 2
5 3
>>> test_kw(e)
0 1
0 2
0 2
0 1
>>> test_kw(g)
1
2
2
1
>>> test_pos_args(f)
3
5
5
3
3
>>> test_noargs(e)
0 0
>>> test_noargs(f)
0
>>> test_noargs(g)
0
# and some errors:
>>> test_noargs(h)
Traceback (most recent call last):
TypeError: h() takes at least 3 positional arguments (0 given)
>>> h(1,2, d=5)
Traceback (most recent call last):
TypeError: h() takes at least 3 positional arguments (2 given)
>>> f(1,2, d=5)
Traceback (most recent call last):
TypeError: f() got an unexpected keyword argument 'd'
>>> f(1, d=5)
Traceback (most recent call last):
TypeError: f() got an unexpected keyword argument 'd'
>>> f(d=5)
Traceback (most recent call last):
TypeError: f() got an unexpected keyword argument 'd'
>>> g(1,2, d=5)
Traceback (most recent call last):
TypeError: g() takes exactly 0 positional arguments (2 given)
>>> g(1,2)
Traceback (most recent call last):
TypeError: g() takes exactly 0 positional arguments (2 given)
>>> g(1)
Traceback (most recent call last):
TypeError: g() takes exactly 0 positional arguments (1 given)
>>> test_int_kwargs(e)
Traceback (most recent call last):
TypeError: e() keywords must be strings
>>> test_int_kwargs(f)
Traceback (most recent call last):
TypeError: f() keywords must be strings
>>> test_int_kwargs(g)
Traceback (most recent call last):
TypeError: g() keywords must be strings
>>> test_int_kwargs(h)
Traceback (most recent call last):
TypeError: h() keywords must be strings
"""
def e(*args, **kwargs):
print len(args), len(kwargs)
def f(*args):
print len(args)
def g(**kwargs):
print len(kwargs)
def h(a, b, c, *args, **kwargs):
print a, b, c, u'*', len(args), len(kwargs)
args = (9,8,7)
import sys
if sys.version_info[0] >= 3:
kwargs = {u"test" : u"toast"}
else:
kwargs = {"test" : u"toast"}
def test_kw_args(f):
f(1,2, c=3)
f(1,2, d=3, *args)
f(1,2, d=3, *(7,8,9))
f(1,2, d=3, *args, **kwargs)
f(1,2, d=3, *args, e=5)
f(1,2, d=3, *args, e=5, **kwargs)
def test_pos_args(f):
f(1,2,3)
f(1,2, *args)
f(1,2, *(7,8,9))
f(*args)
f(*(7,8,9))
def test_kw(f):
f(c=3)
f(d=3, e=5)
f(d=3, **kwargs)
f(**kwargs)
def test_noargs(f):
f()
def test_int_kwargs(f):
f(a=1,b=2,c=3, **{10:20,30:40})
...@@ -5,23 +5,23 @@ __doc__ = u""" ...@@ -5,23 +5,23 @@ __doc__ = u"""
>>> b(1,2,3) >>> b(1,2,3)
>>> b(1,2,3,4) >>> b(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 4 arguments (5 given) TypeError: b() takes exactly 4 positional arguments (5 given)
>>> c(1,2) >>> c(1,2)
>>> c(1,2,3) >>> c(1,2,3)
>>> c(1,2,3,4) >>> c(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 4 arguments (5 given) TypeError: c() takes at most 4 positional arguments (5 given)
>>> d(1,2) >>> d(1,2)
>>> d(1,2, c=1) >>> d(1,2, c=1)
>>> d(1,2,3) >>> d(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given) TypeError: d() takes exactly 3 positional arguments (4 given)
>>> d(1,2, d=1) >>> d(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: d() got an unexpected keyword argument 'd'
>>> e(1,2) >>> e(1,2)
>>> e(1,2, c=1) >>> e(1,2, c=1)
...@@ -30,34 +30,34 @@ __doc__ = u""" ...@@ -30,34 +30,34 @@ __doc__ = u"""
>>> e(1,2,3) >>> e(1,2,3)
>>> e(1,2,3,4) >>> e(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 4 positional arguments (5 given) TypeError: e() takes at most 4 positional arguments (5 given)
>>> f(1,2, c=1) >>> f(1,2, c=1)
>>> f(1,2, c=1, d=2) >>> f(1,2, c=1, d=2)
>>> f(1,2,3) >>> f(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given) TypeError: f() takes exactly 3 positional arguments (4 given)
>>> f(1,2) >>> f(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2) >>> f(1,2, c=1, e=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function TypeError: f() got an unexpected keyword argument 'e'
>>> g(1,2, c=1, f=2) >>> g(1,2, c=1, f=2)
>>> g(1,2, c=1, e=0, f=2, d=11) >>> g(1,2, c=1, e=0, f=2, d=11)
>>> g(1,2, c=1, f=2, e=0, x=25) >>> g(1,2, c=1, f=2, e=0, x=25)
>>> g(1,2,3) #doctest: +ELLIPSIS >>> g(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given) TypeError: g() takes exactly 3 positional arguments (4 given)
>>> g(1,2) >>> g(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: g() needs keyword-only argument c
>>> g(1,2, c=1) >>> g(1,2, c=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: g() needs keyword-only argument f
>>> h(1,2, c=1, f=2) >>> h(1,2, c=1, f=2)
>>> h(1,2, c=1, f=2, e=3) >>> h(1,2, c=1, f=2, e=3)
...@@ -66,10 +66,10 @@ __doc__ = u""" ...@@ -66,10 +66,10 @@ __doc__ = u"""
>>> h(1,2,3) >>> h(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: h() needs keyword-only argument c
>>> h(1,2, d=1) >>> h(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: h() needs keyword-only argument c
>>> k(1,2, c=1, f=2) >>> k(1,2, c=1, f=2)
>>> k(1,2, c=1, f=2, e=3) >>> k(1,2, c=1, f=2, e=3)
...@@ -78,20 +78,12 @@ __doc__ = u""" ...@@ -78,20 +78,12 @@ __doc__ = u"""
>>> k(1,2,3) >>> k(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: k() needs keyword-only argument f
>>> k(1,2, d=1) >>> k(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: k() needs keyword-only argument f
""" """
import sys, re
if sys.version_info >= (2,6):
__doc__ = re.sub(u"Error: (.*)exactly(.*)", u"Error: \\1at most\\2", __doc__)
import sys, re
if sys.version_info >= (2,6):
__doc__ = re.sub(u"(ELLIPSIS[^>]*Error: )[^\n]*\n", u"\\1...\n", __doc__, re.M)
class Spam: class Spam:
def b(self, a, b, c): def b(self, a, b, c):
pass pass
......
...@@ -5,23 +5,23 @@ __doc__ = u""" ...@@ -5,23 +5,23 @@ __doc__ = u"""
>>> b(1,2,3) >>> b(1,2,3)
>>> b(1,2,3,4) >>> b(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (4 given) TypeError: b() takes exactly 3 positional arguments (4 given)
>>> c(1,2) >>> c(1,2)
>>> c(1,2,3) >>> c(1,2,3)
>>> c(1,2,3,4) >>> c(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 arguments (4 given) TypeError: c() takes at most 3 positional arguments (4 given)
>>> d(1,2) >>> d(1,2)
>>> d(1,2, c=1) >>> d(1,2, c=1)
>>> d(1,2,3) >>> d(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: d() takes exactly 2 positional arguments (3 given)
>>> d(1,2, d=1) >>> d(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: d() got an unexpected keyword argument 'd'
>>> e(1,2) >>> e(1,2)
>>> e(1,2, c=1) >>> e(1,2, c=1)
...@@ -30,20 +30,20 @@ __doc__ = u""" ...@@ -30,20 +30,20 @@ __doc__ = u"""
>>> e(1,2,3) >>> e(1,2,3)
>>> e(1,2,3,4) >>> e(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given) TypeError: e() takes at most 3 positional arguments (4 given)
>>> f(1,2, c=1) >>> f(1,2, c=1)
>>> f(1,2, c=1, d=2) >>> f(1,2, c=1, d=2)
>>> f(1,2,3) >>> f(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: f() takes exactly 2 positional arguments (3 given)
>>> f(1,2) >>> f(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2) >>> f(1,2, c=1, e=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function TypeError: f() got an unexpected keyword argument 'e'
>>> g(1,2, c=1, f=2) >>> g(1,2, c=1, f=2)
>>> g(1,2, c=1, e=0, f=2, d=11) >>> g(1,2, c=1, e=0, f=2, d=11)
...@@ -51,13 +51,13 @@ __doc__ = u""" ...@@ -51,13 +51,13 @@ __doc__ = u"""
>>> g(1,2,3) >>> g(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: g() takes exactly 2 positional arguments (3 given)
>>> g(1,2) >>> g(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: g() needs keyword-only argument c
>>> g(1,2, c=1) >>> g(1,2, c=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: g() needs keyword-only argument f
>>> h(1,2, c=1, f=2) >>> h(1,2, c=1, f=2)
>>> h(1,2, c=1, f=2, e=3) >>> h(1,2, c=1, f=2, e=3)
...@@ -66,10 +66,10 @@ __doc__ = u""" ...@@ -66,10 +66,10 @@ __doc__ = u"""
>>> h(1,2,3) >>> h(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: h() needs keyword-only argument c
>>> h(1,2, d=1) >>> h(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: h() needs keyword-only argument c
>>> k(1,2, c=1, f=2) >>> k(1,2, c=1, f=2)
>>> k(1,2, c=1, f=2, e=3) >>> k(1,2, c=1, f=2, e=3)
...@@ -78,16 +78,12 @@ __doc__ = u""" ...@@ -78,16 +78,12 @@ __doc__ = u"""
>>> k(1,2,3) >>> k(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: k() needs keyword-only argument f
>>> k(1,2, d=1) >>> k(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: k() needs keyword-only argument f
""" """
import sys, re
if sys.version_info >= (2,6):
__doc__ = re.sub(u"Error: (.*)exactly(.*)", u"Error: \\1at most\\2", __doc__)
cdef class Ext: cdef class Ext:
def b(self, a, b, c): def b(self, a, b, c):
pass pass
......
...@@ -5,15 +5,15 @@ __doc__ = u""" ...@@ -5,15 +5,15 @@ __doc__ = u"""
>>> spam(1,2,3) >>> spam(1,2,3)
(1, 2, 3) (1, 2, 3)
>>> spam(1,2) #doctest: +ELLIPSIS >>> spam(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (2 given) TypeError: spam() takes exactly 3 positional arguments (2 given)
>>> spam(1,2,3,4) >>> spam(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (4 given) TypeError: spam() takes exactly 3 positional arguments (4 given)
>>> spam(1,2,3, a=1) #doctest: +ELLIPSIS >>> spam(1,2,3, a=1) #doctest: +ELLIPSIS
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: spam() got an unexpected keyword argument 'a'
>>> grail(1,2,3) >>> grail(1,2,3)
(1, 2, 3, ()) (1, 2, 3, ())
...@@ -21,23 +21,23 @@ __doc__ = u""" ...@@ -21,23 +21,23 @@ __doc__ = u"""
(1, 2, 3, (4,)) (1, 2, 3, (4,))
>>> grail(1,2,3,4,5,6,7,8,9) >>> grail(1,2,3,4,5,6,7,8,9)
(1, 2, 3, (4, 5, 6, 7, 8, 9)) (1, 2, 3, (4, 5, 6, 7, 8, 9))
>>> grail(1,2) #doctest: +ELLIPSIS >>> grail(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (2 given) TypeError: grail() takes at least 3 positional arguments (2 given)
>>> grail(1,2,3, a=1) #doctest: +ELLIPSIS >>> grail(1,2,3, a=1) #doctest: +ELLIPSIS
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: grail() got an unexpected keyword argument 'a'
>>> swallow(1,2,3) >>> swallow(1,2,3)
(1, 2, 3, ()) (1, 2, 3, ())
>>> swallow(1,2,3,4) >>> swallow(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given) TypeError: swallow() takes exactly 3 positional arguments (4 given)
>>> swallow(1,2,3, a=1, b=2) >>> swallow(1,2,3, a=1, b=2)
(1, 2, 3, (('a', 1), ('b', 2))) (1, 2, 3, (('a', 1), ('b', 2)))
>>> swallow(1,2,3, x=1) #doctest: +ELLIPSIS >>> swallow(1,2,3, x=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: keyword parameter 'x' was given by position and by name TypeError: swallow() got multiple values for keyword argument 'x'
>>> creosote(1,2,3) >>> creosote(1,2,3)
(1, 2, 3, (), ()) (1, 2, 3, (), ())
...@@ -47,9 +47,9 @@ __doc__ = u""" ...@@ -47,9 +47,9 @@ __doc__ = u"""
(1, 2, 3, (), (('a', 1),)) (1, 2, 3, (), (('a', 1),))
>>> creosote(1,2,3,4, a=1, b=2) >>> creosote(1,2,3,4, a=1, b=2)
(1, 2, 3, (4,), (('a', 1), ('b', 2))) (1, 2, 3, (4,), (('a', 1), ('b', 2)))
>>> creosote(1,2,3,4, x=1) #doctest: +ELLIPSIS >>> creosote(1,2,3,4, x=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: keyword parameter 'x' was given by position and by name TypeError: creosote() got multiple values for keyword argument 'x'
>>> onlyt(1) >>> onlyt(1)
(1,) (1,)
...@@ -57,10 +57,10 @@ __doc__ = u""" ...@@ -57,10 +57,10 @@ __doc__ = u"""
(1, 2) (1, 2)
>>> onlyt(a=1) >>> onlyt(a=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: onlyt() got an unexpected keyword argument 'a'
>>> onlyt(1, a=2) >>> onlyt(1, a=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: onlyt() got an unexpected keyword argument 'a'
>>> onlyk(a=1) >>> onlyk(a=1)
(('a', 1),) (('a', 1),)
...@@ -68,13 +68,13 @@ __doc__ = u""" ...@@ -68,13 +68,13 @@ __doc__ = u"""
(('a', 1), ('b', 2)) (('a', 1), ('b', 2))
>>> onlyk(1) >>> onlyk(1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 0 positional arguments (1 given) TypeError: onlyk() takes exactly 0 positional arguments (1 given)
>>> onlyk(1, 2) >>> onlyk(1, 2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 0 positional arguments (2 given) TypeError: onlyk() takes exactly 0 positional arguments (2 given)
>>> onlyk(1, a=1, b=2) >>> onlyk(1, a=1, b=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 0 positional arguments (1 given) TypeError: onlyk() takes exactly 0 positional arguments (1 given)
>>> tk(a=1) >>> tk(a=1)
(('a', 1),) (('a', 1),)
...@@ -88,10 +88,6 @@ __doc__ = u""" ...@@ -88,10 +88,6 @@ __doc__ = u"""
(1, ('a', 1), ('b', 2)) (1, ('a', 1), ('b', 2))
""" """
import sys, re
if sys.version_info >= (2,6):
__doc__ = re.sub(u"Error: (.*)exactly(.*)", u"Error: \\1at most\\2", __doc__)
import sys, re import sys, re
if sys.version_info >= (2,6): if sys.version_info >= (2,6):
__doc__ = re.sub(u"(ELLIPSIS[^>]*Error: )[^\n]*\n", u"\\1...\n", __doc__, re.M) __doc__ = re.sub(u"(ELLIPSIS[^>]*Error: )[^\n]*\n", u"\\1...\n", __doc__, re.M)
......
__doc__ = u"""
>>> import sys
>>> if not IS_PY3: sys.exc_clear()
>>> def test_py():
... try:
... raise AttributeError
... except AttributeError:
... print(sys.exc_info()[0] == AttributeError or sys.exc_info()[0])
... print((IS_PY3 and sys.exc_info()[0] is None) or
... (not IS_PY3 and sys.exc_info()[0] == AttributeError) or
... sys.exc_info()[0])
>>> print(sys.exc_info()[0]) # 0
None
>>> test_py()
True
True
>>> print(sys.exc_info()[0]) # test_py()
None
>>> test_c()
True
True
>>> print(sys.exc_info()[0]) # test_c()
None
"""
import sys
IS_PY3 = sys.version_info[0] >= 3
def test_c():
try:
raise AttributeError
except AttributeError:
print(sys.exc_info()[0] == AttributeError or sys.exc_info()[0])
print(sys.exc_info()[0] is None or sys.exc_info()[0])
__doc__ = u"""
>>> import sys
>>> if not IS_PY3: sys.exc_clear()
>>> def test_py(outer_exc):
... try:
... raise AttributeError
... except AttributeError:
... print(sys.exc_info()[0] is AttributeError or sys.exc_info()[0])
... try: raise KeyError
... except: print(sys.exc_info()[0] is KeyError or sys.exc_info()[0])
... print((IS_PY3 and sys.exc_info()[0] is AttributeError) or
... (not IS_PY3 and sys.exc_info()[0] is KeyError) or
... sys.exc_info()[0])
... print((IS_PY3 and sys.exc_info()[0] is outer_exc) or
... (not IS_PY3 and sys.exc_info()[0] is KeyError) or
... sys.exc_info()[0])
>>> print(sys.exc_info()[0]) # 0
None
>>> test_py(None)
True
True
True
True
>>> print(sys.exc_info()[0]) # test_py()
None
>>> test_c(None)
True
True
True
True
>>> print(sys.exc_info()[0]) # test_c()
None
>>> def test_py2():
... try:
... raise Exception
... except Exception:
... test_py(Exception)
... print(sys.exc_info()[0] is Exception or sys.exc_info()[0])
... print((IS_PY3 and sys.exc_info()[0] is None) or
... (not IS_PY3 and sys.exc_info()[0] is Exception) or
... sys.exc_info()[0])
>>> test_py2()
True
True
True
True
True
True
>>> print(sys.exc_info()[0]) # test_py2()
None
>>> test_c2()
True
True
True
True
True
True
>>> print(sys.exc_info()[0]) # test_c2()
None
"""
import sys
IS_PY3 = sys.version_info[0] >= 3
def test_c(outer_exc):
try:
raise AttributeError
except AttributeError:
print(sys.exc_info()[0] is AttributeError or sys.exc_info()[0])
try: raise KeyError
except: print(sys.exc_info()[0] is KeyError or sys.exc_info()[0])
print(sys.exc_info()[0] is AttributeError or sys.exc_info()[0])
print(sys.exc_info()[0] is outer_exc or sys.exc_info()[0])
def test_c2():
try:
raise Exception
except Exception:
test_c(Exception)
print(sys.exc_info()[0] is Exception or sys.exc_info()[0])
print(sys.exc_info()[0] is None or sys.exc_info()[0])
__doc__ = u"""
>>> import sys
>>> if not IS_PY3: sys.exc_clear()
>>> def test_py():
... try:
... raise AttributeError
... except AttributeError:
... test_c(error=AttributeError)
... print(sys.exc_info()[0] is AttributeError or sys.exc_info()[0])
... print((IS_PY3 and sys.exc_info()[0] is TestException) or
... (not IS_PY3 and sys.exc_info()[0] is AttributeError) or
... sys.exc_info()[0])
>>> print(sys.exc_info()[0]) # 0
None
>>> test_py()
True
True
True
True
>>> print(sys.exc_info()[0]) # test_py()
None
>>> test_c(test_py)
True
True
True
True
True
True
>>> print(sys.exc_info()[0]) # test_c()
None
"""
import sys
IS_PY3 = sys.version_info[0] >= 3
class TestException(Exception):
pass
def test_c(func=None, error=None):
try:
raise TestException
except TestException:
if func:
func()
print(sys.exc_info()[0] is TestException or sys.exc_info()[0])
print(sys.exc_info()[0] is error or sys.exc_info()[0])
__doc__ = u"""
>>> import sys
>>> if not IS_PY3: sys.exc_clear()
>>> print(sys.exc_info()[0]) # 0
None
>>> exc = test_c()
>>> type(exc) is TestException
True
>>> print(sys.exc_info()[0]) # test_c()
None
"""
import sys
IS_PY3 = sys.version_info[0] >= 3
class TestException(Exception):
pass
def test_c():
try:
raise TestException
except TestException, e:
return e
...@@ -2,23 +2,23 @@ __doc__ = u""" ...@@ -2,23 +2,23 @@ __doc__ = u"""
>>> b(1,2,3) >>> b(1,2,3)
>>> b(1,2,3,4) >>> b(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (4 given) TypeError: b() takes exactly 3 positional arguments (4 given)
>>> c(1,2) >>> c(1,2)
>>> c(1,2,3) >>> c(1,2,3)
>>> c(1,2,3,4) >>> c(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 arguments (4 given) TypeError: c() takes at most 3 positional arguments (4 given)
>>> d(1,2) >>> d(1,2)
>>> d(1,2, c=1) >>> d(1,2, c=1)
>>> d(1,2,3) >>> d(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: d() takes exactly 2 positional arguments (3 given)
>>> d(1,2, d=1) >>> d(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: d() got an unexpected keyword argument 'd'
>>> e(1,2) >>> e(1,2)
>>> e(1,2, c=1) >>> e(1,2, c=1)
...@@ -27,20 +27,20 @@ __doc__ = u""" ...@@ -27,20 +27,20 @@ __doc__ = u"""
>>> e(1,2,3) >>> e(1,2,3)
>>> e(1,2,3,4) >>> e(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given) TypeError: e() takes at most 3 positional arguments (4 given)
>>> f(1,2, c=1) >>> f(1,2, c=1)
>>> f(1,2, c=1, d=2) >>> f(1,2, c=1, d=2)
>>> f(1,2,3) >>> f(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: f() takes exactly 2 positional arguments (3 given)
>>> f(1,2) >>> f(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2) >>> f(1,2, c=1, e=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function TypeError: f() got an unexpected keyword argument 'e'
>>> g(1,2, c=1, f=2) >>> g(1,2, c=1, f=2)
>>> g(1,2, c=1, e=0, f=2, d=11) >>> g(1,2, c=1, e=0, f=2, d=11)
...@@ -48,13 +48,13 @@ __doc__ = u""" ...@@ -48,13 +48,13 @@ __doc__ = u"""
>>> g(1,2,3) >>> g(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: g() takes exactly 2 positional arguments (3 given)
>>> g(1,2) >>> g(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: g() needs keyword-only argument c
>>> g(1,2, c=1) >>> g(1,2, c=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: g() needs keyword-only argument f
>>> h(1,2, c=1, f=2) >>> h(1,2, c=1, f=2)
>>> h(1,2, c=1, f=2, e=3) >>> h(1,2, c=1, f=2, e=3)
...@@ -63,10 +63,10 @@ __doc__ = u""" ...@@ -63,10 +63,10 @@ __doc__ = u"""
>>> h(1,2,3) >>> h(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: h() needs keyword-only argument c
>>> h(1,2, d=1) >>> h(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: h() needs keyword-only argument c
>>> k(1,2, c=1, f=2) >>> k(1,2, c=1, f=2)
>>> k(1,2, c=1, f=2, e=3) >>> k(1,2, c=1, f=2, e=3)
...@@ -75,15 +75,21 @@ __doc__ = u""" ...@@ -75,15 +75,21 @@ __doc__ = u"""
>>> k(1,2,3) >>> k(1,2,3)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: k() needs keyword-only argument f
>>> k(1,2, d=1) >>> k(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: k() needs keyword-only argument f
"""
import sys, re >>> l(a=1, b=2)
if sys.version_info >= (2,6): >>> l(a=1, b=2, c=1)
__doc__ = re.sub(u"Error: (.*)exactly(.*)", u"Error: \\1at most\\2", __doc__)
>>> l(1,2,3)
Traceback (most recent call last):
TypeError: l() takes exactly 0 positional arguments (3 given)
>>> l(1,2, d=1)
Traceback (most recent call last):
TypeError: l() takes exactly 0 positional arguments (2 given)
"""
def b(a, b, c): def b(a, b, c):
pass pass
...@@ -108,3 +114,6 @@ def h(a, b, *args, c, d = 42, e = 17, f, **kwds): ...@@ -108,3 +114,6 @@ def h(a, b, *args, c, d = 42, e = 17, f, **kwds):
def k(a, b, c=1, *args, d = 42, e = 17, f, **kwds): def k(a, b, c=1, *args, d = 42, e = 17, f, **kwds):
pass pass
def l(*, a, b, c = 88):
pass
__doc__ = u""" __doc__ = u"""
>>> call0ab(b)
Traceback (most recent call last):
TypeError: b() takes exactly 3 positional arguments (2 given)
>>> call0abc(b)
1 2 3
>>> call3(b) >>> call3(b)
1 2 3
>>> call4(b) >>> call4(b)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (4 given) TypeError: b() takes exactly 3 positional arguments (4 given)
>>> call0ab(c)
1 2 1
>>> call0abc(c)
1 2 3
>>> call2(c) >>> call2(c)
1 2 1
>>> call3(c) >>> call3(c)
1 2 3
>>> call4(c) >>> call4(c)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 arguments (4 given) TypeError: c() takes at most 3 positional arguments (4 given)
>>> call0abc(d)
1 2 3
>>> call0ab(d)
1 2 88
>>> call2(d) >>> call2(d)
1 2 88
>>> call2c(d) >>> call2c(d)
1 2 1
>>> call3(d) >>> call3(d)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: d() takes exactly 2 positional arguments (3 given)
>>> call2d(d) >>> call2d(d)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: d() got an unexpected keyword argument 'd'
>>> call0abc(e)
1 2 3 []
>>> call2(e) >>> call2(e)
1 2 88 []
>>> call2c(e) >>> call2c(e)
1 2 1 []
>>> call2d(e) >>> call2d(e)
1 2 88 [('d', 1)]
>>> call2cde(e) >>> call2cde(e)
1 2 1 [('d', 2), ('e', 3)]
>>> call3(e) >>> call3(e)
1 2 3 []
>>> call4(e) >>> call4(e)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given) TypeError: e() takes at most 3 positional arguments (4 given)
>>> call0abc(f)
1 2 3 42
>>> call2c(f) >>> call2c(f)
1 2 1 42
>>> call2cd(f) >>> call2cd(f)
1 2 1 2
>>> call3(f) >>> call3(f)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: f() takes exactly 2 positional arguments (3 given)
>>> call2(f) >>> call2(f)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: f() needs keyword-only argument c
>>> call2ce(f) >>> call2ce(f)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function TypeError: f() got an unexpected keyword argument 'e'
>>> call2cf(g) >>> call2cf(g)
1 2 1 42 17 2 []
>>> call2cefd(g) >>> call2cefd(g)
1 2 1 11 0 2 []
>>> call2cfex(g) >>> call2cfex(g)
1 2 1 42 0 2 [('x', 25)]
>>> call3(g) >>> call3(g)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 2 positional arguments (3 given) TypeError: g() takes exactly 2 positional arguments (3 given)
>>> call2(g) >>> call2(g)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: g() needs keyword-only argument c
>>> call2c(g) >>> call2c(g)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: g() needs keyword-only argument f
>>> call2cf(h) >>> call2cf(h)
1 2 1 42 17 2 () []
>>> call2cfe(h) >>> call2cfe(h)
1 2 1 42 3 2 () []
>>> call6cf(h) >>> call6cf(h)
1 2 1 42 17 2 (3, 4, 5, 6) []
>>> call6cfexy(h) >>> call6cfexy(h)
1 2 1 42 3 2 (3, 4, 5, 6) [('x', 25), ('y', 11)]
>>> call3(h) >>> call3(h)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: h() needs keyword-only argument c
>>> call3d(h) >>> call3d(h)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing TypeError: h() needs keyword-only argument c
>>> call2cf(k) >>> call2cf(k)
1 2 1 42 17 2 () []
>>> call2cfe(k) >>> call2cfe(k)
1 2 1 42 3 2 () []
>>> call6df(k) >>> call6df(k)
1 2 3 1 17 2 (4, 5, 6) []
>>> call6dfexy(k) >>> call6dfexy(k)
1 2 3 1 3 2 (4, 5, 6) [('x', 25), ('y', 11)]
>>> call3(k) >>> call3(k)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: k() needs keyword-only argument f
>>> call2d(k) >>> call2d(k)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing TypeError: k() needs keyword-only argument f
"""
import sys, re >>> call0abc(m)
if sys.version_info >= (2,6): 1 2 3
__doc__ = re.sub(u"Error: (.*)exactly(.*)", u"Error: \\1at most\\2", __doc__) >>> call2c(m)
1 2 1
>>> call3(m)
Traceback (most recent call last):
TypeError: m() takes at most 2 positional arguments (3 given)
>>> call2(m)
Traceback (most recent call last):
TypeError: m() needs keyword-only argument c
>>> call2cd(m)
Traceback (most recent call last):
TypeError: m() got an unexpected keyword argument 'd'
"""
# the calls: # the calls:
def call0ab(f):
f(a=1,b=2)
def call0abc(f):
f(a=1,b=2,c=3)
def call2(f): def call2(f):
f(1,2) f(1,2)
...@@ -132,6 +189,10 @@ def call2cefd(f): ...@@ -132,6 +189,10 @@ def call2cefd(f):
def call2cfex(f): def call2cfex(f):
f(1,2, c=1, f=2, e=0, x=25) f(1,2, c=1, f=2, e=0, x=25)
def call6argscfexy(f):
args = (1,2,3,4,5,6)
f(*args, c=1, f=2, e=3, x=25, y=11)
def call6cfexy(f): def call6cfexy(f):
f(1,2,3,4,5,6, c=1, f=2, e=3, x=25, y=11) f(1,2,3,4,5,6, c=1, f=2, e=3, x=25, y=11)
...@@ -141,25 +202,36 @@ def call6dfexy(f): ...@@ -141,25 +202,36 @@ def call6dfexy(f):
# the called functions: # the called functions:
def b(a, b, c): def b(a, b, c):
pass print a,b,c
def c(a, b, c=1): def c(a, b, c=1):
pass print a,b,c
def d(a, b, *, c = 88): def d(a, b, *, c = 88):
pass print a,b,c
def e(a, b, c = 88, **kwds): def e(a, b, c = 88, **kwds):
pass kwlist = list(kwds.items())
kwlist.sort()
print a,b,c, kwlist
def f(a, b, *, c, d = 42): def f(a, b, *, c, d = 42):
pass print a,b,c,d
def g(a, b, *, c, d = 42, e = 17, f, **kwds): def g(a, b, *, c, d = 42, e = 17, f, **kwds):
pass kwlist = list(kwds.items())
kwlist.sort()
print a,b,c,d,e,f, kwlist
def h(a, b, *args, c, d = 42, e = 17, f, **kwds): def h(a, b, *args, c, d = 42, e = 17, f, **kwds):
pass kwlist = list(kwds.items())
kwlist.sort()
print a,b,c,d,e,f, args, kwlist
def k(a, b, c=1, *args, d = 42, e = 17, f, **kwds): def k(a, b, c=1, *args, d = 42, e = 17, f, **kwds):
pass kwlist = list(kwds.items())
kwlist.sort()
print a,b,c,d,e,f, args, kwlist
def m(a, b=1, *, c):
print a,b,c
__doc__ = u"""
>>> a
2
"""
a = 0
try:
raise KeyError
except AttributeError:
a = 1
except KeyError:
a = 2
except:
a = 3
__doc__ = u""" __doc__ = u"""
>>> x = spam() >>> x = spam()
>>> print(repr(x)) >>> print(repr(x))
b'Ftang\\x00Ftang!' u'Ftang\\x00Ftang!'
""" """
import sys import sys
if sys.version_info[0] < 3: if sys.version_info[0] >= 3:
__doc__ = __doc__.replace(u" b'", u" '") __doc__ = __doc__.replace(u" u'", u" '")
cdef extern from "string.h": cdef extern from "string.h":
void memcpy(char *d, char *s, int n) void memcpy(char *d, char *s, int n)
cdef extern from "Python.h": from python_unicode cimport PyUnicode_DecodeUTF8
object PyString_FromStringAndSize(char *s, int len)
def spam(): def spam():
cdef char buf[12] cdef char buf[12]
memcpy(buf, "Ftang\0Ftang!", sizeof(buf)) memcpy(buf, "Ftang\0Ftang!", sizeof(buf))
return PyString_FromStringAndSize(buf, sizeof(buf)) return PyUnicode_DecodeUTF8(buf, sizeof(buf), NULL)
__doc__ = u""" __doc__ = u"""
>>> s = Spam() #doctest: +ELLIPSIS >>> s = Spam()
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (0 given) TypeError: __init__() takes exactly 3 positional arguments (0 given)
""" """
import sys, re
if sys.version_info >= (2,6):
__doc__ = re.sub(u"Error: .*", u"Error: ...", __doc__)
cdef class Spam: cdef class Spam:
def __init__(self, a, b, int c): def __init__(self, a, b, int c):
......
...@@ -4,19 +4,15 @@ __doc__ = u""" ...@@ -4,19 +4,15 @@ __doc__ = u"""
Traceback (most recent call last): Traceback (most recent call last):
TypeError: an integer is required TypeError: an integer is required
>>> fail0(1,2) #doctest: +ELLIPSIS >>> fail0(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 2 arguments (0 given) TypeError: f() takes exactly 2 positional arguments (0 given)
>>> fail1(1,2) #doctest: +ELLIPSIS >>> fail1(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 2 arguments (1 given) TypeError: f() takes exactly 2 positional arguments (1 given)
""" """
import sys, re
if sys.version_info >= (2,6):
__doc__ = re.sub(u"Error: .*exactly.*", u"Error: ...", __doc__)
import sys import sys
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
__doc__ = __doc__.replace(u" b'", u" '") __doc__ = __doc__.replace(u" b'", u" '")
......
__doc__ = u""" __doc__ = u"""
>>> spam(1,2,3) >>> spam(1,2,3)
(1, 2, 3) (1, 2, 3)
>>> spam(1,2) #doctest: +ELLIPSIS >>> spam(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (2 given) TypeError: spam() takes exactly 3 positional arguments (2 given)
>>> spam(1,2,3,4) >>> spam(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (4 given) TypeError: spam() takes exactly 3 positional arguments (4 given)
>>> spam(1,2,3, a=1) #doctest: +ELLIPSIS >>> spam(1,2,3, a=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: spam() got an unexpected keyword argument 'a'
>>> grail(1,2,3) >>> grail(1,2,3)
(1, 2, 3, ()) (1, 2, 3, ())
...@@ -17,23 +17,23 @@ __doc__ = u""" ...@@ -17,23 +17,23 @@ __doc__ = u"""
(1, 2, 3, (4,)) (1, 2, 3, (4,))
>>> grail(1,2,3,4,5,6,7,8,9) >>> grail(1,2,3,4,5,6,7,8,9)
(1, 2, 3, (4, 5, 6, 7, 8, 9)) (1, 2, 3, (4, 5, 6, 7, 8, 9))
>>> grail(1,2) #doctest: +ELLIPSIS >>> grail(1,2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes exactly 3 arguments (2 given) TypeError: grail() takes at least 3 positional arguments (2 given)
>>> grail(1,2,3, a=1) #doctest: +ELLIPSIS >>> grail(1,2,3, a=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: grail() got an unexpected keyword argument 'a'
>>> swallow(1,2,3) >>> swallow(1,2,3)
(1, 2, 3, ()) (1, 2, 3, ())
>>> swallow(1,2,3,4) >>> swallow(1,2,3,4)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 3 positional arguments (4 given) TypeError: swallow() takes exactly 3 positional arguments (4 given)
>>> swallow(1,2,3, a=1, b=2) >>> swallow(1,2,3, a=1, b=2)
(1, 2, 3, (('a', 1), ('b', 2))) (1, 2, 3, (('a', 1), ('b', 2)))
>>> swallow(1,2,3, x=1) #doctest: +ELLIPSIS >>> swallow(1,2,3, x=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: keyword parameter 'x' was given by position and by name TypeError: swallow() got multiple values for keyword argument 'x'
>>> creosote(1,2,3) >>> creosote(1,2,3)
(1, 2, 3, (), ()) (1, 2, 3, (), ())
...@@ -43,9 +43,9 @@ __doc__ = u""" ...@@ -43,9 +43,9 @@ __doc__ = u"""
(1, 2, 3, (), (('a', 1),)) (1, 2, 3, (), (('a', 1),))
>>> creosote(1,2,3,4, a=1, b=2) >>> creosote(1,2,3,4, a=1, b=2)
(1, 2, 3, (4,), (('a', 1), ('b', 2))) (1, 2, 3, (4,), (('a', 1), ('b', 2)))
>>> creosote(1,2,3,4, x=1) #doctest: +ELLIPSIS >>> creosote(1,2,3,4, x=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: keyword parameter 'x' was given by position and by name TypeError: creosote() got multiple values for keyword argument 'x'
>>> onlyt(1) >>> onlyt(1)
(1,) (1,)
...@@ -53,10 +53,10 @@ __doc__ = u""" ...@@ -53,10 +53,10 @@ __doc__ = u"""
(1, 2) (1, 2)
>>> onlyt(a=1) >>> onlyt(a=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: onlyt() got an unexpected keyword argument 'a'
>>> onlyt(1, a=2) >>> onlyt(1, a=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: onlyt() got an unexpected keyword argument 'a'
>>> onlyk(a=1) >>> onlyk(a=1)
(('a', 1),) (('a', 1),)
...@@ -64,13 +64,13 @@ __doc__ = u""" ...@@ -64,13 +64,13 @@ __doc__ = u"""
(('a', 1), ('b', 2)) (('a', 1), ('b', 2))
>>> onlyk(1) >>> onlyk(1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 0 positional arguments (1 given) TypeError: onlyk() takes exactly 0 positional arguments (1 given)
>>> onlyk(1, 2) >>> onlyk(1, 2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 0 positional arguments (2 given) TypeError: onlyk() takes exactly 0 positional arguments (2 given)
>>> onlyk(1, a=1, b=2) >>> onlyk(1, a=1, b=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: function takes at most 0 positional arguments (1 given) TypeError: onlyk() takes exactly 0 positional arguments (1 given)
>>> tk(a=1) >>> tk(a=1)
(('a', 1),) (('a', 1),)
...@@ -84,14 +84,6 @@ __doc__ = u""" ...@@ -84,14 +84,6 @@ __doc__ = u"""
(1, ('a', 1), ('b', 2)) (1, ('a', 1), ('b', 2))
""" """
import sys, re
if sys.version_info >= (2,6):
__doc__ = re.sub(u"Error: (.*)exactly(.*)", u"Error: \\1at most\\2", __doc__)
import sys, re
if sys.version_info >= (2,6):
__doc__ = re.sub(u"(ELLIPSIS[^>]*Error: )[^\n]*\n", u"\\1...\n", __doc__, re.M)
cdef sorteditems(d): cdef sorteditems(d):
l = list(d.items()) l = list(d.items())
l.sort() l.sort()
......
...@@ -62,6 +62,33 @@ __doc__ = u""" ...@@ -62,6 +62,33 @@ __doc__ = u"""
12 12
>>> switch_c(13) >>> switch_c(13)
0 0
>>> switch_or(0)
0
>>> switch_or(1)
1
>>> switch_or(2)
1
>>> switch_or(3)
1
>>> switch_or(4)
0
>>> switch_short(0)
0
>>> switch_short(1)
1
>>> switch_short(2)
2
>>> switch_short(3)
0
>>> switch_off(0)
0
>>> switch_off(1)
1
>>> switch_off(2)
0
""" """
def switch_simple_py(x): def switch_simple_py(x):
...@@ -123,3 +150,26 @@ def switch_c(int x): ...@@ -123,3 +150,26 @@ def switch_c(int x):
else: else:
return 0 return 0
return -1 return -1
def switch_or(int x):
if x == 1 or x == 2 or x == 3:
return 1
else:
return 0
return -1
def switch_short(int x):
if x == 1:
return 1
elif 2 == x:
return 2
else:
return 0
return -1
def switch_off(int x):
if x == 1:
return 1
else:
return 0
return -1
__doc__ = u"""
>>> class PyTest(object):
... def __private(self): pass
>>> py = PyTest()
>>> '_PyTest__private' in dir(py)
True
>>> '__private' in dir(py)
False
>>> cy = CyTest()
>>> '_PyTest__private' in dir(cy)
True
>>> '__private' in dir(cy)
False
"""
class CyTest(object):
def __private(self): pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment