%load_ext cythonmagic
%%cython --annotate
from libc.stdlib cimport strtol as _strtol
def strtol(bytes n, int base=10):
cdef int ret
cdef char *nptr = n
cdef char *endptr
ret = _strtol(nptr, &endptr, base)
if endptr == nptr or endptr[0] != 0:
raise ValueError("Invalid string %r at at character %d" % (n, endptr-nptr+1))
return ret
Generated by Cython 0.16 on Mon Aug 13 17:07:01 2012
1: from libc.stdlib cimport strtol as _strtol
2:
3: def strtol(bytes n, int base=10):
/* "_cython_magic_61312305a41c94b0224908425e795b16.pyx":3 * from libc.stdlib cimport strtol as _strtol * * def strtol(bytes n, int base=10): #<
<
<
<
<
<
<
<
<
<
<
<
<
<
* cdef int ret * cdef char *nptr = n */ static PyObject *__pyx_pf_46_cython_magic_61312305a41c94b0224908425e795b16_strtol(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_n, int __pyx_v_base) { int __pyx_v_ret; char *__pyx_v_nptr; char *__pyx_v_endptr; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("strtol", 0); /* "_cython_magic_61312305a41c94b0224908425e795b16.pyx":3 * from libc.stdlib cimport strtol as _strtol * * def strtol(bytes n, int base=10): #<
<
<
<
<
<
<
<
<
<
<
<
<
<
* cdef int ret * cdef char *nptr = n */ __pyx_k_tuple_2 = PyTuple_New(5); if (unlikely(!__pyx_k_tuple_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_k_tuple_2); __Pyx_INCREF(((PyObject *)__pyx_n_s__n)); PyTuple_SET_ITEM(__pyx_k_tuple_2, 0, ((PyObject *)__pyx_n_s__n)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__n)); __Pyx_INCREF(((PyObject *)__pyx_n_s__base)); PyTuple_SET_ITEM(__pyx_k_tuple_2, 1, ((PyObject *)__pyx_n_s__base)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__base)); __Pyx_INCREF(((PyObject *)__pyx_n_s__ret)); PyTuple_SET_ITEM(__pyx_k_tuple_2, 2, ((PyObject *)__pyx_n_s__ret)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__ret)); __Pyx_INCREF(((PyObject *)__pyx_n_s__nptr)); PyTuple_SET_ITEM(__pyx_k_tuple_2, 3, ((PyObject *)__pyx_n_s__nptr)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__nptr)); __Pyx_INCREF(((PyObject *)__pyx_n_s__endptr)); PyTuple_SET_ITEM(__pyx_k_tuple_2, 4, ((PyObject *)__pyx_n_s__endptr)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__endptr)); __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_2)); /* "_cython_magic_61312305a41c94b0224908425e795b16.pyx":3 * from libc.stdlib cimport strtol as _strtol * * def strtol(bytes n, int base=10): #<
<
<
<
<
<
<
<
<
<
<
<
<
<
* cdef int ret * cdef char *nptr = n */ __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_61312305a41c94b0224908425e795b16_1strtol, NULL, __pyx_n_s_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); if (PyObject_SetAttr(__pyx_m, __pyx_n_s__strtol, __pyx_t_1)<
0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
4: cdef int ret
5: cdef char *nptr = n
/* "_cython_magic_61312305a41c94b0224908425e795b16.pyx":5 * def strtol(bytes n, int base=10): * cdef int ret * cdef char *nptr = n #<
<
<
<
<
<
<
<
<
<
<
<
<
<
* cdef char *endptr * ret = _strtol(nptr, &endptr, base) */ __pyx_t_1 = PyBytes_AsString(((PyObject *)__pyx_v_n)); if (unlikely((!__pyx_t_1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_v_nptr = __pyx_t_1;
6: cdef char *endptr
7: ret = _strtol(nptr, &endptr, base)
/* "_cython_magic_61312305a41c94b0224908425e795b16.pyx":7 * cdef char *nptr = n * cdef char *endptr * ret = _strtol(nptr, &endptr, base) #<
<
<
<
<
<
<
<
<
<
<
<
<
<
* if endptr == nptr or endptr[0] != 0: * raise ValueError("Invalid string %r at at character %d" % (n, endptr-nptr+1)) */ __pyx_v_ret = strtol(__pyx_v_nptr, (&__pyx_v_endptr), __pyx_v_base);
8: if endptr == nptr or endptr[0] != 0:
/* "_cython_magic_61312305a41c94b0224908425e795b16.pyx":8 * cdef char *endptr * ret = _strtol(nptr, &endptr, base) * if endptr == nptr or endptr[0] != 0: #<
<
<
<
<
<
<
<
<
<
<
<
<
<
* raise ValueError("Invalid string %r at at character %d" % (n, endptr-nptr+1)) * return ret */ __pyx_t_2 = (__pyx_v_endptr == __pyx_v_nptr); if (!__pyx_t_2) { __pyx_t_3 = ((__pyx_v_endptr[0]) != 0); __pyx_t_4 = __pyx_t_3; } else { __pyx_t_4 = __pyx_t_2; } if (__pyx_t_4) {
9: raise ValueError("Invalid string %r at at character %d" % (n, endptr-nptr+1))
/* "_cython_magic_61312305a41c94b0224908425e795b16.pyx":9 * ret = _strtol(nptr, &endptr, base) * if endptr == nptr or endptr[0] != 0: * raise ValueError("Invalid string %r at at character %d" % (n, endptr-nptr+1)) #<
<
<
<
<
<
<
<
<
<
<
<
<
<
* return ret */ __pyx_t_5 = PyInt_FromLong(((__pyx_v_endptr - __pyx_v_nptr) + 1)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_5); __pyx_t_6 = PyTuple_New(2); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_6); __Pyx_INCREF(((PyObject *)__pyx_v_n)); PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_v_n)); __Pyx_GIVEREF(((PyObject *)__pyx_v_n)); PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_t_5); __Pyx_GIVEREF(__pyx_t_5); __pyx_t_5 = 0; __pyx_t_5 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_1), ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_5)); __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_6); PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_t_5)); __Pyx_GIVEREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0; __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_6), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_5); __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0; __Pyx_Raise(__pyx_t_5, 0, 0, 0); __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} goto __pyx_L3; } __pyx_L3:;
10: return ret
/* "_cython_magic_61312305a41c94b0224908425e795b16.pyx":10 * if endptr == nptr or endptr[0] != 0: * raise ValueError("Invalid string %r at at character %d" % (n, endptr-nptr+1)) * return ret #<
<
<
<
<
<
<
<
<
<
<
<
<
<
*/ __Pyx_XDECREF(__pyx_r); __pyx_t_5 = PyInt_FromLong(__pyx_v_ret); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_5); __pyx_r = __pyx_t_5; __pyx_t_5 = 0; goto __pyx_L0; __pyx_r = Py_None; __Pyx_INCREF(Py_None); goto __pyx_L0; __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_5); __Pyx_XDECREF(__pyx_t_6); __Pyx_AddTraceback("_cython_magic_61312305a41c94b0224908425e795b16.strtol", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; }
strtol('hello', base=36)
29234652
%timeit strtol('hello', base=36)
1000000 loops, best of 3: 285 ns per loop