# on desktop, we need to create an env :)
# example taken from http://www.inonit.com/cygwin/jni/invocationApi/c.html

cdef extern jint JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args)
cdef extern from "jni.h":
    int JNI_VERSION_1_4
    jboolean JNI_FALSE
    ctypedef struct JavaVMInitArgs:
        jint version
        jint nOptions
        jboolean ignoreUnrecognized
        JavaVMOption *options
    ctypedef struct JavaVMOption:
        char *optionString
        void *extraInfo

cdef JNIEnv *default_env = NULL

cdef void create_jnienv():
    cdef JavaVM* jvm
    cdef JavaVMInitArgs args
    cdef JavaVMOption options[1]
    cdef bytes py_bytes

    from os.path import realpath
    py_bytes = <bytes>('-Djava.class.path={0}'.format(realpath('.')))
    options[0].optionString = py_bytes
    options[0].extraInfo = NULL

    args.version = JNI_VERSION_1_4
    args.options = options
    args.nOptions = 1
    args.ignoreUnrecognized = JNI_FALSE

    JNI_CreateJavaVM(&jvm, <void **>&default_env, &args)

cdef JNIEnv *get_jnienv():
    if default_env == NULL:
        create_jnienv()
    return default_env

Make the desktop use CLASSPATH if defined.

# on desktop, we need to create an env :)
# example taken from http://www.inonit.com/cygwin/jni/invocationApi/c.html

cdef extern jint JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args)
cdef extern from "jni.h":
    int JNI_VERSION_1_4
    jboolean JNI_FALSE
    ctypedef struct JavaVMInitArgs:
        jint version
        jint nOptions
        jboolean ignoreUnrecognized
        JavaVMOption *options
    ctypedef struct JavaVMOption:
        char *optionString
        void *extraInfo

cdef JNIEnv *default_env = NULL

cdef void create_jnienv():
    cdef JavaVM* jvm
    cdef JavaVMInitArgs args
    cdef JavaVMOption options[1]
    cdef bytes py_bytes

    from os import environ
    from os.path import realpath
    cp = environ.get('CLASSPATH') or realpath('.')
    py_bytes = <bytes>('-Djava.class.path={0}'.format(cp))
    options[0].optionString = py_bytes
    options[0].extraInfo = NULL

    args.version = JNI_VERSION_1_4
    args.options = options
    args.nOptions = 1
    args.ignoreUnrecognized = JNI_FALSE

    JNI_CreateJavaVM(&jvm, <void **>&default_env, &args)

cdef JNIEnv *get_jnienv():
    if default_env == NULL:
        create_jnienv()
    return default_env


cimport numpy as np

from .doc cimport Doc

cdef class Span:
    cdef readonly Doc doc
    cdef readonly int start
    cdef readonly int end
    cdef readonly int start_char
    cdef readonly int end_char
    cdef readonly int label

    cdef public np.ndarray tensor

    cdef public _vector
    cdef public _vector_norm

    cpdef int _recalculate_indices(self) except -1

Remove tensor attribute from Span and Token.

cimport numpy as np

from .doc cimport Doc

cdef class Span:
    cdef readonly Doc doc
    cdef readonly int start
    cdef readonly int end
    cdef readonly int start_char
    cdef readonly int end_char
    cdef readonly int label

    cdef public _vector
    cdef public _vector_norm

    cpdef int _recalculate_indices(self) except -1


from thinc.linear.avgtron cimport AveragedPerceptron
from thinc.extra.eg cimport Example
from thinc.structs cimport ExampleC

from .structs cimport TokenC
from .vocab cimport Vocab

cdef class TaggerModel(AveragedPerceptron):
    cdef void set_featuresC(self, ExampleC* eg, const TokenC* tokens, int i) except *

cdef class Tagger:
    cdef readonly Vocab vocab
    cdef readonly TaggerModel model
    cdef public dict freqs

Add cfg field to Tagger

from thinc.linear.avgtron cimport AveragedPerceptron
from thinc.extra.eg cimport Example
from thinc.structs cimport ExampleC

from .structs cimport TokenC
from .vocab cimport Vocab

cdef class TaggerModel(AveragedPerceptron):
    cdef void set_featuresC(self, ExampleC* eg, const TokenC* tokens, int i) except *

cdef class Tagger:
    cdef readonly Vocab vocab
    cdef readonly TaggerModel model
    cdef public dict freqs
    cdef public object cfg


cimport AudioHardware
from AudioHardware cimport *
cimport stdlib

def yo():
	cdef UInt32 sz, ndevices
	cdef AudioDeviceID *devices
	cdef char *name

	AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &sz, NULL)
	ndevices = sz / sizeof(AudioDeviceID)

	print "Number of detected devices:", ndevices

Enumerate core audio devices in cython.

cimport AudioHardware
from AudioHardware cimport *
cimport stdlib

cdef extern from "Python.h":
	object PyString_FromStringAndSize(char *v, int len)

def yo():
	cdef UInt32 sz, ndevices, i
	cdef AudioDeviceID *devices
	cdef char *name

	AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &sz, NULL)
	ndevices = sz / sizeof(AudioDeviceID)

	print "Number of detected devices:", ndevices
	devices = stdlib.malloc(sizeof(*devices) * ndevices)
	res = []
	try:
		AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &sz,
				devices)

		for i in range(ndevices):
			AudioDeviceGetPropertyInfo(devices[i], 0, false,
						   kAudioDevicePropertyDeviceName, &sz,
						   NULL)
			name = malloc(sz + 1)
			AudioDeviceGetProperty(devices[i], 0, false,
					kAudioDevicePropertyDeviceName, &sz,
					name)
			devicename = PyString_FromStringAndSize(name, sz+1)
			free(name)
			res.append(devicename)

	finally:
		stdlib.free(devices)

	return devicename


# distutils: language = c++

cdef extern from '../cuda/cupy_common.h':  # thru parent to import in core
    ctypedef char cpy_byte
    ctypedef unsigned char cpy_ubyte
    ctypedef short cpy_short
    ctypedef unsigned short cpy_ushort
    ctypedef int cpy_int
    ctypedef unsigned int cpy_uint
    ctypedef long cpy_long
    ctypedef unsigned long cpy_ulong
    ctypedef float cpy_float
    ctypedef double cpy_double

Fix long to long long.

# distutils: language = c++

cdef extern from '../cuda/cupy_common.h':  # thru parent to import in core
    ctypedef char cpy_byte
    ctypedef unsigned char cpy_ubyte
    ctypedef short cpy_short
    ctypedef unsigned short cpy_ushort
    ctypedef int cpy_int
    ctypedef unsigned int cpy_uint
    ctypedef long long cpy_long
    ctypedef unsigned long long cpy_ulong
    ctypedef float cpy_float
    ctypedef double cpy_double


#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

cimport cython
from libc.stdint cimport int64_t

cdef type Timestamp

@cython.final
cdef class WindowedValue(object):
  cdef public object value
  cdef public object windows
  cdef public int64_t timestamp_micros
  cdef object timestamp_object

  cpdef WindowedValue with_value(self, new_value)

  @staticmethod
  cdef inline bint _typed_eq(WindowedValue left, WindowedValue right) except? -2

@cython.locals(wv=WindowedValue)
cdef WindowedValue create(
  object value, int64_t timestamp_micros, object windows)

Make create() avaialable to pure python callers

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

cimport cython
from libc.stdint cimport int64_t

cdef type Timestamp

@cython.final
cdef class WindowedValue(object):
  cdef public object value
  cdef public object windows
  cdef public int64_t timestamp_micros
  cdef object timestamp_object

  cpdef WindowedValue with_value(self, new_value)

  @staticmethod
  cdef inline bint _typed_eq(WindowedValue left, WindowedValue right) except? -2

@cython.locals(wv=WindowedValue)
cpdef WindowedValue create(
  object value, int64_t timestamp_micros, object windows)


# Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.

cdef class XMP:

    cdef set namespaces

    def __init__ (self):
        self.namespaces = {
            "stRef",
            "dc",
            "xmp",
            "xmpRights",
            "xmpMM",
            "xmpidq",
        }

    def __len__ (self):
        return 0

    def __iter__ (self):
        return iter(())

    def __getattr__ (self, name):
        if name in self.namespaces:
            return ()

        else:
            self.__raise_no_attr(name)

    def __repr__ (self):
        return "<{}>".format(self.__class__.__name__)

    def __raise_no_attr (self, name):
        obj = "{}.{}".format(self.__class__.__module__,
                             self.__class__.__name__)

        message = "{} object has no attribute {}".format(repr(obj),
                                                         repr(name))

        raise AttributeError(message)

Replace __ prefixes with cdefs for better encapsulation

# Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.

cdef set DEFAULT_XMP_NAMESPACES = {
    "stRef",
    "dc",
    "xmp",
    "xmpRights",
    "xmpMM",
    "xmpidq",
}

cdef class XMP:

    def __len__ (self):
        return 0

    def __iter__ (self):
        return iter(())

    def __getattr__ (self, name):
        if name in DEFAULT_XMP_NAMESPACES:
            return ()

        else:
            self.raise_error_no_attr(name)

    def __repr__ (self):
        return "<{}>".format(self.__class__.__name__)

    cdef raise_error_no_attr (self, name):
        obj = "{}.{}".format(self.__class__.__module__,
                             self.__class__.__name__)

        message = "{} object has no attribute {}".format(repr(obj),
                                                         repr(name))

        raise AttributeError(message)


cdef class LocalRef:
    cdef jobject obj

    def __cinit__(self):
        self.obj = NULL

    def __dealloc__(self):
        cdef JNIEnv *j_env
        if self.obj != NULL:
            j_env = get_jnienv()
            j_env[0].DeleteGlobalRef(j_env, self.obj)
        self.obj = NULL

    cdef void create(self, JNIEnv *env, jobject obj):
        self.obj = env[0].NewGlobalRef(env, obj)

    def __repr__(self):
        return '<LocalRef obj=0x{:x} at 0x{:x}>'.format(
            <long><void *>self.obj, id(self))

cdef LocalRef create_local_ref(JNIEnv *env, jobject obj):
    cdef LocalRef ret = LocalRef()
    ret.create(env, obj)
    return ret

Fix string format error in 2.6

cdef class LocalRef:
    cdef jobject obj

    def __cinit__(self):
        self.obj = NULL

    def __dealloc__(self):
        cdef JNIEnv *j_env
        if self.obj != NULL:
            j_env = get_jnienv()
            j_env[0].DeleteGlobalRef(j_env, self.obj)
        self.obj = NULL

    cdef void create(self, JNIEnv *env, jobject obj):
        self.obj = env[0].NewGlobalRef(env, obj)

    def __repr__(self):
        return '<LocalRef obj=0x{0:x} at 0x{1:x}>'.format(
            <long><void *>self.obj, id(self))

cdef LocalRef create_local_ref(JNIEnv *env, jobject obj):
    cdef LocalRef ret = LocalRef()
    ret.create(env, obj)
    return ret


# 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
447:72: Argument evaluation order in C function call is undefined and may not be as expected
750:34: Argument evaluation order in C function call is undefined and may not be as expected
944:27: Ambiguous exception value, same as default return value: 0
944:27: Ambiguous exception value, same as default return value: 0
975:29: Ambiguous exception value, same as default return value: 0
975:29: Ambiguous exception value, same as default return value: 0
1003:46: Ambiguous exception value, same as default return value: 0
1003:46: Ambiguous exception value, same as default return value: 0
1093:29: Ambiguous exception value, same as default return value: 0
1093:29: Ambiguous exception value, same as default return value: 0
"""

Update (unrelated) test warnings after changing utility code in MemoryView.pyx.

# 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
"""


cimport ccygroonga

def init():
    return ccygroonga.grn_init()

#cdef class Database:
#    cdef ccygroonga.grn_ctx* _c_ctx
#    cdef ccygroonga.grn_obj* _c_cb
#
#    def __cinit__(self, ccygroonga.grn_ctx* c_ctx, ccygroonga.grn_obj* c_db):
#        self._c_ctx = c_ctx
#        self._c_db = c_db

cdef class Context:
    cdef ccygroonga.grn_ctx* _c_ctx
    cdef ccygroonga.grn_obj* _c_cb

    def __cinit__(self):
        self._c_ctx = ccygroonga.grn_ctx_open(0)

    def db_create(self, path):
        c_path = path.encode('UTF-8')
        c_db = ccygroonga.grn_db_create(self._c_ctx, c_path, NULL)
        self._c_db = c_db
        #return Database(self._c_ctx, c_db)

    def fin(self):
        return ccygroonga.grn_ctx_fin(self._c_ctx)

def fin():
    return ccygroonga.grn_fin()

Fix typo and now it compiles successfully

cimport ccygroonga

def init():
    return ccygroonga.grn_init()

#cdef class Database:
#    cdef ccygroonga.grn_ctx* _c_ctx
#    cdef ccygroonga.grn_obj* _c_cb
#
#    def __cinit__(self, ccygroonga.grn_ctx* c_ctx, ccygroonga.grn_obj* c_db):
#        self._c_ctx = c_ctx
#        self._c_db = c_db

cdef class Context:
    cdef ccygroonga.grn_ctx* _c_ctx
    cdef ccygroonga.grn_obj* _c_db

    def __cinit__(self):
        self._c_ctx = ccygroonga.grn_ctx_open(0)

    def db_create(self, path):
        c_path = path.encode('UTF-8')
        c_db = ccygroonga.grn_db_create(self._c_ctx, c_path, NULL)
        self._c_db = c_db
        #return Database(self._c_ctx, c_db)

    def fin(self):
        return ccygroonga.grn_ctx_fin(self._c_ctx)

def fin():
    return ccygroonga.grn_fin()


#cython: embedsignature=True
include "PETSc/PETSc.pyx"

Add Cython compiler directives waiting release 0.11.2

#cython: embedsignature=True
#cython: cdivision=True
#cython: always_allow_keywords=True
include "PETSc/PETSc.pyx"


# 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

Change test to initialize by `import_datetime`

# coding: utf-8

from cpython.datetime cimport import_datetime
from cpython.datetime cimport date, time, datetime, timedelta, PyDateTime_IMPORT

import_datetime()

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


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

cdef inline int comm_set_eh(MPI_Comm ob) nogil except -1:
    if ob == MPI_COMM_NULL: return 0
    cdef int opt = options.errors
    if   opt == 0: pass
    elif opt == 1: CHKERR( MPI_Comm_set_errhandler(ob, MPI_ERRORS_RETURN) )
    elif opt == 2: CHKERR( MPI_Comm_set_errhandler(ob, MPI_ERRORS_ARE_FATAL) )
    return 0

cdef inline int win_set_eh(MPI_Win ob) nogil except -1:
    if ob == MPI_WIN_NULL: return 0
    cdef int opt = options.errors
    if   opt == 0: pass
    elif opt == 1: CHKERR( MPI_Win_set_errhandler(ob, MPI_ERRORS_RETURN) )
    elif opt == 2: CHKERR( MPI_Win_set_errhandler(ob, MPI_ERRORS_ARE_FATAL) )
    return 0

cdef inline int file_set_eh(MPI_File ob) nogil except -1:
    if ob != MPI_FILE_NULL: return 0
    cdef int opt = options.errors
    if   opt == 0: pass
    elif opt == 1: CHKERR( MPI_File_set_errhandler(ob, MPI_ERRORS_RETURN) )
    elif opt == 2: CHKERR( MPI_File_set_errhandler(ob, MPI_ERRORS_ARE_FATAL) )
    return 0

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

Fix File error handling rules

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

cdef inline int comm_set_eh(MPI_Comm ob) nogil except -1:
    if ob == MPI_COMM_NULL: return 0
    cdef int opt = options.errors
    if   opt == 0: pass
    elif opt == 1: CHKERR( MPI_Comm_set_errhandler(ob, MPI_ERRORS_RETURN) )
    elif opt == 2: CHKERR( MPI_Comm_set_errhandler(ob, MPI_ERRORS_ARE_FATAL) )
    return 0

cdef inline int win_set_eh(MPI_Win ob) nogil except -1:
    if ob == MPI_WIN_NULL: return 0
    cdef int opt = options.errors
    if   opt == 0: pass
    elif opt == 1: CHKERR( MPI_Win_set_errhandler(ob, MPI_ERRORS_RETURN) )
    elif opt == 2: CHKERR( MPI_Win_set_errhandler(ob, MPI_ERRORS_ARE_FATAL) )
    return 0

cdef inline int file_set_eh(MPI_File ob) nogil except -1:
    if ob == MPI_FILE_NULL: return 0
    cdef int opt = options.errors
    if   opt == 0: pass
    elif opt == 1: CHKERR( MPI_File_set_errhandler(ob, MPI_ERRORS_RETURN) )
    elif opt == 2: CHKERR( MPI_File_set_errhandler(ob, MPI_ERRORS_ARE_FATAL) )
    return 0

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


cdef list _reduction_backends = []
cdef list _routine_backends = []

def _get_backend(str s):
    if s == 'cub':
        return BACKEND_CUB
    # if s == 'cutensor':
    #     return BACKEND_CUTENSOR
    raise ValueError('Unknown backend: {}'.format(s))

def set_reduction_backends(str s):
    global _reduction_backends
    _reduction_backends = [_get_backend(t) for t in s.split(',')]

def set_routine_backends(str s):
    global _routine_backends
    _routine_backends = [_get_backend(t) for t in s.split(',')]

Set default backends from environment variables

import os

cdef list _reduction_backends = []
cdef list _routine_backends = []

def _get_backend(str s):
    if s == 'cub':
        return BACKEND_CUB
    # if s == 'cutensor':
    #     return BACKEND_CUTENSOR
    raise ValueError('Unknown backend: {}'.format(s))

def set_reduction_backends(str s):
    global _reduction_backends
    _reduction_backends = [_get_backend(t) for t in s.split(',') if t]

def set_routine_backends(str s):
    global _routine_backends
    _routine_backends = [_get_backend(t) for t in s.split(',') if t]

cdef _set_default_backends():
    cdef str default_backends = os.getenv('CUPY_BACKENDS', '')
    set_reduction_backends(default_backends)
    set_routine_backends(default_backends)

_set_default_backends()


from model cimport LineModel

cdef class FitGaussian(LineModel):

    cdef:
        double[:] data
        double[:] weights

        # Prior parameters

        # Mean and center of the prior radial velocity
        # of each profile and gaussian.
        double[:] v_center_mean
        double[:] v_center_std

        # Rotation
        public double v_rot_k
        public double v_rot_theta

        # Solid body rotation
        public double fsolid_p
        public double fsolid_q

        # Asymmetry
        public double asym_p
        public double asym_q

        # Turbulent motions
        public double turbulence_min
        public double turbulence_k
        public double turbulence_theta

        # Baseline
        public double baseline_std

    cdef int model_params_offset(self)
    cdef double ln_bounds_model(self, double[:] p)
    cdef double ln_prior_model(self, double[:] p)
    cdef double ln_bounds_components(self, double[:] p)
    cdef double ln_prior_components(self, double[:] p)
    cdef double ln_likelihood(self, double[:] p)

cdef class FitLaplacian(FitGaussian):
    pass

Add definition of Mixture Model

from model cimport LineModel

cdef class FitGaussian(LineModel):

    cdef:
        double[:] data
        double[:] weights

        # Prior parameters

        # Mean and center of the prior radial velocity
        # of each profile and gaussian.
        double[:] v_center_mean
        double[:] v_center_std

        # Rotation
        public double v_rot_k
        public double v_rot_theta

        # Solid body rotation
        public double fsolid_p
        public double fsolid_q

        # Asymmetry
        public double asym_p
        public double asym_q

        # Turbulent motions
        public double turbulence_min
        public double turbulence_k
        public double turbulence_theta

        # Baseline
        public double baseline_std

    cdef int model_params_offset(self)
    cdef double ln_bounds_model(self, double[:] p)
    cdef double ln_prior_model(self, double[:] p)
    cdef double ln_bounds_components(self, double[:] p)
    cdef double ln_prior_components(self, double[:] p)
    cdef double ln_likelihood(self, double[:] p)

cdef class FitLaplacian(FitGaussian):
    pass

cdef class FitMixture(FitGaussian):
    pass


cimport cython
from libcpp.memory cimport unique_ptr, make_unique, shared_ptr

from thrift_server cimport (
    cThriftServer,
    cServerInterface,
    ServiceInterface
)
from folly_futures cimport cFollyPromise, cFollyUnit, c_unit

import asyncio

cdef class ThriftServer:
  cdef unique_ptr[cThriftServer] server
  cdef ServiceInterface handler
  cdef object loop

  def __cinit__(self):
    self.server = make_unique[cThriftServer]()

  def __init__(self, ServiceInterface handler, port, loop=None):
    self.loop = loop or asyncio.get_event_loop()
    self.handler = handler
    self.server.get().setInterface(handler.interface_wrapper)
    self.server.get().setPort(port)

  async def serve(self):
    def _serve():
      with nogil:
        self.server.get().serve()
    try:
        await self.loop.run_in_executor(None, _serve)
    except asyncio.CancelledError:
      self.server.get().stop()
      raise

Correct cleanup when server fails

cimport cython
from libcpp.memory cimport unique_ptr, make_unique, shared_ptr

from thrift_server cimport (
    cThriftServer,
    cServerInterface,
    ServiceInterface
)
from folly_futures cimport cFollyPromise, cFollyUnit, c_unit

import asyncio

cdef class ThriftServer:
  cdef unique_ptr[cThriftServer] server
  cdef ServiceInterface handler
  cdef object loop

  def __cinit__(self):
    self.server = make_unique[cThriftServer]()

  def __init__(self, ServiceInterface handler, port, loop=None):
    self.loop = loop or asyncio.get_event_loop()
    self.handler = handler
    self.server.get().setInterface(handler.interface_wrapper)
    self.server.get().setPort(port)

  async def serve(self):
    def _serve():
      with nogil:
        self.server.get().serve()
    try:
        await self.loop.run_in_executor(None, _serve)
    except Exception:
        self.server.get().stop()
        raise


# distutils: extra_compile_args=-std=c++11
# mode: run
# tag: cpp

from libcpp.memory cimport unique_ptr, shared_ptr

cdef extern from "cpp_smart_ptr_helper.h":
    cdef cppclass CountAllocDealloc:
        CountAllocDealloc(int*, int*)

def test_unique_ptr():
    """
    >>> test_unique_ptr()
    """
    cdef int alloc_count = 0, dealloc_count = 0
    cdef unique_ptr[CountAllocDealloc] x_ptr
    x_ptr.reset(new CountAllocDealloc(&alloc_count, &dealloc_count))
    assert alloc_count == 1
    x_ptr.reset()
    assert alloc_count == 1
    assert dealloc_count == 1

Use c++0x rather than c++11 when building tests to allow for versions of gcc older than 4.7. This fixes a recurring Travis build failure.

# distutils: extra_compile_args=-std=c++0x
# mode: run
# tag: cpp, werror

from libcpp.memory cimport unique_ptr, shared_ptr

cdef extern from "cpp_smart_ptr_helper.h":
    cdef cppclass CountAllocDealloc:
        CountAllocDealloc(int*, int*)

def test_unique_ptr():
    """
    >>> test_unique_ptr()
    """
    cdef int alloc_count = 0, dealloc_count = 0
    cdef unique_ptr[CountAllocDealloc] x_ptr
    x_ptr.reset(new CountAllocDealloc(&alloc_count, &dealloc_count))
    assert alloc_count == 1
    x_ptr.reset()
    assert alloc_count == 1
    assert dealloc_count == 1


from libcpp.vector cimport vector
from libcpp.utility cimport pair
from libc.stddef cimport size_t

from microscopes.common._random_fwd_h cimport rng_t

cdef extern from "microscopes/common/entity_state.hpp" namespace "microscopes::common":
    # expose enough of the API here
    cdef cppclass entity_based_state_object:
        vector[ssize_t] assignments() except +
        size_t nentities()
        size_t ngroups()

        void add_value(size_t, size_t, rng_t &) except +
        size_t remove_value(size_t, rng_t &) except +
        pair[vector[size_t], vector[float]] score_value(size_t, rng_t &) except +

        vector[size_t] empty_groups() except +
        size_t create_group(rng_t &) except +
        void delete_group(size_t) except +

Add line break to end of file


#cython: embedsignature=True

import zmq
cimport zmq.backend.cython.libzmq as libzmq
from zmq.backend.cython.socket cimport Socket
from libc.stdint cimport uint64_t

class ZMQLinkingError(Exception):
    pass

def check_zeromq():
    """Check that zeromq is correctly installed."""
    cdef void * handle
    cdef uint64_t affinity
    cdef size_t sz = sizeof(uint64_t)
    cdef int rc, errno
    with zmq.Context.instance() as ctx:
        with ctx.socket(zmq.REQ) as socket:
            handle = (<Socket>socket).handle
            rc = libzmq.zmq_getsockopt(handle, libzmq.ZMQ_AFFINITY, &affinity, &sz)
            if rc != 0:
                errno = libzmq.zmq_errno()
                if errno == libzmq.EINVAL:
                    raise ZMQLinkingError("Something has gone wrong with ZMQ Linking")
                raise zmq.ZMQError()

Fix a bug in zmq.check

#cython: embedsignature=True

import zmq
cimport zmq.backend.cython.libzmq as libzmq
from zmq.backend.cython.socket cimport Socket
from libc.stdint cimport uint64_t

class ZMQLinkingError(Exception):
    pass

def check_zeromq():
    """Check that zeromq is correctly installed."""
    cdef void * handle
    cdef uint64_t affinity
    cdef size_t sz = sizeof(uint64_t)
    cdef int rc, errno
    with zmq.Context() as ctx:
        with ctx.socket(zmq.REQ) as socket:
            handle = (<Socket>socket).handle
            rc = libzmq.zmq_getsockopt(handle, libzmq.ZMQ_AFFINITY, &affinity, &sz)
            if rc != 0:
                errno = libzmq.zmq_errno()
                if errno == libzmq.EINVAL:
                    raise ZMQLinkingError("Something has gone wrong with ZMQ Linking")
                raise zmq.ZMQError()


from libc.stdio cimport sprintf
from cpython cimport PyType_Check
from cpython cimport PyType_Check as PyType_Check2
from cpython.type cimport PyType_Check as PyType_Check3

def libc_cimports():
    """
    >>> libc_cimports()
    hello
    """
    cdef char[10] buf
    sprintf(buf, "%s", b'hello')
    print (<object>buf).decode('ASCII')

def cpython_cimports():
    """
    >>> cpython_cimports()
    True
    False
    True
    False
    True
    False
    """
    print PyType_Check(list)
    print PyType_Check([])
    print PyType_Check2(list)
    print PyType_Check2([])
    print PyType_Check3(list)
    print PyType_Check3([])

Extend CPython C-API test to at least include (and thus parser) all .pxd files.

from libc.stdio cimport sprintf
from cpython cimport PyType_Check
from cpython cimport PyType_Check as PyType_Check2
from cpython.type cimport PyType_Check as PyType_Check3

# Make sure we can cimport all .pxd files.
cimport cpython.array
cimport cpython.bool
cimport cpython.buffer
cimport cpython.bytearray
cimport cpython.bytes
cimport cpython.ceval
cimport cpython.cobject
cimport cpython.complex
cimport cpython.datetime
cimport cpython.dict
cimport cpython.exc
cimport cpython.float
cimport cpython.function
cimport cpython.getargs
cimport cpython.instance
cimport cpython.int
cimport cpython.iterator
cimport cpython.list
cimport cpython.long
cimport cpython.longintrepr
cimport cpython.mapping
cimport cpython.mem
cimport cpython.method
cimport cpython.module
cimport cpython.number
cimport cpython.object
cimport cpython.oldbuffer
cimport cpython.pycapsule
cimport cpython.pylifecycle
cimport cpython.pystate
cimport cpython.pythread
cimport cpython.ref
cimport cpython.sequence
cimport cpython.set
cimport cpython.slice
cimport cpython.string
cimport cpython.tuple
cimport cpython.type
cimport cpython.unicode
cimport cpython.version
cimport cpython.weakref

def libc_cimports():
    """
    >>> libc_cimports()
    hello
    """
    cdef char[10] buf
    sprintf(buf, "%s", b'hello')
    print (<object>buf).decode('ASCII')

def cpython_cimports():
    """
    >>> cpython_cimports()
    True
    False
    True
    False
    True
    False
    """
    print PyType_Check(list)
    print PyType_Check([])
    print PyType_Check2(list)
    print PyType_Check2([])
    print PyType_Check3(list)
    print PyType_Check3([])


def floor_div_float(double a, double b):
    """
    >>> floor_div_float(2, 1.5)
    1.0
    >>> floor_div_float(2, -1.5)
    -2.0
    >>> floor_div_float(-2.3, 1.5)
    -2.0
    >>> floor_div_float(1e10, 1e-10)
    1e+20
    """
    return a // b

Fix for different Windows float repr

def floor_div_float(double a, double b):
    """
    >>> floor_div_float(2, 1.5)
    1.0
    >>> floor_div_float(2, -1.5)
    -2.0
    >>> floor_div_float(-2.3, 1.5)
    -2.0
    >>> floor_div_float(1e10, 1e-10) == 1e20
    True
    """
    return a // b


from cymem.cymem cimport Pool

from ..structs cimport TemplateC, FeatureC
from ..typedefs cimport atom_t

cdef class ConjunctionExtracter:
    cdef Pool mem
    cdef readonly int nr_templ
    cdef readonly int nr_embed
    cdef readonly int nr_atom
    cdef public int linear_mode

    cdef int set_features(self, FeatureC* feats, const atom_t* atoms) nogil

    cdef TemplateC* templates
    cdef object _py_templates

Add bag of words to header

from cymem.cymem cimport Pool

from ..structs cimport TemplateC, FeatureC
from ..typedefs cimport atom_t

cdef class ConjunctionExtracter:
    cdef Pool mem
    cdef readonly int nr_templ
    cdef readonly int nr_embed
    cdef readonly int nr_atom
    cdef public int linear_mode

    cdef int set_features(self, FeatureC* feats, const atom_t* atoms) nogil

    cdef TemplateC* templates
    cdef object _py_templates

cdef class BagOfWords(ConjunctionExtracter):
    pass


from libcpp.vector cimport vector
from libcpp.map cimport map
from libc.stddef cimport size_t

from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.common._random_fwd_h cimport rng_t
from microscopes.common.variadic._dataview_h cimport dataview

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda":
    cdef cppclass model_definition:
        model_definition(size_t, size_t) except +

    cdef cppclass state:
        void inference()
        double perplexity()

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda::state":
    shared_ptr[state] initialize(const model_definition &,
                                 float,
                                 float,
                                 float,
                                 vector[vector[size_t]],
                                 rng_t &) except +

Make parameter a reference parameter

from libcpp.vector cimport vector
from libcpp.map cimport map
from libc.stddef cimport size_t

from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.common._random_fwd_h cimport rng_t
from microscopes.common.variadic._dataview_h cimport dataview

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda":
    cdef cppclass model_definition:
        model_definition(size_t, size_t) except +

    cdef cppclass state:
        void inference()
        double perplexity()

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda::state":
    shared_ptr[state] initialize(const model_definition &,
                                 float,
                                 float,
                                 float,
                                 vector[vector[size_t]] &,
                                 rng_t &) except +


cimport cython
from cython cimport floating

cdef extern from "rank_filter_base.hxx" namespace "rank_filter":
    void lineRankOrderFilter1D[I1, I2](const I1& src_begin,
                                       const I1& src_end,
                                       I2& dest_begin,
                                       I2& dest_end,
                                       size_t half_length,
                                       double rank) nogil

@cython.boundscheck(False)
@cython.initializedcheck(False)
@cython.nonecheck(False)
cdef inline void lineRankOrderFilter1D_floating(floating[::1] src,
                                                floating[::1] dest,
                                                size_t half_length,
                                                double rank) nogil:
    cdef floating* src_begin = &src[0]
    cdef floating* src_end = &src[-1] + 1
    cdef floating* dest_begin = &dest[0]
    cdef floating* dest_end = &dest[-1] + 1

    lineRankOrderFilter1D(
        src_begin, src_end, dest_begin, dest_end, half_length, rank
    )

Fix C++ header name in Cython header

cimport cython
from cython cimport floating

cdef extern from "rank_filter.hxx" namespace "rank_filter":
    void lineRankOrderFilter1D[I1, I2](const I1& src_begin,
                                       const I1& src_end,
                                       I2& dest_begin,
                                       I2& dest_end,
                                       size_t half_length,
                                       double rank) nogil

@cython.boundscheck(False)
@cython.initializedcheck(False)
@cython.nonecheck(False)
cdef inline void lineRankOrderFilter1D_floating(floating[::1] src,
                                                floating[::1] dest,
                                                size_t half_length,
                                                double rank) nogil:
    cdef floating* src_begin = &src[0]
    cdef floating* src_end = &src[-1] + 1
    cdef floating* dest_begin = &dest[0]
    cdef floating* dest_end = &dest[-1] + 1

    lineRankOrderFilter1D(
        src_begin, src_end, dest_begin, dest_end, half_length, rank
    )


from libc.stdlib cimport malloc, free

from gssapi.raw.cython_types cimport *

cdef class ChannelBindings:
    cdef public object initiator_address_type
    cdef public bytes initiator_address

    cdef public object acceptor_address_type
    cdef public bytes acceptor_address

    cdef public bytes application_data

    cdef gss_channel_bindings_t __cvalue__(ChannelBindings self)

Fix channel bindings type signature

from libc.stdlib cimport malloc, free

from gssapi.raw.cython_types cimport *

cdef class ChannelBindings:
    cdef public object initiator_address_type
    cdef public bytes initiator_address

    cdef public object acceptor_address_type
    cdef public bytes acceptor_address

    cdef public bytes application_data

    cdef gss_channel_bindings_t __cvalue__(ChannelBindings self) except NULL


cimport cython
from cython cimport floating

cdef extern from "rank_filter.hxx" namespace "rank_filter":
    void lineRankOrderFilter1D[I1, I2](const I1& src_begin,
                                       const I1& src_end,
                                       I2& dest_begin,
                                       I2& dest_end,
                                       size_t half_length,
                                       double rank) nogil

@cython.boundscheck(False)
@cython.initializedcheck(False)
@cython.nonecheck(False)
cdef inline void lineRankOrderFilter1D_floating_inplace(floating[::1] a,
                                                        size_t half_length,
                                                        double rank) nogil:
    cdef floating* a_begin = &a[0]
    cdef floating* a_end = &a[-1] + 1

    lineRankOrderFilter1D(
        a_begin, a_end, a_begin, a_end, half_length, rank
    )

Implement an `ndindex`-like Cython function

cimport cython
from cython cimport floating

cimport numpy

cdef extern from "rank_filter.hxx" namespace "rank_filter":
    void lineRankOrderFilter1D[I1, I2](const I1& src_begin,
                                       const I1& src_end,
                                       I2& dest_begin,
                                       I2& dest_end,
                                       size_t half_length,
                                       double rank) nogil

@cython.boundscheck(False)
@cython.initializedcheck(False)
@cython.nonecheck(False)
cdef inline void lineRankOrderFilter1D_floating_inplace(floating[::1] a,
                                                        size_t half_length,
                                                        double rank) nogil:
    cdef floating* a_begin = &a[0]
    cdef floating* a_end = &a[-1] + 1

    lineRankOrderFilter1D(
        a_begin, a_end, a_begin, a_end, half_length, rank
    )

@cython.boundscheck(False)
@cython.initializedcheck(False)
@cython.nonecheck(False)
cdef inline bint ndindex(const numpy.npy_intp[:] shape,
                         numpy.npy_intp[:] pos) nogil:
    cdef Py_ssize_t i = shape.shape[0]
    while i > 0:
        i -= 1
        pos[i] += 1

        if pos[i] < shape[i]:
            return False
        elif i > 0:
            pos[i] = 0

    return True


import hashlib
import struct

from simhash cimport compute as c_compute
from simhash cimport find_all as c_find_all

def unsigned_hash(bytes obj):
    '''Returns a hash suitable for use as a hash_t.'''
    return struct.unpack('>Q', hashlib.md5(obj).digest()[0:8])[0] & 0xFFFFFFFFFFFFFFFF

def compute(hashes):
    '''Compute the simhash of a vector of hashes.'''
    return c_compute(hashes)

def find_all(hashes, number_of_blocks, different_bits):
    '''
    Find the set of all matches within the provided vector of hashes.

    The provided hashes are manipulated in place, but upon completion are
    restored to their original state.
    '''
    cdef matches_t results_set = c_find_all(hashes, number_of_blocks, different_bits)
    cdef vector[match_t] results_vector
    results_vector.assign(results_set.begin(), results_set.end())
    return results_vector

Add comments around cryptic stuct.unpack invocation.

import hashlib
import struct

from simhash cimport compute as c_compute
from simhash cimport find_all as c_find_all

def unsigned_hash(bytes obj):
    '''Returns a hash suitable for use as a hash_t.'''
    # Takes first 8 bytes of MD5 digest
    digest = hashlib.md5(obj).digest()[0:8]
    # Unpacks the binary bytes in digest into a Python integer
    return struct.unpack('>Q', digest)[0] & 0xFFFFFFFFFFFFFFFF

def compute(hashes):
    '''Compute the simhash of a vector of hashes.'''
    return c_compute(hashes)

def find_all(hashes, number_of_blocks, different_bits):
    '''
    Find the set of all matches within the provided vector of hashes.

    The provided hashes are manipulated in place, but upon completion are
    restored to their original state.
    '''
    cdef matches_t results_set = c_find_all(hashes, number_of_blocks, different_bits)
    cdef vector[match_t] results_vector
    results_vector.assign(results_set.begin(), results_set.end())
    return results_vector


cimport numpy as np
from scipy import sparse

def extents_count(Py_ssize_t[::1] labels,
                  Py_ssize_t[::1] curr_loc, Py_ssize_t[::1] indices):
    cdef Py_ssize_t i
    for i, label in enumerate(labels):
        indices[curr_loc[label]] = i
        curr_loc[label] += 1

Optimize construction of label indices

cimport numpy as np
from scipy import sparse
import cython

@cython.boundscheck(False)
@cython.wraparound(False)
def extents_count(Py_ssize_t[::1] labels,
                  Py_ssize_t[::1] curr_loc, Py_ssize_t[::1] indices):
    cdef Py_ssize_t i = 0
    cdef Py_ssize_t label
    cdef Py_ssize_t size = len(labels)
    for i in range(size):
        label = labels[i]
        indices[curr_loc[label]] = i
        curr_loc[label] += 1
        i += 1


# -*- 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
from cvodes_cxx cimport realtype

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

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

Revert nogil .pxd to master (appears unused?)

# -*- 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

cdef extern from "cvodes_anyode.hpp" namespace "cvodes_anyode":
    cdef pair[vector[double], vector[double]] simple_adaptive[U](
        U * const,
        vector[double],
        double,
        LMM,
        const double * const,
        double,
        const double,
        vector[int]&,
        long int,
        double,
        double,
        double,
        bool,
        IterType,
        LinSol,
        int,
        double,
        unsigned,
        bool,
        int,
        bool,
        bool
    ) nogil except +

    cdef int simple_predefined[U](
        U * const,
        vector[double],
        double,
        LMM,
        const double * const,
        size_t,
        const double * const,
        double * const,
        vector[int]&,
        vector[double]&,
        long int,
        double,
        double,
        double,
        bool,
        IterType,
        LinSol,
        int,
        double,
        unsigned,
        int,
        bool,
        bool
    ) nogil except +


from opencog.atomspace cimport Atom, AtomSpace, TruthValue, PtrHolder
from opencog.atomspace cimport cAtomSpace, cTruthValue
from opencog.atomspace cimport tv_ptr, strength_t, count_t, shared_ptr
from opencog.atomspace cimport handle_cast
from cython.operator cimport dereference as deref

from opencog.atomspace import is_a, types, create_value_by_type

def execute_atom(AtomSpace atomspace, Atom atom):
    if atom == None: raise ValueError("execute_atom atom is: None")
    cdef cValuePtr c_value_ptr = c_execute_atom(atomspace.atomspace,
                                           deref(atom.handle))
    type = deref(c_value_ptr).get_type()
    if is_a(type, types.Atom):
        return Atom.createAtom(handle_cast(c_value_ptr), atomspace)
    cdef PtrHolder ptr_holder = PtrHolder.create(<shared_ptr[void]&>c_value_ptr)
    return create_value_by_type(type, ptr_holder, atomspace)

def evaluate_atom(AtomSpace atomspace, Atom atom):
    if atom == None: raise ValueError("evaluate_atom atom is: None")
    cdef tv_ptr result_tv_ptr = c_evaluate_atom(atomspace.atomspace,
                                                deref(atom.handle))
    cdef cTruthValue* result_tv = result_tv_ptr.get()
    cdef strength_t strength = deref(result_tv).get_mean()
    cdef strength_t confidence = deref(result_tv).get_confidence()
    return TruthValue(strength, confidence)

Use only create_value_by_type to create Value or Atom in execute_atom() method

from opencog.atomspace cimport Atom, AtomSpace, TruthValue, PtrHolder
from opencog.atomspace cimport cAtomSpace, cTruthValue
from opencog.atomspace cimport tv_ptr, strength_t, count_t, shared_ptr
from opencog.atomspace cimport handle_cast
from cython.operator cimport dereference as deref

from opencog.atomspace import is_a, types, create_value_by_type

def execute_atom(AtomSpace atomspace, Atom atom):
    if atom == None: raise ValueError("execute_atom atom is: None")
    cdef cValuePtr c_value_ptr = c_execute_atom(atomspace.atomspace,
                                           deref(atom.handle))
    type = deref(c_value_ptr).get_type()
    cdef PtrHolder ptr_holder = PtrHolder.create(<shared_ptr[void]&>c_value_ptr)
    return create_value_by_type(type, ptr_holder, atomspace)

def evaluate_atom(AtomSpace atomspace, Atom atom):
    if atom == None: raise ValueError("evaluate_atom atom is: None")
    cdef tv_ptr result_tv_ptr = c_evaluate_atom(atomspace.atomspace,
                                                deref(atom.handle))
    cdef cTruthValue* result_tv = result_tv_ptr.get()
    cdef strength_t strength = deref(result_tv).get_mean()
    cdef strength_t confidence = deref(result_tv).get_confidence()
    return TruthValue(strength, confidence)


#cython: cdivison=True
#cython: boundscheck=False
#cython: nonecheck=False
#cython: wraparound=False

cdef inline unsigned char point_in_polygon(int nr_verts, double *xp, double *yp,
                                           double x, double y):
    cdef int i
    cdef unsigned char c = 0
    cdef int j = nr_verts - 1
    for i in range(nr_verts):
        if (
            (((yp[i] <= y) and (y < yp[j])) or
            ((yp[j] <= y) and (y < yp[i])))
            and (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])
        ):
            c = not c
        j = i
    return c

cdef void points_in_polygon(int nr_verts, double *xp, double *yp,
                            int nr_points, double *x, double *y,
                            unsigned char *result):
    cdef int n
    for n in range(nr_points):
        result[n] = point_in_polygon(nr_verts, xp, yp, x[n], y[n])

Add doc string to point in polygon functions

#cython: cdivison=True
#cython: boundscheck=False
#cython: nonecheck=False
#cython: wraparound=False

cdef inline unsigned char point_in_polygon(int nr_verts, double *xp, double *yp,
                                           double x, double y):
    """Test whether point lies inside a polygon.

    Parameters
    ----------
    nr_verts : int
        Number of vertices of polygon.
    xp, yp : double array
        Coordinates of polygon with length nr_verts.
    x, y : double
        Coordinates of point.
    """
    cdef int i
    cdef unsigned char c = 0
    cdef int j = nr_verts - 1
    for i in range(nr_verts):
        if (
            (((yp[i] <= y) and (y < yp[j])) or
            ((yp[j] <= y) and (y < yp[i])))
            and (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])
        ):
            c = not c
        j = i
    return c

cdef void points_in_polygon(int nr_verts, double *xp, double *yp,
                            int nr_points, double *x, double *y,
                            unsigned char *result):
    """Test whether points lie inside a polygon.

    Parameters
    ----------
    nr_verts : int
        Number of vertices of polygon.
    xp, yp : double array
        Coordinates of polygon with length nr_verts.
    nr_points : int
        Number of points to test.
    x, y : double array
        Coordinates of points.
    result : unsigned char array
        Test results for each point.
    """
    cdef int n
    for n in range(nr_points):
        result[n] = point_in_polygon(nr_verts, xp, yp, x[n], y[n])


cdef extern from "folly/Range.h" namespace "folly":
    cdef cppclass Range[T]:
        Range()
        Range(T, int)
        T data()
        T begin()
        T end()
        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()]

Revert D22499183: refactor Enum code generation

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()]


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

Add nr_out attribute to AveragedPerceptron

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


# -*- 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 +

Remove unused cimport of indextype

# -*- 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 +


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.

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)


# 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


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")

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

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")


# 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)

Use format string with SDL_SetError.

# 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)


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

Change demo file back to original output style.

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()


# 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 testcase for local variable deletion

# 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


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])

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

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])


# 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
"""

Fix unrelated test after changing MemoryView.pyx.

# 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
"""


#!/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)

Simplify and speed up dit.math.close

#!/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)


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

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

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

Use PyUnicode_EncodeFSDefault() to encode unicode paths

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

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

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


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

Make fnv object more like hashlib hashes

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


# 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

Use size_t to store Python object id

# 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


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

Document the motivation for the fix

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


# 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


# 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>'

Add some more test cases

# 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>'


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)

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__.

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)


# 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
"""

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

# 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
"""


# 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

Use False instead 0 for bint

# 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


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)

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

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)


# 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
"""

Add testcase for closure variable deletion

# 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
"""


# 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.

# mode: compile
# tag: pep492, await

# Need to include all utility code !

async def sleep(x):
    pass

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


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)

Update code to pass tests

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


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

Use 32 bit floats, not 64 bit.

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


# 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
"""

Make a working test actually test something.

# 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
"""


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))

Fix queue Python property in StateClass

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))


# 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
"""

Fix unrelated test after changing MemoryView.pyx.

# 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
"""


Add test for memoryview of extension type

# 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 some simple run tests for control-flow

# 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