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

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

    cdef cppclass state:
        double perplexity()
        size_t nentities()
        size_t ntopics()
        size_t nwords()
        size_t nterms(size_t) except +

        vector[vector[size_t]] assignments()
        vector[vector[size_t]] dish_assignments()
        vector[vector[size_t]] table_assignments()
        vector[vector[float]] document_distribution()
        vector[map[size_t, float]] word_distribution()

        float score_assignment()
        float score_data(rng_t &)

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

    initialize_explicit "microscopes::lda::state::initialize" (
                                 const model_definition &defn,
                                 float alpa, float beta, float gamma,
                                 const vector[vector[size_t]] &dish_assignments,
                                 const vector[vector[size_t]] &table_assignments,
                                 vector[vector[size_t]] &docs,
                                 rng_t &rng) except +

Improve dish/table assignment initialization function


Fix ctypedef for size_t, should be removed when Cython-0.11 gets released


#
# 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 inline WindowedValue create(
  object value, int64_t timestamp_micros, object windows)

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

Remove the inline from WindowedValue.create()


cimport ccygroonga

def init():
    return ccygroonga.grn_init()

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

    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

    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)
        return Database(self._c_ctx, c_db)

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

def fin():
    return ccygroonga.grn_fin()

cimport ccygroonga

def init():
    return ccygroonga.grn_init()

def fin():
    return ccygroonga.grn_fin()

cdef new_database(ccygroonga.grn_ctx* c_ctx, ccygroonga.grn_obj* c_db):
    db = Database()
    db._c_ctx = c_ctx
    db._c_db = c_db

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

cdef class Context:
    cdef ccygroonga.grn_ctx* _c_ctx

    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)
        return new_database(self._c_ctx, c_db)

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

Write a factory function to create a Database instance


from math import log

cdef extern from "mtwist-1.1/mtwist.c":
    double mt_drand()
    void mt_seed32new(unsigned int)

def uniform():
    return mt_drand()

def exp(p):
    u = 1.0 - uniform()
    return -log(u)/p

def seed(s):
    mt_seed32new (s)

from math import log

# cdef extern void c_eject_tomato "eject_tomato" (float speed)
cdef extern from "mtwist-1.1/mtwist.c":
    double c_mt_drand "mt_drand" ()
    void c_mt_seed32new "mt_seed32new" (unsigned int)

#def uniform():
#    return mt_drand()
#
#def exp(p):
#    u = 1.0 - uniform()
#    return -log(u)/p
#
#def seed(s):
#    mt_seed32new (s)

def mt_drand():
    return c_mt_drand()

def mt_seed32new(s):
    c_mt_seed32new (s)

Use identical names for C and python

Use identical names for C and python


__doc__ = u"""
    >>> test()
    neg False
    pos True
    neg
    pos
    neg
    pos
"""

def test():
    cdef long neg = -1
    cdef unsigned long pos = -2 # will be a large positive number

    print "neg", neg > 0
    print "pos", pos > -

    D = { neg: 'neg', pos: 'pos' }

    print D[<object>neg]
    print D[<object>pos]

    print D[neg]
    print D[pos]

__doc__ = u"""
    >>> test()
    neg False
    pos True
    neg
    pos
    neg
    pos
"""

def test():
    cdef long neg = -1
    cdef unsigned long pos = -2 # will be a large positive number

    print "neg", neg > 0
    print "pos", pos > 0

    D = { neg: 'neg', pos: 'pos' }

    print D[<object>neg]
    print D[<object>pos]

    print D[neg]
    print D[pos]

Fix test case syntax error

Fix test case syntax error


from daapserver cimport constants

cimport cython

cdef class TreeRevisionStorage(object):
    cpdef public int revision
    cdef int last_operation
    cdef object storage

    @cython.locals(low=cython.int, middle=cython.int, high=cython.int)
    cdef get_index(self, object key, int revision)

from daapserver cimport constants

cimport cython

cdef class TreeRevisionStorage(object):
    cpdef public int revision
    cdef public int last_operation
    cdef object storage

    @cython.locals(low=cython.int, middle=cython.int, high=cython.int)
    cdef get_index(self, object key, int revision)

Fix tests for compiled code.

Fix tests for compiled code.


cimport libc

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

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

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

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

cimport libc

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

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

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

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

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


cdef extern from "<utility>" namespace "std" nogil:
    cdef cppclass pair[T, U]:
        ctypedef T first_type
        ctypedef U second_type
        T first
        U second
        pair() except +
        pair(pair&) except +
        pair(T&, U&) except +
        bint operator==(pair&, pair&)
        bint operator!=(pair&, pair&)
        bint operator<(pair&, pair&)
        bint operator>(pair&, pair&)
        bint operator<=(pair&, pair&)
        bint operator>=(pair&, pair&)

cdef extern from * namespace "cython_std" nogil:
    """
    #if __cplusplus > 199711L
    #include <type_traits>

    namespace cython_std {
    template <typename T> typename std::remove_reference<T>::type&& move(T& t) noexcept { return std::move(t); }
    template <typename T> typename std::remove_reference<T>::type&& move(T&& t) noexcept { return std::move(t); }
    }

    #endif
    """
    cdef T move[T](T)

cdef extern from "<utility>" namespace "std" nogil:
    cdef cppclass pair[T, U]:
        ctypedef T first_type
        ctypedef U second_type
        T first
        U second
        pair() except +
        pair(pair&) except +
        pair(T&, U&) except +
        bint operator==(pair&, pair&)
        bint operator!=(pair&, pair&)
        bint operator<(pair&, pair&)
        bint operator>(pair&, pair&)
        bint operator<=(pair&, pair&)
        bint operator>=(pair&, pair&)

cdef extern from * namespace "cython_std" nogil:
    """
    #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
    // move should be defined for these versions of MSVC, but __cplusplus isn't set usefully
    #include <type_traits>

    namespace cython_std {
    template <typename T> typename std::remove_reference<T>::type&& move(T& t) noexcept { return std::move(t); }
    template <typename T> typename std::remove_reference<T>::type&& move(T&& t) noexcept { return std::move(t); }
    }

    #endif
    """
    cdef T move[T](T)

Enable usage of std::move() only in the supported versions of MSVC (GH-4191)


#!/usr/bin/python

import json
from Bio import SeqIO

def compute(inputfile, format):
    cdef str line = ''
    cdef int i = 0
    cdef int j = 0
    cdef int line_len = 0
    cdef str token = ''
    cdef int token_cnt = 0
    for seq_record in SeqIO.parse(inputfile, format):
        cnt = {}
        line = str(seq_record.seq)
        line_len = len(line)
        for j in xrange(line_len / 3):
            i = j * 3
            token = line[i:i+3]
            try:
                token_cnt = cnt[token]
            except KeyError:
                cnt[token] = 1
            else:
                cnt[token] = token_cnt + 1
            print json.dumps(
                [{
                    "id": seq_record.id,
                    "ratios": cnt
                }])

Fix bug introduced by pep8 changes

Fix bug introduced by pep8 changes


# mode: run

cdef object executable, version_info
cdef long hexversion

ctypedef struct MyStruct:
    int x, y, z

# conversion code for this struct will be generated but not used
# (there used to be a problem getting Cython conversion code generated here)
cdef MyStruct _no_such_name_ = MyStruct(1, 2, 3)

from libc.math cimport NAN

# Danger ahead!
from sys import *

def test_cdefed_objects():
    """
    >>> ex, vi = test_cdefed_objects()
    >>> assert ex is not None
    >>> assert vi is not None
    """
    return executable, version_info

def test_cdefed_cvalues():
    """
    >>> hexver = test_cdefed_cvalues()
    >>> assert hexver is not None
    >>> assert hexver > 0x02020000
    """
    return hexversion

def test_non_cdefed_names():
    """
    >>> mod, pth = test_non_cdefed_names()
    >>> assert mod is not None
    >>> assert pth is not None
    """
    return modules, path

def test_cimported_NAN():
    """
    >>> from math import isnan
    >>> nan = test_cimported_NAN()
    >>> isnan(nan)
    True
    """
    return NAN

# mode: run

cdef object executable, version_info
cdef long hexversion

ctypedef struct MyStruct:
    int x, y, z

# conversion code for this struct will be generated but not used
# (there used to be a problem getting Cython conversion code generated here)
cdef MyStruct _no_such_name_ = MyStruct(1, 2, 3)

from libc.math cimport M_PI

# Danger ahead!
from sys import *

def test_cdefed_objects():
    """
    >>> ex, vi = test_cdefed_objects()
    >>> assert ex is not None
    >>> assert vi is not None
    """
    return executable, version_info

def test_cdefed_cvalues():
    """
    >>> hexver = test_cdefed_cvalues()
    >>> assert hexver is not None
    >>> assert hexver > 0x02020000
    """
    return hexversion

def test_non_cdefed_names():
    """
    >>> mod, pth = test_non_cdefed_names()
    >>> assert mod is not None
    >>> assert pth is not None
    """
    return modules, path

def test_cimported_pi():
    """
    >>> pi = test_cimported_pi()
    >>> 3.14 < pi < 3.15
    True
    """
    return M_PI

Use a different constant (PI instead of NAN) in the "import *" test since NAN does not seem to be defined by MSVC.


from cython.operator cimport dereference as deref
from libcpp.vector cimport vector
from libc.stddef cimport size_t

from microscopes.common.variadic._dataview cimport (
    abstract_dataview,
)
from microscopes.common._rng cimport rng
from microscopes.lda._model_h cimport (
    state as c_state,
    initialize as c_initialize,
)
from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.lda.definition cimport model_definition

cdef class state:
    """The underlying state of a Hierarchial Dirichlet Process LDA

    You should not explicitly construct a state object.
    Instead, use `initialize`.
    """
    cdef shared_ptr[c_state] _thisptr
    # cdef model_definition _defn
    # cdef abstract_dataview _data

from cython.operator cimport dereference as deref
from libcpp.vector cimport vector
from libc.stddef cimport size_t

from microscopes.common.variadic._dataview cimport (
    abstract_dataview,
)
from microscopes.common._rng cimport rng
from microscopes.lda._model_h cimport (
    state as c_state,
    initialize as c_initialize,
)
from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.lda.definition cimport model_definition

cdef class state:
    """The underlying state of a Hierarchial Dirichlet Process LDA

    You should not explicitly construct a state object.
    Instead, use `initialize`.
    """
    cdef shared_ptr[c_state] _thisptr
    cdef model_definition _defn
    # cdef abstract_dataview _data

Add _defn to state object

Add _defn to state object


# -*- coding: utf-8 -*-
"""
Created on Thu Mar 26 17:59:28 2015

@author: sljg2
"""

__all__ = ["friends_of_friends"]
__version__ = "0.1"
__author__ = "Simon Gibbons (sljg2@ast.cam.ac.uk)"
__copyright__ = "Copyright 2015 Simon Gibbons"

cimport numpy as np
from libcpp.list cimport list

cdef extern from "fof.hpp":
    cdef list[list[size_t]] _friends_of_friends "friends_of_friends"(double*, size_t, size_t, double) except +

def friends_of_friends(np.ndarray[double, ndim=2] data, double linking_length):
    """ Computes friends-of-friends clustering of data. Distances are computed
    using a euclidian metric.

        :param data: A numpy array with dimensions (npoints x ndim)

        :param linking_length: The linking length between cluster members

        :rtype: A list of lists of indices in each cluster type
    """

    cdef np.ndarray[double, ndim=2, mode='c'] data_corder

    data_corder = data if data.flags['C_CONTIGUOUS'] else data.copy(order='C')

    return _friends_of_friends(&data_corder[0,0],\
                               data.shape[0],\
                               data.shape[1],\
                               linking_length)

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 26 17:59:28 2015

@author: sljg2
"""

__all__ = ["friends_of_friends"]
__version__ = "0.1.3-dev"
__author__ = "Simon Gibbons (sljg2@ast.cam.ac.uk)"
__copyright__ = "Copyright 2015 Simon Gibbons"

cimport numpy as np
from libcpp.list cimport list

cdef extern from "fof.hpp":
    cdef list[list[size_t]] _friends_of_friends "friends_of_friends"(double*, size_t, size_t, double) except +

def friends_of_friends(np.ndarray[double, ndim=2] data, double linking_length):
    """ Computes friends-of-friends clustering of data. Distances are computed
    using a euclidian metric.

        :param data: A numpy array with dimensions (npoints x ndim)

        :param linking_length: The linking length between cluster members

        :rtype: A list of lists of indices in each cluster type
    """

    cdef np.ndarray[double, ndim=2, mode='c'] data_corder

    data_corder = data if data.flags['C_CONTIGUOUS'] else data.copy(order='C')

    if np.any( np.isnan(data_corder) ):
        raise ValueError("NaN detected in pyfof")

    return _friends_of_friends(&data_corder[0,0],\
                               data.shape[0],\
                               data.shape[1],\
                               linking_length)

Raise an exception if a NaN is passed to prevent a memory leak


from cpython.ref cimport Py_INCREF, Py_DECREF

cdef class PtrValue(Value):

    def __init__(self, obj = None, value_ptr = None):
        if obj is not None:
            Py_INCREF(obj)
            cvalue = createPtrValue(<void*>obj, decref)
            super(PtrValue, self).__init__(ValuePtr.create(cvalue))
        else:
            super(PtrValue, self).__init__(value_ptr)

    def value(self):
        return <object>((<cPtrValue*>self.get_c_value_ptr().get()).value())

cdef void decref(void* obj):
    Py_DECREF(<object>obj)

def valueToPtrValue(value):
    return PtrValue(value_ptr =
                    ValuePtr.create((<Value>value).get_c_value_ptr()))

from cpython.ref cimport Py_INCREF, Py_DECREF

cdef class PtrValue(Value):

    def __init__(self, obj = None, value_ptr = None):
        if obj is not None:
            Py_INCREF(obj)
            cvalue = createPtrValue(<void*>obj, decref)
            super(PtrValue, self).__init__(PtrHolder.create(<shared_ptr[void]&>cvalue))
        else:
            super(PtrValue, self).__init__(value_ptr)

    def value(self):
        return <object>((<cPtrValue*>self.get_c_value_ptr().get()).value())

cdef void decref(void* obj):
    Py_DECREF(<object>obj)

def valueToPtrValue(value):
    return PtrValue(value_ptr = (<Value>value).ptr_holder)

Fix build after latest API changes from opencog


Remove ancient ctypedef for size_t

Remove ancient ctypedef for size_t


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

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

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

Fix compile weight_t back to double, not float


# ticket: 290
# mode: error

cdef extern:
    cdef packed struct MyStruct:
        char a

_ERRORS = u"""
5:9: Cannot declare extern struct as 'packed'
"""

# mode: compile

cdef extern from *:
    cdef packed struct MyStruct:
        char a

Fix extern packed struct test.

Fix extern packed struct test.


EMPTY_PLAYER = ' '
EMPTY_CSA = <Koma>None

cdef class Grid:

    '''Grid class

    Args
    -------------------
    which_player : optional (default = None)
    csa_piece : optional (default = None)
    '''
    def __init__(self, which_player=EMPTY_PLAYER, csa_piece=EMPTY_CSA):
        if which_player in [EMPTY_PLAYER, '+', '-']:
            self.which_player = which_player
        else:
            raise RuntimeError('Invalid which_player:', which_player)

        if csa_piece != EMPTY_CSA:
            self.koma = Koma(csa_piece)
        else:
            self.koma = EMPTY_CSA

    def __str__(self):
        if self.which_player != EMPTY_PLAYER and self.koma != EMPTY_CSA:
            return self.which_player + self.koma.csa
        else:
            return EMPTY_PLAYER + EMPTY_CSA

    cpdef is_empty(self):
        return self.which_player == EMPTY_PLAYER

    cpdef is_of_sente(self):
        return self.which_player == '+'

    cpdef reset(self):
        self.which_player = EMPTY_PLAYER
        self.koma = EMPTY_CSA

EMPTY_PLAYER = ' '
EMPTY_CSA = <Koma>None

cdef class Grid:

    '''Grid class

    Args
    -------------------
    which_player : optional (default = None)
    csa_piece : optional (default = None)
    '''
    def __init__(self, which_player=EMPTY_PLAYER, csa_piece=EMPTY_CSA):
        if which_player in [EMPTY_PLAYER, '+', '-']:
            self.which_player = which_player
        else:
            raise RuntimeError('Invalid which_player:', which_player)

        if csa_piece != EMPTY_CSA:
            self.koma = Koma(csa_piece)
        else:
            self.koma = EMPTY_CSA

    def __str__(self):
        if self.which_player != EMPTY_PLAYER and self.koma != EMPTY_CSA:
            return self.which_player + self.koma.csa
        else:
            return '   '

    cpdef is_empty(self):
        return self.which_player == EMPTY_PLAYER

    cpdef is_of_sente(self):
        return self.which_player == '+'

    cpdef reset(self):
        self.which_player = EMPTY_PLAYER
        self.koma = EMPTY_CSA

Fix return value of Grid.__str__()

Fix return value of Grid.__str__()


cdef extern from "shapes.h" namespace shapes:

    cdef cppclass Shape:
        area()

    cdef cppclass Rectangle(Shape):
        int width
        int height
        __init__(int, int)

    cdef cppclass Square(Shape):
        int side
        __init__(int)

cdef Rectangle *rect = new Rectangle(10, 20)
cdef Square *sqr = new Square(15)

del rect, sqr

__doc__ = u"""
    >>> test_new_del()
    >>> test_rect_area(3, 4)
    12
    >>> test_square_area(15)
    225
"""

cdef extern from "shapes.cpp" namespace shapes:

    cdef cppclass Shape:
        float area()

    cdef cppclass Rectangle(Shape):
        int width
        int height
        __init__(int, int)

    cdef cppclass Square(Shape):
        int side
        __init__(int)

def test_new_del():
    cdef Rectangle *rect = new Rectangle(10, 20)
    cdef Square *sqr = new Square(15)
    del rect, sqr

def test_rect_area(w, h):
    cdef Rectangle *rect = new Rectangle(w, h)
    try:
        return rect.area()
    finally:
        del rect

def test_square_area(w):
    cdef Square *sqr = new Square(w)
    cdef Rectangle *rect = sqr
    try:
        return rect.area(), sqr.area()
    finally:
        del sqr

Expand and fix cpp tests

Expand and fix cpp tests


from cpython.unicode cimport PyUnicode_DecodeUTF8, PyUnicode_AsUTF8String

cdef inline to_unicode(char *s):
	try:
		ns = PyUnicode_DecodeUTF8(s, len(s), NULL)
	except:
		ns = s
	return ns

cdef inline from_unicode(object o):
	if isinstance(o, unicode):
		return PyUnicode_AsUTF8String(o)
	else:
		return o

from cpython.unicode cimport PyUnicode_DecodeUTF8, PyUnicode_AsUTF8String
from libc.string cimport const_char, strlen

cdef inline to_unicode(const_char *s):
	return PyUnicode_DecodeUTF8(s, strlen(s), "replace")

cdef inline from_unicode(object o):
	if isinstance(o, unicode):
		return PyUnicode_AsUTF8String(o)
	else:
		return o

Fix string constness in to_unicode

OTHER: Fix string constness in to_unicode


# it'd be nice to define the type of atomspace but I can't get this working
from opencog.cogserver cimport cAgent, stim_t, string

cdef class MindAgent:
    # TODO add a pointer to the Agent C++ object so that calls to stimulate atoms
    # can be made (among other things)

    def __cinit__(self):
        pass

    property frequency:
        def __get__(self):
            return self.c_obj.frequency()
        def __set__(self,int f):
            self.c_obj.setFrequency(f)

# These methods are not available until we have support for MindAgents running
# continuously in their own threads
#    def start(self, AtomSpace atomspace):
#        pass

#    def end(self):
#        pass

    def run(self, atomspace):
        print "Implement me in your MindAgent subclass: " + atomspace

# it'd be nice to define the type of atomspace but I can't get this working
from opencog.cogserver cimport cAgent, stim_t, string

from opencog.atomspace cimport cHandle, Atom

cdef class MindAgent:

    def __cinit__(self):
        pass

    property frequency:
        def __get__(self):
            return self.c_obj.frequency()
        def __set__(self,int f):
            self.c_obj.setFrequency(f)

    def stimulate_atom(self, Atom atom, stim_t amount):
        cdef cHandle ch
        ch = cHandle(atom.h.value())

        return self.c_obj.stimulateAtom(ch, amount)

# These methods are not available until we have support for MindAgents running
# continuously in their own threads
#    def start(self, AtomSpace atomspace):
#        pass

#    def end(self):
#        pass

    def run(self, atomspace):
        print "Implement me in your MindAgent subclass: " + atomspace

Add MindAgent::stimulateAtom to the python bindings


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 not xs:
        raise ValueError("active information not defined on empty containers")

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

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

cimport cython.array
cimport numpy
import numpy

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

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

def activeinfo(xs, uint64_t k, uint64_t b = 0):
    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)

Check activeinfo inputs for empty arrays

The prior check, `not xs`, only works for sequence-like objects.
Unfortunately numpy arrays are not sequences, so we have to
actually check the length.


cdef object[int] buf
cdef class A:
    cdef object[int] buf

def f():
    cdef object[fakeoption=True] buf1
    cdef object[int, -1] buf1b
    cdef object[ndim=-1] buf2
    cdef object[int, 'a'] buf3
    cdef object[int,2,3,4,5,6] buf4
    cdef object[int, 2, 'foo'] buf5
    cdef object[int, 2, well] buf6

_ERRORS = u"""
1:17: Buffer types only allowed as function local variables
3:21: Buffer types only allowed as function local variables
6:31: "fakeoption" is not a buffer option
"""
#TODO:
#7:22: "ndim" must be non-negative
#8:15: "dtype" missing
#9:21: "ndim" must be an integer
#10:15: Too many buffer options
#11:24: Only allowed buffer modes are "full" or "strided" (as a compile-time string)
#12:28: Only allowed buffer modes are "full" or "strided" (as a compile-time string)
#"""

cdef object[int] buf
cdef class A:
    cdef object[int] buf

def f():
    cdef object[fakeoption=True] buf1
    cdef object[int, -1] buf1b
    cdef object[ndim=-1] buf2
    cdef object[int, 'a'] buf3
    cdef object[int,2,3,4,5,6] buf4
    cdef object[int, 2, 'foo'] buf5
    cdef object[int, 2, well] buf6
    cdef object[x, 1] buf0

_ERRORS = u"""
1:17: Buffer types only allowed as function local variables
3:21: Buffer types only allowed as function local variables
6:31: "fakeoption" is not a buffer option
"""
#TODO:
#7:22: "ndim" must be non-negative
#8:15: "dtype" missing
#9:21: "ndim" must be an integer
#10:15: Too many buffer options
#11:24: Only allowed buffer modes are "full" or "strided" (as a compile-time string)
#12:28: Only allowed buffer modes are "full" or "strided" (as a compile-time string)
#13:17: Invalid type.
#"""

Test for bad buffer types.

Test for bad buffer types.


"A bunch of improper (not normalized) prior distributions"
from libc.math cimport log, fabs

cdef inline double ln_normal(double value, double mean, double stddev):
    cdef double tmp = (value - mean) / stddev
    tmp *= tmp
    return -0.5 * tmp - log(stddev)

cdef inline double ln_laplace(double value, double mean, double stddev):
    return -1.0 * fabs(value - mean) / stddev - log(stddev)

cdef inline double ln_log(double value):
    return -1.0 * log(value)

cdef inline double ln_gamma(double value, double k, double theta):
    return (k - 1.0) * log(value) - value / theta

cdef inline double ln_beta(double value, double p, double q):
    return (p - 1.0) * log(value) + (q - 1.0) * log(1 - value)

"A bunch of improper (not normalized) prior distributions"
from libc.math cimport log, fabs, sqrt, exp, M_PI

cdef inline double ln_normal(double value, double mean, double stddev):
    cdef double tmp = (value - mean) / stddev
    tmp *= tmp
    return -0.5 * tmp - log(stddev)

cdef inline double ln_laplace(double value, double mean, double stddev):
    return -1.0 * fabs(value - mean) / stddev - log(stddev)

cdef inline double ln_log(double value):
    return -1.0 * log(value)

cdef inline double ln_gamma(double value, double k, double theta):
    return (k - 1.0) * log(value) - value / theta

cdef inline double ln_beta(double value, double p, double q):
    return (p - 1.0) * log(value) + (q - 1.0) * log(1 - value)

cdef inline double normal(double value, double mean, double stddev):
    cdef double tmp = (value - mean) / stddev
    tmp *= tmp
    return exp(-0.5 * tmp) / sqrt(2. * M_PI) / stddev

Add non-logarithmic normal for mixture model


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

msleep(milliseconds=1)

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

msleep(milliseconds=1)

Fix test compile failure in MSVC.

Fix test compile failure in MSVC.


from libcpp cimport bool as cpp_bool
from libcpp.string cimport string
from libcpp.vector cimport vector
from libc.stdint cimport uint32_t
from libc.stdint cimport int64_t
from libc.stdint cimport uint64_t

from status cimport Status
from db cimport DB
from env cimport Env

cdef extern from "utilities/backupable_db.h" namespace "rocksdb":
    ctypedef uint32_t BackupID

    cdef cppclass BackupableDBOptions:
        BackupableDBOptions(const string& backup_dir)

    cdef struct BackupInfo:
        BackupID backup_id
        int64_t timestamp
        uint64_t size

    cdef cppclass BackupEngine:
        Status CreateNewBackup(DB*, cpp_bool) nogil except+
        Status PurgeOldBackups(uint32_t) nogil except+
        Status DeleteBackup(BackupID) nogil except+
        void StopBackup() nogil except+
        void GetBackupInfo(vector[BackupInfo]*) nogil except+
        Status RestoreDBFromBackup(BackupID, string&, string&) nogil except+
        Status RestoreDBFromLatestBackup(string&, string&) nogil except+

    cdef BackupEngine* NewBackupEngine "rocksdb::BackupEngine::NewBackupEngine"(
            Env*,
            BackupableDBOptions&)

from libcpp cimport bool as cpp_bool
from libcpp.string cimport string
from libcpp.vector cimport vector
from libc.stdint cimport uint32_t
from libc.stdint cimport int64_t
from libc.stdint cimport uint64_t

from status cimport Status
from db cimport DB
from env cimport Env

cdef extern from "rocksdb/utilities/backupable_db.h" namespace "rocksdb":
    ctypedef uint32_t BackupID

    cdef cppclass BackupableDBOptions:
        BackupableDBOptions(const string& backup_dir)

    cdef struct BackupInfo:
        BackupID backup_id
        int64_t timestamp
        uint64_t size

    cdef cppclass BackupEngine:
        Status CreateNewBackup(DB*, cpp_bool) nogil except+
        Status PurgeOldBackups(uint32_t) nogil except+
        Status DeleteBackup(BackupID) nogil except+
        void StopBackup() nogil except+
        void GetBackupInfo(vector[BackupInfo]*) nogil except+
        Status RestoreDBFromBackup(BackupID, string&, string&) nogil except+
        Status RestoreDBFromLatestBackup(string&, string&) nogil except+

    cdef BackupEngine* NewBackupEngine "rocksdb::BackupEngine::NewBackupEngine"(
            Env*,
            BackupableDBOptions&)

Change include path and get rid of the warning.


cdef extern from "Python.h":

    ############################################################################
    # 7.2.3
    ############################################################################
    # PyFloatObject
    #
    # This subtype of PyObject represents a Python floating point object.

    # PyTypeObject PyFloat_Type
    #
    # This instance of PyTypeObject represents the Python floating
    # point type. This is the same object as float and
    # types.FloatType.

    bint PyFloat_Check(object p)
    # Return true if its argument is a PyFloatObject or a subtype of
    # PyFloatObject.

    bint PyFloat_CheckExact(object p)
    # Return true if its argument is a PyFloatObject, but not a
    # subtype of PyFloatObject.

    object PyFloat_FromString(object str, char **pend)
    # Return value: New reference.
    # Create a PyFloatObject object based on the string value in str,
    # or NULL on failure. The pend argument is ignored. It remains
    # only for backward compatibility.

    object PyFloat_FromDouble(double v)
    # Return value: New reference.
    # Create a PyFloatObject object from v, or NULL on failure.

    double PyFloat_AsDouble(object pyfloat) except? -1
    # Return a C double representation of the contents of pyfloat.

    double PyFloat_AS_DOUBLE(object pyfloat)
    # Return a C double representation of the contents of pyfloat, but
    # without error checking.

cdef extern from "Python.h":
    """
    #if PY_MAJOR_VERSION >= 3
      #define __Pyx_PyFloat_FromString(obj)  PyFloat_FromString(obj)
    #else
      #define __Pyx_PyFloat_FromString(obj)  PyFloat_FromString(obj, NULL)
    #endif
    """

    ############################################################################
    # 7.2.3
    ############################################################################
    # PyFloatObject
    #
    # This subtype of PyObject represents a Python floating point object.

    # PyTypeObject PyFloat_Type
    #
    # This instance of PyTypeObject represents the Python floating
    # point type. This is the same object as float and
    # types.FloatType.

    bint PyFloat_Check(object p)
    # Return true if its argument is a PyFloatObject or a subtype of
    # PyFloatObject.

    bint PyFloat_CheckExact(object p)
    # Return true if its argument is a PyFloatObject, but not a
    # subtype of PyFloatObject.

    object PyFloat_FromString "__Pyx_PyFloat_FromString" (object str)
    # Return value: New reference.
    # Create a PyFloatObject object based on the string value in str,
    # or NULL on failure. The pend argument is ignored. It remains
    # only for backward compatibility.

    object PyFloat_FromDouble(double v)
    # Return value: New reference.
    # Create a PyFloatObject object from v, or NULL on failure.

    double PyFloat_AsDouble(object pyfloat) except? -1
    # Return a C double representation of the contents of pyfloat.

    double PyFloat_AS_DOUBLE(object pyfloat)
    # Return a C double representation of the contents of pyfloat, but
    # without error checking.

Change the declaration of PyFloat_FromString to the Py3 signature and provide a fallback for Py2.

Closes https://github.com/cython/cython/issues/3909


#/*##########################################################################
#
# The fisx library for X-Ray Fluorescence
#
# Copyright (c) 2014-2016 European Synchrotron Radiation Facility
#
# This file is part of the fisx X-ray developed by V.A. Sole
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
cimport cython

#from libcpp.string cimport string as std_string

from Version cimport fisxVersion as _fisxVersion

def fisxVersion():
    return _fisxVersion()

#/*##########################################################################
#
# The fisx library for X-Ray Fluorescence
#
# Copyright (c) 2014-2016 European Synchrotron Radiation Facility
#
# This file is part of the fisx X-ray developed by V.A. Sole
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
cimport cython

#from libcpp.string cimport string as std_string

from Version cimport fisxVersion as _fisxVersion

def fisxVersion():
    return toString(_fisxVersion())

Return as string and not as bytes.

Return as string and not as bytes.


# tag: cpp
# mode: compile

cdef extern from "point.h" namespace "geometry":

    cdef struct Point:
        double x
        double y
        int color

cdef Point p = Point(0.0, 0.0, 0)
the_point = p

# tag: cpp
# mode: compile

from cython.view import array

cdef extern from "point.h" namespace "geometry":

    cdef struct Point:
        double x
        double y
        int color

cdef Point p = Point(0.0, 0.0, 0)
the_point = p

cdef Point[::1] ps = array((10,), itemsize=sizeof(Point), format='ddi')

Add a test for namespaced struct with memoryview


# cython: warn.maybe_uninitialized=True
# mode: error
# tag: werror

def foo(x):
    a = 1
    del a, b
    b = 2
    return a, b

_ERRORS = """
7:9: Deletion of non-Python, non-C++ object
7:12: local variable 'b' referenced before assignment
7:12: Deletion of non-Python, non-C++ object
9:12: local variable 'a' referenced before assignment
"""

# cython: warn.maybe_uninitialized=True
# mode: error
# tag: werror

def foo(x):
    a = 1
    del a, b
    b = 2
    return a, b

_ERRORS = """
7:12: local variable 'b' referenced before assignment
9:12: local variable 'a' referenced before assignment
"""

Fix w_uninitilized_del testcase, now inferred as python object


from libc.stdio cimport FILE, fopen
from libc.stdlib cimport malloc, free
from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject

def open_file():
    cdef FILE* p
    p = fopen("spam.txt", "r")
    if p is NULL:
        PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt")
    ...

def allocating_memory(number=10):
    cdef double *my_array = <double *> malloc(number * sizeof(double))
    if not my_array:
        raise MemoryError()
    ...
    free(my_array)

from libc.stdio cimport FILE, fopen
from libc.stdlib cimport malloc, free
from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject

def open_file():
    cdef FILE* p
    p = fopen("spam.txt", "r")
    if p is NULL:
        PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt")
    ...

def allocating_memory(number=10):
    cdef double *my_array = <double *> malloc(number * sizeof(double))
    if not my_array:  # same as 'is NULL' above
        raise MemoryError()
    ...
    free(my_array)

Add comment in doc example to explain that two ways to compare pointers are equivalent


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

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

    cdef cppclass state:
        double perplexity()
        size_t nentities()
        size_t ntopics()
        size_t nwords()
        size_t nterms(size_t) except +

        vector[vector[size_t]] assignments()
        vector[vector[size_t]] dish_assignments()
        vector[vector[size_t]] table_assignments()
        vector[vector[float]] document_distribution()
        vector[map[size_t, float]] word_distribution()

        float score_assignment()
        float score_data(rng_t &)

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

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

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

    cdef cppclass state:
        double perplexity()
        size_t nentities()
        size_t ntopics()
        size_t nwords()
        size_t nterms(size_t) except +

        vector[vector[size_t]] assignments()
        vector[vector[size_t]] dish_assignments()
        vector[vector[size_t]] table_assignments()
        vector[vector[float]] document_distribution()
        vector[map[size_t, float]] word_distribution()

        float score_assignment()
        float score_data(rng_t &)

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

Fix spelling error in variable name

Fix spelling error in variable name


Test for bad buffer types.

Test for bad buffer types.


__doc__ = u"""
    >>> test()
    neg False
    pos True
    neg
    pos
    neg
    pos
"""

def test():
    cdef long neg = -1
    cdef unsigned long pos = -2 # will be a large positive number

    print "neg", neg > 0
    print "pos", pos > -

    D = { neg: 'neg', pos: 'pos' }

    print D[<object>neg]
    print D[<object>pos]

    print D[neg]
    print D[pos]

__doc__ = u"""
    >>> test()
    neg False
    pos True
    neg
    pos
    neg
    pos
"""

def test():
    cdef long neg = -1
    cdef unsigned long pos = -2 # will be a large positive number

    print "neg", neg > 0
    print "pos", pos > 0

    D = { neg: 'neg', pos: 'pos' }

    print D[<object>neg]
    print D[<object>pos]

    print D[neg]
    print D[pos]

Fix test case syntax error

Fix test case syntax error


# 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

# 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

Make the desktop use CLASSPATH if defined.


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

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

Remove tensor attribute from Span and Token.


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

Add cfg field to Tagger

Add cfg field to Tagger


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

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

Enumerate core audio devices in cython.

Enumerate core audio devices in cython.


# 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

# 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

Fix long to long long.

Fix long to long long.


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

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

Make create() avaialable to pure python callers


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

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

Replace __ prefixes with cdefs for better encapsulation


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

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

Fix string format error in 2.6

Fix string format error in 2.6

In 2.6, you should give index value to format.


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

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

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


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

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

Fix typo and now it compiles successfully


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

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

Add Cython compiler directives waiting release 0.11.2


# 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

# 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

Change test to initialize by `import_datetime`


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

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

Fix File error handling rules


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

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

Set default backends from environment variables


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

Add definition of Mixture Model

Add definition of Mixture Model


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

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

Correct cleanup when server fails

Correct cleanup when server fails

Summary:
Server has been crashing hard when it should be crashing soft (for
example, when binding to the same port. This diff fixes it.

Reviewed By: yfeldblum

Differential Revision: D4242841

fbshipit-source-id: 242ee09ea244ce662c3122f25a96b8b4e8d60fcf


# 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

# 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

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.


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

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

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

Fix a bug in zmq.check

Fix a bug in zmq.check


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

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

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


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

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

Fix for different Windows float repr

Fix for different Windows float repr


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

Add bag of words to header

Add bag of words to header


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 +

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

Make parameter a reference parameter


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
    )

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
    )

Fix C++ header name in Cython header

Fix C++ header name in Cython header

As the `_base` suffix has been dropped, update the Cython header
accordingly.


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

Fix channel bindings type signature

Fix channel bindings type signature

Cython 0.22 didn't like that we didn't have the `except NULL`
clause on the `__cvalue__` method in `ChannelBindings` in the
.pxd file.  This adds the clause to the .pxd file.


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
    )

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

Implement an `ndindex`-like Cython function

Provides a Cython function that acts similarly to `ndindex`. However it
is written in C-style. So it does not yield, does not use classes, etc.
It also doesn't keep state. However it can be used like a generator
except in a `while`-loop where state external to the function is update
on each run.


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

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

Add comments around cryptic stuct.unpack invocation.


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

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

Optimize construction of label indices

Optimize construction of label indices


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

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

Revert nogil .pxd to master (appears unused?)


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)

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)

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


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

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

Add doc string to point in polygon functions


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

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

Revert D22499183: refactor Enum code generation

Differential Revision:
D22499183 (https://github.com/facebook/folly/commit/24364356023d04f966a0ef6fc7536363932bdc82)

Original commit changeset: b2af8d94024c

fbshipit-source-id: a6bd56cdb4d4b73618a9374528371c5b18a0df10