Eujain Ting | 29 Aug 05:52 2015
Picon

Cython 0.23.1 generates unwanted casts in overloaded division operator c++ wrapper.

I have found a bug in v0.23.1 that didn't exist in v0.22:

When using the division operator on dissimilar types of object to int, eg. `foo :Foo / bar :int`, cython generates code that casts bar as type foo: `foo / (foo)bar`. 
This is when trying to utilize an overloaded / for `Foo operator/(int)`. It is possible that other arithmetic operators have the same problem.

This is breaking code of the pattern `foo operator/(int)` since the generated c++ code will attempt to cast the int argument for which there is no conversion.


Here is a concrete example that compiles under 0.22, but throws a compile error (cannot convert type) under 0.23.1:

### .h file, target to be wrapped ###
template<typename T, int Scale>
class Point{
...
Point<T, Scale> operator / (const T &v) const 
}

### .pxd file ###
cdef cppclass Point[T,Scale]:
Point operator/(T) const
...
ctypedef Point[int,bScale] TilePosition

### .pyx file ###
def __truediv__(TilePosition self, int other):
        cdef cPosition.TilePosition tt
        tt = self.thisobj / <int>other

### Generated .cpp file ###
 (BWAPI::Point<int,32> )__pyx_v_self->thisobj) / ((BWAPI::Point<int,32> )((int)__pyx_v_other)

### Cython v0.22 generates correct code without casting ###
 (__pyx_v_self->thisobj / ((int)__pyx_v_other))


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
smbat.makiyan | 28 Aug 12:57 2015

Calling destructor for object with overriden operator ~

Hi All,

 

On the project I am working for we are using technology that is very close to Microsoft COM.

There we use a smart pointer class for reference counting which is almost identical to well known CComPtr

 

Unfortunately it brought us a big annoyance - overridden operator&

 

template <typename T>

class MyPtr

{

private:

    /// Current referenced interface

    T * _reference;

 

public:

                /// Allows passing this smart ptr as argument to methods which expect

                /// a T** or a void**, in order to fill it in. (e.g. QueryInterface). NOTE: if the internal

                /// reference is non-NULL it will be Released.

                /// to this object.

                /// \deprecated Please use the outArg() method (or inOutArg()) instead.

                T** operator& ()

                {

                                clear();

                                return &_reference;

                }

 

...           

 

Well, it is deprecated but it still there. The issue is that operator& returns not the object's address, but address of its internal member

 

And this cause issues when used in cython objects like this

 

cdef class MyClass:

                cdef MyPtr[IInterface] obj

 

Generated deallocator does this:

                __Pyx_call_destructor(&p->obj);

and therefore __Pyx_call_destructor function gets wrong address. Luckily it works just because operator& clears internal reference and returns pointer to a NULL.

 

Is there any way to tweak tp_dealloc function generation and call destructor using object reference instead of getting object's address?

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Carl Meyer | 27 Aug 19:38 2015
Picon

[cython-users] Compilation error "lvalue required as left operand of assignment" in function ‘__pyx_import_star_set’, new in Cython 0.23.1

Hello,

I am not really a direct user of Cython, just a user of a package written in Cython. I believe that I've found a bug (or at least a backwards-incompatibility) introduced between Cython 0.23 and 0.23.1 which is causing the `xmlsec` (https://pypi.python.org/pypi/xmlsec) package to now fail to compile, where it previously compiled and installed fine.

Since the recent release of Cython 0.23.1, I am seeing a compilation error when I `pip install xmlsec` (requires libxml2, xmlsec1, and openssl). Full traceback is at the end of this email.

Prior to the release of Cython 0.23.1, the installation worked. When I force the use of a manually-downloaded Cython 0.23 instead of 0.23.1 (via the technique described at https://pip.pypa.io/en/latest/reference/pip_install.html#controlling-setup-requires), the installation works again.

Just posting here to confirm that this is in fact a Cython bug, and I should report it to the developers mailing list.

```
$ pip install xmlsec
Collecting xmlsec
  Using cached xmlsec-0.3.1.tar.gz
Collecting lxml>=3.0 (from xmlsec)
Building wheels for collected packages: xmlsec
  Running setup.py bdist_wheel for xmlsec
  Complete output from command /home/carljm/.venvs/tmp-3545f39dbf727860/bin/python -c "import setuptools;__file__='/tmp/pip-build-HQCz3j/xmlsec/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /tmp/tmpv6Xu5Kpip-wheel-:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-2.7
  creating build/lib.linux-x86_64-2.7/xmlsec
  copying src/xmlsec/error.py -> build/lib.linux-x86_64-2.7/xmlsec
  copying src/xmlsec/__init__.py -> build/lib.linux-x86_64-2.7/xmlsec
  copying src/xmlsec/meta.py -> build/lib.linux-x86_64-2.7/xmlsec
  running build_ext
  cythoning src/xmlsec/constants.pyx to src/xmlsec/constants.c
  building 'xmlsec.constants' extension
  creating build/temp.linux-x86_64-2.7
  creating build/temp.linux-x86_64-2.7/src
  creating build/temp.linux-x86_64-2.7/src/xmlsec
  x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_OPENSSL_098=1 -DXMLSEC_NO_GOST=1 -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_NO_XKMS=1 -DXMLSEC_NO_SIZE_T= -DXMLSEC_CRYPTO=openssl -Isrc -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml/includes -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml -I/usr/include/xmlsec1 -I/usr/include/libxml2 -I/usr/include/python2.7 -c src/xmlsec/constants.c -o build/temp.linux-x86_64-2.7/src/xmlsec/constants.o -DXMLSEC_CRYPTO_OPENSSL=1
  x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/xmlsec/constants.o -lxmlsec1 -lssl -lxslt -lxml2 -lcrypto -lxmlsec1-openssl -o build/lib.linux-x86_64-2.7/xmlsec/constants.so
  cythoning src/xmlsec/utils.pyx to src/xmlsec/utils.c
  building 'xmlsec.utils' extension
  x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_OPENSSL_098=1 -DXMLSEC_NO_GOST=1 -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_NO_XKMS=1 -DXMLSEC_NO_SIZE_T= -DXMLSEC_CRYPTO=openssl -Isrc -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml/includes -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml -I/usr/include/xmlsec1 -I/usr/include/libxml2 -I/usr/include/python2.7 -c src/xmlsec/utils.c -o build/temp.linux-x86_64-2.7/src/xmlsec/utils.o -DXMLSEC_CRYPTO_OPENSSL=1
  x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/xmlsec/utils.o -lxmlsec1 -lssl -lxslt -lxml2 -lcrypto -lxmlsec1-openssl -o build/lib.linux-x86_64-2.7/xmlsec/utils.so
  cythoning src/xmlsec/tree.pyx to src/xmlsec/tree.c
  building 'xmlsec.tree' extension
  x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_OPENSSL_098=1 -DXMLSEC_NO_GOST=1 -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_NO_XKMS=1 -DXMLSEC_NO_SIZE_T= -DXMLSEC_CRYPTO=openssl -Isrc -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml/includes -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml -I/usr/include/xmlsec1 -I/usr/include/libxml2 -I/usr/include/python2.7 -c src/xmlsec/tree.c -o build/temp.linux-x86_64-2.7/src/xmlsec/tree.o -DXMLSEC_CRYPTO_OPENSSL=1
  x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/xmlsec/tree.o -lxmlsec1 -lssl -lxslt -lxml2 -lcrypto -lxmlsec1-openssl -o build/lib.linux-x86_64-2.7/xmlsec/tree.so
  cythoning src/xmlsec/key.pyx to src/xmlsec/key.c
  building 'xmlsec.key' extension
  x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_OPENSSL_098=1 -DXMLSEC_NO_GOST=1 -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_NO_XKMS=1 -DXMLSEC_NO_SIZE_T= -DXMLSEC_CRYPTO=openssl -Isrc -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml/includes -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml -I/usr/include/xmlsec1 -I/usr/include/libxml2 -I/usr/include/python2.7 -c src/xmlsec/key.c -o build/temp.linux-x86_64-2.7/src/xmlsec/key.o -DXMLSEC_CRYPTO_OPENSSL=1
  x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/xmlsec/key.o -lxmlsec1 -lssl -lxslt -lxml2 -lcrypto -lxmlsec1-openssl -o build/lib.linux-x86_64-2.7/xmlsec/key.so
  cythoning src/xmlsec/ds.pyx to src/xmlsec/ds.c
  building 'xmlsec.ds' extension
  x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_OPENSSL_098=1 -DXMLSEC_NO_GOST=1 -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_NO_XKMS=1 -DXMLSEC_NO_SIZE_T= -DXMLSEC_CRYPTO=openssl -Isrc -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml/includes -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml -I/usr/include/xmlsec1 -I/usr/include/libxml2 -I/usr/include/python2.7 -c src/xmlsec/ds.c -o build/temp.linux-x86_64-2.7/src/xmlsec/ds.o -DXMLSEC_CRYPTO_OPENSSL=1
  x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/xmlsec/ds.o -lxmlsec1 -lssl -lxslt -lxml2 -lcrypto -lxmlsec1-openssl -o build/lib.linux-x86_64-2.7/xmlsec/ds.so
  cythoning src/xmlsec/enc.pyx to src/xmlsec/enc.c
  building 'xmlsec.enc' extension
  x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_OPENSSL_098=1 -DXMLSEC_NO_GOST=1 -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_NO_XKMS=1 -DXMLSEC_NO_SIZE_T= -DXMLSEC_CRYPTO=openssl -Isrc -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml/includes -I/tmp/pip-build-HQCz3j/xmlsec/.eggs/lxml-3.4.4-py2.7-linux-x86_64.egg/lxml -I/usr/include/xmlsec1 -I/usr/include/libxml2 -I/usr/include/python2.7 -c src/xmlsec/enc.c -o build/temp.linux-x86_64-2.7/src/xmlsec/enc.o -DXMLSEC_CRYPTO_OPENSSL=1
  src/xmlsec/enc.c: In function ‘__pyx_import_star_set’:
  src/xmlsec/enc.c:3384:37: error: lvalue required as left operand of assignment
       XMLSEC_ENC_RETURN_REPLACED_NODE = __Pyx_PyInt_As_unsigned_int(o); if ((XMLSEC_ENC_RETURN_REPLACED_NODE == (unsigned int)-1) && PyErr_Occurred()) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L2_error;};
                                       ^
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
 
  ----------------------------------------
  Failed building wheel for xmlsec
Failed to build xmlsec
Installing collected packages: lxml, xmlsec
  Running setup.py install for xmlsec
    Complete output from command /home/carljm/.venvs/tmp-3545f39dbf727860/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-HQCz3j/xmlsec/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-xqY4Oa-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/carljm/.venvs/tmp-3545f39dbf727860/include/site/python2.7/xmlsec:
    running install
    running build
    running build_py
    running build_ext
    skipping 'src/xmlsec/constants.c' Cython extension (up-to-date)
    skipping 'src/xmlsec/utils.c' Cython extension (up-to-date)
    skipping 'src/xmlsec/tree.c' Cython extension (up-to-date)
    skipping 'src/xmlsec/key.c' Cython extension (up-to-date)
    skipping 'src/xmlsec/ds.c' Cython extension (up-to-date)
    skipping 'src/xmlsec/enc.c' Cython extension (up-to-date)
    building 'xmlsec.enc' extension
    x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DXMLSEC_NO_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_OPENSSL_098=1 -DXMLSEC_NO_GOST=1 -D__XMLSEC_FUNCTION__=__FUNCTION__ -DXMLSEC_CRYPTO_OPENSSL=1 -DXMLSEC_NO_XKMS=1 -DXMLSEC_NO_SIZE_T= -DXMLSEC_CRYPTO=openssl -Isrc -I/home/carljm/.venvs/tmp-3545f39dbf727860/local/lib/python2.7/site-packages/lxml/includes -I/home/carljm/.venvs/tmp-3545f39dbf727860/local/lib/python2.7/site-packages/lxml -I/usr/include/xmlsec1 -I/usr/include/libxml2 -I/usr/include/python2.7 -c src/xmlsec/enc.c -o build/temp.linux-x86_64-2.7/src/xmlsec/enc.o -DXMLSEC_CRYPTO_OPENSSL=1
    src/xmlsec/enc.c: In function ‘__pyx_import_star_set’:
    src/xmlsec/enc.c:3384:37: error: lvalue required as left operand of assignment
         XMLSEC_ENC_RETURN_REPLACED_NODE = __Pyx_PyInt_As_unsigned_int(o); if ((XMLSEC_ENC_RETURN_REPLACED_NODE == (unsigned int)-1) && PyErr_Occurred()) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L2_error;};
                                         ^
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
   
    ----------------------------------------
Command "/home/carljm/.venvs/tmp-3545f39dbf727860/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-HQCz3j/xmlsec/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-xqY4Oa-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/carljm/.venvs/tmp-3545f39dbf727860/include/site/python2.7/xmlsec" failed with error code 1 in /tmp/pip-build-HQCz3j/xmlsec
```

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Bright Dadson | 27 Aug 18:09 2015
Picon

Passing Cython class as argument to C function

HI Guys,

I am trying to pass a cython class object instance as argument to C function so I can make a callback to one of its functions.

Below is what I am trying to achieve:

/* C function */
#include "python.h"

PyObject c_func(PyObject f){
   /* How do I make a callback to Foo objects method func_a from here ? */


  PyGILState_STATE gstate;
  gstate = PyGILState_Ensure();

  PyObject_CallMethod(f, "func_a", "dict", dict_obj);

  PyGILState_Release(gstate);
}


#sim.pyd

cdef
extern from "my_sim.h":
   
object c_func(object f)

cdef class Foo:
    cdef public func_a
(object a)
#-- End of sim.pyd --

# sim.pyx
from sim cimport Foo

cdef
class Foo:
    cdef func_a
(object a):
     
print(a)
     
    cdef func_b
():
       c_func
(self)
#-- End of sim.pyx --


Unfortunately callback from c_func to cython object method a_func doesn't seems to work - anyone knows how to make such callbacks?

Thanks


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
smbat.makiyan | 27 Aug 13:02 2015

Compilation error "unary minus operator applied to unsigned type, result still unsigned"

Hi all, 

After update from cython 0.22.1 to cython version 0.23 or 0.23.1 cython generates code that fails to compile on VS2012.
The error is "error C4146: unary minus operator applied to unsigned type, result still unsigned"

For example, error happens in function __Pyx_PyInt_As_int() in the following lines: 

... return (int) -(((((int)digits[1]) << PyLong_SHIFT) | digits[0]));
... return (int) -(((((((int)digits[2]) << PyLong_SHIFT) | digits[1]) << PyLong_SHIFT) | digits[0]));
... return (int) -(((((((((int)digits[3]) << PyLong_SHIFT) | digits[2]) << PyLong_SHIFT) | digits[1]) << PyLong_SHIFT) | digits[0]));

note, digits is defined as unsigned.

Same thing for my typedeffed unsigned types:

typedef unsigned short int myUInt16;
typedef unsigned long int myUInt32;

Generated functions __Pyx_PyInt_As_long, __Pyx_PyInt_As_myUInt16,__Pyx_PyInt_As_myUInt32 and many others generate similar code that can't compile


Could you please take a look and fix this?

In attach new and old versions of __Pyx_PyInt_As_int 

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
/** new version 0.23.1 */
static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {
    const int neg_one = (int) -1, const_zero = (int) 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        if (sizeof(int) < sizeof(long)) {
            __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG(x))
        } else {
            long val = PyInt_AS_LONG(x);
            if (is_unsigned && unlikely(val < 0)) {
                goto raise_neg_overflow;
            }
            return (int) val;
        }
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_USE_PYLONG_INTERNALS
            const digit* digits = ((PyLongObject*)x)->ob_digit;
            switch (Py_SIZE(x)) {
                case  0: return (int) 0;
                case  1: __PYX_VERIFY_RETURN_INT(int, digit, digits[0])
                case 2:
                    if (8 * sizeof(int) > 1 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) >= 2 * PyLong_SHIFT) {
                            return (int) (((((int)digits[1]) << PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
                case 3:
                    if (8 * sizeof(int) > 2 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) |
digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) >= 3 * PyLong_SHIFT) {
                            return (int) (((((((int)digits[2]) << PyLong_SHIFT) | digits[1]) << PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
                case 4:
                    if (8 * sizeof(int) > 3 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) |
digits[2]) << PyLong_SHIFT) | digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) >= 4 * PyLong_SHIFT) {
                            return (int) (((((((((int)digits[3]) << PyLong_SHIFT) | digits[2]) << PyLong_SHIFT) | digits[1]) <<
PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
            }
#endif
#if CYTHON_COMPILING_IN_CPYTHON
            if (unlikely(Py_SIZE(x) < 0)) {
                goto raise_neg_overflow;
            }
#else
            {
                int result = PyObject_RichCompareBool(x, Py_False, Py_LT);
                if (unlikely(result < 0))
                    return (int) -1;
                if (unlikely(result == 1))
                    goto raise_neg_overflow;
            }
#endif
            if (sizeof(int) <= sizeof(unsigned long)) {
                __PYX_VERIFY_RETURN_INT_EXC(int, unsigned long, PyLong_AsUnsignedLong(x))
            } else if (sizeof(int) <= sizeof(unsigned PY_LONG_LONG)) {
                __PYX_VERIFY_RETURN_INT_EXC(int, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x))
            }
        } else {
#if CYTHON_USE_PYLONG_INTERNALS
            const digit* digits = ((PyLongObject*)x)->ob_digit;
            switch (Py_SIZE(x)) {
                case  0: return (int) 0;
                case -1: __PYX_VERIFY_RETURN_INT(int, sdigit, -(sdigit) digits[0])
                case  1: __PYX_VERIFY_RETURN_INT(int,  digit, +digits[0])
                case -2:
                    if (8 * sizeof(int) - 1 > 1 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) {
                            return (int) -(((((int)digits[1]) << PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
                case 2:
                    if (8 * sizeof(int) > 1 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) {
                            return (int) (((((int)digits[1]) << PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
                case -3:
                    if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) |
digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) {
                            return (int) -(((((((int)digits[2]) << PyLong_SHIFT) | digits[1]) << PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
                case 3:
                    if (8 * sizeof(int) > 2 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) |
digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) {
                            return (int) (((((((int)digits[2]) << PyLong_SHIFT) | digits[1]) << PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
                case -4:
                    if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) |
digits[2]) << PyLong_SHIFT) | digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) {
                            return (int) -(((((((((int)digits[3]) << PyLong_SHIFT) | digits[2]) << PyLong_SHIFT) | digits[1]) <<
PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
                case 4:
                    if (8 * sizeof(int) > 3 * PyLong_SHIFT) {
                        if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) {
                            __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) |
digits[2]) << PyLong_SHIFT) | digits[1]) << PyLong_SHIFT) | digits[0])))
                        } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) {
                            return (int) (((((((((int)digits[3]) << PyLong_SHIFT) | digits[2]) << PyLong_SHIFT) | digits[1]) <<
PyLong_SHIFT) | digits[0]));
                        }
                    }
                    break;
            }
#endif
            if (sizeof(int) <= sizeof(long)) {
                __PYX_VERIFY_RETURN_INT_EXC(int, long, PyLong_AsLong(x))
            } else if (sizeof(int) <= sizeof(PY_LONG_LONG)) {
                __PYX_VERIFY_RETURN_INT_EXC(int, PY_LONG_LONG, PyLong_AsLongLong(x))
            }
        }
        {
#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
            PyErr_SetString(PyExc_RuntimeError,
                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
#else
            int val;
            PyObject *v = __Pyx_PyNumber_Int(x);
 #if PY_MAJOR_VERSION < 3
            if (likely(v) && !PyLong_Check(v)) {
                PyObject *tmp = v;
                v = PyNumber_Long(tmp);
                Py_DECREF(tmp);
            }
 #endif
            if (likely(v)) {
                int one = 1; int is_little = (int)*(unsigned char *)&one;
                unsigned char *bytes = (unsigned char *)&val;
                int ret = _PyLong_AsByteArray((PyLongObject *)v,
                                              bytes, sizeof(val),
                                              is_little, !is_unsigned);
                Py_DECREF(v);
                if (likely(!ret))
                    return val;
            }
#endif
            return (int) -1;
        }
    } else {
        int val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (int) -1;
        val = __Pyx_PyInt_As_int(tmp);
        Py_DECREF(tmp);
        return val;
    }
raise_overflow:
    PyErr_SetString(PyExc_OverflowError,
        "value too large to convert to int");
    return (int) -1;
raise_neg_overflow:
    PyErr_SetString(PyExc_OverflowError,
        "can't convert negative value to int");
    return (int) -1;
}

/** old version 0.22.1  */
static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {
    const int neg_one = (int) -1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        if (sizeof(int) < sizeof(long)) {
            __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG(x))
        } else {
            long val = PyInt_AS_LONG(x);
            if (is_unsigned && unlikely(val < 0)) {
                goto raise_neg_overflow;
            }
            return (int) val;
        }
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
 #if CYTHON_USE_PYLONG_INTERNALS
            switch (Py_SIZE(x)) {
                case  0: return 0;
                case  1: __PYX_VERIFY_RETURN_INT(int, digit, ((PyLongObject*)x)->ob_digit[0]);
            }
 #endif
#endif
#if CYTHON_COMPILING_IN_CPYTHON
            if (unlikely(Py_SIZE(x) < 0)) {
                goto raise_neg_overflow;
            }
#else
            {
                int result = PyObject_RichCompareBool(x, Py_False, Py_LT);
                if (unlikely(result < 0))
                    return (int) -1;
                if (unlikely(result == 1))
                    goto raise_neg_overflow;
            }
#endif
            if (sizeof(int) <= sizeof(unsigned long)) {
                __PYX_VERIFY_RETURN_INT(int, unsigned long, PyLong_AsUnsignedLong(x))
            } else if (sizeof(int) <= sizeof(unsigned PY_LONG_LONG)) {
                __PYX_VERIFY_RETURN_INT(int, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x))
            }
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
 #if CYTHON_USE_PYLONG_INTERNALS
            switch (Py_SIZE(x)) {
                case  0: return 0;
                case  1: __PYX_VERIFY_RETURN_INT(int,  digit, +(((PyLongObject*)x)->ob_digit[0]));
                case -1: __PYX_VERIFY_RETURN_INT(int, sdigit, -(sdigit) ((PyLongObject*)x)->ob_digit[0]);
            }
 #endif
#endif
            if (sizeof(int) <= sizeof(long)) {
                __PYX_VERIFY_RETURN_INT(int, long, PyLong_AsLong(x))
            } else if (sizeof(int) <= sizeof(PY_LONG_LONG)) {
                __PYX_VERIFY_RETURN_INT(int, PY_LONG_LONG, PyLong_AsLongLong(x))
            }
        }
        {
#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
            PyErr_SetString(PyExc_RuntimeError,
                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
#else
            int val;
            PyObject *v = __Pyx_PyNumber_Int(x);
 #if PY_MAJOR_VERSION < 3
            if (likely(v) && !PyLong_Check(v)) {
                PyObject *tmp = v;
                v = PyNumber_Long(tmp);
                Py_DECREF(tmp);
            }
 #endif
            if (likely(v)) {
                int one = 1; int is_little = (int)*(unsigned char *)&one;
                unsigned char *bytes = (unsigned char *)&val;
                int ret = _PyLong_AsByteArray((PyLongObject *)v,
                                              bytes, sizeof(val),
                                              is_little, !is_unsigned);
                Py_DECREF(v);
                if (likely(!ret))
                    return val;
            }
#endif
            return (int) -1;
        }
    } else {
        int val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (int) -1;
        val = __Pyx_PyInt_As_int(tmp);
        Py_DECREF(tmp);
        return val;
    }
raise_overflow:
    PyErr_SetString(PyExc_OverflowError,
        "value too large to convert to int");
    return (int) -1;
raise_neg_overflow:
    PyErr_SetString(PyExc_OverflowError,
        "can't convert negative value to int");
    return (int) -1;
}
smbat.makiyan | 27 Aug 13:13 2015

Warning c4701 in VS2012

Hi all,

Cython generates following function

static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
                                  CYTHON_UNUSED int lineno, CYTHON_UNUSED const char *filename,
                                  int full_traceback, CYTHON_UNUSED int nogil) {
    PyObject *old_exc, *old_val, *old_tb;
    PyObject *ctx;
#ifdef WITH_THREAD
    PyGILState_STATE state;
    if (nogil)
        state = PyGILState_Ensure();
#endif
    __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
    if (full_traceback) {
        Py_XINCREF(old_exc);
        Py_XINCREF(old_val);
        Py_XINCREF(old_tb);
        __Pyx_ErrRestore(old_exc, old_val, old_tb);
        PyErr_PrintEx(1);
    }
    #if PY_MAJOR_VERSION < 3
    ctx = PyString_FromString(name);
    #else
    ctx = PyUnicode_FromString(name);
    #endif
    __Pyx_ErrRestore(old_exc, old_val, old_tb);
    if (!ctx) {
        PyErr_WriteUnraisable(Py_None);
    } else {
        PyErr_WriteUnraisable(ctx);
        Py_DECREF(ctx);
    }
#ifdef WITH_THREAD
    if (nogil)
        PyGILState_Release(state);
#endif
}

Function works perfect, but VS2012 have "warning C4701: potentially uninitialized local variable 'state' used" for  PyGILState_Release(state); line

Could you take a look and fix this warning?

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marco Neumann | 25 Aug 16:53 2015
Picon

PyPy: NotImplementError + Abort when using __sub__

Environment

foo.pyx:
cdef class C:
   
def __cinit__(self):
       
pass

   
def __sub__(self, o):
       
return 0

test.py
import foo
a
= foo.C()
b
= foo.C()
a
- b


cython --version
Cython version 0.23.1


pypy --version
Python 2.7.9 (295ee98b6928, Jun 02 2015, 16:33:44)
[PyPy 2.6.0 with GCC 5.1.0]


Steps to reproduce

  1.  cython foo.pyx
  2.  gcc -shared -IPATH/TO/YOUR/PYTHON/INCLUDE -fPIC foo.c -o foo.pypy-26.so
  3.  pypy test.py


Result

RPython traceback:
 
File "pypy_interpreter_gateway.c", line 1493, in BuiltinCodePassThroughArguments1_funcrun_obj
 
File "pypy_module_cpyext_slotdefs.c", line 2662, in wrap_binaryfunc_l_11
Fatal RPython error: NotImplementedError
fish
: ipython terminated by signal SIGABRT (Abort)



--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
slonik | 24 Aug 03:43 2015
Picon

Bug in 0.23.x: missing bool operator!() in unique_ptr & shared_ptr

Cython master branch recently merged in my pull request
https://github.com/cython/cython/pull/432
adding missing bool operator!() to unique_ptr & shared_ptr (file memory.pxd)
Unfortunately, the patch did not make it into 0.23.x series.
As a result one cannot use 'if not up:' or 'if not sp:' in 0.23.1.

--Leo

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Aman Singh | 24 Aug 01:44 2015
Picon

Getting errors while using nogil

Hi 

Can anyone please explain me why I am getting error in this code:

cimport cython import numpy as np cimport numpy as np ctypedef fused data_t: np.int8_t np.int16_t cdef inline findObjectsPoint(data_t *data): return data[0] + data[1] cpdef my_func(np.ndarray[data_t] arr): with nogil: findObjectsPoint(np.PyArray_DATA(arr)) return 0

The error I am getting is :

In [15]: reload(tpp) Error compiling Cython file: ------------------------------------------------------------ ... cdef inline findObjectsPoint(data_t *data): return data[0] + data[1] cpdef my_func(np.ndarray[data_t] arr): with nogil: findObjectsPoint(np.PyArray_DATA(arr)) ^ ------------------------------------------------------------ tpp.pyx:28:24: Discarding owned Python object not allowed without gil

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Diogo Alves | 23 Aug 14:49 2015
Picon

Cython for Visual Studio 2015 to deploy on Windows 10 IoT Core

Hi

I am currently trying to port an application I have working on raspbian to Windows 10 IoT Core.

My project uses a module called python-openzwave that strongly rely on cython.

I don't have a big knoledge in cython so I decided that the first step to successfully port everything would be make it run on a windows 10 computer


So far I installed VS2015 I loaded the source code I have in there and installed cython using "pip install cython"

Everything goes well, but now I am strugling to build the project

I will post here the configuration of setup-lib.py and the output:

if os_name == 'win32' or os_name=='nt':
    ext_modules = [Extension("libopenzwave",
                             sources=["src-lib/libopenzwave/libopenzwave.cpp"],
                             libraries=['setupapi', 'msvc'],
                             language="c++",
                             extra_objects=['openzwave/libopenzwave.a'],
                             include_dirs=['openzwave/cpp/src', 'openzwave/cpp/src/value_classes', 'openzwave/cpp/src/platform', 'openzwave/cpp/build/windows', "src-lib/libopenzwave" , "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include"]
    )]




And here is the output of the build:


running build
running build_ext
cythoning src-lib/libopenzwave/libopenzwave.pyx to src-lib/libopenzwave\libopenzwave.cpp
building 'libopenzwave' extension
creating build
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\src-lib
creating build\temp.win-amd64-2.7\Release\src-lib\libopenzwave
C:\Users\Diogo Alves\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Iopenzwave/cpp/src -Iopenzwave/cpp/src/value_classes -Iopenzwave/cpp/src/platform -Iopenzwave/cpp/build/windows -Isrc-lib/libopenzwave "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include" -IC:\Python27\include -IC:\Python27\PC /Tpsrc-lib/libopenzwave\libopenzwave.cpp /Fobuild\temp.win-amd64-2.7\Release\src-lib/libopenzwave\libopenzwave.objlibopenzwave.cpp
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(127) : error C3168: 'bool' : illegal underlying type for enum
        C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(128) : see reference to class template instantiation '__vcrt_va_list_is_reference<_Ty>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(133) : error C3168: 'bool' : illegal underlying type for enum
        C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(134) : see reference to class template instantiation '__vcrt_va_list_is_reference<_Ty&>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(137) : error C2143: syntax error : missing ',' before '&&'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(138) : error C2143: syntax error : missing ';' before '{'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(139) : error C2143: syntax error : missing ';' before '{'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(140) : error C2143: syntax error : missing ';' before '}'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(144) : error C2143: syntax error : missing ';' before '{'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(146) : error C2143: syntax error : missing ';' before '}'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vadefs.h(147) : error C2143: syntax error : missing ';' before '}'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(81) : error C2143: syntax error : missing ';' before '{'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(238): error C2143: syntax error : missing ';' before '{'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(243): error C2143: syntax error : missing ';' before '}'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(307): error C2143: syntax error : missing ';' before '}'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\crtdefs.h(10) : fatal error C1083: Cannot open include file: 'corecrt.h': No such file or directory


Anyone knows if what I am trying to do is possible and if possible how to solve this issue?

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Simon King | 23 Aug 10:19 2015
Picon
Picon

cmp of integers with nogil

Hi!

I try to speed-up some code using "with nogil", and found that  
comparison of integers by cmp(i,j) requires gil. Is there a function  
(e.g. from cpython), say,
cmp_int, that compares two <int> and does not require gil?

Best regards,
Simon

--

-- 

--- 
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Gmane