from cymem.cymem cimport Pool
from preshed.maps cimport PreshMap

from .features cimport ConjunctionExtracter
from ..typedefs cimport weight_t, feat_t, class_t
from ..structs cimport FeatureC
from ..structs cimport ExampleC

cdef class AveragedPerceptron:
    cdef readonly Pool mem
    cdef readonly PreshMap weights
    cdef readonly PreshMap averages
    cdef readonly PreshMap lasso_ledger
    cdef ConjunctionExtracter extracter
    cdef public int time
    cdef public weight_t learn_rate
    cdef public weight_t l1_penalty
    cdef public weight_t momentum

    cdef void set_scoresC(self, weight_t* scores, const FeatureC* feats, int nr_feat) nogil
    cdef int updateC(self, const ExampleC* eg) except -1
    cpdef int update_weight(self, feat_t feat_id, class_t clas, weight_t upd) except -1

from cymem.cymem cimport Pool
from preshed.maps cimport PreshMap

from .features cimport ConjunctionExtracter
from ..typedefs cimport weight_t, feat_t, class_t
from ..structs cimport FeatureC
from ..structs cimport ExampleC

cdef class AveragedPerceptron:
    cdef readonly Pool mem
    cdef readonly PreshMap weights
    cdef readonly PreshMap averages
    cdef readonly PreshMap lasso_ledger
    cdef ConjunctionExtracter extracter
    cdef public int time
    cdef readonly int nr_out
    cdef public weight_t learn_rate
    cdef public weight_t l1_penalty
    cdef public weight_t momentum

    cdef void set_scoresC(self, weight_t* scores, const FeatureC* feats, int nr_feat) nogil
    cdef int updateC(self, const ExampleC* eg) except -1
    cpdef int update_weight(self, feat_t feat_id, class_t clas, weight_t upd) except -1

Add nr_out attribute to AveragedPerceptron


# -*- mode: cython -*-
# -*- coding: utf-8 -*-

from libcpp cimport bool
from libcpp.vector cimport vector
from libcpp.utility cimport pair
from cvodes_cxx cimport LMM, IterType, LinSol, realtype, indextype

cdef extern from "cvodes_anyode_parallel.hpp" namespace "cvodes_anyode_parallel":
    cdef vector[pair[int, vector[int]]] multi_adaptive[U](
        realtype **,
        int *,
        vector[U*],
        vector[realtype],
        const realtype,
        const LMM,
        const realtype *,
        const long int,
        const realtype *,
        const realtype *,
        const realtype *,
        const bool,
        IterType,
        LinSol,
        const int,
        const realtype,
        const unsigned,
        const bool,
        const int,
        const bool,
        const bool
    ) except +

    cdef vector[pair[int, pair[vector[int], vector[realtype]]]] multi_predefined[U](
        vector[U*],
        vector[realtype],
        const realtype,
        const LMM,
        realtype *,
        const size_t,
        realtype *,
        realtype *,
        const long int,
        const realtype *,
        const realtype *,
        const realtype *,
        const bool,
        IterType,
        LinSol,
        const int,
        const realtype,
        const unsigned,
        const int,
        const bool,
        const bool
    ) except +

# -*- mode: cython -*-
# -*- coding: utf-8 -*-

from libcpp cimport bool
from libcpp.vector cimport vector
from libcpp.utility cimport pair
from cvodes_cxx cimport LMM, IterType, LinSol, realtype

cdef extern from "cvodes_anyode_parallel.hpp" namespace "cvodes_anyode_parallel":
    cdef vector[pair[int, vector[int]]] multi_adaptive[U](
        realtype **,
        int *,
        vector[U*],
        vector[realtype],
        const realtype,
        const LMM,
        const realtype *,
        const long int,
        const realtype *,
        const realtype *,
        const realtype *,
        const bool,
        IterType,
        LinSol,
        const int,
        const realtype,
        const unsigned,
        const bool,
        const int,
        const bool,
        const bool
    ) except +

    cdef vector[pair[int, pair[vector[int], vector[realtype]]]] multi_predefined[U](
        vector[U*],
        vector[realtype],
        const realtype,
        const LMM,
        realtype *,
        const size_t,
        realtype *,
        realtype *,
        const long int,
        const realtype *,
        const realtype *,
        const realtype *,
        const bool,
        IterType,
        LinSol,
        const int,
        const realtype,
        const unsigned,
        const int,
        const bool,
        const bool
    ) except +

Remove unused cimport of indextype

Remove unused cimport of indextype


cdef extern from *:
    """
    #if defined(WIN32) || defined(MS_WINDOWS)
      #include "synchapi.h"
      #define myapp_sleep(m)  Sleep(m)
    #else
      #include <unistd.h>
      #define myapp_sleep(m)  ((void) usleep((m) * 1000))
    #endif
    """
    # using "myapp_" prefix in the C code to prevent C naming conflicts
    void msleep "myapp_sleep"(int milliseconds) nogil

msleep(milliseconds=1)

cdef extern from *:
    """
    #if defined(WIN32) || defined(MS_WINDOWS)
      #include <synchapi.h>
      #define myapp_sleep(m)  Sleep(m)
    #else
      #include <unistd.h>
      #define myapp_sleep(m)  ((void) usleep((m) * 1000))
    #endif
    """
    # using "myapp_" prefix in the C code to prevent C naming conflicts
    void msleep "myapp_sleep"(int milliseconds) nogil

msleep(milliseconds=1)

Fix test compile failure in MSVC.

Fix test compile failure in MSVC.


# Cython/distutils can only handle a single file as the source for
# a python module.  Since it is helpful to be able to split the binding
# code into separate files, we just include them here.
#
# Note that the ordering of include statements may influence whether
# things work or not

include "nameserver.pyx"
include "truth_value.pyx"
include "atomspace_details.pyx"
include "atom.pyx"
include "protoatom.pyx"

Add newline at the end of file

Add newline at the end of file


cimport libc

cimport libc.stdio
cimport libc.errno
cimport libc.float
cimport libc.limits
cimport libc.locale
cimport libc.signal
cimport libc.stddef
cimport libc.stdint
cimport libc.stdio
cimport libc.stdlib
cimport libc.string

from libc cimport errno
from libc cimport float
from libc cimport limits
from libc cimport locale
from libc cimport signal
from libc cimport stddef
from libc cimport stdint
from libc cimport stdio
from libc cimport stdlib
from libc cimport string

from libc.errno  cimport *
from libc.float  cimport *
from libc.limits cimport *
from libc.locale cimport *
from libc.signal cimport *
from libc.stddef cimport *
from libc.stdint cimport *
from libc.stdio  cimport *
from libc.stdlib cimport *
from libc.string cimport *

libc.stdio.printf("hello %s\n", b"world")
stdio.printf("hello %s\n", b"world")
printf("hello %s\n", b"world")

cimport libc

cimport libc.stdio
cimport libc.errno
cimport libc.float
cimport libc.limits
cimport libc.locale
cimport libc.signal
cimport libc.stddef
#cimport libc.stdint # XXX MSVC
cimport libc.stdio
cimport libc.stdlib
cimport libc.string

from libc cimport errno
from libc cimport float
from libc cimport limits
from libc cimport locale
from libc cimport signal
from libc cimport stddef
#from libc cimport stdint # XXX MSVC
from libc cimport stdio
from libc cimport stdlib
from libc cimport string

from libc.errno  cimport *
from libc.float  cimport *
from libc.limits cimport *
from libc.locale cimport *
from libc.signal cimport *
from libc.stddef cimport *
#from libc.stdint cimport * # XXX MSVC
from libc.stdio  cimport *
from libc.stdlib cimport *
from libc.string cimport *

libc.stdio.printf("hello %s\n", b"world")
stdio.printf("hello %s\n", b"world")
printf("hello %s\n", b"world")

Disable test for libc.stdint (missing stdint.h in MSVC)


# Copyright 2014 Tom Rothamel <tom@rothamel.us>
#
# This software is provided 'as-is', without any express or implied
# warranty.  In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
#    claim that you wrote the original software. If you use this software
#    in a product, an acknowledgment in the product documentation would be
#    appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
#    misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.

from sdl2 cimport *

class error(RuntimeError):

    def __init__(self, message=None):
        if message is None:
            message = str(SDL_GetError())

        RuntimeError.__init__(self, message)

def get_error():
    cdef const char *message = SDL_GetError()

    if message:
        return str(message)
    else:
        return ''

def set_error(message):
    message = bytes(message)
    SDL_SetError(message)

# Copyright 2014 Tom Rothamel <tom@rothamel.us>
#
# This software is provided 'as-is', without any express or implied
# warranty.  In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
#    claim that you wrote the original software. If you use this software
#    in a product, an acknowledgment in the product documentation would be
#    appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
#    misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.

from sdl2 cimport *

class error(RuntimeError):

    def __init__(self, message=None):
        if message is None:
            message = str(SDL_GetError())

        RuntimeError.__init__(self, message)

def get_error():
    cdef const char *message = SDL_GetError()

    if message:
        return str(message)
    else:
        return ''

def set_error(message):
    message = bytes(message)
    SDL_SetError("%s", <char *> message)

Use format string with SDL_SetError.

Use format string with SDL_SetError.

Fixes #26, although the fix wasn't tested.


def fib(n):
    """Print the Fibonacci series up to n."""
    a, b = 0, 1
    while b < n:
        print(b)
        a, b = b, a + b

from __future__ import print_function

def fib(n):
    """Print the Fibonacci series up to n."""
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a + b

    print()

Change demo file back to original output style.


# mode: run
# tag: control-flow, uninitialized

def conditional(cond):
    """
    >>> conditional(True)
    []
    >>> conditional(False)
    Traceback (most recent call last):
    ...
    UnboundLocalError: a
    """
    if cond:
        a = []
    return a

def inside_loop(iter):
    """
    >>> inside_loop([1,2,3])
    3
    >>> inside_loop([])
    Traceback (most recent call last):
    ...
    UnboundLocalError: i
    """
    for i in iter:
        pass
    return i

def try_except(cond):
    """
    >>> try_except(True)
    []
    >>> try_except(False)
    Traceback (most recent call last):
    ...
    UnboundLocalError: a
    """
    try:
        if cond:
            a = []
        raise ValueError
    except ValueError:
        return a

def try_finally(cond):
    """
    >>> try_finally(True)
    []
    >>> try_finally(False)
    Traceback (most recent call last):
    ...
    UnboundLocalError: a
    """
    try:
        if cond:
            a = []
        raise ValueError
    finally:
        return a

def deleted(cond):
    """
    >>> deleted(False)
    {}
    >>> deleted(True)
    Traceback (most recent call last):
    ...
    UnboundLocalError: a
    """
    a = {}
    if cond:
        del a
    return a

Add testcase for local variable deletion


def process_byte_data(unsigned char[:] data, bint return_all):
    # ... process the data
    if return_all:
        return bytes(data)
    else:
        # example for returning a slice
        return bytes(data[5:35])

def process_byte_data(unsigned char[:] data):
    # ... process the data, here, dummy processing.
    cdef bint return_all = (data[0] == 108)

    if return_all:
        return bytes(data)
    else:
        # example for returning a slice
        return bytes(data[5:7])

Put a dummy processing in the function rather than asking the user for decision.


# ticket: 3111
# mode: compile
# tag: warnings

ctypedef unsigned char npy_uint8
ctypedef unsigned short npy_uint16

ctypedef fused dtype_t:
    npy_uint8

ctypedef fused dtype_t_out:
    npy_uint8
    npy_uint16

def foo(dtype_t[:] a, dtype_t_out[:, :] b):
    pass

# The primary thing we're trying to test here is the _absence_ of the warning
# "__pyxutil:16:4: '___pyx_npy_uint8' redeclared".  The remaining warnings are
# unrelated to this test.
_WARNINGS = """
# cpdef redeclaration bug, from TestCythonScope.pyx
25:10: 'cpdef_method' redeclared
36:10: 'cpdef_cname_method' redeclared
# from MemoryView.pyx
984:29: Ambiguous exception value, same as default return value: 0
984:29: Ambiguous exception value, same as default return value: 0
1011:46: Ambiguous exception value, same as default return value: 0
1011:46: Ambiguous exception value, same as default return value: 0
1101:29: Ambiguous exception value, same as default return value: 0
1101:29: Ambiguous exception value, same as default return value: 0
"""

# ticket: 3111
# mode: compile
# tag: warnings

ctypedef unsigned char npy_uint8
ctypedef unsigned short npy_uint16

ctypedef fused dtype_t:
    npy_uint8

ctypedef fused dtype_t_out:
    npy_uint8
    npy_uint16

def foo(dtype_t[:] a, dtype_t_out[:, :] b):
    pass

# The primary thing we're trying to test here is the _absence_ of the warning
# "__pyxutil:16:4: '___pyx_npy_uint8' redeclared".  The remaining warnings are
# unrelated to this test.
_WARNINGS = """
# cpdef redeclaration bug, from TestCythonScope.pyx
25:10: 'cpdef_method' redeclared
36:10: 'cpdef_cname_method' redeclared
# from MemoryView.pyx
987:29: Ambiguous exception value, same as default return value: 0
987:29: Ambiguous exception value, same as default return value: 0
1014:46: Ambiguous exception value, same as default return value: 0
1014:46: Ambiguous exception value, same as default return value: 0
1104:29: Ambiguous exception value, same as default return value: 0
1104:29: Ambiguous exception value, same as default return value: 0
"""

Fix unrelated test after changing MemoryView.pyx.


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Low level implementation of scalar `allclose`.

"""

cdef extern from "math.h":
    double fabs(double)
    int isinf(double)
    int isnan(double)

def close(double x, double y, double rtol, double atol):
    """Returns True if the scalars x and y are close.

    The relative error rtol must be positive and << 1.0
    The absolute error atol usually comes into play when y is very small or
    zero; it says how small x must be also.

    """
    # Test for nan
    if isnan(x) or isnan(y):
        return False

    # Make sure they are both inf or non-inf
    cdef int xinf, yinf
    xinf = isinf(x)
    yinf = isinf(y)

    if not xinf == yinf:
        return False

    cdef bint xgz, ygz
    if xinf:
        # If they are inf, make sure the signs are the same.
        xgz = x>0
        ygz = y>0
        if (xgz and not ygz) or (not xgz and ygz):
            return False
        else:
            return True
    else:
        # Otherwise, make sure they are close.
        return fabs(x-y) <= atol + rtol * fabs(y)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Low level implementation of scalar `allclose`.

"""

cdef extern from "math.h":
    double fabs(double)
    int isinf(double)
    int isnan(double)

def close(double x, double y, double rtol, double atol):
    """Returns True if the scalars x and y are close.

    The relative error rtol must be positive and << 1.0
    The absolute error atol usually comes into play when y is very small or
    zero; it says how small x must be also.

    """
    # Test for nan
    if isnan(x) or isnan(y):
        return False

    # Make sure they are both inf or non-inf
    cdef int xinf, yinf
    xinf = isinf(x)
    yinf = isinf(y)

    if not xinf == yinf:
        return False

    if xinf:
        # If they are both inf, make sure the signs are the same.
        return (x > 0) == (y > 0)
    else:
        # Otherwise, make sure they are close.
        return fabs(x-y) <= atol + rtol * fabs(y)

Simplify and speed up dit.math.close

Simplify and speed up dit.math.close


#------------------------------------------------------------------------------

cdef extern from "Python.h":
    object PyOS_FSPath(object)

cdef os_fsencode
from os import fsencode as os_fsencode

cdef object asmpifspath(object path, char *p[]):
    path = PyOS_FSPath(path)
    path = os_fsencode(path)
    PyBytes_AsStringAndSize(path, p, NULL)
    return path

#------------------------------------------------------------------------------

#------------------------------------------------------------------------------

cdef extern from "Python.h":
    object PyOS_FSPath(object)
    object PyUnicode_EncodeFSDefault(object)

cdef object asmpifspath(object path, char *p[]):
    path = PyOS_FSPath(path)
    if PyUnicode_Check(path):
        path = PyUnicode_EncodeFSDefault(path)
    PyBytes_AsStringAndSize(path, p, NULL)
    return path

#------------------------------------------------------------------------------

File: Use PyUnicode_EncodeFSDefault() to encode unicode paths


cdef extern from "<stdint.h>" nogil:
    ctypedef unsigned long long uint64_t

cdef extern from "fnv1a.h":
    void fnv1a(const char *foo, const int length, uint64_t *hash);

cdef class fnv1a64:
    cdef readonly uint64_t h

    def __cinit__(self, s=None):
        self.h = <uint64_t>0
        if s is not None:
            fnv1a(s, len(s), &self.h)

    cpdef void update(self, const char *s):
        fnv1a(s, len(s), &self.h)

    cpdef unsigned long long digest(self):
        return self.h

    cpdef char * hexdigest(self):
        return b'%x' % self.h

cdef extern from "<stdint.h>" nogil:
    ctypedef unsigned long long uint64_t

cdef extern from "fnv1a.h":
    void fnv1a(const char *foo, const int length, uint64_t *hash)

cdef class fnv1a64:

    cdef readonly uint64_t h
    name        = "fnv1a64"
    digestsize  = 8
    digest_size = 8

    def __cinit__(self, s=None):
        self.h = <uint64_t>0
        if s is not None:
            fnv1a(s, len(s), &self.h)

    cpdef void update(self, const char *s):
        fnv1a(s, len(s), &self.h)

    cpdef unsigned long long digest(self):
        return self.h

    cpdef char * hexdigest(self):
        return b'%x' % self.h

    cpdef void reset(self):
        self.h = <uint64_t>0

Make fnv object more like hashlib hashes


# This file is part of h5py, a Python interface to the HDF5 library.
#
# http://www.h5py.org
#
# Copyright 2008-2013 Andrew Collette and contributors
#
# License:  Standard 3-clause BSD; see "license.txt" for full license terms
#           and contributor agreement.

from defs cimport *

cdef class ObjectID:

    cdef object __weakref__
    cdef readonly hid_t id
    cdef public int locked              # Cannot be closed, explicitly or auto
    cdef object _hash
    cdef unsigned long _pyid

# Convenience functions
cdef hid_t pdefault(ObjectID pid)
cdef int is_h5py_obj_valid(ObjectID obj)

# Inheritance scheme (for top-level cimport and import statements):
#
# _objects, _proxy, h5fd, h5z
# h5i, h5r, utils
# _conv, h5t, h5s
# h5p
# h5d, h5a, h5f, h5g
# h5l

# This file is part of h5py, a Python interface to the HDF5 library.
#
# http://www.h5py.org
#
# Copyright 2008-2013 Andrew Collette and contributors
#
# License:  Standard 3-clause BSD; see "license.txt" for full license terms
#           and contributor agreement.

from defs cimport *

cdef class ObjectID:

    cdef object __weakref__
    cdef readonly hid_t id
    cdef public int locked              # Cannot be closed, explicitly or auto
    cdef object _hash
    cdef size_t _pyid

# Convenience functions
cdef hid_t pdefault(ObjectID pid)
cdef int is_h5py_obj_valid(ObjectID obj)

# Inheritance scheme (for top-level cimport and import statements):
#
# _objects, _proxy, h5fd, h5z
# h5i, h5r, utils
# _conv, h5t, h5s
# h5p
# h5d, h5a, h5f, h5g
# h5l

Use size_t to store Python object id

Use size_t to store Python object id


from av.video.frame cimport VideoFrame

cdef class VideoPlane(Plane):

    def __cinit__(self, VideoFrame frame, int index):

        for i in range(frame.format.ptr.nb_components):
            if frame.format.ptr.comp[i].plane == index:
                self.component = frame.format.components[i]
                break
        else:
            raise RuntimeError('could not find plane %d of %r' % (index, frame.format))

        self.buffer_size = (abs(self.frame.ptr.linesize[self.index])
                            * self.component.height)

    property width:
        """Pixel width of this plane."""
        def __get__(self):
            return self.component.width

    property height:
        """Pixel height of this plane."""
        def __get__(self):
            return self.component.height

from av.video.frame cimport VideoFrame

cdef class VideoPlane(Plane):

    def __cinit__(self, VideoFrame frame, int index):

        for i in range(frame.format.ptr.nb_components):
            if frame.format.ptr.comp[i].plane == index:
                self.component = frame.format.components[i]
                break
        else:
            raise RuntimeError('could not find plane %d of %r' % (index, frame.format))

        # Sometimes, linesize is negative (and that is meaningful). We are only
        # insisting that the buffer size be based on the extent of linesize, and
        # ignore it's direction.
        self.buffer_size = abs(self.frame.ptr.linesize[self.index]) * self.component.height

    property width:
        """Pixel width of this plane."""
        def __get__(self):
            return self.component.width

    property height:
        """Pixel height of this plane."""
        def __get__(self):
            return self.component.height

Document the motivation for the fix

Document the motivation for the fix


# coding: utf-8

#import datetime
from cpython.datetime cimport date, time, datetime, timedelta, PyDateTime_IMPORT

PyDateTime_IMPORT

def test_date(int year, int month, int day):
    '''
    >>> val = test_date(2012, 12, 31)
    >>> print(val)
    2012-12-31
    '''
    val = date(year, month, day)
    return val

def test_time(int hour, int minute, int second, int microsecond):
    '''
    >>> val = test_time(12, 20, 55, 0)
    >>> print(val)
    12:20:55
    '''
    val = time(hour, minute, second, microsecond)
    return val

def test_datetime(int year, int month, int day, int hour, int minute, int second, int microsecond):
    '''
    >>> val = test_datetime(2012, 12, 31, 12, 20, 55, 0)
    >>> print(val)
    2012-12-31 12:20:55
    '''
    val = datetime(year, month, day, hour, minute, second, microsecond)
    return val

def test_timedelta(int days, int seconds, int useconds):
    '''
    >>> val = test_timedelta(30, 0, 0)
    >>> print(val)
    30 days, 0:00:00
    '''
    val = timedelta(days, seconds, useconds)
    return val

Add new line at the end of file

Add new line at the end of file


# cython: binding=True
# mode: run
# tag: cyfunction

def test_dict():
    """
    >>> test_dict.foo = 123
    >>> test_dict.__dict__
    {'foo': 123}
    """

def test_name():
    """
    >>> test_name.__name__
    'test_name'
    """

def test_reduce():
    """
    >>> import pickle
    >>> pickle.loads(pickle.dumps(test_reduce))()
    'Hello, world!'
    """
    return 'Hello, world!'

def test_method(self):
    return self

class BindingTest:
    """
    >>> BindingTest.test_method = test_method
    >>> BindingTest.test_method() #doctest:+ELLIPSIS
    Traceback (most recent call last):
    TypeError: ...
    >>> BindingTest().test_method()
    <BindingTest instance>
    """
    def __repr__(self):
        return '<BindingTest instance>'

# cython: binding=True
# mode: run
# tag: cyfunction

def test_dict():
    """
    >>> test_dict.foo = 123
    >>> test_dict.__dict__
    {'foo': 123}
    >>> test_dict.__dict__ = {'bar': 321}
    >>> test_dict.__dict__
    {'bar': 321}
    >>> test_dict.func_dict
    {'bar': 321}
    """

def test_name():
    """
    >>> test_name.__name__
    'test_name'
    >>> test_name.func_name
    'test_name'
    >>> test_name.__name__ = 123 #doctest:+ELLIPSIS
    Traceback (most recent call last):
    TypeError: __name__ must be set to a ... object
    >>> test_name.__name__ = 'foo'
    >>> test_name.__name__
    'foo'
    """

def test_doc():
    """
    >>> del test_doc.__doc__
    >>> test_doc.__doc__
    >>> test_doc.__doc__ = 'docstring'
    >>> test_doc.__doc__
    'docstring'
    >>> test_doc.func_doc
    'docstring'
    """

def test_reduce():
    """
    >>> import pickle
    >>> pickle.loads(pickle.dumps(test_reduce))()
    'Hello, world!'
    """
    return 'Hello, world!'

def test_method(self):
    return self

class BindingTest:
    """
    >>> BindingTest.test_method = test_method
    >>> BindingTest.test_method() #doctest:+ELLIPSIS
    Traceback (most recent call last):
    TypeError: ...
    >>> BindingTest().test_method()
    <BindingTest instance>
    """
    def __repr__(self):
        return '<BindingTest instance>'

Add some more test cases

Add some more test cases


import sys
import numpy as np
cimport numpy as cnp

if sys.version_info[0] == 3:
    INTEGER_TYPES = (int, np.integer)
else:
    INTEGER_TYPES = (int, long, np.integer)

# Annoying boilerplate that we shouldn't have to write; Cython should
# have this built in (some versions do, but the ctypedefs are still lacking,
# or what is available is Cython version dependent)
ctypedef object (*binaryfunc)(object, object)

cdef extern from "Python.h":
    ctypedef struct PyMappingMethods:
        binaryfunc mp_subscript

    ctypedef struct PyTypeObject:
        PyMappingMethods* tp_as_mapping

ndarray = np.ndarray
ctypedef cnp.ndarray ndarray_t

cdef class _ColumnGetitemShim:
    def __getitem__(self, item):
        if (<ndarray_t>self).ndim > 1 and isinstance(item, INTEGER_TYPES):
            return self.data[item]
        else:
            return (<PyTypeObject *>ndarray).tp_as_mapping.mp_subscript(self, item)

MaskedArray = np.ma.MaskedArray

cdef class _MaskedColumnGetitemShim(_ColumnGetitemShim):
    def __getitem__(self, item):
        if (<ndarray_t>self).ndim > 1 and isinstance(item, INTEGER_TYPES):
            return self.data[item]
        else:
            return MaskedArray.__getitem__(self, item)

import sys
import numpy as np

if sys.version_info[0] == 3:
    INTEGER_TYPES = (int, np.integer)
else:
    INTEGER_TYPES = (int, long, np.integer)

# Annoying boilerplate that we shouldn't have to write; Cython should
# have this built in (some versions do, but the ctypedefs are still lacking,
# or what is available is Cython version dependent)
ctypedef object (*binaryfunc)(object, object)

cdef extern from "Python.h":
    ctypedef struct PyMappingMethods:
        binaryfunc mp_subscript

    ctypedef struct PyTypeObject:
        PyMappingMethods* tp_as_mapping

cdef extern from "numpy/arrayobject.h":
    ctypedef class numpy.ndarray [object PyArrayObject]:
        cdef int ndim "nd"

cdef class _ColumnGetitemShim:
    def __getitem__(self, item):
        if (<ndarray>self).ndim > 1 and isinstance(item, INTEGER_TYPES):
            return self.data[item]
        else:
            return (<PyTypeObject *>ndarray).tp_as_mapping.mp_subscript(self, item)

MaskedArray = np.ma.MaskedArray

cdef class _MaskedColumnGetitemShim(_ColumnGetitemShim):
    def __getitem__(self, item):
        if (<ndarray>self).ndim > 1 and isinstance(item, INTEGER_TYPES):
            return self.data[item]
        else:
            return MaskedArray.__getitem__(self, item)

Use a very minimal typedef of the ndarray class (the only struct member we need access to in this code is 'nd')--this allows us to avoid a global variable lookup of 'ndarray' when calling 'ndarray.tp_as_mapping.mp_subscript'. Now, in the common case of ndim==1, Column (but not MaskedColumn) adds virtually no overhead compared to the built-in __getitem__.


# ticket: 3111
# mode: compile
# tag: warnings

ctypedef unsigned char npy_uint8
ctypedef unsigned short npy_uint16

ctypedef fused dtype_t:
    npy_uint8

ctypedef fused dtype_t_out:
    npy_uint8
    npy_uint16

def foo(dtype_t[:] a, dtype_t_out[:, :] b):
    pass

# The primary thing we're trying to test here is the _absence_ of the warning
# "__pyxutil:16:4: '___pyx_npy_uint8' redeclared".  The remaining warnings are
# unrelated to this test.
_WARNINGS = """
20:10: 'cpdef_method' redeclared
31:10: 'cpdef_cname_method' redeclared
446:72: Argument evaluation order in C function call is undefined and may not be as expected
446:72: Argument evaluation order in C function call is undefined and may not be as expected
749:34: Argument evaluation order in C function call is undefined and may not be as expected
749:34: Argument evaluation order in C function call is undefined and may not be as expected
"""

# ticket: 3111
# mode: compile
# tag: warnings

ctypedef unsigned char npy_uint8
ctypedef unsigned short npy_uint16

ctypedef fused dtype_t:
    npy_uint8

ctypedef fused dtype_t_out:
    npy_uint8
    npy_uint16

def foo(dtype_t[:] a, dtype_t_out[:, :] b):
    pass

# The primary thing we're trying to test here is the _absence_ of the warning
# "__pyxutil:16:4: '___pyx_npy_uint8' redeclared".  The remaining warnings are
# unrelated to this test.
_WARNINGS = """
22:10: 'cpdef_method' redeclared
33:10: 'cpdef_cname_method' redeclared
446:72: Argument evaluation order in C function call is undefined and may not be as expected
446:72: Argument evaluation order in C function call is undefined and may not be as expected
749:34: Argument evaluation order in C function call is undefined and may not be as expected
749:34: Argument evaluation order in C function call is undefined and may not be as expected
943:27: Ambiguous exception value, same as default return value: 0
943:27: Ambiguous exception value, same as default return value: 0
974:29: Ambiguous exception value, same as default return value: 0
974:29: Ambiguous exception value, same as default return value: 0
1002:46: Ambiguous exception value, same as default return value: 0
1002:46: Ambiguous exception value, same as default return value: 0
1092:29: Ambiguous exception value, same as default return value: 0
1092:29: Ambiguous exception value, same as default return value: 0
"""

Revert "Fix test." (which only applied to the 0.29.x branch).

This reverts commit bfcaf163e3dcf472ef2af5dec6ac3fffd0d34e66.


# Copyright 2019 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cdef bint _grpc_aio_initialized = 0

def init_grpc_aio():
    global _grpc_aio_initialized

    if _grpc_aio_initialized:
        return

    install_asyncio_iomgr()
    grpc_init()

    # Timers are triggered by the Asyncio loop. We disable
    # the background thread that is being used by the native
    # gRPC iomgr.
    grpc_timer_manager_set_threading(0)

    # gRPC callbaks are executed within the same thread used by the Asyncio
    # event loop, as it is being done by the other Asyncio callbacks.
    Executor.SetThreadingAll(0)

    _grpc_aio_initialized = 1

# Copyright 2019 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cdef bint _grpc_aio_initialized = 0

def init_grpc_aio():
    global _grpc_aio_initialized

    if _grpc_aio_initialized:
        return

    install_asyncio_iomgr()
    grpc_init()

    # Timers are triggered by the Asyncio loop. We disable
    # the background thread that is being used by the native
    # gRPC iomgr.
    grpc_timer_manager_set_threading(False)

    # gRPC callbaks are executed within the same thread used by the Asyncio
    # event loop, as it is being done by the other Asyncio callbacks.
    Executor.SetThreadingAll(False)

    _grpc_aio_initialized = 1

Use False instead 0 for bint

Use False instead 0 for bint


from .itertoolz cimport (groupby, frequencies, reduceby,
                         first, second, nth, take, drop, rest, last,
                         get, concat, concatv, isdistinct, interleave,
                         interpose, unique, isiterable, remove, iterate,
                         accumulate, partition, count, cons, take_nth)

from .functoolz cimport (memoize, c_memoize, curry, c_compose, c_thread_first,
                         c_thread_last, identity, c_pipe, complement, c_juxt,
                         do)

from .dicttoolz cimport (c_merge, c_merge_with, keymap, valmap, assoc,
                         keyfilter, valfilter)

from cytoolz.itertoolz cimport (
    accumulate, cons, count, drop, get, groupby, first, frequencies,
    interleave, interpose, isdistinct, isiterable, iterate, last, nth,
    partition, reduceby, remove, rest, second, take, take_nth, unique)

from cytoolz.functoolz cimport (
    c_compose, c_juxt, c_memoize, c_pipe, c_thread_first, c_thread_last,
    complement, curry, do, identity, memoize)

from cytoolz.dicttoolz cimport (
    assoc, c_merge, c_merge_with, keyfilter, keymap, valfilter, valmap)

Use absolute `cimport` in *.pxd files.  Relative cimporting doesn't appear to work.

Also, alphabetize import in "cytoolz/__init__.pxd" (just because).  The main
reason we don't do `from cytoolz.itertoolz cimport *` is so there is a quick
and easy reference for what is cimport-able.


# mode: error

cdef struct S:
    int m

def f(a):
    cdef int i, x[2]
    cdef S s
    global j
    del f() # error
    del i # error: deletion of non-Python object
    del j # error: deletion of non-Python object
    del x[i] # error: deletion of non-Python object
    del s.m # error: deletion of non-Python object
_ERRORS = u"""
10:9: Cannot assign to or delete this
11:48: Deletion of non-Python, non-C++ object
13:9: Deletion of non-Python, non-C++ object
14:9: Deletion of non-Python, non-C++ object
"""

# mode: error

cdef struct S:
    int m

def f(a):
    cdef int i, x[2]
    cdef S s
    global j
    del f() # error
    del i # error: deletion of non-Python object
    del j # error: deletion of non-Python object
    del x[i] # error: deletion of non-Python object
    del s.m # error: deletion of non-Python object

def outer(a):
    def inner():
        print a
    del a
    return inner()

_ERRORS = u"""
10:9: Cannot assign to or delete this
11:48: Deletion of non-Python, non-C++ object
13:9: Deletion of non-Python, non-C++ object
14:9: Deletion of non-Python, non-C++ object
19:9: can not delete variable 'a' referenced in nested scope
"""

Add testcase for closure variable deletion


# mode: compile
# tag: pep492, await

# Need to include all utility code !

async def sleep(x):
    pass

async def call():
    await sleep(1)
    yield

Fix test setup for compile-only test file.


cimport cython.array
cimport numpy
import numpy

cdef extern from "stdint.h":
    ctypedef unsigned long uint64_t

cdef extern from "inform/time_series.h":
    double inform_active_info(const uint64_t* series, size_t n, uint64_t base, uint64_t k)

def activeinfo(xs, uint64_t k, uint64_t b = 0):
    if len(xs) == 0:
        raise ValueError("active information not defined on empty containers")

    if b < 2:
        b = max(2,max(xs)+1)

    cdef uint64_t [:] ys = numpy.asarray(xs, dtype=numpy.uint64)
    return inform_active_info(&ys[0], <uint64_t>len(xs), b, k)

cimport cython.array
cimport numpy
import numpy

cdef extern from "stdint.h":
    ctypedef unsigned long uint64_t

cdef extern from "inform/time_series.h":
    double inform_active_info(const uint64_t* series, size_t n, uint64_t base, uint64_t k)

def activeinfo(xs, uint64_t k, uint64_t b = 0):
    from math import isnan

    if len(xs) < k+1 or len(xs) == 0:
        raise ValueError("container is too short ({0}) for history length ({1})".format(len(xs),k))

    if k == 0:
        raise ValueError("history length is too short")

    if b < 2:
        b = max(2,max(xs)+1)

    cdef uint64_t [:] ys = numpy.asarray(xs, dtype=numpy.uint64)
    ai = inform_active_info(&ys[0], <uint64_t>len(xs), b, k)

    if isnan(ai):
        raise ValueError("invalid active information computed (NaN)")
    return ai

Update code to pass tests

Update code to pass tests


from libc.stdint cimport uint64_t
from libc.stdint cimport uint32_t
from libc.stdint cimport uint16_t
from libc.stdint cimport int32_t

ctypedef double weight_t
ctypedef uint64_t atom_t
ctypedef uint64_t feat_t
ctypedef uint64_t hash_t
ctypedef int32_t class_t
ctypedef uint32_t count_t
ctypedef uint32_t time_t
ctypedef int32_t len_t
ctypedef int32_t idx_t

from libc.stdint cimport uint64_t
from libc.stdint cimport uint32_t
from libc.stdint cimport uint16_t
from libc.stdint cimport int32_t

ctypedef float weight_t
ctypedef uint64_t atom_t
ctypedef uint64_t feat_t
ctypedef uint64_t hash_t
ctypedef int32_t class_t
ctypedef uint32_t count_t
ctypedef uint32_t time_t
ctypedef int32_t len_t
ctypedef int32_t idx_t

Use 32 bit floats, not 64 bit.

Use 32 bit floats, not 64 bit.


# tag: warnings

cpdef str a = "123"
cpdef b = 2

cdef class C:
    cpdef float c

def func():
    cpdef d=C()
    return d

_WARNINGS = """
3:6: cpdef variables will not be supported in Cython 3; currently they are no different from cdef variables
4:6: cpdef variables will not be supported in Cython 3; currently they are no different from cdef variables
7:10: cpdef variables will not be supported in Cython 3; currently they are no different from cdef variables
10:10: cpdef variables will not be supported in Cython 3; currently they are no different from cdef variables
"""

# tag: warnings

cpdef str a = "123"
cpdef b = 2

cdef class C:
    cpdef float c

def func():
    """
    >>> c = func()
    >>> isinstance(c, C) or c
    True
    """
    cpdef d = C()
    return d

_WARNINGS = """
3:6: cpdef variables will not be supported in Cython 3; currently they are no different from cdef variables
4:6: cpdef variables will not be supported in Cython 3; currently they are no different from cdef variables
7:10: cpdef variables will not be supported in Cython 3; currently they are no different from cdef variables
15:10: cpdef variables will not be supported in Cython 3; currently they are no different from cdef variables
"""

Make a working test actually test something.


from libc.string cimport memcpy, memset
from libc.stdint cimport uint32_t
from ..vocab cimport EMPTY_LEXEME
from ..structs cimport Entity
from ..lexeme cimport Lexeme
from ..symbols cimport punct
from ..attrs cimport IS_SPACE

cdef class StateClass:
    def __init__(self, int length):
        cdef Pool mem = Pool()
        self.mem = mem

    def __dealloc__(self):
        del self.c

    @property
    def stack(self):
        return {self.S(i) for i in range(self.c._s_i)}

    @property
    def queue(self):
        return {self.B(i) for i in range(self.c._b_i)}

    def print_state(self, words):
        words = list(words) + ['_']
        top = words[self.S(0)] + '_%d' % self.S_(0).head
        second = words[self.S(1)] + '_%d' % self.S_(1).head
        third = words[self.S(2)] + '_%d' % self.S_(2).head
        n0 = words[self.B(0)]
        n1 = words[self.B(1)]
        return ' '.join((third, second, top, '|', n0, n1))

from libc.string cimport memcpy, memset
from libc.stdint cimport uint32_t
from ..vocab cimport EMPTY_LEXEME
from ..structs cimport Entity
from ..lexeme cimport Lexeme
from ..symbols cimport punct
from ..attrs cimport IS_SPACE

cdef class StateClass:
    def __init__(self, int length):
        cdef Pool mem = Pool()
        self.mem = mem

    def __dealloc__(self):
        del self.c

    @property
    def stack(self):
        return {self.S(i) for i in range(self.c._s_i)}

    @property
    def queue(self):
        return {self.B(i) for i in range(self.c.buffer_length())}

    def print_state(self, words):
        words = list(words) + ['_']
        top = words[self.S(0)] + '_%d' % self.S_(0).head
        second = words[self.S(1)] + '_%d' % self.S_(1).head
        third = words[self.S(2)] + '_%d' % self.S_(2).head
        n0 = words[self.B(0)]
        n1 = words[self.B(1)]
        return ' '.join((third, second, top, '|', n0, n1))

Fix queue Python property in StateClass

Fix queue Python property in StateClass


# ticket: 3111
# mode: compile
# tag: warnings

ctypedef unsigned char npy_uint8
ctypedef unsigned short npy_uint16

ctypedef fused dtype_t:
    npy_uint8

ctypedef fused dtype_t_out:
    npy_uint8
    npy_uint16

def foo(dtype_t[:] a, dtype_t_out[:, :] b):
    pass

# The primary thing we're trying to test here is the _absence_ of the warning
# "__pyxutil:16:4: '___pyx_npy_uint8' redeclared".  The remaining warnings are
# unrelated to this test.
_WARNINGS = """
# cpdef redeclaration bug
22:10: 'cpdef_method' redeclared
33:10: 'cpdef_cname_method' redeclared
# from MemoryView.pyx
974:29: Ambiguous exception value, same as default return value: 0
974:29: Ambiguous exception value, same as default return value: 0
1001:46: Ambiguous exception value, same as default return value: 0
1001:46: Ambiguous exception value, same as default return value: 0
1091:29: Ambiguous exception value, same as default return value: 0
1091:29: Ambiguous exception value, same as default return value: 0
"""

# ticket: 3111
# mode: compile
# tag: warnings

ctypedef unsigned char npy_uint8
ctypedef unsigned short npy_uint16

ctypedef fused dtype_t:
    npy_uint8

ctypedef fused dtype_t_out:
    npy_uint8
    npy_uint16

def foo(dtype_t[:] a, dtype_t_out[:, :] b):
    pass

# The primary thing we're trying to test here is the _absence_ of the warning
# "__pyxutil:16:4: '___pyx_npy_uint8' redeclared".  The remaining warnings are
# unrelated to this test.
_WARNINGS = """
# cpdef redeclaration bug
22:10: 'cpdef_method' redeclared
33:10: 'cpdef_cname_method' redeclared
# from MemoryView.pyx
981:29: Ambiguous exception value, same as default return value: 0
981:29: Ambiguous exception value, same as default return value: 0
1008:46: Ambiguous exception value, same as default return value: 0
1008:46: Ambiguous exception value, same as default return value: 0
1098:29: Ambiguous exception value, same as default return value: 0
1098:29: Ambiguous exception value, same as default return value: 0
"""


# mode: run
# tag: numpy

import numpy as np

cdef class ExtensionType(object):
    cdef public int dummy

    def __init__(self, n):
        self.dummy = n

items = [ExtensionType(1), ExtensionType(2)]
cdef ExtensionType[:] view = np.array(items, dtype=ExtensionType)

def test_getitem():
    """
    >>> test_getitem()
    1
    2
    """
    for i in range(view.shape[0]):
        item = view[i]
        print item.dummy

def test_getitem_typed():
    """
    >>> test_getitem_typed()
    1
    2
    """
    cdef ExtensionType item
    for i in range(view.shape[0]):
        item = view[i]
        print item.dummy

Add test for memoryview of extension type

A test for a bug fixed in commit 478b939a4c99a77c0863cfd.

v2: add commit link above
v3: # tag: instead of # tags:, drop cpp tag as it means something different
    that I originally thought

There was a bug that produced C code where gcc emitted warnings:
extension_type_memoryview.c: In function ‘__pyx_pf_25extension_type_memoryview_test_getitem’:
extension_type_memoryview.c:1468:15: warning: assignment from incompatible pointer type
extension_type_memoryview.c: In function ‘__pyx_pf_25extension_type_memoryview_2test_getitem_typed’:
extension_type_memoryview.c:1565:15: warning: assignment from incompatible pointer type
extension_type_memoryview.c:1568:18: warning: assignment from incompatible pointer type

And g++ failed with errors:
extension_type_memoryview.c: In function ‘PyObject* __pyx_pf_25extension_type_memoryview_test_getitem(PyObject*)’:
extension_type_memoryview.c:1468:213: error: cannot convert ‘__pyx_obj_25extension_type_memoryview_ExtensionType*’ to ‘PyObject*’ in assignment
extension_type_memoryview.c: In function ‘PyObject* __pyx_pf_25extension_type_memoryview_2test_getitem_typed(PyObject*)’:
extension_type_memoryview.c:1565:213: error: cannot convert ‘__pyx_obj_25extension_type_memoryview_ExtensionType*’ to ‘PyObject*’ in assignment
extension_type_memoryview.c:1568:20: error: cannot convert ‘PyObject*’ to ‘__pyx_obj_25extension_type_memoryview_ExtensionType*’ in assignment

--HG--
extra : transplant_source : %02N%D4%B99N%D6%FBv%7C%F0%94%E5%BE%CE%C9t%D6%04%11


# mode: run
# tag: control-flow, uninitialized

def conditional(cond):
    """
    >>> conditional(True)
    []
    >>> conditional(False)
    Traceback (most recent call last):
    ...
    UnboundLocalError: a
    """
    if cond:
        a = []
    return a

def inside_loop(iter):
    """
    >>> inside_loop([1,2,3])
    3
    >>> inside_loop([])
    Traceback (most recent call last):
    ...
    UnboundLocalError: i
    """
    for i in iter:
        pass
    return i

def try_except(cond):
    """
    >>> try_except(True)
    []
    >>> try_except(False)
    Traceback (most recent call last):
    ...
    UnboundLocalError: a
    """
    try:
        if cond:
            a = []
        raise ValueError
    except ValueError:
        return a

def try_finally(cond):
    """
    >>> try_finally(True)
    []
    >>> try_finally(False)
    Traceback (most recent call last):
    ...
    UnboundLocalError: a
    """
    try:
        if cond:
            a = []
        raise ValueError
    finally:
        return a

Add some simple run tests for control-flow


# distutils: language = c++
# distutils: extra_compile_args = -fno-rtti -fno-exceptions

from enum import IntEnum

from libcpp cimport bool
from libcpp.vector cimport vector
from cython.operator import dereference

class ShaderType(IntEnum):
    Compute = 0
    Vertex = 1
    TessControl = 2
    TessEvaluation = 3
    Geometry = 4
    Fragment = 5

cdef extern from '../glsl-parser/ast.h' namespace 'glsl':
    cdef cppclass astType:
        bool builtin

    cdef cppclass astBuiltin:
        int type

    cdef cppclass astFunction:
        char* name
        astType* returnType
        bool isPrototype

    cdef cppclass astTU:
        astTU(int type)
        vector[astFunction*] functions

cdef extern from '../glsl-parser/parser.h' namespace 'glsl':
    cdef cppclass parser:
        parser(const char* source, const char* fileName)
        astTU* parse(int type)
        const char* error() const

cdef class Parser:
    cdef parser* c_parser

    def __cinit__(self, source, filename=''):
        self.c_parser = new parser(source.encode(), filename.encode())

    def __dealloc__(self):
        del self.c_parser

    def parse(self, shader_type):
        c_ast = self.c_parser.parse(int(shader_type))
        if c_ast:
            ast = Ast()
            ast.c_ast = c_ast
            return ast
        else:
            return None

    def error(self):
        return self.c_parser.error().decode()

cdef class Ast:
    cdef astTU* c_ast

    def functions(self):
        for c_function in self.c_ast.functions:
            func = Function()
            func.c_function = c_function
            yield func

cdef class Type:
    # TODO, not sure how this is supposed to be done
    cdef astType* c_type

    def builtin(self):
        return self.c_type.builtin

    def __repr__(self):
        if self.builtin():
            c_builtin = <astBuiltin*>(self.c_type)
            return 'TODO, typen={}'.format(c_builtin.type)
        else:
            raise NotImplementedError()

cdef class Function:
    cdef astFunction* c_function

    def name(self):
        return self.c_function.name.decode()

    def is_prototype(self):
        return self.c_function.isPrototype

    def return_type(self):
        typ = Type()
        typ.c_type = self.c_function.returnType
        return typ

    def __repr__(self):
        body = ';' if self.is_prototype() else '{...}'
        return '{ret} {name}(){body}'.format(ret=self.return_type(),
                                             name=self.name(),
                                             body=body)

Add initial Cython pyx file for glsl-parser


cdef public int broadcast_arrays(list arrays, tuple broadcast_shape, int ndim,
                                 cnp.npy_intp **shape_out,
                                 cnp.npy_intp **strides_out) except -1:
    """
    Broadcast the given arrays. Returns the total broadcast shape of size
    ndim in shape_out. Returns the strides for each array in strides_out
    in an array of size len(arrays) * ndim.

    Note: Leading broadcasting dimensions should not have strides if the
          minivect function was compiled using an array type with less
          dimensions than `ndim`. Instead, the actual strides should be given
          without leading zero strides.
    """
    cdef cnp.ndarray array
    cdef int i, j, start, stop

    cdef cnp.npy_intp *shape = <cnp.npy_intp *> stdlib.malloc(
                                              ndim * sizeof(cnp.npy_intp))
    cdef cnp.npy_intp *strides_list = <cnp.npy_intp *> stdlib.malloc(
                                len(arrays) * ndim * sizeof(cnp.npy_intp))

    if shape == NULL or strides_list == NULL:
        raise MemoryError

    # Build a shape list of size ndim
    for i in range(ndim):
        shape[i] = broadcast_shape[i]

    # Build a strides list for all arrays of size ndim
    for i, array in enumerate(arrays):
        start = i * ndim

        if array.ndim < ndim:
            # broadcast leading dimensions
            for j in range(start, start + ndim - array.ndim):
                strides_list[j] = 0
            start = j

        for j in range(ndim):
            if array.shape[j] == 1:
                strides_list[start + j] = 0
            else:
                strides_list[start + j] = cnp.PyArray_STRIDE(array, j)

    # for j in range(ndim):
    #     print 'shape%d:' % j, shape[j]

    # for i in range(len(arrays)):
    #     for j in range(ndim):
    #         print 'stride%d:' % j, strides_list[i * ndim + j]
    #     print

    shape_out[0] = shape
    strides_out[0] = strides_list
    return 0

cdef public bint is_broadcasting(list arrays, broadcast) except -1:
    for array in arrays:
        if broadcast.nd != array.ndim or array.shape != broadcast.shape:
            return True

    return False

Add some broadcasting Cython utilities

Add some broadcasting Cython utilities


__doc__ = """
   >>> B().coeffs_bitsize()
   [2]
"""

cdef class A:
    def numerator(self):
        return self

cdef int  bitsize(A a):
    return 1

coeffs = [A()]

class B:
    def coeffs_bitsize(self):
        r = [bitsize(c.numerator())+1 for c in coeffs]
        return r

Add corner case temp (de)allocation test.

This is as simple as I could get it.


# mode: run
# tag: cpp, werror, cpp11

from libcpp cimport nullptr
from libcpp.memory cimport shared_ptr, make_shared
from libcpp.utility cimport move
from cython.operator cimport dereference

cdef extern from *:
    """
    #include <string>

    template<typename T> const char* move_helper(T&) { return "lvalue-ref"; }
    template<typename T> const char* move_helper(T&&) { return "rvalue-ref"; }
    """
    const char* move_helper[T](T)

def test_move_assignment():
    """
    >>> test_move_assignment()
    """
    cdef shared_ptr[int] p1, p2
    p1 = make_shared[int](1337)
    p2 = move(p1)
    assert p1 == nullptr
    assert dereference(p2) == 1337

def test_move_func_call():
    """
    >>> test_move_func_call()
    """
    cdef shared_ptr[int] p
    assert move_helper(p) == b'lvalue-ref'
    assert move_helper(move(p)) == b'rvalue-ref'

Add tests for std::move wrapper

Add tests for std::move wrapper


# mode: error
# tags: werror

cdef foo():
    pass

def foo():
    pass

_ERRORS = u"""
7:0: 'foo' redeclared
7:0: Overriding cdef method with def method.
"""

Add simple test for cdef override warning


# coding=utf-8
__doc__ = u"""
    >>> do_slice(u'abcdef', 2, 3)
    (u'c', u'cdef', u'ab', u'abcdef')
    >>> do_slice(u'abcdef', 0, 5)
    (u'abcde', u'abcdef', u'', u'abcdef')
"""

import sys

if sys.version_info[0] >= 3:
    __doc__ = __doc__.replace(u"(u'", u"('").replace(u" u'", u" '")

def do_slice(unicode s, int i, int j):
    return s[i:j], s[i:], s[:i], s[:]

Add support for unicode slicing

Add support for unicode slicing


from .parser cimport Parser
from ..structs cimport TokenC
from thinc.typedefs cimport weight_t

cdef class BeamParser(Parser):
    cdef public int beam_width
    cdef public weight_t beam_density

    cdef int _parseC(self, TokenC* tokens, int length, int nr_feat, int nr_class) except -1

Add header for beam parser

Add header for beam parser


from cpython.type cimport PyType_IsSubtype

class mylist(list): pass

def test_issubtype(a, b):
    """
    >>> test_issubtype(mylist, list)
    True
    >>> test_issubtype(mylist, dict)
    False

    >>> o = object()
    >>> test_issubtype(o, list)
    Traceback (most recent call last):
    ...
    TypeError: Cannot convert object to type
    """
    return PyType_IsSubtype(a, b)

Add run test for type ctypedef and PyType_IsSubtype.


cdef extern from "folly/Range.h" namespace "folly":
    cdef cppclass Range[T]:
        Range()
        Range(T, int)
        T data()
        int size()

ctypedef Range[const char*] StringPiece
ctypedef Range[const unsigned char*] ByteRange

ctypedef fused R:
    StringPiece
    ByteRange

# Conversion Helpers
cdef inline bytes to_bytes(R range):
    return <bytes>range.data()[:range.size()]

Add ByteRange/StringPiece Conversion for Cython

Summary:
Moving the cython definition to folly/python.
Adding simple conversion helper to_bytes

This is to cut down on duplicate folly::range cython definitions

Reviewed By: yfeldblum

Differential Revision: D6291125

fbshipit-source-id: 314b732a1516a03fb5c9a57939552bbabd81970b


"""
This demo depicts how we can support overriding of numba methods in python.
In Cython, cpdef methods check to see if the object has a dict, and if so
look up the method at runtime and dispatch if found:

    cpdef mymethod(self):
        if hasattr(self, '__dict__'):
            return self.mymethod()

        <method body>

This is needed for calls that go through the vtable, and hence hit the
inherited Cython version, but not the Python one (see the 'obj.mymethod' call
of a typed object below in 'func').

We can use a metaclass to detect overridden methods and patch the vtable (
create a new one for each class). We can further detect monkey-patching on
the class and instances through descriptors:

    * monkey patches on the class mutate the vtable
    * monkey patches on the object create a new object-specific vtable
        - or perhaps are simply disallowed
"""

import ctypes

from cpython cimport PyObject

cdef extern from *:
    ctypedef unsigned int Py_uintptr_t

    struct __pyx_obj_4test_Base:
        void *__pyx_vtab

    ctypedef struct PyTypeObject:
        PyObject *ob_type

cdef mymethod_wrapper(self, int arg):
    self.mymethod(<object> arg)

class VTable(ctypes.Structure):
    _fields_ = [('mymethod', ctypes.c_void_p)]

cdef class MetaClass(type):

    cdef public object vtable

    def __init__(self, name, bases, dict):
        if "mymethod" in dict:
            mymethod_p = <Py_uintptr_t> &mymethod_wrapper
            self.vtable = VTable(ctypes.c_void_p(mymethod_p))
        else:
            self.vtable = None

# --- Test classes ---

cdef class Base(object):
    cdef mymethod(self, int arg):
        print "mymethod in Base", arg

(<PyTypeObject *> Base).ob_type = <PyObject *> MetaClass

class Derived(Base):
    def __init__(self):
        cdef Py_uintptr_t vtable_p

        vtable = type(self).vtable

        if vtable is not None:
            vtable_p = ctypes.addressof(vtable)
            (<__pyx_obj_4test_Base *> self).__pyx_vtab = <void *> vtable_p

    def mymethod(self, int arg):
        print "mymethod in Derived", arg

cdef func(Base obj):
    obj.mymethod(10)

func(Base()) # prints: mymethod in Base 10
func(Derived()) # prints: mymethod in Derived 10

Add prototype for method overriding from Python


from ..config cimport translate_exception
from .callable cimport callable

cdef extern from "dynd/functional.hpp" namespace "dynd::nd::functional" nogil:
    callable apply(...) except +translate_exception

Add a minimal wrapper for apply.

Add a minimal wrapper for apply.


# mode: run
# tag: pep489

import reload_ext_module

def test_reload(module):
    """
    >>> module = test_reload(reload_ext_module)
    >>> module is reload_ext_module  # Py_mod_create enforces a singleton.
    True
    """
    return reload(module)

Add reload(module) test that checks the current module singleton property.


Add tests for std::move wrapper

Add tests for std::move wrapper


Add a dedicated test for and/or operators in a boolean context.


# mode: run
# tag: c-api

from cpython cimport mem

def test_pymalloc():
    """
    >>> test_pymalloc()
    3
    """
    cdef char* m2
    cdef char* m = <char*> mem.PyMem_Malloc(20)
    assert m
    try:
        m[0] = 1
        m[1] = 2
        m[2] = 3
        m2 = <char*> mem.PyMem_Realloc(m, 10)
        assert m2
        m = m2
        return m[2]
    finally:
        mem.PyMem_Free(m)

def test_pymalloc_raw():
    """
    >>> test_pymalloc_raw()
    3
    """
    cdef char* m
    cdef char* m2 = NULL
    with nogil:
        m = <char*> mem.PyMem_RawMalloc(20)
        if not m:
            raise MemoryError()
        try:
            m[0] = 1
            m[1] = 2
            m[2] = 3
            m2 = <char*> mem.PyMem_RawRealloc(m, 10)
            if m2:
                m = m2
            retval = m[2]
        finally:
            mem.PyMem_RawFree(m)
    assert m2
    return retval

Implement test for new PyMem_RawMalloc() functions.


# tag: cpp
# mode: compile

cdef extern from "templates.h":
    cdef cppclass TemplateTest1[T]:
        TemplateTest1()
        T value
        int t
        T getValue()

    cdef cppclass TemplateTest1[T]

Add unit test for commit 2de8934

Add unit test for commit 2de8934


# mode: run
# tag: readonly, const, numpy

import numpy as np

def new_array():
    return np.arange(10).astype('float')

ARRAY = new_array()

cdef getmax(double[:] x):
    """Example code, should work with both
    ro and rw memoryviews"""
    cdef double max_val = -float('inf')
    for val in x:
        if val > max_val:
            max_val = val
    return max_val

cdef update_array(double [:] x):
    """Modifying a ro memoryview should raise an error"""
    x[0] = 23.

cdef getconst(const double [:] x):
    """Should only accept ro memoryviews"""
    return x[0]

def test_mmview_rw(x):
    """
    >>> test_mmview_rw(ARRAY)
    9.0
    """
    return getmax(x)

def test_mmview_ro(x):
    """
    >>> test_mmview_ro(new_array())
    9.0
    """
    x.setflags(write=False)
    assert x.flags.writeable is False
    return getmax(x)

def test_update_mmview_rw(x):
    """
    >>> test_update_mmview_rw(new_array())
    23.0
    """
    update_array(x)
    return x[0]

def test_update_mmview_ro(x):
    """
    >>> test_update_mmview_ro(new_array())
    0.0
    """
    x.setflags(write=False)
    assert x.flags.writeable is False
    try:
        update_array(x)
    except ValueError: pass
    else:
        assert False, "RO error not raised!"
    return getconst(x)

def test_rw_call_getmax(double[:] x):
    """
    >>> test_rw_call_getmax(new_array())
    23.0
    """
    update_array(x)
    assert getconst(x) == 23
    return getmax(x)

def test_const_mmview_ro(x):
    """
    >>> test_const_mmview_ro(new_array())
    0.0
    """
    x.setflags(write=False)
    assert x.flags.writeable is False
    return getconst(x)

Add dedicated test file for read-only memory views.


# -*- coding: utf-8 -*-

################################################################################
#
#   PyDAS - A Python wrapper for several differential algebraic system solvers
#
#   Copyright (c) 2010-2014 by Joshua W. Allen (jwallen@mit.edu)
#   and Connie W. Gao (connieg@mit.edu)
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the 'Software'),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#   DEALINGS IN THE SOFTWARE.
#
################################################################################

import numpy as np
cimport numpy as np

cdef class DASPK:

    cdef public int maxOrder
    cdef public object tstop
    cdef public double initialStep
    cdef public double maximumStep
    cdef public object bandwidths
    cdef public bint nonnegative
    cdef public bint sensitivity
    cdef public int sensmethod

    cdef public double t
    cdef public np.ndarray y
    cdef public np.ndarray dydt
    cdef public np.ndarray senpar

    cdef np.ndarray info
    cdef np.ndarray atol
    cdef np.ndarray rtol
    cdef np.ndarray rwork
    cdef np.ndarray iwork
    cdef np.ndarray rpar
    cdef np.ndarray ipar
    cdef int idid

    cpdef initialize(self, double t0, np.ndarray y0, np.ndarray dydt0=?, np.ndarray senpar=?, atol=?, rtol=?)

    cpdef advance(self, double tout)

    cpdef step(self, double tout)

    cdef solve(self, double tout)

Add PyDASPK wrapper file for DASPK solver required for sensitivity analysis.


#
# Cython version of Knuth's "man or boy" test -- "It separates the man
# Algol 60 compilers from the boy Algol 60 compilers." Here's the
# original (from wikipedia):
#
# begin
#   real procedure A (k, x1, x2, x3, x4, x5);
#   value k; integer k;
#   begin
#     real procedure B;
#     begin k:= k - 1;
#           B:= A := A (k, B, x1, x2, x3, x4);
#     end;
#     if k <= 0 then A:= x4 + x5 else B;
#   end;
#   outreal (A (10, 1, -1, -1, 1, 0));
# end;
#
# and a table of values:
#
#   k           A
#   0           1
#   1           0
#   2   	-2
#   3   	0
#   4   	1
#   5   	0
#   6   	1
#   7   	-1
#   8   	-10
#   9   	-30
#   10   	-67
#
# Past 10 or so, we blow the C stack -- can't just set a higher recursion limit
# to get around that one.
#

def compute(val):
    if isinstance(val, int):
        return val
    else:
        return val()

def a(in_k, x1, x2, x3, x4, x5):
    """
    >>> import sys
    >>> sys.setrecursionlimit(1350)
    >>> a(10, 1, -1, -1, 1, 0)
    -67
    """
    k = [in_k]
    def b():
        k[0] -= 1
        return a(k[0], b, x1, x2, x3, x4)
    return compute(x4) + compute(x5) if k[0] <= 0 else b()

Add Knuth's "man or boy" test.

Add Knuth's "man or boy" test.


#cython profile=True
"""
cython -a fast_linbin.pyx
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -I/usr/local/lib/python2.7/dist-packages/numpy/core/include/ -o fast_linbin.so fast_linbin.c
"""

cimport cython
cimport numpy as np
import numpy as np

ctypedef np.float64_t DOUBLE
ctypedef np.int_t INT

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def linbin(np.ndarray[DOUBLE, ndim=1] X, double a, double b, int M,
           int trunc=1):
    """
    Linear Binning as described in Fan and Marron (1994)
    """
    cdef int i, li_i
    cdef int nobs = X.shape[0]
    cdef double delta = (b - a)/(M - 1)

    cdef np.ndarray[DOUBLE, ndim=1] gcnts = np.zeros(M, np.float)
    cdef np.ndarray[DOUBLE, ndim=1] lxi = (X - a)/delta
    cdef np.ndarray[INT, ndim=1] li = lxi.astype(int)
    cdef np.ndarray[DOUBLE, ndim=1] rem = lxi - li

    for i in range(nobs):
        li_i = li[i]
        if li_i > 1 and li_i < M:
            gcnts[li_i] = gcnts[li_i] + 1 - rem[i]
            gcnts[li_i+1] = gcnts[li_i+1] + rem[i]
        if li_i > M and trunc == 0:
            gcnts[M] = gcnts[M] + 1
    return gcnts

Add Cython source for fast linbin

ENH: Add Cython source for fast linbin


cdef void raise_py_error():
    pass

cdef extern from "foo.h":
    cdef int generic_error() except +
    cdef int specified_error() except +MemoryError
    cdef int dynamic_error() except +raise_py_error

def test_it():
    generic_error()
    specified_error()
    dynamic_error()

Test for cpp exception handling

Test for cpp exception handling


Test for cpp exception handling

Test for cpp exception handling


# mode: run
# tag: numpy

import numpy as np

cdef class ExtensionType(object):
    cdef public int dummy

    def __init__(self, n):
        self.dummy = n

items = [ExtensionType(1), ExtensionType(2)]
cdef ExtensionType[:] view = np.array(items, dtype=ExtensionType)

def test_getitem():
    """
    >>> test_getitem()
    1
    2
    """
    for i in range(view.shape[0]):
        item = view[i]
        print item.dummy

def test_getitem_typed():
    """
    >>> test_getitem_typed()
    1
    2
    """
    cdef ExtensionType item
    for i in range(view.shape[0]):
        item = view[i]
        print item.dummy

Add test for memoryview of extension type

A test for a bug fixed in commit 478b939a4c99a77c0863cfd.

v2: add commit link above
v3: # tag: instead of # tags:, drop cpp tag as it means something different
    that I originally thought

There was a bug that produced C code where gcc emitted warnings:
extension_type_memoryview.c: In function ‘__pyx_pf_25extension_type_memoryview_test_getitem’:
extension_type_memoryview.c:1468:15: warning: assignment from incompatible pointer type
extension_type_memoryview.c: In function ‘__pyx_pf_25extension_type_memoryview_2test_getitem_typed’:
extension_type_memoryview.c:1565:15: warning: assignment from incompatible pointer type
extension_type_memoryview.c:1568:18: warning: assignment from incompatible pointer type

And g++ failed with errors:
extension_type_memoryview.c: In function ‘PyObject* __pyx_pf_25extension_type_memoryview_test_getitem(PyObject*)’:
extension_type_memoryview.c:1468:213: error: cannot convert ‘__pyx_obj_25extension_type_memoryview_ExtensionType*’ to ‘PyObject*’ in assignment
extension_type_memoryview.c: In function ‘PyObject* __pyx_pf_25extension_type_memoryview_2test_getitem_typed(PyObject*)’:
extension_type_memoryview.c:1565:213: error: cannot convert ‘__pyx_obj_25extension_type_memoryview_ExtensionType*’ to ‘PyObject*’ in assignment
extension_type_memoryview.c:1568:20: error: cannot convert ‘PyObject*’ to ‘__pyx_obj_25extension_type_memoryview_ExtensionType*’ in assignment


Add Knuth's "man or boy" test.

Add Knuth's "man or boy" test.