Skip to content

Instantly share code, notes, and snippets.

@llllllllll
Last active February 1, 2018 19:55
Show Gist options
  • Save llllllllll/d03146174771b354d3f6c0b186dd4ad2 to your computer and use it in GitHub Desktop.
Save llllllllll/d03146174771b354d3f6c0b186dd4ad2 to your computer and use it in GitHub Desktop.
I sure love allocating PyObjects!
"""Tested with:
- cython==0.27.3
- cython==0.25.2
"""
cdef class PairWithConstructor:
cdef int a;
cdef int b;
def __cinit__(PairWithConstructor self, int a, int b):
self.a = a
self.b = b
cdef class PairWithoutConstructor:
cdef int a;
cdef int b;
cdef pair_without_constructor(int a, int b):
cdef PairWithoutConstructor ret = PairWithoutConstructor()
ret.a = a
ret.b = b
return ret
cpdef example():
cdef int a = 1
cdef int b = 2
cdef PairWithConstructor boxed = PairWithConstructor(a, b)
cdef PairWithoutConstructor not_boxed = pair_without_constructor(a, b)
/* ... */
/* "a.pyx":26
* cdef int b = 2
*
* cdef PairWithConstructor boxed = PairWithConstructor(a, b) # <<<<<<<<<<<<<<
* cdef PairWithoutConstructor not_boxed = pair_without_constructor(a, b)
*/
__pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_a); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 26, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_b); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 26, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) __PYX_ERR(1, 26, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_3);
__Pyx_GIVEREF(__pyx_t_1);
PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
__Pyx_GIVEREF(__pyx_t_2);
PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_2);
__pyx_t_1 = 0;
__pyx_t_2 = 0;
__pyx_t_2 = __Pyx_PyObject_Call(((PyObject *)__pyx_ptype_1a_PairWithConstructor), __pyx_t_3, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 26, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
__pyx_v_boxed = ((struct __pyx_obj_1a_PairWithConstructor *)__pyx_t_2);
__pyx_t_2 = 0;
/* "a.pyx":27
*
* cdef PairWithConstructor boxed = PairWithConstructor(a, b)
* cdef PairWithoutConstructor not_boxed = pair_without_constructor(a, b) # <<<<<<<<<<<<<<
*/
__pyx_t_2 = __pyx_f_1a_pair_without_constructor(__pyx_v_a, __pyx_v_b); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 27, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_1a_PairWithoutConstructor))))) __PYX_ERR(1, 27, __pyx_L1_error)
__pyx_v_not_boxed = ((struct __pyx_obj_1a_PairWithoutConstructor *)__pyx_t_2);
__pyx_t_2 = 0;
/* ... */
/* "a.pyx":14
*
*
* cdef pair_without_constructor(int a, int b): # <<<<<<<<<<<<<<
* cdef PairWithoutConstructor ret = PairWithoutConstructor()
* ret.a = a
*/
static PyObject *__pyx_f_1a_pair_without_constructor(int __pyx_v_a, int __pyx_v_b) {
struct __pyx_obj_1a_PairWithoutConstructor *__pyx_v_ret = 0;
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
PyObject *__pyx_t_1 = NULL;
__Pyx_RefNannySetupContext("pair_without_constructor", 0);
/* "a.pyx":15
*
* cdef pair_without_constructor(int a, int b):
* cdef PairWithoutConstructor ret = PairWithoutConstructor() # <<<<<<<<<<<<<<
* ret.a = a
* ret.b = b
*/
__pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)__pyx_ptype_1a_PairWithoutConstructor), __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 15, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_v_ret = ((struct __pyx_obj_1a_PairWithoutConstructor *)__pyx_t_1);
__pyx_t_1 = 0;
/* "a.pyx":16
* cdef pair_without_constructor(int a, int b):
* cdef PairWithoutConstructor ret = PairWithoutConstructor()
* ret.a = a # <<<<<<<<<<<<<<
* ret.b = b
* return ret
*/
__pyx_v_ret->a = __pyx_v_a;
/* "a.pyx":17
* cdef PairWithoutConstructor ret = PairWithoutConstructor()
* ret.a = a
* ret.b = b # <<<<<<<<<<<<<<
* return ret
*
*/
__pyx_v_ret->b = __pyx_v_b;
/* "a.pyx":18
* ret.a = a
* ret.b = b
* return ret # <<<<<<<<<<<<<<
*
*
*/
__Pyx_XDECREF(__pyx_r);
__Pyx_INCREF(((PyObject *)__pyx_v_ret));
__pyx_r = ((PyObject *)__pyx_v_ret);
goto __pyx_L0;
/* "a.pyx":14
*
*
* cdef pair_without_constructor(int a, int b): # <<<<<<<<<<<<<<
* cdef PairWithoutConstructor ret = PairWithoutConstructor()
* ret.a = a
*/
/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
__Pyx_AddTraceback("a.pair_without_constructor", __pyx_clineno, __pyx_lineno, __pyx_filename);
__pyx_r = 0;
__pyx_L0:;
__Pyx_XDECREF((PyObject *)__pyx_v_ret);
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}
@1st1
Copy link

1st1 commented Feb 1, 2018

cdef pair_without_constructor(int a, int b):
    cdef PairWithoutConstructor ret = PairWithoutConstructor.__new__(PairWithoutConstructor)
    ret.a = a
    ret.b = b
    return ret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment