From ondrej at certik.cz Wed Mar 3 21:22:04 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 3 Mar 2010 12:22:04 -0800 Subject: [Cython] using the C API Declarations in several C++ files Message-ID: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> Hi, I am using the C API Declarations[0], which generate the file _hermes_common_api.h, that I include in my C++ files. I am having problems with initializing the module properly. Here is an example of my C++ function, that internally calls scipy's sparse solver to solve the system: void solve_linear_system_scipy_cg(CooMatrix *mat, double *res) { if (import__hermes_common()) throw std::runtime_error("hermes_common failed to import."); CSRMatrix M(mat); insert_object("m", c2py_CSRMatrix(&M)); insert_object("rhs", c2numpy_double_inplace(res, mat->get_size())); cmd("A = m.to_scipy_csr()"); cmd("from scipy.sparse.linalg import cg"); cmd("x, res = cg(A, rhs)"); double *x; int n; numpy2c_double_inplace(get_object("x"), &x, &n); memcpy(res, x, n*sizeof(double)); } the functions insert_object(), cmd(), c2numpy_double_inplace() etc. are declared in my .pyx file to ease C++ and Python integration (the whole code is at [1]). Everything works, as long as I call the "import__hermes_common()" function. Since I call Python things all over my C++ code, it's kind of annoying to always call this by hand. Also, doesn't it slow things down? What is the best way to do this? It occurred to me that I could create a "Python" C++ class, whose constructor would import the module if needed, and it would be used like this: Python p; p.insert_object(...) p.cmd(...) p.get_object(...) Another annoying thing is that I currently need to have the following at the beginning of my C++ main() function: // This is a hack, this should be set somewhere else: putenv((char *)"PYTHONPATH=../.."); Py_Initialize(); PySys_SetArgv(argc, argv); I would like this to be handled automatically somehow, probably in the constructor of the C++ class "Python". The problem that I am having is that it seems to me, that I need to call import__hermes_common() in every single .cpp file that I have, even though that I link all of them into a shared library. It segfaults if I don't do it. This confuses me. Here is a part of the code in _hermes_common_api.h: static PyObject *(*c2py_CooMatrix)(struct CooMatrix *); static PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *); static void (*cmd)(const char*); static void (*set_verbose_cmd)(int); static void (*insert_object)(const char*, PyObject *); those have to be initalized by calling import__hermes_common(), that is clear, but why isn't it enough to call it once per the project/shared lib? I call it in matrix.cpp, but if I don't call it in python_solvers.cpp as well, it seems it won't get initialized and it segfaults. Maybe it has something to do with how C++ header files work, but I thought that symbols are shared in the whole program/library. Thanks for any hints. Ondrej [0] http://docs.cython.org/docs/external_C_code.html#c-api-declarations [1] http://github.com/certik/hermes_common From dalcinl at gmail.com Wed Mar 3 22:55:48 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 3 Mar 2010 18:55:48 -0300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> Message-ID: On 3 March 2010 17:22, Ondrej Certik wrote: > Hi, > > I am using the C API Declarations[0], which generate the file > _hermes_common_api.h, that I include in my C++ files. I am having > problems with initializing the module properly. > > Here is an example of my C++ function, that internally calls scipy's > sparse solver to solve the system: > > void solve_linear_system_scipy_cg(CooMatrix *mat, double *res) > { > ? ?if (import__hermes_common()) > ? ? ? ?throw std::runtime_error("hermes_common failed to import."); > ? ?CSRMatrix M(mat); > ? ?insert_object("m", c2py_CSRMatrix(&M)); > ? ?insert_object("rhs", c2numpy_double_inplace(res, mat->get_size())); > ? ?cmd("A = m.to_scipy_csr()"); > ? ?cmd("from scipy.sparse.linalg import cg"); > ? ?cmd("x, res = cg(A, rhs)"); > ? ?double *x; > ? ?int n; > ? ?numpy2c_double_inplace(get_object("x"), &x, &n); > ? ?memcpy(res, x, n*sizeof(double)); > } > > the functions insert_object(), cmd(), c2numpy_double_inplace() etc. > are declared in my .pyx file to ease C++ and Python integration (the > whole code is at [1]). Everything works, as long as I call the > "import__hermes_common()" function. Since I call Python things all > over my C++ code, it's kind of annoying to always call this by hand. > Also, doesn't it slow things down? > > What is the best way to do this? > > It occurred to me that I could create a "Python" C++ class, whose > constructor would import the module if needed, and it would be used > like this: > > Python p; > p.insert_object(...) > p.cmd(...) > p.get_object(...) > That's the way I would do it... Moreover, all C++ -- Python interactions should go via this object, so you should wrap the C API calls of your module as methos of the "Python" class. > Another annoying thing is that I currently need to have the following > at the beginning of my C++ main() function: > > ? ? ? ?// This is a hack, this should be set somewhere else: > ? ? ? ?putenv((char *)"PYTHONPATH=../.."); > ? ? ? ?Py_Initialize(); > ? ? ? ?PySys_SetArgv(argc, argv); > > I would like this to be handled automatically somehow, probably in the > constructor of the C++ class "Python". > Yep.. > The problem that I am having is that it seems to me, that I need to > call import__hermes_common() in every single .cpp file that I have, > even though that I link all of them into a shared library. It > segfaults if I don't do it. This confuses me. Here is a part of the > code in _hermes_common_api.h: > > static PyObject *(*c2py_CooMatrix)(struct CooMatrix *); > static PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *); > static void (*cmd)(const char*); > static void (*set_verbose_cmd)(int); > static void (*insert_object)(const char*, PyObject *); > > those have to be initalized by calling import__hermes_common(), that > is clear, but why isn't it enough to call it once per the > project/shared lib? Because these are "static" ... and these are not visible outside compilation units (i.e. outside each C source) > I call it in matrix.cpp, but if I don't call it in > python_solvers.cpp as well, it seems it won't get initialized and it > segfaults. Of course. Each source has it's own copy of these pointers, then you have to init them at each source... Again, if you route stuff using an globally accessible instance of a "Python" class, this is no longer an issue. > Maybe it has something to do with how C++ header files > work, but I thought that symbols are shared in the whole > program/library. > Yes. Symbols are shared, as long as then are not "static" :-) ... Cython could grow support for your use case... If you really feel you will need this (as opposed to implementing the "Python" class), perhaps I could take a look at it and implement something... -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From ondrej at certik.cz Thu Mar 4 00:28:52 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 3 Mar 2010 15:28:52 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> Message-ID: <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> On Wed, Mar 3, 2010 at 1:55 PM, Lisandro Dalcin wrote: > On 3 March 2010 17:22, Ondrej Certik wrote: >> Hi, >> >> I am using the C API Declarations[0], which generate the file >> _hermes_common_api.h, that I include in my C++ files. I am having >> problems with initializing the module properly. >> >> Here is an example of my C++ function, that internally calls scipy's >> sparse solver to solve the system: >> >> void solve_linear_system_scipy_cg(CooMatrix *mat, double *res) >> { >> ? ?if (import__hermes_common()) >> ? ? ? ?throw std::runtime_error("hermes_common failed to import."); >> ? ?CSRMatrix M(mat); >> ? ?insert_object("m", c2py_CSRMatrix(&M)); >> ? ?insert_object("rhs", c2numpy_double_inplace(res, mat->get_size())); >> ? ?cmd("A = m.to_scipy_csr()"); >> ? ?cmd("from scipy.sparse.linalg import cg"); >> ? ?cmd("x, res = cg(A, rhs)"); >> ? ?double *x; >> ? ?int n; >> ? ?numpy2c_double_inplace(get_object("x"), &x, &n); >> ? ?memcpy(res, x, n*sizeof(double)); >> } >> >> the functions insert_object(), cmd(), c2numpy_double_inplace() etc. >> are declared in my .pyx file to ease C++ and Python integration (the >> whole code is at [1]). Everything works, as long as I call the >> "import__hermes_common()" function. Since I call Python things all >> over my C++ code, it's kind of annoying to always call this by hand. >> Also, doesn't it slow things down? >> >> What is the best way to do this? >> >> It occurred to me that I could create a "Python" C++ class, whose >> constructor would import the module if needed, and it would be used >> like this: >> >> Python p; >> p.insert_object(...) >> p.cmd(...) >> p.get_object(...) >> > > That's the way I would do it... Moreover, all C++ -- Python > interactions should go via this object, so you should wrap the C API > calls of your module as methos of the "Python" class. > >> Another annoying thing is that I currently need to have the following >> at the beginning of my C++ main() function: >> >> ? ? ? ?// This is a hack, this should be set somewhere else: >> ? ? ? ?putenv((char *)"PYTHONPATH=../.."); >> ? ? ? ?Py_Initialize(); >> ? ? ? ?PySys_SetArgv(argc, argv); >> >> I would like this to be handled automatically somehow, probably in the >> constructor of the C++ class "Python". >> > > Yep.. > >> The problem that I am having is that it seems to me, that I need to >> call import__hermes_common() in every single .cpp file that I have, >> even though that I link all of them into a shared library. It >> segfaults if I don't do it. This confuses me. Here is a part of the >> code in _hermes_common_api.h: >> >> static PyObject *(*c2py_CooMatrix)(struct CooMatrix *); >> static PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *); >> static void (*cmd)(const char*); >> static void (*set_verbose_cmd)(int); >> static void (*insert_object)(const char*, PyObject *); >> >> those have to be initalized by calling import__hermes_common(), that >> is clear, but why isn't it enough to call it once per the >> project/shared lib? > > Because these are "static" ... and these are not visible outside > compilation units (i.e. outside each C source) Ah --- that explains everything. I am still learning C++. :) > >> I call it in matrix.cpp, but if I don't call it in >> python_solvers.cpp as well, it seems it won't get initialized and it >> segfaults. > > Of course. Each source has it's own copy of these pointers, then you > have to init them at each source... > > Again, if you route stuff using an globally accessible instance of a > "Python" class, this is no longer an issue. In this case, that'd be cool. It'd be a bit tedious to repeat all the functions in the Python class, but maybe I'll figure out some shortcut. > >> Maybe it has something to do with how C++ header files >> work, but I thought that symbols are shared in the whole >> program/library. >> > > Yes. Symbols are shared, as long as then are not "static" :-) ... Got it --- everything is clear now. > > Cython could grow support for your use case... If you really feel you > will need this (as opposed to implementing the "Python" class), > perhaps I could take a look at it and implement something... Do you have some ideas how Cython could be improved to make my use case easier? I can try to write something too --- I am still in the stage of figuring things out, not sure yet what I really want in details. I only know my general goal --- it should be easy for C++ users to use it, should be robust (no segfaults) and Python internals should not be visible at all. I am half way there already. Thanks for the help, Ondrej From dalcinl at gmail.com Thu Mar 4 00:54:32 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 3 Mar 2010 20:54:32 -0300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> Message-ID: On 3 March 2010 20:28, Ondrej Certik wrote: >> >> Cython could grow support for your use case... If you really feel you >> will need this (as opposed to implementing the "Python" class), >> perhaps I could take a look at it and implement something... > > Do you have some ideas how Cython could be improved to make my use case easier? > The easier way to get something working would be to use a preprocessor macro to control the storage (i.e. static vs. extern) of the symbols you get in the generated C header... Then you just have to #include the module API header like this: #define CYTHON_USE_GLOBAL_API #include You will still need to call import_mymodule(), but you just need to do it earlier in your code, likely a few lines below Py_Initialize() Does this look good/easy enough for you? > I can try to write something too --- I am still in the stage of > figuring things out, not sure yet what I really want in details. I > only know my general goal --- it should be easy for C++ users to use > it, should be robust (no segfaults) and Python internals should not be > visible at all. I am half way there already. > I have to insist: The best (though admittedly complex) way to do this is you have a 'Python' class, in such a way its instances serve as a execution context... If you start with this, using separate interpreters (like Apache's mod_python) in the near future could be straightforward. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From ondrej at certik.cz Thu Mar 4 02:01:01 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 3 Mar 2010 17:01:01 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> Message-ID: <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> On Wed, Mar 3, 2010 at 3:54 PM, Lisandro Dalcin wrote: > On 3 March 2010 20:28, Ondrej Certik wrote: >>> >>> Cython could grow support for your use case... If you really feel you >>> will need this (as opposed to implementing the "Python" class), >>> perhaps I could take a look at it and implement something... >> >> Do you have some ideas how Cython could be improved to make my use case easier? >> > > The easier way to get something working would be to use a preprocessor > macro to control the storage (i.e. static vs. extern) of the symbols > you get in the generated C header... Then you just have to #include > the module API header like this: > > #define CYTHON_USE_GLOBAL_API > #include > > You will still need to call import_mymodule(), but you just need to do > it earlier in your code, likely a few lines below Py_Initialize() > > Does this look good/easy enough for you? That looks cool, I'll give it a shot. > >> I can try to write something too --- I am still in the stage of >> figuring things out, not sure yet what I really want in details. I >> only know my general goal --- it should be easy for C++ users to use >> it, should be robust (no segfaults) and Python internals should not be >> visible at all. I am half way there already. >> > > I have to insist: The best (though admittedly complex) way to do this > is you have a 'Python' class, in such a way its instances serve as a > execution context... If you start with this, using separate > interpreters (like Apache's mod_python) in the near future could be > straightforward. Yes, I currently store the context in the "namespace" global dictionary in Cython. I'll give it a shot too. I don't mind using one interpreter, but at least each Python class instance should have it's own namespace. One more question --- can there be any problem if I call for example from Python: m.add() and add() is implemented in C++, and internally it calls some stuff in Python (like scipy). In principle it could be recursively nested couple times. Can the Python interpreter be confused by this "nesting"? Normally it's not an issue since one usually just wraps a C/C++ code, which doesn't call Python. Ondrej From dalcinl at gmail.com Thu Mar 4 02:50:53 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 3 Mar 2010 22:50:53 -0300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> Message-ID: On 3 March 2010 22:01, Ondrej Certik wrote: > On Wed, Mar 3, 2010 at 3:54 PM, Lisandro Dalcin wrote: >> On 3 March 2010 20:28, Ondrej Certik wrote: >>>> >>>> Cython could grow support for your use case... If you really feel you >>>> will need this (as opposed to implementing the "Python" class), >>>> perhaps I could take a look at it and implement something... >>> >>> Do you have some ideas how Cython could be improved to make my use case easier? >>> >> >> The easier way to get something working would be to use a preprocessor >> macro to control the storage (i.e. static vs. extern) of the symbols >> you get in the generated C header... Then you just have to #include >> the module API header like this: >> >> #define CYTHON_USE_GLOBAL_API >> #include >> >> You will still need to call import_mymodule(), but you just need to do >> it earlier in your code, likely a few lines below Py_Initialize() >> >> Does this look good/easy enough for you? > > That looks cool, I'll give it a shot. > OK, but you will need an additional step... You have to create an additional C source, #include the generated API header, and make it 'define' all these pointers... So perhaps you will need an additional macro (let say, named CYTHON_DEFINE_GLOBAL_API) to tell the header file "define these symbols at this point" Am I being clear enough? >> >>> I can try to write something too --- I am still in the stage of >>> figuring things out, not sure yet what I really want in details. I >>> only know my general goal --- it should be easy for C++ users to use >>> it, should be robust (no segfaults) and Python internals should not be >>> visible at all. I am half way there already. >>> >> >> I have to insist: The best (though admittedly complex) way to do this >> is you have a 'Python' class, in such a way its instances serve as a >> execution context... If you start with this, using separate >> interpreters (like Apache's mod_python) in the near future could be >> straightforward. > > Yes, I currently store the context in the "namespace" global > dictionary in Cython. I'll give it a shot too. I don't mind using one > interpreter, but at least each Python class instance should have it's > own namespace. > That should work (don't remember the C-API equivalent of "exec in namespace", but Cython sources have the trick in order to implement the 'exec' statement) > One more question --- can there be any problem if I call for example > from Python: > > m.add() > > and add() is implemented in C++, and internally it calls some stuff in > Python (like scipy). In principle it could be recursively nested > couple times. Normal recursion limit applies here... A couple of times should not be a problem, a couple thousand times, better don't do that... > Can the Python interpreter be confused by this > "nesting"? Normally it's not an issue since one usually just wraps a > C/C++ code, which doesn't call Python. > Cython itself can call Python, that calls Cython that call Python, that calls ... -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From ondrej at certik.cz Thu Mar 4 03:08:24 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 3 Mar 2010 18:08:24 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> Message-ID: <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> On Wed, Mar 3, 2010 at 5:50 PM, Lisandro Dalcin wrote: > On 3 March 2010 22:01, Ondrej Certik wrote: >> On Wed, Mar 3, 2010 at 3:54 PM, Lisandro Dalcin wrote: >>> On 3 March 2010 20:28, Ondrej Certik wrote: >>>>> >>>>> Cython could grow support for your use case... If you really feel you >>>>> will need this (as opposed to implementing the "Python" class), >>>>> perhaps I could take a look at it and implement something... >>>> >>>> Do you have some ideas how Cython could be improved to make my use case easier? >>>> >>> >>> The easier way to get something working would be to use a preprocessor >>> macro to control the storage (i.e. static vs. extern) of the symbols >>> you get in the generated C header... Then you just have to #include >>> the module API header like this: >>> >>> #define CYTHON_USE_GLOBAL_API >>> #include >>> >>> You will still need to call import_mymodule(), but you just need to do >>> it earlier in your code, likely a few lines below Py_Initialize() >>> >>> Does this look good/easy enough for you? >> >> That looks cool, I'll give it a shot. >> > > OK, but you will need an additional step... You have to create an > additional C source, #include the generated API header, and make it > 'define' all these pointers... So perhaps you will need an additional > macro (let say, named CYTHON_DEFINE_GLOBAL_API) to tell the header > file "define these symbols at this point" > > Am I being clear enough? Yes, that is clear to me, thanks. > >>> >>>> I can try to write something too --- I am still in the stage of >>>> figuring things out, not sure yet what I really want in details. I >>>> only know my general goal --- it should be easy for C++ users to use >>>> it, should be robust (no segfaults) and Python internals should not be >>>> visible at all. I am half way there already. >>>> >>> >>> I have to insist: The best (though admittedly complex) way to do this >>> is you have a 'Python' class, in such a way its instances serve as a >>> execution context... If you start with this, using separate >>> interpreters (like Apache's mod_python) in the near future could be >>> straightforward. >> >> Yes, I currently store the context in the "namespace" global >> dictionary in Cython. I'll give it a shot too. I don't mind using one >> interpreter, but at least each Python class instance should have it's >> own namespace. >> > > That should work (don't remember the C-API equivalent of "exec in > namespace", but Cython sources have the trick in order to implement > the 'exec' statement) That is already implemented, here is my exec function: cdef api object run_cmd(const_char_p text, object namespace): try: verbose = namespace.get("verbose") if verbose: print "got a text:", text if verbose: print "evaluting in the namespace:" print namespace code = compile(text, "", "exec") eval(code, {}, namespace) if verbose: print "new namespace:" print namespace return namespace except SystemExit, e: try: exit_code = int(e) except: exit_code = -1 exit(exit_code) except: etype, value, tb = sys.exc_info() s = "".join(traceback.format_exception(etype, value, tb)) s = "Exception raised in the Python code:\n" + s throw_exception(s) > >> One more question --- can there be any problem if I call for example >> from Python: >> >> m.add() >> >> and add() is implemented in C++, and internally it calls some stuff in >> Python (like scipy). In principle it could be recursively nested >> couple times. > > Normal recursion limit applies here... A couple of times should not be > a problem, a couple thousand times, better don't do that... > >> Can the Python interpreter be confused by this >> "nesting"? Normally it's not an issue since one usually just wraps a >> C/C++ code, which doesn't call Python. >> > > Cython itself can call Python, that calls Cython that call Python, > that calls ... Ok, great! i was worried if there were some issues, but it seems everything will work very smoothly and the C++ guys in my group will not even notice that they are using Python. :) Ondrej From dalcinl at gmail.com Thu Mar 4 03:23:19 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 3 Mar 2010 23:23:19 -0300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> Message-ID: On 3 March 2010 23:08, Ondrej Certik wrote: >> >> Am I being clear enough? > > Yes, that is clear to me, thanks. > OK. Once you have something working, let's polish it and ask other for opinions, then push to cython-devel. > > That is already implemented, here is my exec function: > > cdef api object run_cmd(const_char_p text, object namespace): > ? ?try: > ? ? ? ?verbose = namespace.get("verbose") > ? ? ? ?if verbose: > ? ? ? ? ? ?print "got a text:", text > ? ? ? ?if verbose: > ? ? ? ? ? ?print "evaluting in the namespace:" > ? ? ? ? ? ?print namespace > ? ? ? ?code = compile(text, "", "exec") > ? ? ? ?eval(code, {}, namespace) > ? ? ? ?if verbose: > ? ? ? ? ? ?print "new namespace:" > ? ? ? ? ? ?print namespace > ? ? ? ?return namespace > ? ?except SystemExit, e: > ? ? ? ?try: > ? ? ? ? ? ?exit_code = int(e) > ? ? ? ?except: > ? ? ? ? ? ?exit_code = -1 > ? ? ? ?exit(exit_code) > ? ?except: > ? ? ? ?etype, value, tb = sys.exc_info() > ? ? ? ?s = "".join(traceback.format_exception(etype, value, tb)) > ? ? ? ?s = "Exception raised in the Python code:\n" + s > ? ? ? ?throw_exception(s) > Mmm, are you sure you want to handle SystemExit by actually calling exit(e)? Is this 'exit' actually 'sys.exit' ? In such case, why just not use 'raise' to re-raise the exception? > > Ok, great! i was worried if there were some issues, but it seems > everything will work very smoothly and the C++ guys in my group will > not even notice that they are using Python. :) > But they should notice, just to have a chance to fall in love with the beast :-) -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From greg.ewing at canterbury.ac.nz Thu Mar 4 04:06:23 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Thu, 04 Mar 2010 16:06:23 +1300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> Message-ID: <4B8F23AF.7070701@canterbury.ac.nz> Ondrej Certik wrote: > and add() is implemented in C++, and internally it calls some stuff in > Python (like scipy). In principle it could be recursively nested > couple times. Can the Python interpreter be confused by this > "nesting"? No, there shouldn't be any problem calling back and forth between Python and extension code as deep as you want (subject to the Python recursion depth limitation). -- Greg From yytomo at googlemail.com Thu Mar 4 04:22:19 2010 From: yytomo at googlemail.com (Toni Moll) Date: Thu, 4 Mar 2010 04:22:19 +0100 Subject: [Cython] pxd pyx parser bug for mismatch in function argument with default value Message-ID: <9010d0261003031922g47c7fb3cv466e075b8eac106f@mail.gmail.com> Hello, this is a bug report for cython 0.12.1. The bug occurs when a function argument with default value is named differently in the .pyx and the .pxd file. A minimal example is: foo.pyx contains: =========================== cpdef test(test=5): pass =========================== foo.pxd contains: =========================== cpdef test(FJKHFKLJDHF=*) =========================== the traceback is: /tmp/Cython-0.12.1$ python2.5 cython.py foo.pyx Traceback (most recent call last): File "cython.py", line 8, in main(command_line = 1) File "/tmp/Cython-0.12.1/Cython/ Compiler/Main.py", line 743, in main result = compile(sources, options) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 718, in compile return compile_multiple(source, options) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 690, in compile_multiple result = run_pipeline(source, options) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 561, in run_pipeline err, enddata = context.run_pipeline(pipeline, source) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 221, in run_pipeline data = phase(data) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 154, in generate_pyx_code module_node.process_implementation(options, result) File "/tmp/Cython-0.12.1/Cython/Compiler/ModuleNode.py", line 72, in process_implementation self.generate_c_code(env, options, result) File "/tmp/Cython-0.12.1/Cython/Compiler/ModuleNode.py", line 274, in generate_c_code self.body.generate_function_definitions(env, code) File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 341, in generate_function_definitions stat.generate_function_definitions(env, code) File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 1106, in generate_function_definitions self.generate_argument_parsing_code(env, code) File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 1493, in generate_argument_parsing_code code.putln('%s = %s->%s;' % (arg.cname, Naming.optional_args_cname, self.type.opt_arg_cname(declarator.name))) File "/tmp/Cython-0.12.1/Cython/Compiler/PyrexTypes.py", line 1571, in opt_arg_cname return self.op_arg_struct.base_type.scope.lookup(arg_name).cname AttributeError: 'NoneType' object has no attribute 'cname' best regards and thanks a lot for cython, I like it a lot! Toni -------------- next part -------------- An HTML attachment was scrubbed... URL: http://codespeak.net/pipermail/cython-dev/attachments/20100304/3e6b57bf/attachment-0001.htm From yytomo at googlemail.com Thu Mar 4 04:13:52 2010 From: yytomo at googlemail.com (Toni Moll) Date: Thu, 4 Mar 2010 04:13:52 +0100 Subject: [Cython] parser bug for mismatches in function arguments with default value Message-ID: <9010d0261003031913v4d8220f3ufa3fada390b7ba75@mail.gmail.com> Hello, this is a bug report for cython 0.12.1. The bug occurs when an function argument with default value is named differently in the .pyx and the .pxd file. A minimal example is: foo.pyx contains: =========================== cpdef test(test=5): pass =========================== foo.pxd contains: =========================== cpdef test(FJKHFKLJDHF=*) =========================== the traceback is: /tmp/Cython-0.12.1$ python2.5 cython.py foo.pyx Traceback (most recent call last): File "cython.py", line 8, in main(command_line = 1) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 743, in main result = compile(sources, options) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 718, in compile return compile_multiple(source, options) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 690, in compile_multiple result = run_pipeline(source, options) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 561, in run_pipeline err, enddata = context.run_pipeline(pipeline, source) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 221, in run_pipeline data = phase(data) File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 154, in generate_pyx_code module_node.process_implementation(options, result) File "/tmp/Cython-0.12.1/Cython/Compiler/ModuleNode.py", line 72, in process_implementation self.generate_c_code(env, options, result) File "/tmp/Cython-0.12.1/Cython/Compiler/ModuleNode.py", line 274, in generate_c_code self.body.generate_function_definitions(env, code) File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 341, in generate_function_definitions stat.generate_function_definitions(env, code) File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 1106, in generate_function_definitions self.generate_argument_parsing_code(env, code) File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 1493, in generate_argument_parsing_code code.putln('%s = %s->%s;' % (arg.cname, Naming.optional_args_cname, self.type.opt_arg_cname(declarator.name))) File "/tmp/Cython-0.12.1/Cython/Compiler/PyrexTypes.py", line 1571, in opt_arg_cname return self.op_arg_struct.base_type.scope.lookup(arg_name).cname AttributeError: 'NoneType' object has no attribute 'cname' best regards and thanks a lot for cython, I like it a lot! Toni -------------- next part -------------- An HTML attachment was scrubbed... URL: http://codespeak.net/pipermail/cython-dev/attachments/20100304/007f2e57/attachment.htm From robertwb at math.washington.edu Thu Mar 4 10:00:08 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Thu, 4 Mar 2010 01:00:08 -0800 Subject: [Cython] parser bug for mismatches in function arguments with default value In-Reply-To: <9010d0261003031913v4d8220f3ufa3fada390b7ba75@mail.gmail.com> References: <9010d0261003031913v4d8220f3ufa3fada390b7ba75@mail.gmail.com> Message-ID: <9921D75D-E42A-40E9-98D2-B9E5035CCBBE@math.washington.edu> Thanks. I've filed a report at http://trac.cython.org/cython_trac/ticket/512 I'm not sure when I'll have time to look at it, but it doesn't look like it'll be too hard to fix. - Robert On Mar 3, 2010, at 7:13 PM, Toni Moll wrote: > Hello, > > this is a bug report for cython 0.12.1. The bug occurs when an > function argument with default value is named differently in > the .pyx and the .pxd file. > A minimal example is: > > foo.pyx contains: > =========================== > cpdef test(test=5): > pass > =========================== > > foo.pxd contains: > =========================== > cpdef test(FJKHFKLJDHF=*) > =========================== > > the traceback is: > > /tmp/Cython-0.12.1$ python2.5 cython.py foo.pyx > Traceback (most recent call last): > File "cython.py", line 8, in > main(command_line = 1) > File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 743, in main > result = compile(sources, options) > File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 718, in > compile > return compile_multiple(source, options) > File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 690, in > compile_multiple > result = run_pipeline(source, options) > File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 561, in > run_pipeline > err, enddata = context.run_pipeline(pipeline, source) > File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 221, in > run_pipeline > data = phase(data) > File "/tmp/Cython-0.12.1/Cython/Compiler/Main.py", line 154, in > generate_pyx_code > module_node.process_implementation(options, result) > File "/tmp/Cython-0.12.1/Cython/Compiler/ModuleNode.py", line 72, > in process_implementation > self.generate_c_code(env, options, result) > File "/tmp/Cython-0.12.1/Cython/Compiler/ModuleNode.py", line 274, > in generate_c_code > self.body.generate_function_definitions(env, code) > File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 341, in > generate_function_definitions > stat.generate_function_definitions(env, code) > File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 1106, in > generate_function_definitions > self.generate_argument_parsing_code(env, code) > File "/tmp/Cython-0.12.1/Cython/Compiler/Nodes.py", line 1493, in > generate_argument_parsing_code > code.putln('%s = %s->%s;' % (arg.cname, > Naming.optional_args_cname, self.type.opt_arg_cname(declarator.name))) > File "/tmp/Cython-0.12.1/Cython/Compiler/PyrexTypes.py", line > 1571, in opt_arg_cname > return self.op_arg_struct.base_type.scope.lookup(arg_name).cname > AttributeError: 'NoneType' object has no attribute 'cname' > > best regards and thanks a lot for cython, I like it a lot! > Toni > _______________________________________________ > Cython-dev mailing list > Cython-dev at codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev From dagss at student.matnat.uio.no Thu Mar 4 13:05:35 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Thu, 04 Mar 2010 13:05:35 +0100 Subject: [Cython] Library for ODEs with Cython bindings? Message-ID: <4B8FA20F.4010309@student.matnat.uio.no> Are anybody aware of a library for solving ODEs with fast callbacks to Cython? The ones in SciPy require a Python callback at every step... My needs aren't particularily complicated. I'll just wrap something in C otherwise, of course (such as GSL). Dag Sverre From dalcinl at gmail.com Thu Mar 4 14:22:27 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 4 Mar 2010 10:22:27 -0300 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B8FA20F.4010309@student.matnat.uio.no> References: <4B8FA20F.4010309@student.matnat.uio.no> Message-ID: On 4 March 2010 09:05, Dag Sverre Seljebotn wrote: > Are anybody aware of a library for solving ODEs with fast callbacks to > Cython? The ones in SciPy require a Python callback at every step... > IIRC, Jon Olav Vik was working on a Sundials wrapper... Search cython-devel. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From gregor.thalhammer at gmail.com Thu Mar 4 17:16:56 2010 From: gregor.thalhammer at gmail.com (Gregor Thalhammer) Date: Thu, 4 Mar 2010 17:16:56 +0100 Subject: [Cython] Problems with callback function declaration Message-ID: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> Dear all, I experience problems with callbacks declared with the __stdcall calling convention and custom data types as return type. To show the problem I modified the cython callback demo cheese.pyx, other files are the same like in http://hg.cython.org/cython-devel/file/2137466cab4a/Demos/callback cheese.pyx: <<<<<<<<<<<<<<<<<<<<<<<< cdef extern from "cheesefinder.h": ctypedef void myvoid ctypedef myvoid (__stdcall *cheesefunc)(char *name, void *user_data) #this gives 'Syntax error in ctypedef statement' #ctypedef void (__stdcall *cheesefunc)(char *name, void *user_data) #OK void find_cheeses(cheesefunc user_func, void *user_data) def find(f): find_cheeses(callback, f) cdef void __stdcall callback(char *name, void *f): (f)(name) >>>>>>>>>>>>>>>>>>>>>>> This produces a cython syntax error: <<<<<<<<<<<<<<<<<<<<<< cdef extern from "cheesefinder.h": ctypedef void myvoid ctypedef myvoid (__stdcall *cheesefunc)(char *name, void *user_data) #this g ives 'Syntax error in ctypedef statement' ^ ------------------------------------------------------------ C:\Documents and Settings\q014gt\Desktop\callback modified\cheese.pyx:7:21: Synt ax error in ctypedef statement >>>>>>>>>>>>>>>>>>>>>> Is this behaviour a bug? I tried cython 0.11.2 and 0.12.1 on WinXP. A workaround I found is to replace the custom data type by its definition, as indicated above. Gregor From greg.ewing at canterbury.ac.nz Thu Mar 4 23:45:04 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Fri, 05 Mar 2010 11:45:04 +1300 Subject: [Cython] Problems with callback function declaration In-Reply-To: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> References: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> Message-ID: <4B9037F0.5030400@canterbury.ac.nz> Gregor Thalhammer wrote: > cdef extern from "cheesefinder.h": > ctypedef void myvoid > ctypedef myvoid (__stdcall *cheesefunc)(char *name, void *user_data) #this g > ives 'Syntax error in ctypedef statement' Seems to be a Cython-only problem, as this compiles okay with Pyrex 0.9.8.6. -- Greg From jonovik at gmail.com Fri Mar 5 09:55:41 2010 From: jonovik at gmail.com (Jon Olav Vik) Date: Fri, 5 Mar 2010 08:55:41 +0000 (UTC) Subject: [Cython] Library for ODEs with Cython bindings? References: <4B8FA20F.4010309@student.matnat.uio.no> Message-ID: Lisandro Dalcin writes: > On 4 March 2010 09:05, Dag Sverre Seljebotn student.matnat.uio.no> wrote: > > Are anybody aware of a library for solving ODEs with fast callbacks to > > Cython? The ones in SciPy require a Python callback at every step... > > > > IIRC, Jon Olav Vik was working on a Sundials wrapper... Search cython-devel. http://thread.gmane.org/gmane.comp.python.cython.devel/6497 Quick summary (not of the previous discussion, but of some options now): Sundials is a feature-rich C library with a fairly fine-grained API for integrating differential equations. https://computation.llnl.gov/casc/sundials/ It also has Fortran bindings. Pysundials http://pysundials.sourceforge.net/ is based on J. G. Dominy's MSc thesis, "PySUNDIALS: Providing python bindings to a robust suite of mathematical tools for computational systems biology". http://etd.sun.ac.za/handle/10019/2523 I've built a coarser-grained wrapper for CVODE that you're welcome to try out. As mentioned in our previous discussion, Pysundials has an almost one-to-one correspondence with the fine-grained Sundials API. With your expertise you might be able to cythonize Sundials directly with little effort, with the end result being very similar to Pysundials. I tried, but am not familiar enough with C to generate the Cython declarations for all public parts of Sundials. Another option is to just cythonize Pysundials, but I encountered two main problems: * Nested functions (I tried checking out Cython-closures, but couldn't get it to work in a few quick attempts.) * Pysundials creates a callback function by eval'ing an expression involving the RHS function. I think it might be possible to rewrite those problem spots to allow Pysundials to bypass Python for Cython RHS's. However, just "rewriting" Pysundials in Cython is probably the cleaner option, and possibly just as easy for you. There is also PyDSTool, http://sourceforge.net/projects/pydstool/ http://www.cam.cornell.edu/~rclewley/cgi-bin/moin.cgi/MainProjectPage which I haven't taken the time to check out, though I have this nagging feeling that it might have saved me quite some time... Jon Olav From dalcinl at gmail.com Fri Mar 5 15:09:15 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 5 Mar 2010 11:09:15 -0300 Subject: [Cython] Problems with callback function declaration In-Reply-To: <4B9037F0.5030400@canterbury.ac.nz> References: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> <4B9037F0.5030400@canterbury.ac.nz> Message-ID: On 4 March 2010 19:45, Greg Ewing wrote: > Gregor Thalhammer wrote: > >> cdef extern from "cheesefinder.h": >> ? ? ctypedef void myvoid >> ? ? ctypedef myvoid (__stdcall *cheesefunc)(char *name, void *user_data) #this g >> ives 'Syntax error in ctypedef statement' > > Seems to be a Cython-only problem, as this compiles > okay with Pyrex 0.9.8.6. > Indeed. IIRC, Robert changed the parser in order to support other things, and then some function typedefs stop working. Gregor: I would declare things like things: ctypedef myvoid __stdcall cheesefunc(char *name, void *user_data) void find_cheeses(cheesefunc* user_func, void *user_data) -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From gregor.thalhammer at gmail.com Fri Mar 5 17:16:04 2010 From: gregor.thalhammer at gmail.com (Gregor Thalhammer) Date: Fri, 5 Mar 2010 17:16:04 +0100 Subject: [Cython] Problems with callback function declaration In-Reply-To: References: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> <4B9037F0.5030400@canterbury.ac.nz> Message-ID: <42de02941003050816o57a77a8x18643b121e29be05@mail.gmail.com> Am 05.03.2010 15:09, schrieb Lisandro Dalcin: On 4 March 2010 19:45, Greg Ewing wrote: Gregor Thalhammer wrote: cdef extern from "cheesefinder.h": ctypedef void myvoid ctypedef myvoid (__stdcall *cheesefunc)(char *name, void *user_data) #this g ives 'Syntax error in ctypedef statement' Seems to be a Cython-only problem, as this compiles okay with Pyrex 0.9.8.6. Indeed. IIRC, Robert changed the parser in order to support other things, and then some function typedefs stop working. Gregor: I would declare things like things: ctypedef myvoid __stdcall cheesefunc(char *name, void *user_data) void find_cheeses(cheesefunc* user_func, void *user_data) Thanks for this advice. I tried and found that even ctypedef myvoid __stdcall cheesefunc(char *name, void *user_data) void find_cheeses(cheesefunc user_func, void *user_data) works, it compiles to the same C code. I like this version since it is a) more ease to write and b) matches better the definition of find_cheeses in the header file. Should the callback demo be changed? Gregor -------------- next part -------------- An HTML attachment was scrubbed... URL: http://codespeak.net/pipermail/cython-dev/attachments/20100305/db226ad9/attachment.htm From dalcinl at gmail.com Fri Mar 5 18:51:21 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 5 Mar 2010 14:51:21 -0300 Subject: [Cython] Problems with callback function declaration In-Reply-To: <42de02941003050816o57a77a8x18643b121e29be05@mail.gmail.com> References: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> <4B9037F0.5030400@canterbury.ac.nz> <42de02941003050816o57a77a8x18643b121e29be05@mail.gmail.com> Message-ID: On 5 March 2010 13:16, Gregor Thalhammer wrote: > Am 05.03.2010 15:09, schrieb Lisandro Dalcin: > > On 4 March 2010 19:45, Greg Ewing wrote: > > > Gregor Thalhammer wrote: > > > > cdef extern from "cheesefinder.h": > ? ? ctypedef void myvoid > ? ? ctypedef myvoid (__stdcall *cheesefunc)(char *name, void *user_data) > #this g > ives 'Syntax error in ctypedef statement' > > > Seems to be a Cython-only problem, as this compiles > okay with Pyrex 0.9.8.6. > > > > Indeed. IIRC, Robert changed the parser in order to support other > things, and then some function typedefs stop working. > > Gregor: I would declare things like things: > > ctypedef myvoid __stdcall cheesefunc(char *name, void *user_data) > > void find_cheeses(cheesefunc* user_func, void *user_data) > > > > Thanks for this advice. I tried and found that even > > ctypedef myvoid __stdcall cheesefunc(char *name, void *user_data) > void find_cheeses(cheesefunc user_func, void *user_data) > > works, it compiles to the same C code. I like this version since it is a) > more ease to write and b) matches better the definition of find_cheeses in > the header file. Are you sure this definition do actually matches he header file? IANAL regarding C, but a function type is not the same that a function pointer type, despite the fact that in some cases the function and function pointers seems to be/behave the same... > > Should the callback demo be changed? > Let's wait for Robert's comments... Perhaps your original problem is indeed a bug, and we should fix it. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From robertwb at math.washington.edu Fri Mar 5 18:48:13 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Fri, 5 Mar 2010 09:48:13 -0800 Subject: [Cython] Problems with callback function declaration In-Reply-To: References: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> <4B9037F0.5030400@canterbury.ac.nz> Message-ID: <51E67AF1-FFA5-4AF3-9F44-56C2CC1EE88D@math.washington.edu> On Mar 5, 2010, at 6:09 AM, Lisandro Dalcin wrote: > On 4 March 2010 19:45, Greg Ewing wrote: >> Gregor Thalhammer wrote: >> >>> cdef extern from "cheesefinder.h": >>> ctypedef void myvoid >>> ctypedef myvoid (__stdcall *cheesefunc)(char *name, void >>> *user_data) #this g >>> ives 'Syntax error in ctypedef statement' >> >> Seems to be a Cython-only problem, as this compiles >> okay with Pyrex 0.9.8.6. >> > > Indeed. IIRC, Robert changed the parser in order to support other > things, and then some function typedefs stop working. Sorry, I didn't realize I broke anything. (Insufficient regression tests I guess, as I never use __stdcall etc.) > Gregor: I would declare things like things: > > ctypedef myvoid __stdcall cheesefunc(char *name, void *user_data) > > void find_cheeses(cheesefunc* user_func, void *user_data) The above code certainly looks cleaner to me, though that's a matter of preference. - Robert From robertwb at math.washington.edu Fri Mar 5 18:55:44 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Fri, 5 Mar 2010 09:55:44 -0800 Subject: [Cython] Problems with callback function declaration In-Reply-To: References: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> <4B9037F0.5030400@canterbury.ac.nz> <42de02941003050816o57a77a8x18643b121e29be05@mail.gmail.com> Message-ID: <7690B811-F738-4F90-8EA1-1693B4112819@math.washington.edu> On Mar 5, 2010, at 9:51 AM, Lisandro Dalcin wrote: > Let's wait for Robert's comments... Perhaps your original problem is > indeed a bug, and we should fix it. Yes, I consider that a bug. - Robert From dalcinl at gmail.com Fri Mar 5 20:43:22 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 5 Mar 2010 16:43:22 -0300 Subject: [Cython] Problems with callback function declaration In-Reply-To: <7690B811-F738-4F90-8EA1-1693B4112819@math.washington.edu> References: <42de02941003040816t448de9f4wc7292d26e9aab3cc@mail.gmail.com> <4B9037F0.5030400@canterbury.ac.nz> <42de02941003050816o57a77a8x18643b121e29be05@mail.gmail.com> <7690B811-F738-4F90-8EA1-1693B4112819@math.washington.edu> Message-ID: On 5 March 2010 14:55, Robert Bradshaw wrote: > On Mar 5, 2010, at 9:51 AM, Lisandro Dalcin wrote: > >> Let's wait for Robert's comments... Perhaps your original problem is >> indeed a bug, and we should fix it. > > Yes, I consider that a bug. > > - Robert > Here you have an extended testcase: diff -r 2137466cab4a tests/compile/funcptr.pyx --- a/tests/compile/funcptr.pyx Wed Feb 24 17:21:15 2010 -0300 +++ b/tests/compile/funcptr.pyx Fri Mar 05 16:42:36 2010 -0300 @@ -8,3 +8,15 @@ cdef inline funcptr_t* dummy(): return &grail + +ctypedef int MyInt + +ctypedef int __stdcall foo0_t() +foo0_t *a = grail +ctypedef MyInt __stdcall foo1_t() +foo1_t *b = grail + +ctypedef int (__stdcall *bar0_t)() +bar0_t x = grail +ctypedef MyInt (__stdcall *bar1_t)() +bar0_t y = grail -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From ondrej at certik.cz Tue Mar 9 01:52:56 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Mon, 8 Mar 2010 16:52:56 -0800 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B8FA20F.4010309@student.matnat.uio.no> References: <4B8FA20F.4010309@student.matnat.uio.no> Message-ID: <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> On Thu, Mar 4, 2010 at 4:05 AM, Dag Sverre Seljebotn wrote: > Are anybody aware of a library for solving ODEs with fast callbacks to > Cython? The ones in SciPy require a Python callback at every step... > > My needs aren't particularily complicated. > > I'll just wrap something in C otherwise, of course (such as GSL). If you do so, please post the code online. I want to see how you did the callbacks (so that it works both in python and cython), as I will need to implement the same things for our solvers. So far I just created a cdef function, that I register for the C callback. But it is not visible from Python. Ondrej From dalcinl at gmail.com Tue Mar 9 02:16:13 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Mon, 8 Mar 2010 22:16:13 -0300 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> Message-ID: On 8 March 2010 21:52, Ondrej Certik wrote: > On Thu, Mar 4, 2010 at 4:05 AM, Dag Sverre Seljebotn > wrote: >> Are anybody aware of a library for solving ODEs with fast callbacks to >> Cython? The ones in SciPy require a Python callback at every step... >> >> My needs aren't particularily complicated. >> >> I'll just wrap something in C otherwise, of course (such as GSL). > > If you do so, please post the code online. I want to see how you did > the callbacks (so that it works both in python and cython), as I will > need to implement the same things for our solvers. So far I just > created a cdef function, that I register for the C callback. But it is > not visible from Python. > You could use cpdef to get both the C and Python versions... not sure if that's what you really need. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dagss at student.matnat.uio.no Tue Mar 9 09:35:26 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Tue, 09 Mar 2010 09:35:26 +0100 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> Message-ID: <4B96084E.60209@student.matnat.uio.no> Ondrej Certik wrote: > On Thu, Mar 4, 2010 at 4:05 AM, Dag Sverre Seljebotn > wrote: > >> Are anybody aware of a library for solving ODEs with fast callbacks to >> Cython? The ones in SciPy require a Python callback at every step... >> >> My needs aren't particularily complicated. >> >> I'll just wrap something in C otherwise, of course (such as GSL). >> > > If you do so, please post the code online. I want to see how you did > the callbacks (so that it works both in python and cython), as I will > need to implement the same things for our solvers. So far I just > created a cdef function, that I register for the C callback. But it is > not visible from Python. > I'll have a look at the stuff in Sage first, then try sundials if my needs aren't met there... As for doing the callbacks, I'd do what Sage essentially does: cdef class DoubleFunction: cdef double evaluate(double x): raise NotImplementedError() On the Python frontend side, one can trivially write a coercer object to wrap Python callbacks if such are supplied. On the backend side, if you need to feed it to a C library, then assuming the C library is a good citizen and takes contexts, simply do cdef double call_double_func(double x, void* ctx): return (ctx).evaluate(x) my_c_function(&call_double_func, ...., passed_in_DoubleFunction_instance) Dag Sverre From ondrej at certik.cz Tue Mar 9 18:12:12 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Tue, 9 Mar 2010 09:12:12 -0800 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B96084E.60209@student.matnat.uio.no> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> Message-ID: <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> On Tue, Mar 9, 2010 at 12:35 AM, Dag Sverre Seljebotn wrote: > Ondrej Certik wrote: >> On Thu, Mar 4, 2010 at 4:05 AM, Dag Sverre Seljebotn >> wrote: >> >>> Are anybody aware of a library for solving ODEs with fast callbacks to >>> Cython? The ones in SciPy require a Python callback at every step... >>> >>> My needs aren't particularily complicated. >>> >>> I'll just wrap something in C otherwise, of course (such as GSL). >>> >> >> If you do so, please post the code online. I want to see how you did >> the callbacks (so that it works both in python and cython), as I will >> need to implement the same things for our solvers. So far I just >> created a cdef function, that I register for the C callback. But it is >> not visible from Python. >> > I'll have a look at the stuff in Sage first, then try sundials if my > needs aren't met there... > > As for doing the callbacks, I'd do what Sage essentially does: > > cdef class DoubleFunction: > ? ?cdef double evaluate(double x): > ? ? ? ?raise NotImplementedError() > > On the Python frontend side, one can trivially write a coercer object to > wrap Python callbacks if such are supplied. On the backend side, if you I understand this on the backend (C) side, that's clear. But on the Python side --- how would that be done? E.g. subclassing it? Would then cdef double call_double_func(double x, void* ctx): return (ctx).evaluate(x) evaluate my Python method? Wow, that would be super awesome, never thought about it (I also wanted to try cpdef instead). Let me try it. Ondrej From dagss at student.matnat.uio.no Tue Mar 9 18:22:44 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Tue, 09 Mar 2010 18:22:44 +0100 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> Message-ID: <4B9683E4.30508@student.matnat.uio.no> Ondrej Certik wrote: > On Tue, Mar 9, 2010 at 12:35 AM, Dag Sverre Seljebotn > wrote: > >> Ondrej Certik wrote: >> >>> On Thu, Mar 4, 2010 at 4:05 AM, Dag Sverre Seljebotn >>> wrote: >>> >>> >>>> Are anybody aware of a library for solving ODEs with fast callbacks to >>>> Cython? The ones in SciPy require a Python callback at every step... >>>> >>>> My needs aren't particularily complicated. >>>> >>>> I'll just wrap something in C otherwise, of course (such as GSL). >>>> >>>> >>> If you do so, please post the code online. I want to see how you did >>> the callbacks (so that it works both in python and cython), as I will >>> need to implement the same things for our solvers. So far I just >>> created a cdef function, that I register for the C callback. But it is >>> not visible from Python. >>> >>> >> I'll have a look at the stuff in Sage first, then try sundials if my >> needs aren't met there... >> >> As for doing the callbacks, I'd do what Sage essentially does: >> >> cdef class DoubleFunction: >> cdef double evaluate(double x): >> raise NotImplementedError() >> >> On the Python frontend side, one can trivially write a coercer object to >> wrap Python callbacks if such are supplied. On the backend side, if you >> > > > I understand this on the backend (C) side, that's clear. But on the > Python side --- how would that be done? E.g. subclassing it? Would > then > > cdef double call_double_func(double x, void* ctx): > return (ctx).evaluate(x) > > evaluate my Python method? Wow, that would be super awesome, never > thought about it (I also wanted to try cpdef instead). Let me try it. > If you do cdef class DoubleFunction: cpdef double evaluate(...) # NOTE cpdef then yes, you can subclass it in Python. However there's a slight performance penalty for Cython code then (one if-test for NULL-ness of object dict) which might or might not matter. I simply referred to def odeint(func, ...): # This is O(1) so we don't care if not isinstance(func, DoubleFunction): # Not a DoubleFunction, assume it is callable func = CallableAsDoubleFunction(func) where CallableAsDoubleFunction has cdef double evaluate(self, double x) return self.func(x) Dag Sverre From dagss at student.matnat.uio.no Tue Mar 9 18:29:45 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Tue, 09 Mar 2010 18:29:45 +0100 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B9683E4.30508@student.matnat.uio.no> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B9683E4.30508@student.matnat.uio.no> Message-ID: <4B968589.6060703@student.matnat.uio.no> Dag Sverre Seljebotn wrote: > Ondrej Certik wrote: > >> On Tue, Mar 9, 2010 at 12:35 AM, Dag Sverre Seljebotn >> wrote: >> >> >>> Ondrej Certik wrote: >>> >>> >>>> On Thu, Mar 4, 2010 at 4:05 AM, Dag Sverre Seljebotn >>>> wrote: >>>> >>>> >>>> >>>>> Are anybody aware of a library for solving ODEs with fast callbacks to >>>>> Cython? The ones in SciPy require a Python callback at every step... >>>>> >>>>> My needs aren't particularily complicated. >>>>> >>>>> I'll just wrap something in C otherwise, of course (such as GSL). >>>>> >>>>> >>>>> >>>> If you do so, please post the code online. I want to see how you did >>>> the callbacks (so that it works both in python and cython), as I will >>>> need to implement the same things for our solvers. So far I just >>>> created a cdef function, that I register for the C callback. But it is >>>> not visible from Python. >>>> >>>> >>>> >>> I'll have a look at the stuff in Sage first, then try sundials if my >>> needs aren't met there... >>> >>> As for doing the callbacks, I'd do what Sage essentially does: >>> >>> cdef class DoubleFunction: >>> cdef double evaluate(double x): >>> raise NotImplementedError() >>> >>> On the Python frontend side, one can trivially write a coercer object to >>> wrap Python callbacks if such are supplied. On the backend side, if you >>> >>> >> I understand this on the backend (C) side, that's clear. But on the >> Python side --- how would that be done? E.g. subclassing it? Would >> then >> >> cdef double call_double_func(double x, void* ctx): >> return (ctx).evaluate(x) >> >> evaluate my Python method? Wow, that would be super awesome, never >> thought about it (I also wanted to try cpdef instead). Let me try it. >> >> > > If you do > > cdef class DoubleFunction: > cpdef double evaluate(...) # NOTE cpdef > > then yes, you can subclass it in Python. However there's a slight > performance penalty for Cython code then (one if-test for NULL-ness of > object dict) which might or might not matter. > Well, in practice it never matters I suppose. The main point is that I consider it unfriendly to not allow any callables from Python code, and the code below satisfies both requirements (fast => DoubleFunction in Cython without anything in-between; don't care about speed => use slower wrapper and generic callable). Dag Sverre > I simply referred to > > def odeint(func, ...): > # This is O(1) so we don't care > if not isinstance(func, DoubleFunction): > # Not a DoubleFunction, assume it is callable > func = CallableAsDoubleFunction(func) > > where CallableAsDoubleFunction has > > cdef double evaluate(self, double x) > return self.func(x) > > > Dag Sverre > _______________________________________________ > Cython-dev mailing list > Cython-dev at codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev > From ondrej at certik.cz Tue Mar 9 18:34:05 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Tue, 9 Mar 2010 09:34:05 -0800 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B968589.6060703@student.matnat.uio.no> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B9683E4.30508@student.matnat.uio.no> <4B968589.6060703@student.matnat.uio.no> Message-ID: <85b5c3131003090934p1f3357dcy3f948354f223aa34@mail.gmail.com> On Tue, Mar 9, 2010 at 9:29 AM, Dag Sverre Seljebotn wrote: > Dag Sverre Seljebotn wrote: >> Ondrej Certik wrote: >> >>> On Tue, Mar 9, 2010 at 12:35 AM, Dag Sverre Seljebotn >>> wrote: >>> >>> >>>> Ondrej Certik wrote: >>>> >>>> >>>>> On Thu, Mar 4, 2010 at 4:05 AM, Dag Sverre Seljebotn >>>>> wrote: >>>>> >>>>> >>>>> >>>>>> Are anybody aware of a library for solving ODEs with fast callbacks to >>>>>> Cython? The ones in SciPy require a Python callback at every step... >>>>>> >>>>>> My needs aren't particularily complicated. >>>>>> >>>>>> I'll just wrap something in C otherwise, of course (such as GSL). >>>>>> >>>>>> >>>>>> >>>>> If you do so, please post the code online. I want to see how you did >>>>> the callbacks (so that it works both in python and cython), as I will >>>>> need to implement the same things for our solvers. So far I just >>>>> created a cdef function, that I register for the C callback. But it is >>>>> not visible from Python. >>>>> >>>>> >>>>> >>>> I'll have a look at the stuff in Sage first, then try sundials if my >>>> needs aren't met there... >>>> >>>> As for doing the callbacks, I'd do what Sage essentially does: >>>> >>>> cdef class DoubleFunction: >>>> ? ?cdef double evaluate(double x): >>>> ? ? ? ?raise NotImplementedError() >>>> >>>> On the Python frontend side, one can trivially write a coercer object to >>>> wrap Python callbacks if such are supplied. On the backend side, if you >>>> >>>> >>> I understand this on the backend (C) side, that's clear. But on the >>> Python side --- how would that be done? E.g. subclassing it? Would >>> then >>> >>> cdef double call_double_func(double x, void* ctx): >>> ? ?return (ctx).evaluate(x) >>> >>> evaluate my Python method? Wow, that would be super awesome, never >>> thought about it (I also wanted to try cpdef instead). Let me try it. >>> >>> >> >> If you do >> >> cdef class DoubleFunction: >> ? ? cpdef double evaluate(...) # NOTE cpdef >> >> then yes, you can subclass it in Python. However there's a slight >> performance penalty for Cython code then (one if-test for NULL-ness of >> object dict) which might or might not matter. >> > Well, in practice it never matters I suppose. The main point is that I > consider it unfriendly to not allow any callables from Python code, and Yes, I totally agree. > the code below satisfies both requirements (fast => DoubleFunction in > Cython without anything in-between; don't care about speed => use slower > wrapper and generic callable). Where is the code in Sage? I did: sage: search_src("odeint") sage: and it didn't find it. Ondrej From dagss at student.matnat.uio.no Tue Mar 9 18:44:29 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Tue, 09 Mar 2010 18:44:29 +0100 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <85b5c3131003090934p1f3357dcy3f948354f223aa34@mail.gmail.com> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B9683E4.30508@student.matnat.uio.no> <4B968589.6060703@student.matnat.uio.no> <85b5c3131003090934p1f3357dcy3f948354f223aa34@mail.gmail.com> Message-ID: <4B9688FD.2060505@student.matnat.uio.no> Ondrej Certik wrote: > On Tue, Mar 9, 2010 at 9:29 AM, Dag Sverre Seljebotn > wrote: > >> Dag Sverre Seljebotn wrote: >> >>> Ondrej Certik wrote: >>> >>> >>>> On Tue, Mar 9, 2010 at 12:35 AM, Dag Sverre Seljebotn >>>> wrote: >>>> >>>> >>>> >>>>> Ondrej Certik wrote: >>>>> >>>>> >>>>> >>>>>> On Thu, Mar 4, 2010 at 4:05 AM, Dag Sverre Seljebotn >>>>>> wrote: >>>>>> >>>>>> >>>>>> >>>>>> >>>>>>> Are anybody aware of a library for solving ODEs with fast callbacks to >>>>>>> Cython? The ones in SciPy require a Python callback at every step... >>>>>>> >>>>>>> My needs aren't particularily complicated. >>>>>>> >>>>>>> I'll just wrap something in C otherwise, of course (such as GSL). >>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>> If you do so, please post the code online. I want to see how you did >>>>>> the callbacks (so that it works both in python and cython), as I will >>>>>> need to implement the same things for our solvers. So far I just >>>>>> created a cdef function, that I register for the C callback. But it is >>>>>> not visible from Python. >>>>>> >>>>>> >>>>>> >>>>>> >>>>> I'll have a look at the stuff in Sage first, then try sundials if my >>>>> needs aren't met there... >>>>> >>>>> As for doing the callbacks, I'd do what Sage essentially does: >>>>> >>>>> cdef class DoubleFunction: >>>>> cdef double evaluate(double x): >>>>> raise NotImplementedError() >>>>> >>>>> On the Python frontend side, one can trivially write a coercer object to >>>>> wrap Python callbacks if such are supplied. On the backend side, if you >>>>> >>>>> >>>>> >>>> I understand this on the backend (C) side, that's clear. But on the >>>> Python side --- how would that be done? E.g. subclassing it? Would >>>> then >>>> >>>> cdef double call_double_func(double x, void* ctx): >>>> return (ctx).evaluate(x) >>>> >>>> evaluate my Python method? Wow, that would be super awesome, never >>>> thought about it (I also wanted to try cpdef instead). Let me try it. >>>> >>>> >>>> >>> If you do >>> >>> cdef class DoubleFunction: >>> cpdef double evaluate(...) # NOTE cpdef >>> >>> then yes, you can subclass it in Python. However there's a slight >>> performance penalty for Cython code then (one if-test for NULL-ness of >>> object dict) which might or might not matter. >>> >>> >> Well, in practice it never matters I suppose. The main point is that I >> consider it unfriendly to not allow any callables from Python code, and >> > > Yes, I totally agree. > BTW, exceptions raised in the callback is an important matter too! I'm debugging an ODE as I write this and raising exceptions from the callback is extremely valuable. So ideally you should add "except *" to the evaluate method above, and then find a way of propagating exceptions and interrupting the C library (typically with a try/except in the callback which invokes DoubleFunction, which then returns a status code to the C lib, or if that's not possible, switches to a dummy implementation which returns nans for the remaining steps). One happy day we'll support "except nan" for a slight performance boost (if anybody feel like doing me a favor then file a feature request for that in Trac). > >> the code below satisfies both requirements (fast => DoubleFunction in >> Cython without anything in-between; don't care about speed => use slower >> wrapper and generic callable). >> > > Where is the code in Sage? I did: > > sage: search_src("odeint") > sage.gsl.ode http://www.sagemath.org/doc/reference/sage/gsl/ode.html I must say I almost fell off my chair when I found a Python library using lists of floating point numbers for solving ODEs; the API could do with a NumPy-aware remake or addition. But apart from that I like the explicitness of method selection etc. and code quality in general better than SciPy -- SciPy tends to dump things to stdout and not propagate exceptions raised in the callback all the way to the odeint -- ugh!! Dag Sverre From dalcinl at gmail.com Tue Mar 9 20:12:12 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Tue, 9 Mar 2010 16:12:12 -0300 Subject: [Cython] getting rid of CPython's structmember.h Message-ID: We could get rid of structmember.h by using the transform+property machinery Dag implemented long ago So far, Robert has two concerns: 1) Dag's code had a few issues. 2) The generated code is less readable and bigger. Regarding (1), I'm working on it... the issues are minor, basically handle 'readonly' and auto-generate a docstring like "attribute 'ATTR' of 'TYPE' objects" (BTW, should this be on by default?). Regarding (2), I can certainly trade readability and code size, for many reasons: a) The header "structmember.h" is some sort of private stuff b) It is not even #include'd by default from Python.h, c) It is helpful for hand-written C code, but easy to get rid of in code generators like Cython. d) A big offence in that headers is that some definitions T_XXX are not properly namespace-protected with a Py_ prefix, then they can conflict with user code (as posted today in cython-users). d) Other issue is that C<->Python conversions are outside Cython's control of to_py/from_py converters, and this is IMO inconsistent. f) Finally, if readability and code size are so important in this case, we could latter provide a better implementation that does not rely on a tree transform. PS: Actually, I do not care too much about readability and code size in this case; Cython has already abandoned other readable, small-sized Python C-API idioms (e.g. argument parsing). -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From stefan_ml at behnel.de Tue Mar 9 20:26:21 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 09 Mar 2010 20:26:21 +0100 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: Message-ID: <4B96A0DD.5070107@behnel.de> Lisandro Dalcin, 09.03.2010 20:12: > d) Other issue is that C<->Python conversions are outside Cython's > control of to_py/from_py converters, and this is IMO inconsistent. I find this a very convincing argument. Stefan From ellisonbg.net at gmail.com Tue Mar 9 20:30:06 2010 From: ellisonbg.net at gmail.com (Brian Granger) Date: Tue, 9 Mar 2010 11:30:06 -0800 Subject: [Cython] Signal handler question Message-ID: <6ce0ac131003091130n174f5925he4afc88e48e4e196@mail.gmail.com> Hi, A few years ago, Ondrej asked a question about _sig_on/_sig_off being in Cython itself: http://codespeak.net/pipermail/cython-dev/2008-November/003081.html William brought up the possibility of moving the relevant code from Sage to Cython. Questions? * Has anyone worked on this? * If not, is it still a possibility? * Can the relevant code from Sage be re-licensed for Cython? With some help, I might be able to work on this. Cheers, Brian -------------- next part -------------- An HTML attachment was scrubbed... URL: http://codespeak.net/pipermail/cython-dev/attachments/20100309/9ceeb071/attachment.htm From kwmsmith at gmail.com Tue Mar 9 21:04:55 2010 From: kwmsmith at gmail.com (Kurt Smith) Date: Tue, 9 Mar 2010 14:04:55 -0600 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B9688FD.2060505@student.matnat.uio.no> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B9683E4.30508@student.matnat.uio.no> <4B968589.6060703@student.matnat.uio.no> <85b5c3131003090934p1f3357dcy3f948354f223aa34@mail.gmail.com> <4B9688FD.2060505@student.matnat.uio.no> Message-ID: On Tue, Mar 9, 2010 at 11:44 AM, Dag Sverre Seljebotn wrote: > Ondrej Certik wrote: >> On Tue, Mar 9, 2010 at 9:29 AM, Dag Sverre Seljebotn >> wrote: > > I must say I almost fell off my chair when I found a Python library > using lists of floating point numbers for solving ODEs; the API could do > with a NumPy-aware remake or addition. But apart from that I like the > explicitness of method selection etc. and code quality in general better > than SciPy -- SciPy tends to dump things to stdout and not propagate > exceptions raised in the callback all the way to the odeint -- ugh!! I noticed the same (I'm doing some ODE integration currently, too) -- taking a look at the code, it's coming from odepack code wrapped by f2py. The original f77 code is, as you'd expect, an enormous monolith that has control & error logic all mashed together. I'd be tempted to leave it be myself :-) For the record, I'm using scipy.integrate.ode which is a nicer interface than scipy.integrate.odeint. The ode class has a .successful method that easily lets you check to see if anything's wrong. And scipy.integrate.ode propogates exceptions raised in the rhs callback properly, too. It's all in python, of course, so not amenable to fast Cython callbacks, however... Kurt From greg.ewing at canterbury.ac.nz Wed Mar 10 01:01:32 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Wed, 10 Mar 2010 13:01:32 +1300 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> Message-ID: <4B96E15C.9040707@canterbury.ac.nz> Ondrej Certik wrote: > cdef double call_double_func(double x, void* ctx): > return (ctx).evaluate(x) Not sure about the specifics of ODE solving, but speaking about callbacks in general, it suffices to be able to pass an arbitrary callable object from Python. Any required context can be encapsulated by using a bound method or nested function as the callback. There is no need to require the Python code to provide an object with an "execute" method or anything like that. -- Greg From greg.ewing at canterbury.ac.nz Wed Mar 10 01:05:23 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Wed, 10 Mar 2010 13:05:23 +1300 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B9688FD.2060505@student.matnat.uio.no> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B9683E4.30508@student.matnat.uio.no> <4B968589.6060703@student.matnat.uio.no> <85b5c3131003090934p1f3357dcy3f948354f223aa34@mail.gmail.com> <4B9688FD.2060505@student.matnat.uio.no> Message-ID: <4B96E243.606@canterbury.ac.nz> Dag Sverre Seljebotn wrote: > One happy day we'll support "except nan" for a slight performance boost That might have to be "except? nan", since a nan could arise accidentally without an exception being set. -- Greg From robertwb at math.washington.edu Wed Mar 10 01:25:49 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Tue, 9 Mar 2010 16:25:49 -0800 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B96E15C.9040707@canterbury.ac.nz> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B96E15C.9040707@canterbury.ac.nz> Message-ID: <1409068A-E7F8-4087-B427-57780DDA05A2@math.washington.edu> On Mar 9, 2010, at 4:01 PM, Greg Ewing wrote: > Ondrej Certik wrote: > >> cdef double call_double_func(double x, void* ctx): >> return (ctx).evaluate(x) > > Not sure about the specifics of ODE solving, but speaking > about callbacks in general, it suffices to be able to > pass an arbitrary callable object from Python. > > Any required context can be encapsulated by using a bound > method or nested function as the callback. There is no > need to require the Python code to provide an object with > an "execute" method or anything like that. The issue here is speed. Using a bound method, nested function, or other arbitrary Python callable enforces Python calling semantics (METH_KEYWORDS for __call__), which for a double -> double function that's going to be called thousands or millions of times in the course of solving differential equations is way to much overhead. Hence a special class with a cdef method. Also, we're taking about wrapping a C api that takes a (double*)(double, void*) and void* context, so it's a natural thing to do. - Robert From dalcinl at gmail.com Wed Mar 10 02:03:15 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Tue, 9 Mar 2010 22:03:15 -0300 Subject: [Cython] GCC warnings Message-ID: Folks, I've been doing some housekeeping work in the testsuite to make GCC warnings under control. In the process, I've added a few hacks to remove warnings from unused stuff coming from NumPy headers. My fixes work for NumPy 1.2.x and above, but I've not tested with older versions... If anyone is running Cython testsuite with an older NumPy and have any trouble, let me know and I'll fix things. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From stefan_ml at behnel.de Wed Mar 10 06:59:43 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 10 Mar 2010 06:59:43 +0100 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: <4B96A0DD.5070107@behnel.de> Message-ID: <4B97354F.4020801@behnel.de> Lisandro Dalcin, 10.03.2010 02:09: > On 9 March 2010 16:26, Stefan Behnel wrote: >> Lisandro Dalcin, 09.03.2010 20:12: >>> >>> d) Other issue is that C<->Python conversions are outside Cython's >>> control of to_py/from_py converters, and this is IMO inconsistent. >> >> I find this a very convincing argument. >> > > I have a working patch with tests, all the testsuite pass... Worth a > ticket, right? Sure, let's move this to the bug tracker. Stefan From dagss at student.matnat.uio.no Wed Mar 10 08:20:34 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Wed, 10 Mar 2010 08:20:34 +0100 Subject: [Cython] Signal handler question In-Reply-To: <6ce0ac131003091130n174f5925he4afc88e48e4e196@mail.gmail.com> References: <6ce0ac131003091130n174f5925he4afc88e48e4e196@mail.gmail.com> Message-ID: <4B974842.5060103@student.matnat.uio.no> Brian Granger wrote: > Hi, > > A few years ago, Ondrej asked a question about _sig_on/_sig_off being > in Cython itself: > > http://codespeak.net/pipermail/cython-dev/2008-November/003081.html > > William brought up the possibility of moving the relevant code from > Sage to Cython. > > Questions? > > * Has anyone worked on this? > * If not, is it still a possibility? > * Can the relevant code from Sage be re-licensed for Cython? > > With some help, I might be able to work on this. I can't help you, I'll just +1 it, I'd love to see features for signal handling in Cython. The level of integration is worth thinking about though. The simplest thing is probably to just include Sage's stuff in the "Cython standard library" in Cython/Includes (assuming relicensing can be arranged). At least on a longer term, I think we should aim for something like (I'm just throwing it out there, let's wait with discussing the details until someone is ready to tackle it...) try: with cython.trap_signals(): call_my_c_func() except cython.TrappedSignalError as e: cleanup_c_library_state() e.raise_python() # could raise e.g. KeyboardInterrupt Dag Sverre From dagss at student.matnat.uio.no Wed Mar 10 08:34:50 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Wed, 10 Mar 2010 08:34:50 +0100 Subject: [Cython] Standard C pxd conventions In-Reply-To: <4B6F1B62.7010207@behnel.de> References: <4B67F457.3020107@student.matnat.uio.no> <4B68170F.5040304@behnel.de> <4B681DF3.7070802@student.matnat.uio.no> <4B681E8B.8020305@student.matnat.uio.no> <4B68A0A5.6000908@noaa.gov> <92964A85-DC69-4B42-AE2D-1BDA82081764@math.washington.edu> <4B6BFA49.70408@student.matnat.uio.no> <3A27302D-155D-4006-BD37-EE5B3C1DAE9E@math.washington.edu> <4B6F1B62.7010207@behnel.de> Message-ID: <4B974B9A.8030007@student.matnat.uio.no> Stefan Behnel wrote: > Robert Bradshaw, 05.02.2010 12:18: > >> On Feb 5, 2010, at 3:00 AM, Dag Sverre Seljebotn wrote: >> >> >>> There's no difference in principle between, say, stdlib.h and >>> opengl.h. >>> >>> How about just "libc" and "libcpp" then? >>> >> Sure. And we should move all the Python ones to "python." >> > > +1 > > Stefan > > I'm preparing a patch now. I need some help with Cython semantics -- for backwards compatability, is it perfectly safe to leave replace the current Cython/Includes/stdio.pxd with a file containing only from libc.stdio cimport * ? Or is that in any way different from keeping a full copy in Cython/Includes/stdio.pxd? Dag Sverre From robertwb at math.washington.edu Wed Mar 10 08:48:01 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Tue, 9 Mar 2010 23:48:01 -0800 Subject: [Cython] Standard C pxd conventions In-Reply-To: <4B974B9A.8030007@student.matnat.uio.no> References: <4B67F457.3020107@student.matnat.uio.no> <4B68170F.5040304@behnel.de> <4B681DF3.7070802@student.matnat.uio.no> <4B681E8B.8020305@student.matnat.uio.no> <4B68A0A5.6000908@noaa.gov> <92964A85-DC69-4B42-AE2D-1BDA82081764@math.washington.edu> <4B6BFA49.70408@student.matnat.uio.no> <3A27302D-155D-4006-BD37-EE5B3C1DAE9E@math.washington.edu> <4B6F1B62.7010207@behnel.de> <4B974B9A.8030007@student.matnat.uio.no> Message-ID: <36D9BAB0-8A78-4316-A8D3-2A24B4D5FEAD@math.washington.edu> On Mar 9, 2010, at 11:34 PM, Dag Sverre Seljebotn wrote: > Stefan Behnel wrote: >> Robert Bradshaw, 05.02.2010 12:18: >> >>> On Feb 5, 2010, at 3:00 AM, Dag Sverre Seljebotn wrote: >>> >>> >>>> There's no difference in principle between, say, stdlib.h and >>>> opengl.h. >>>> >>>> How about just "libc" and "libcpp" then? >>>> >>> Sure. And we should move all the Python ones to "python." >>> >> >> +1 >> >> Stefan >> >> > I'm preparing a patch now. Thanks. > I need some help with Cython semantics -- for > backwards compatability, is it perfectly safe to leave replace the > current Cython/Includes/stdio.pxd with a file containing only > > from libc.stdio cimport * > > ? Or is that in any way different from keeping a full copy in > Cython/Includes/stdio.pxd? That should be exactly the same (and totally safe). You do know about hg rename, right? - Robert From greg.ewing at canterbury.ac.nz Wed Mar 10 10:51:03 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Wed, 10 Mar 2010 22:51:03 +1300 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <1409068A-E7F8-4087-B427-57780DDA05A2@math.washington.edu> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B96E15C.9040707@canterbury.ac.nz> <1409068A-E7F8-4087-B427-57780DDA05A2@math.washington.edu> Message-ID: <4B976B87.7050406@canterbury.ac.nz> Robert Bradshaw wrote: > The issue here is speed. ... Hence a > special class with a cdef method. Ah, okay. Although if you're doing this from cython, using the C callback API directly would be even faster. I suppose you're just trying to create something a bit more convenient or type-safe? -- Greg From dagss at student.matnat.uio.no Wed Mar 10 10:47:36 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Wed, 10 Mar 2010 10:47:36 +0100 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B976B87.7050406@canterbury.ac.nz> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B96E15C.9040707@canterbury.ac.nz> <1409068A-E7F8-4087-B427-57780DDA05A2@math.washington.edu> <4B976B87.7050406@canterbury.ac.nz> Message-ID: <4B976AB8.2040705@student.matnat.uio.no> Greg Ewing wrote: > Robert Bradshaw wrote: > > >> The issue here is speed. ... Hence a >> special class with a cdef method. >> > > Ah, okay. Although if you're doing this from cython, using > the C callback API directly would be even faster. I suppose > you're just trying to create something a bit more convenient > or type-safe? > Ondrej would have to answer what his exact usecase is, but making sweeping generalizations: - Scientific Python/Cython programmers need to call C libraries with fast callbacks - Scientific Python/Cython programmers don't know much C (or any C at all, in many cases), the C function pointer notation is frightening, and the C libraries often have APIs which are very low-level and need a higher-level wrapper anyway. So there's quite a demand for nice Cython libraries which are fast callable from Cython and provides a "Python-like" API while being fast. I think you'd be surprised how little C some Cython coders know; and that's also one of Cython's target audiences. Dag Sverre From dagss at student.matnat.uio.no Wed Mar 10 12:57:02 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Wed, 10 Mar 2010 12:57:02 +0100 Subject: [Cython] Namespace for CPython API? Message-ID: <4B97890E.5060407@student.matnat.uio.no> It was agreed earlier to put pxds for the CPython API into the namespace "python". However I'm wondering if "cpython" is more appropriate, given that the CPython API is very CPython-specific and has very little to do with Python as a language. "python" seems a little to generic to me, while "cpython" seems specific and to the point. Thoughts? Dag Sverre From dagss at student.matnat.uio.no Wed Mar 10 13:00:20 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Wed, 10 Mar 2010 13:00:20 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B97890E.5060407@student.matnat.uio.no> References: <4B97890E.5060407@student.matnat.uio.no> Message-ID: <4B9789D4.9090604@student.matnat.uio.no> Dag Sverre Seljebotn wrote: > It was agreed earlier to put pxds for the CPython API into the > namespace "python". However I'm wondering if "cpython" is more > appropriate, given that the CPython API is very CPython-specific and > has very little to do with Python as a language. "python" seems a > little to generic to me, while "cpython" seems specific and to the point. > > Thoughts? Note that for backwards compatability, "python" would be available as well for some time (at least until we start deprecation cycles and cleanups). However one could only use cpython for accessing specific pxd's, as in from cpython.bool cimport ... Dag Sverre From stefan_ml at behnel.de Wed Mar 10 14:04:30 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 10 Mar 2010 14:04:30 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B97890E.5060407@student.matnat.uio.no> References: <4B97890E.5060407@student.matnat.uio.no> Message-ID: <4B9798DE.2030909@behnel.de> Dag Sverre Seljebotn, 10.03.2010 12:57: > It was agreed earlier to put pxds for the CPython API into the namespace > "python". However I'm wondering if "cpython" is more appropriate, given > that the CPython API is very CPython-specific and has very little to do > with Python as a language. "python" seems a little to generic to me, > while "cpython" seems specific and to the point. I think that Cython is so CPython-specific itself that this is a non-issue. Stefan From dagss at student.matnat.uio.no Wed Mar 10 14:33:22 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Wed, 10 Mar 2010 14:33:22 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B9798DE.2030909@behnel.de> References: <4B97890E.5060407@student.matnat.uio.no> <4B9798DE.2030909@behnel.de> Message-ID: <4B979FA2.60507@student.matnat.uio.no> Stefan Behnel wrote: > Dag Sverre Seljebotn, 10.03.2010 12:57: > >> It was agreed earlier to put pxds for the CPython API into the namespace >> "python". However I'm wondering if "cpython" is more appropriate, given >> that the CPython API is very CPython-specific and has very little to do >> with Python as a language. "python" seems a little to generic to me, >> while "cpython" seems specific and to the point. >> > > I think that Cython is so CPython-specific itself that this is a non-issue. > It's more a question of friendliness and readability -- I find "python" non-obvious/confusing myself in this context... (Also, theoretically, it could, at least in the future, be possible to have a .py file with pure Python mode Cython code that can both run under Jython or under very heavily optimized Cython/CPython API, using some if-tests etc.) Dag Sverre From jonovik at gmail.com Wed Mar 10 15:30:12 2010 From: jonovik at gmail.com (Jon Olav Vik) Date: Wed, 10 Mar 2010 14:30:12 +0000 (UTC) Subject: [Cython] Library for ODEs with Cython bindings? References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> Message-ID: Dag Sverre Seljebotn writes: > On the backend side, if you > need to feed it to a C library, then assuming the C library is a good > citizen and takes contexts, simply do Could someone please point me to a link on what a "context" is, in this context? From robertwb at math.washington.edu Wed Mar 10 19:38:04 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Wed, 10 Mar 2010 10:38:04 -0800 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B97890E.5060407@student.matnat.uio.no> References: <4B97890E.5060407@student.matnat.uio.no> Message-ID: On Mar 10, 2010, at 3:57 AM, Dag Sverre Seljebotn wrote: > It was agreed earlier to put pxds for the CPython API into the > namespace > "python". However I'm wondering if "cpython" is more appropriate, > given > that the CPython API is very CPython-specific and has very little to > do > with Python as a language. "python" seems a little to generic to me, > while "cpython" seems specific and to the point. I like that idea. It makes it more clear that we're really talking about the C/Python API here. - Robert From robertwb at math.washington.edu Wed Mar 10 19:46:14 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Wed, 10 Mar 2010 10:46:14 -0800 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: Message-ID: On Mar 9, 2010, at 11:12 AM, Lisandro Dalcin wrote: > We could get rid of structmember.h by using the transform+property > machinery Dag implemented long ago > > So far, Robert has two concerns: > > 1) Dag's code had a few issues. > > 2) The generated code is less readable and bigger. > > Regarding (1), I'm working on it... the issues are minor, basically > handle 'readonly' and auto-generate a docstring like "attribute 'ATTR' > of 'TYPE' objects" (BTW, should this be on by default?). > > Regarding (2), I can certainly trade readability and code size, for > many reasons: > > a) The header "structmember.h" is some sort of private stuff > b) It is not even #include'd by default from Python.h, > c) It is helpful for hand-written C code, but easy to get rid of in > code generators like Cython. > d) A big offence in that headers is that some definitions T_XXX are > not properly namespace-protected with a Py_ prefix, then they can > conflict with user code (as posted today in cython-users). > d) Other issue is that C<->Python conversions are outside Cython's > control of to_py/from_py converters, and this is IMO inconsistent. > f) Finally, if readability and code size are so important in this > case, we could latter provide a better implementation that does not > rely on a tree transform. > > PS: Actually, I do not care too much about readability and code size > in this case; Cython has already abandoned other readable, small-sized > Python C-API idioms (e.g. argument parsing). Well, there are significant time savings in the argument parsing code. In any case, I agree the pros of getting rid of structmember.h outweigh the cons, so +1 to doing it. - Robert From ondrej at certik.cz Wed Mar 10 20:26:40 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 10 Mar 2010 11:26:40 -0800 Subject: [Cython] Library for ODEs with Cython bindings? In-Reply-To: <4B976AB8.2040705@student.matnat.uio.no> References: <4B8FA20F.4010309@student.matnat.uio.no> <85b5c3131003081652s248d1ec7k50f83b218a7d7b1d@mail.gmail.com> <4B96084E.60209@student.matnat.uio.no> <85b5c3131003090912k4c8a9692ofdb63f7593034248@mail.gmail.com> <4B96E15C.9040707@canterbury.ac.nz> <1409068A-E7F8-4087-B427-57780DDA05A2@math.washington.edu> <4B976B87.7050406@canterbury.ac.nz> <4B976AB8.2040705@student.matnat.uio.no> Message-ID: <85b5c3131003101126x70137ccdoa673530d46245dfa@mail.gmail.com> On Wed, Mar 10, 2010 at 1:47 AM, Dag Sverre Seljebotn wrote: > Greg Ewing wrote: >> Robert Bradshaw wrote: >> >> >>> The issue here is speed. ... Hence a >>> special class with a cdef method. >>> >> >> Ah, okay. Although if you're doing this from cython, using >> the C callback API directly would be even faster. I suppose >> you're just trying to create something a bit more convenient >> or type-safe? >> > Ondrej would have to answer what his exact usecase is, but making > sweeping generalizations: > > ?- Scientific Python/Cython programmers need to call C libraries with > fast callbacks > ?- Scientific Python/Cython programmers don't know much C (or any C at > all, in many cases), the C function pointer notation is frightening, and > the C libraries often have APIs which are very low-level and need a > higher-level wrapper anyway. > > So there's quite a demand for nice Cython libraries which are fast > callable from Cython and provides a "Python-like" API while being fast. > I think you'd be surprised how little C some Cython coders know; and > that's also one of Cython's target audiences. I want: * to write Cython callbacks that are as fast as pure C++ * if possible, also provide python callbacks, which will be way slower, but it will be nice for experimentation and prototyping Ondrej From stefan_ml at behnel.de Wed Mar 10 20:39:44 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 10 Mar 2010 20:39:44 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: References: <4B97890E.5060407@student.matnat.uio.no> Message-ID: <4B97F580.7040904@behnel.de> Robert Bradshaw, 10.03.2010 19:38: > On Mar 10, 2010, at 3:57 AM, Dag Sverre Seljebotn wrote: > >> It was agreed earlier to put pxds for the CPython API into the >> namespace >> "python". However I'm wondering if "cpython" is more appropriate, >> given >> that the CPython API is very CPython-specific and has very little to >> do >> with Python as a language. "python" seems a little to generic to me, >> while "cpython" seems specific and to the point. > > I like that idea. It makes it more clear that we're really talking > about the C/Python API here. Fine with me. Stefan From dsurviver at gmail.com Wed Mar 10 20:44:08 2010 From: dsurviver at gmail.com (Danilo Freitas) Date: Wed, 10 Mar 2010 16:44:08 -0300 Subject: [Cython] .pxd for some STL classes Message-ID: <45239151003101144g18c3c3dchd4710cfb05614e04@mail.gmail.com> I wrote some .pxd for use. They are on Cython/Includes/libcpp (http://hg.cython.org/cython-devel/rev/d7b27e929888) There are only 5 wrapped classes (queue, deque, stack, vector and set). Also there is the struct pair (here, declared as a class), that aux the STL. There aren't secific tests for it yet, but we'll have soon. Also, there are still some issues for wrapping them (like template declarations inside a templated class). Please report if you find some bug. -- - Danilo Freitas From dalcinl at gmail.com Wed Mar 10 21:17:11 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 10 Mar 2010 17:17:11 -0300 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: Message-ID: On 10 March 2010 15:46, Robert Bradshaw wrote: > On Mar 9, 2010, at 11:12 AM, Lisandro Dalcin wrote: > >> We could get rid of structmember.h by using the transform+property >> machinery Dag implemented long ago >> >> So far, Robert has two concerns: >> >> 1) Dag's code had a few issues. >> >> 2) The generated code is less readable and bigger. >> >> Regarding (1), I'm working on it... the issues are minor, basically >> handle 'readonly' and auto-generate a docstring like "attribute 'ATTR' >> of 'TYPE' objects" (BTW, should this be on by default?). >> >> Regarding (2), I can certainly trade readability and code size, for >> many reasons: >> >> a) The header "structmember.h" is some sort of private stuff >> b) It is not even #include'd by default from Python.h, >> c) It is helpful for hand-written C code, but easy to get rid of in >> code generators like Cython. >> d) A big offence in that headers is that some definitions T_XXX are >> not properly namespace-protected with a Py_ prefix, then they can >> conflict with user code (as posted today in cython-users). >> d) Other issue is that C<->Python conversions are outside Cython's >> control of to_py/from_py converters, and this is IMO inconsistent. >> f) Finally, if readability and code size are so important in this >> case, we could latter provide a better implementation that does not >> rely on a tree transform. >> >> PS: Actually, I do not care too much about readability and code size >> in this case; Cython has already abandoned other readable, small-sized >> Python C-API idioms (e.g. argument parsing). > > Well, there are significant time savings in the argument parsing code. > > In any case, I agree the pros of getting rid of structmember.h > outweigh the cons, so +1 to doing it. > OK.. BTW, Any suggestions about docstring generation? I mean: 1) Should the docstring be generated inconditionally, or under the control of 'embedsignatures' ? 2) The actual contents of the docstrig: currently it is: "attribute 'NAME' for 'CLASS' objects"... But it could also be "'TYPE' 'NAME'", where 'TYPE' is the C type of the member (so the rendering would be like the one for function arguments) -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From ondrej at certik.cz Thu Mar 11 00:49:13 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 10 Mar 2010 15:49:13 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> Message-ID: <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> On Wed, Mar 3, 2010 at 6:23 PM, Lisandro Dalcin wrote: > On 3 March 2010 23:08, Ondrej Certik wrote: >>> >>> Am I being clear enough? >> >> Yes, that is clear to me, thanks. >> > > OK. Once you have something working, let's polish it and ask other for > opinions, then push to cython-devel. If I go the global Python() class route, I would need to generate the Python() class automatically, since the list of my Cython functions can be quite long, so I don't want to keep updating the Python class interface by hand. So I am probably going to take the combination of both approaches that you suggested: 1) implement the global Python() class, that takes care of execution contexts, initializing python and the Cython API and has the following methods: void cmd(const char*) // runs a Python command in the (possibly local) context/namespace void insert_object(const char*, PyObject *) // inserts a Python object into the context/namespace PyObject *get_object(const char*) // gets a Python object from the context 2) the helper functions for doing the actual conversion to and from Python like: static PyObject *(*c2py_CooMatrix)(struct CooMatrix *); static PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *); static PyObject *(*c2py_int)(int); static int (*py2c_int)(PyObject *); static char *(*py2c_str)(PyObject *); static double (*py2c_double)(PyObject *); static PyObject *(*c2numpy_int)(int *, int); static PyObject *(*c2numpy_int_inplace)(int *, int); static PyObject *(*c2numpy_double)(double *, int); static PyObject *(*c2numpy_double_inplace)(double *, int); static void (*numpy2c_int_inplace)(PyObject *, int **, int *); static void (*numpy2c_double_inplace)(PyObject *, double **, int *); .... [way more] Would be global and they would work out of the box in any C++ file, as long as you instantiate the Python() class once anywhere. > >> >> That is already implemented, here is my exec function: >> >> cdef api object run_cmd(const_char_p text, object namespace): >> ? ?try: >> ? ? ? ?verbose = namespace.get("verbose") >> ? ? ? ?if verbose: >> ? ? ? ? ? ?print "got a text:", text >> ? ? ? ?if verbose: >> ? ? ? ? ? ?print "evaluting in the namespace:" >> ? ? ? ? ? ?print namespace >> ? ? ? ?code = compile(text, "", "exec") >> ? ? ? ?eval(code, {}, namespace) >> ? ? ? ?if verbose: >> ? ? ? ? ? ?print "new namespace:" >> ? ? ? ? ? ?print namespace >> ? ? ? ?return namespace >> ? ?except SystemExit, e: >> ? ? ? ?try: >> ? ? ? ? ? ?exit_code = int(e) >> ? ? ? ?except: >> ? ? ? ? ? ?exit_code = -1 >> ? ? ? ?exit(exit_code) >> ? ?except: >> ? ? ? ?etype, value, tb = sys.exc_info() >> ? ? ? ?s = "".join(traceback.format_exception(etype, value, tb)) >> ? ? ? ?s = "Exception raised in the Python code:\n" + s >> ? ? ? ?throw_exception(s) >> > > Mmm, are you sure you want to handle SystemExit by actually calling > exit(e)? ?Is this 'exit' actually 'sys.exit' ? In such case, why just > not use 'raise' to re-raise the exception? Yes, maybe. I haven't thought about this much. > >> >> Ok, great! i was worried if there were some issues, but it seems >> everything will work very smoothly and the C++ guys in my group will >> not even notice that they are using Python. :) >> > > But they should notice, just to have a chance to fall in love with the beast :-) :) Ondrej From ondrej at certik.cz Thu Mar 11 01:01:10 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 10 Mar 2010 16:01:10 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> Message-ID: <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> On Wed, Mar 10, 2010 at 3:49 PM, Ondrej Certik wrote: > On Wed, Mar 3, 2010 at 6:23 PM, Lisandro Dalcin wrote: >> On 3 March 2010 23:08, Ondrej Certik wrote: >>>> >>>> Am I being clear enough? >>> >>> Yes, that is clear to me, thanks. >>> >> >> OK. Once you have something working, let's polish it and ask other for >> opinions, then push to cython-devel. > > If I go the global Python() class route, I would need to generate the > Python() class automatically, since the list of my Cython functions > can be quite long, so I don't want to keep updating the Python class > interface by hand. > > So I am probably going to take the combination of both approaches that > you suggested: > > 1) implement the global Python() class, that takes care of execution > contexts, initializing python and the Cython API and has the following > methods: > > void cmd(const char*) // runs a Python command in the (possibly local) > context/namespace > void insert_object(const char*, PyObject *) // inserts a Python object > into the context/namespace > PyObject *get_object(const char*) // gets a Python object from the context > > 2) the helper functions for doing the actual conversion to and from Python like: > > static PyObject *(*c2py_CooMatrix)(struct CooMatrix *); > static PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *); > static PyObject *(*c2py_int)(int); > static int (*py2c_int)(PyObject *); > static char *(*py2c_str)(PyObject *); > static double (*py2c_double)(PyObject *); > static PyObject *(*c2numpy_int)(int *, int); > static PyObject *(*c2numpy_int_inplace)(int *, int); > static PyObject *(*c2numpy_double)(double *, int); > static PyObject *(*c2numpy_double_inplace)(double *, int); > static void (*numpy2c_int_inplace)(PyObject *, int **, int *); > static void (*numpy2c_double_inplace)(PyObject *, double **, int *); > .... [way more] Here is how it would be used: in the main.cpp: #include "python_api.h" int main(int argc, char* argv[]) { python = Python(argc, argv); } and then somewhere else in the C++ program, like linsystem.cpp: #include "python_api.h" ... python.cmd("print 'ok'"); python.insert_object("i", c2py_int(5)); python.cmd("i += 5"); int i = py2c_int(python.get_object("i")); // i == 10 now ... This assumes that "python" is a global variable, that is defined in my project only, e.g. then the user can define it locally for each class that needs to access python. And then, when I expose more of my functionality either from C or from Python, I just add more c2py_* and py2c_* methods in the .pyx file and that's it. Ondrej From ondrej at certik.cz Thu Mar 11 01:03:23 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 10 Mar 2010 16:03:23 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> Message-ID: <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> On Wed, Mar 10, 2010 at 4:01 PM, Ondrej Certik wrote: > On Wed, Mar 10, 2010 at 3:49 PM, Ondrej Certik wrote: >> On Wed, Mar 3, 2010 at 6:23 PM, Lisandro Dalcin wrote: >>> On 3 March 2010 23:08, Ondrej Certik wrote: >>>>> >>>>> Am I being clear enough? >>>> >>>> Yes, that is clear to me, thanks. >>>> >>> >>> OK. Once you have something working, let's polish it and ask other for >>> opinions, then push to cython-devel. >> >> If I go the global Python() class route, I would need to generate the >> Python() class automatically, since the list of my Cython functions >> can be quite long, so I don't want to keep updating the Python class >> interface by hand. >> >> So I am probably going to take the combination of both approaches that >> you suggested: >> >> 1) implement the global Python() class, that takes care of execution >> contexts, initializing python and the Cython API and has the following >> methods: >> >> void cmd(const char*) // runs a Python command in the (possibly local) >> context/namespace >> void insert_object(const char*, PyObject *) // inserts a Python object >> into the context/namespace >> PyObject *get_object(const char*) // gets a Python object from the context >> >> 2) the helper functions for doing the actual conversion to and from Python like: >> >> static PyObject *(*c2py_CooMatrix)(struct CooMatrix *); >> static PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *); >> static PyObject *(*c2py_int)(int); >> static int (*py2c_int)(PyObject *); >> static char *(*py2c_str)(PyObject *); >> static double (*py2c_double)(PyObject *); >> static PyObject *(*c2numpy_int)(int *, int); >> static PyObject *(*c2numpy_int_inplace)(int *, int); >> static PyObject *(*c2numpy_double)(double *, int); >> static PyObject *(*c2numpy_double_inplace)(double *, int); >> static void (*numpy2c_int_inplace)(PyObject *, int **, int *); >> static void (*numpy2c_double_inplace)(PyObject *, double **, int *); >> .... [way more] > > Here is how it would be used: > > in the main.cpp: > > #include "python_api.h" > > int main(int argc, char* argv[]) > { > ? ?python = Python(argc, argv); > } > > and then somewhere else in the C++ program, like linsystem.cpp: > > #include "python_api.h" > > ... > python.cmd("print 'ok'"); > python.insert_object("i", c2py_int(5)); > python.cmd("i += 5"); > int i = py2c_int(python.get_object("i")); > // i == 10 now > ... > > > > This assumes that "python" is a global variable, that is defined in my > project only, e.g. then the user can define it locally for each class > that needs to access python. And then, when I expose more of my > functionality either from C or from Python, I just add more c2py_* and > py2c_* methods in the .pyx file and that's it. After I get this working, and test that it indeed works, I'll try to put it into cython somehow, so that anyone can use it. One of my friends told me that he prefers Lua because it's super easy to use from C/C++. Currently unfortunately Python is way more difficult to use, but with my approach above, Python will be the same easy to use. Ondrej From dalcinl at gmail.com Thu Mar 11 03:23:07 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 10 Mar 2010 23:23:07 -0300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031528gdc143a6s5e046eb87dd24f5@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> Message-ID: On 10 March 2010 21:03, Ondrej Certik wrote: > > After I get this working, and test that it indeed works, I'll try to > put it into cython somehow, so that anyone can use it. > Or start a new project. IMO, such a tool has a broader scope. BTW, you can get some ideas from here http://www.boost.org/doc/libs/1_42_0/libs/python/doc/tutorial/doc/html/python/embedding.html > One of my friends told me that he prefers Lua because it's super easy > to use from C/C++. Really? Looking here http://www.lua.org/pil/24.1.html, I cannot buy such assertion. Is your friend talking about the stack-based C<->Lua flow? -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From ondrej at certik.cz Thu Mar 11 07:38:58 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 10 Mar 2010 22:38:58 -0800 Subject: [Cython] GCC warnings In-Reply-To: References: Message-ID: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> On Tue, Mar 9, 2010 at 5:03 PM, Lisandro Dalcin wrote: > Folks, I've been doing some housekeeping work in the testsuite to make > GCC warnings under control. In the process, I've added a few hacks to > remove warnings from unused stuff coming from NumPy headers. My fixes > work for NumPy 1.2.x and above, but I've not tested with older > versions... If anyone is running Cython testsuite with an older NumPy > and have any trouble, let me know and I'll fix things. This is the output of my tests: ondrej at crow:~/repos/cython-devel$ python runtests.py Running tests against Cython 0.12.1 Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) [GCC 4.4.1] warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:90:5: Unraisable exception in function 'refnanny.GOTREF'. warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:103:5: Unraisable exception in function 'refnanny.GIVEREF_and_report'. warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:129:5: Unraisable exception in function 'refnanny.FinishContext'. specmethdocstring.c:424: warning: ?__pyx_doc_17specmethdocstring_1C___init__? defined but not used specmethdocstring.c:446: warning: ?__pyx_doc_17specmethdocstring_1C_3foo___get__? defined but not used specmethdocstring.c:465: warning: ?__pyx_doc_17specmethdocstring_1C_3foo___set__? defined but not used specmethdocstring.cpp:424: warning: ?__pyx_doc_17specmethdocstring_1C___init__? defined but not used specmethdocstring.cpp:446: warning: ?__pyx_doc_17specmethdocstring_1C_3foo___get__? defined but not used specmethdocstring.cpp:465: warning: ?__pyx_doc_17specmethdocstring_1C_3foo___set__? defined but not used autotestdict.c:966: warning: ?__pyx_doc_12autotestdict_11MyCdefClass___cinit__? defined but not used autotestdict.c:988: warning: ?__pyx_doc_12autotestdict_11MyCdefClass___dealloc__? defined but not used autotestdict.c:1004: warning: ?__pyx_doc_12autotestdict_11MyCdefClass___richcmp__? defined but not used autotestdict.c:1024: warning: ?__pyx_doc_12autotestdict_11MyCdefClass___nonzero__? defined but not used autotestdict.cpp:966: warning: ?__pyx_doc_12autotestdict_11MyCdefClass___cinit__? defined but not used autotestdict.cpp:988: warning: ?__pyx_doc_12autotestdict_11MyCdefClass___dealloc__? defined but not used autotestdict.cpp:1004: warning: ?__pyx_doc_12autotestdict_11MyCdefClass___richcmp__? defined but not used autotestdict.cpp:1024: warning: ?__pyx_doc_12autotestdict_11MyCdefClass___nonzero__? defined but not used extpropertyref.c:416: warning: ?__pyx_doc_14extpropertyref_4Spam_4eggs___get__? defined but not used extpropertyref.cpp:416: warning: ?__pyx_doc_14extpropertyref_4Spam_4eggs___get__? defined but not used ---------------------------------------------------------------------- Ran 3206 tests in 478.458s OK ondrej at crow:~/repos/cython-devel$ Is that the way to execute them? What are the warnings in there, those are innocent? Ondrej From ondrej at certik.cz Thu Mar 11 08:15:15 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 10 Mar 2010 23:15:15 -0800 Subject: [Cython] new C++ syntax error Message-ID: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> Hi, I am trying the new C++ syntax. I got the following error (all files attached): $ cython _hermes_common.pyx Traceback (most recent call last): File "/home/ondrej/usr/bin/cython", line 8, in main(command_line = 1) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 749, in main result = compile(sources, options) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 724, in compile return compile_multiple(source, options) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 696, in compile_multiple result = run_pipeline(source, options) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 567, in run_pipeline err, enddata = context.run_pipeline(pipeline, source) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 224, in run_pipeline data = phase(data) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 524, in parse scope = context.find_module(full_module_name, pos = initial_pos, need_pxd = 0) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 297, in find_module err, result = self.process_pxd(source_desc, scope, module_name) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 209, in process_pxd result = self.run_pipeline(pipeline, source_desc) File "/home/ondrej/usr/lib/python/Cython/Compiler/Main.py", line 224, in run_pipeline data = phase(data) File "/home/ondrej/usr/lib/python/Cython/Compiler/ParseTreeTransforms.py", line 960, in __call__ return super(AnalyseDeclarationsTransform, self).__call__(root) File "Visitor.py", line 276, in Cython.Compiler.Visitor.CythonTransform.__call__ (Cython/Compiler/Visitor.c:4972) File "Visitor.py", line 259, in Cython.Compiler.Visitor.VisitorTransform.__call__ (Cython/Compiler/Visitor.c:4732) File "Visitor.py", line 28, in Cython.Compiler.Visitor.BasicVisitor.visit (Cython/Compiler/Visitor.c:1187) File "/home/ondrej/usr/lib/python/Cython/Compiler/ParseTreeTransforms.py", line 968, in visit_ModuleNode node.analyse_declarations(self.env_stack[-1]) File "/home/ondrej/usr/lib/python/Cython/Compiler/ModuleNode.py", line 63, in analyse_declarations self.body.analyse_declarations(env) File "/home/ondrej/usr/lib/python/Cython/Compiler/Nodes.py", line 341, in analyse_declarations stat.analyse_declarations(env) File "/home/ondrej/usr/lib/python/Cython/Compiler/Nodes.py", line 396, in analyse_declarations self.body.analyse_declarations(env) File "/home/ondrej/usr/lib/python/Cython/Compiler/Nodes.py", line 341, in analyse_declarations stat.analyse_declarations(env) File "/home/ondrej/usr/lib/python/Cython/Compiler/Nodes.py", line 1029, in analyse_declarations if len(self.attributes) != 0: TypeError: object of type 'NoneType' has no len() It's some mistake in the file, but I don't know where, when cython exits with an exception. Ondrej -------------- next part -------------- A non-text attachment was scrubbed... Name: _hermes_common.pxd Type: application/octet-stream Size: 2669 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100310/5481bd3d/attachment.obj -------------- next part -------------- A non-text attachment was scrubbed... Name: _hermes_common.pyx Type: application/octet-stream Size: 10763 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100310/5481bd3d/attachment-0001.obj From stefan_ml at behnel.de Thu Mar 11 08:18:46 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 11 Mar 2010 08:18:46 +0100 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: Message-ID: <4B989956.7040601@behnel.de> Lisandro Dalcin, 10.03.2010 21:17: >>> We could get rid of structmember.h by using the transform+property >>> machinery Dag implemented long ago > > Any suggestions about docstring generation? I mean: > > 2) The actual contents of the docstrig: currently it is: "attribute > 'NAME' for 'CLASS' objects"... But it could also be "'TYPE' 'NAME'", > where 'TYPE' is the C type of the member (so the rendering would be > like the one for function arguments) First of all, there doesn't have to be a docstring. CPython won't generate one for you either, not for '@property' and not for class/instance attributes. That being said, if we already have to generate a descriptor, and since you currently can't assign a docstring to a class field at all, I wouldn't mind adding some information to it that provides at least the (C-)type of the value that is returned. Given that you can't copy/move this kind of property to a different class, providing the names of the attribute and its defining type doesn't sound unreasonable (I assume the class name would be the current type, not the base type the really defined it). If we ever support default values, that should go in as well. So I could imagine providing this: "CTYPE F.Q.CLASSNAME.ATTRNAME[ = DEFAULT]" ([] meaning 'optional') which basically mimics the complete definition. I wouldn't mind letting the class name out of this, though, as the only way to get to the property docstring is through the class or instance, so it doesn't really add anything and makes the output look more verbose. So IMHO it would be cleaner to stick to this: "CTYPE ATTRNAME[ = DEFAULT]" An alternative is the argument type annotation syntax in Py3: "ATTRNAME : CTYPE [ = DEFAULT]" I have a preference for the last one, as it looks very readable: name first, potentially lengthy C definition follows after a clear separator, arbitrary default value last, after another clear separator. And since this is meant to be read in Python space, this is the syntax that Python users will be more used to. It also makes it easy to parse and to prepend the FQCN if people ever feel the need to process the docstring in their code. > 1) Should the docstring be generated inconditionally, or under the > control of 'embedsignatures' ? If we provide the definition as docstring, requiring 'embedsignatures' sounds like the right thing to do. Note that the above may be considered leaking implementation details (C type and default value) and the embedded string also implies a a tiny bit of overhead for the module, so some users may want to disable it. Stefan From stefan_ml at behnel.de Thu Mar 11 08:23:38 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 11 Mar 2010 08:23:38 +0100 Subject: [Cython] new C++ syntax error In-Reply-To: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> Message-ID: <4B989A7A.50901@behnel.de> Ondrej Certik, 11.03.2010 08:15: > I am trying the new C++ syntax. I got the following error (all files attached): > > $ cython _hermes_common.pyx > [...] > 396, in analyse_declarations > self.body.analyse_declarations(env) > File "/home/ondrej/usr/lib/python/Cython/Compiler/Nodes.py", line > 341, in analyse_declarations > stat.analyse_declarations(env) > File "/home/ondrej/usr/lib/python/Cython/Compiler/Nodes.py", line > 1029, in analyse_declarations > if len(self.attributes) != 0: > TypeError: object of type 'NoneType' has no len() > > It's some mistake in the file, but I don't know where, when cython > exits with an exception. Try setting "debug_no_exception_intercept" to False in Cython/Compiler/DebugFlags.py. It looks like someone accidentally enabled it. That will hopefully get you a better hint on what's happening. Stefan From ondrej at certik.cz Thu Mar 11 08:16:03 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 10 Mar 2010 23:16:03 -0800 Subject: [Cython] new C++ syntax error In-Reply-To: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> Message-ID: <85b5c3131003102316m6156d94aue771bdd65115ca46@mail.gmail.com> On Wed, Mar 10, 2010 at 11:15 PM, Ondrej Certik wrote: > Hi, > > I am trying the new C++ syntax. I got the following error (all files attached): And I am using the latest cython-devel, this is my latest patch: changeset: 3080:982665cb1a01 tag: tip user: Lisandro Dalcin date: Wed Mar 10 17:03:40 2010 -0300 summary: casting to typedef pointer/array types (ticket #518) Ondrej From craigcitro at gmail.com Thu Mar 11 08:30:02 2010 From: craigcitro at gmail.com (Craig Citro) Date: Wed, 10 Mar 2010 23:30:02 -0800 Subject: [Cython] new C++ syntax error In-Reply-To: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> Message-ID: Hi Ondrej, > I am trying the new C++ syntax. I got the following error (all files attached): > > $ cython _hermes_common.pyx > ... > ? ?if len(self.attributes) != 0: > TypeError: object of type 'NoneType' has no len() > Yep, that's a bug. I'm pretty sure you just want to change that line in Cython/Compiler/Nodes.py from "if len(self.attributes) != 0:" to just "if self.attributes:" and you'll be good. Amusingly, I tried to grab the source files you attached and test this -- and hit a different bug (a problem in parsing). I don't know why -- you just posted and confirmed that we're using the same version (before I even asked!). Is there any chance your source files are slightly out of sync with the ones you posted? (Maybe forgot to save changes in your editor before posting?) Has anyone else tried and hit the same bug, or is my setup wonky? In any event, there are definitely some bugs -- I'm currently trying to get type inference and the Sage library to get along, but I'll look at this soon if no one beats me to it. -cc From stefan_ml at behnel.de Thu Mar 11 08:44:15 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 11 Mar 2010 08:44:15 +0100 Subject: [Cython] new C++ syntax error In-Reply-To: References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> Message-ID: <4B989F4F.9030606@behnel.de> Craig Citro, 11.03.2010 08:30: > Amusingly, I tried to grab the source files you attached and test this > -- and hit a different bug (a problem in parsing). I don't know why -- Did you run "python setup.py build_ext -i" locally? The compiled Cython parser is not automatically rebuilt on source changes. Try "make clean". Stefan From craigcitro at gmail.com Thu Mar 11 09:00:53 2010 From: craigcitro at gmail.com (Craig Citro) Date: Thu, 11 Mar 2010 00:00:53 -0800 Subject: [Cython] new C++ syntax error In-Reply-To: <4B989F4F.9030606@behnel.de> References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> <4B989F4F.9030606@behnel.de> Message-ID: > Did you run "python setup.py build_ext -i" locally? The compiled Cython > parser is not automatically rebuilt on source changes. Try "make clean". > Oh, it was just PEBKAC -- I had already made the fix I suggested to Ondrej in my local copy of Cython before I even downloaded the example. :) Ondrej -- even with that fix, you hit some other issues. I'm not going to look at this now -- hopefully tomorrow night or Friday, if no one beats me to it. -cc From craigcitro at gmail.com Thu Mar 11 08:57:53 2010 From: craigcitro at gmail.com (Craig Citro) Date: Wed, 10 Mar 2010 23:57:53 -0800 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: Message-ID: Hi Lisandro, > BTW, Any suggestions about docstring generation? I mean: > So I'm going to jump in and make a few comments on this thread without actually reading the whole thing first (always a dangerous move) ... I just want to give you the heads-up that support for docstrings on Cython properties is currently in iffy shape. I ended up wrestling with this a little bit while trying to get Cython to build Sage -- I'm planning on filing a handful of trac tickets (and hopefully working up some fixes) as soon as I've got an 0.13 alpha that can build Sage with type inferencing turned on. I think that no one's really tried out docstrings with properties before: they don't really play together well. Here are two big issues I've hit so far ... let's say we've got a class Foo with property my_prop: * I don't know how to get my hands on the docstring for Foo.my_prop.__get__ or Foo.my_prop.__set__ at all. I made a cursory glance through the C code, and wasn't sure that it was correctly getting attached anywhere, but it does at least appear in the C file. Actually, that's not completely true -- if the autotestdict is turned on, then the docstring comes up in mod.__test__. This was actually broken until a day or two ago (fixing it is how I ran into this whole mess in the first place). But it clearly should get attached to Foo somewhere. The fact that I couldn't find it another way is also why the test that I submitted goes via the __test__ dict's keys, instead of doing something more sane. (I plan to clean up the test once I fix the issues with properties.) * If you try to give a docstring to Foo.my_prop.__del__, Cython crashes. The underlying issue is this: we currently wrap the __set__ and __del__ methods into the "setter" part of a PyGetSetDef, so it's not clear where to attach the __del__ docstring -- or, at least, where to attach *both* a __set__ and __del__ docstring. (I don't think this is insurmountable, but we might need to think harder.) I was planning on starting a new thread once I had some more concrete suggestions for fixes. For comparison, Python attaches the docstrings to the Foo.my_prop.fget, fset, and fdel methods. (The Foo.my_prop.__get__ and whatnot all have generic docstrings.) -cc From dalcinl at gmail.com Thu Mar 11 15:29:29 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 11 Mar 2010 11:29:29 -0300 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: Message-ID: On 11 March 2010 04:57, Craig Citro wrote: > Hi Lisandro, > >> BTW, Any suggestions about docstring generation? I mean: >> > > So I'm going to jump in and make a few comments on this thread without > actually reading the whole thing first (always a dangerous move) ... > > I just want to give you the heads-up that support for docstrings on > Cython properties is currently in iffy shape. I ended up wrestling > with this a little bit while trying to get Cython to build Sage -- I'm > planning on filing a handful of trac tickets (and hopefully working up > some fixes) as soon as I've got an 0.13 alpha that can build Sage with > type inferencing turned on. > > I think that no one's really tried out docstrings with properties > before: they don't really play together well. Here are two big issues > I've hit so far ... let's say we've got a class Foo with property > my_prop: > See my own code: http://code.google.com/p/mpi4py/source/browse/trunk/src/MPI/Comm.pyx#55 > ?* I don't know how to get my hands on the docstring for > Foo.my_prop.__get__ or Foo.my_prop.__set__ at all. I made a cursory > glance through the C code, and wasn't sure that it was correctly > getting attached anywhere, but it does at least appear in the C file. You have to add the docstrings at the top-level of the property block. > Actually, that's not completely true -- if the autotestdict is turned > on, then the docstring comes up in mod.__test__. Ups. That's a bug. > > I was planning on starting a new thread once I had some more concrete > suggestions for fixes. But here you are talking about @property defined at the Python level. In case of a cdef class, Cython does a different thing at the C level (i.e. filling the tp_getset slot of the PyTypeObject struct). > For comparison, Python attaches the docstrings > to the Foo.my_prop.fget, fset, and fdel methods. (The > Foo.my_prop.__get__ and whatnot all have generic docstrings.) Perhaps Cython could append the docstrings from __get__/__set__/__del__ to the top-level docstring? IIRC, Stefan did something similar for __cinit__ docstrings. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dalcinl at gmail.com Thu Mar 11 15:40:59 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 11 Mar 2010 11:40:59 -0300 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: <4B989956.7040601@behnel.de> References: <4B989956.7040601@behnel.de> Message-ID: On 11 March 2010 04:18, Stefan Behnel wrote: > Lisandro Dalcin, 10.03.2010 21:17: >>>> We could get rid of structmember.h by using the transform+property >>>> machinery Dag implemented long ago > ?> >> Any suggestions about docstring generation? I mean: >> > > First of all, there doesn't have to be a docstring. CPython won't generate > one for you either, not for '@property' and not for class/instance attributes. > I know. But I see this feature similar to adding function signatures. After all, it is not too much work. > So IMHO it would be cleaner to stick to this: > > ? ? "CTYPE ATTRNAME[ = DEFAULT]" > > An alternative is the argument type annotation syntax in Py3: > > ? ? "ATTRNAME : CTYPE [ = DEFAULT]" > OK. You convinced me... > > I have a preference for the last one, as it looks very readable: > Me too, I'llgo for it > > ?> 1) Should the docstring be generated inconditionally, or under the > ?> control of 'embedsignatures' ? > > If we provide the definition as docstring, requiring 'embedsignatures' > sounds like the right thing to do. Note that the above may be considered > leaking implementation details (C type and default value) and the embedded > string also implies a a tiny bit of overhead for the module, so some users > may want to disable it. > OK. PS: Are you aware that default values does not apply here? Cython does not support default values for cdef members... I cannot remember if it was decided that this support should be in (after all, it is sugar for saving lines in __cinit__). -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From stefan_ml at behnel.de Thu Mar 11 15:45:28 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 11 Mar 2010 15:45:28 +0100 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: Message-ID: <4B990208.4020005@behnel.de> Lisandro Dalcin, 11.03.2010 15:29: >> For comparison, Python attaches the docstrings >> to the Foo.my_prop.fget, fset, and fdel methods. (The >> Foo.my_prop.__get__ and whatnot all have generic docstrings.) > > Perhaps Cython could append the docstrings from > __get__/__set__/__del__ to the top-level docstring? -1, docstrings are often just used for code documentation, not necessarily for user documentation purposes. In any case, appending all three can have all sorts of ugly effects. I'm fine with promoting the docstring of either __get__ or __set__ iff only one of them is used and no docstring has been defined. Doing more than that doesn't seem helpful. > IIRC, Stefan did something similar for __cinit__ docstrings. Nope. Stefan From stefan_ml at behnel.de Thu Mar 11 15:50:38 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 11 Mar 2010 15:50:38 +0100 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: <4B989956.7040601@behnel.de> Message-ID: <4B99033E.4090702@behnel.de> Lisandro Dalcin, 11.03.2010 15:40: > On 11 March 2010 04:18, Stefan Behnel wrote: >> "ATTRNAME : CTYPE [ = DEFAULT]" > > PS: Are you aware that default values does not apply here? Cython does > not support default values for cdef members... I cannot remember if it > was decided that this support should be in (after all, it is sugar for > saving lines in __cinit__). I know that they are not currently supported, but if we think about an automatically generated docstring here, we should try to catch all cases before hand, especially the IMHO not so uninteresting one of documenting a default value. If we ever decide to support that feature, the generated docstring would make it more than just syntactic sugar. Stefan From dalcinl at gmail.com Thu Mar 11 15:53:10 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 11 Mar 2010 11:53:10 -0300 Subject: [Cython] GCC warnings In-Reply-To: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> Message-ID: On 11 March 2010 03:38, Ondrej Certik wrote: > On Tue, Mar 9, 2010 at 5:03 PM, Lisandro Dalcin wrote: >> Folks, I've been doing some housekeeping work in the testsuite to make >> GCC warnings under control. In the process, I've added a few hacks to >> remove warnings from unused stuff coming from NumPy headers. My fixes >> work for NumPy 1.2.x and above, but I've not tested with older >> versions... If anyone is running Cython testsuite with an older NumPy >> and have any trouble, let me know and I'll fix things. > > This is the output of my tests: > Many thanks... > > warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:90:5: > Unraisable exception in function 'refnanny.GOTREF'. > warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:103:5: > Unraisable exception in function 'refnanny.GIVEREF_and_report'. > warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:129:5: > Unraisable exception in function 'refnanny.FinishContext'. This one I'm not sure about (since long time ago).. Looking at the code, it seems that exception handling is done the right way, but perhaps I'm missing something.. The idea is that these functions absolutely cannot exit with an exception. > specmethdocstring.c:424: warning: > ?__pyx_doc_17specmethdocstring_1C___init__? defined but not used > specmethdocstring.c:446: warning: > ?__pyx_doc_17specmethdocstring_1C_3foo___get__? defined but not used > specmethdocstring.c:465: warning: > ?__pyx_doc_17specmethdocstring_1C_3foo___set__? defined but not used > specmethdocstring.cpp:424: warning: > ?__pyx_doc_17specmethdocstring_1C___init__? defined but not used > specmethdocstring.cpp:446: warning: > ?__pyx_doc_17specmethdocstring_1C_3foo___get__? defined but not used > specmethdocstring.cpp:465: warning: > ?__pyx_doc_17specmethdocstring_1C_3foo___set__? defined but not used > autotestdict.c:966: warning: > ?__pyx_doc_12autotestdict_11MyCdefClass___cinit__? defined but not > used > autotestdict.c:988: warning: > ?__pyx_doc_12autotestdict_11MyCdefClass___dealloc__? defined but not > used > autotestdict.c:1004: warning: > ?__pyx_doc_12autotestdict_11MyCdefClass___richcmp__? defined but not > used > autotestdict.c:1024: warning: > ?__pyx_doc_12autotestdict_11MyCdefClass___nonzero__? defined but not > used > autotestdict.cpp:966: warning: > ?__pyx_doc_12autotestdict_11MyCdefClass___cinit__? defined but not > used > autotestdict.cpp:988: warning: > ?__pyx_doc_12autotestdict_11MyCdefClass___dealloc__? defined but not > used > autotestdict.cpp:1004: warning: > ?__pyx_doc_12autotestdict_11MyCdefClass___richcmp__? defined but not > used > autotestdict.cpp:1024: warning: > ?__pyx_doc_12autotestdict_11MyCdefClass___nonzero__? defined but not > used > extpropertyref.c:416: warning: > ?__pyx_doc_14extpropertyref_4Spam_4eggs___get__? defined but not used > extpropertyref.cpp:416: warning: > ?__pyx_doc_14extpropertyref_4Spam_4eggs___get__? defined but not used > ---------------------------------------------------------------------- These are innocent warnings... These are docstrings from special methods... Cython generates them, but as Python C-API does not have a place to put them, then they are unused in the C code... Mmm... perhaps we should not emit these strings in the C code? After all, we know they will not be used... What's the point of emitting them? -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dalcinl at gmail.com Thu Mar 11 18:57:03 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 11 Mar 2010 14:57:03 -0300 Subject: [Cython] [PATCH] Fix autotestdict to ignore __get__/__set__/__del__ docstrings in properties of cdef classes Message-ID: Craig, could you please review and test this? -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 -------------- next part -------------- A non-text attachment was scrubbed... Name: autotestdict.diff Type: text/x-patch Size: 4548 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100311/187f6c5e/attachment.bin From ondrej at certik.cz Thu Mar 11 20:45:21 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 11 Mar 2010 11:45:21 -0800 Subject: [Cython] new C++ syntax error In-Reply-To: References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> <4B989F4F.9030606@behnel.de> Message-ID: <85b5c3131003111145g239cbae6tc927b9740f7153c1@mail.gmail.com> On Thu, Mar 11, 2010 at 12:00 AM, Craig Citro wrote: >> Did you run "python setup.py build_ext -i" locally? The compiled Cython >> parser is not automatically rebuilt on source changes. Try "make clean". >> > > Oh, it was just PEBKAC -- I had already made the fix I suggested to > Ondrej in my local copy of Cython before I even downloaded the > example. :) > > Ondrej -- even with that fix, you hit some other issues. I'm not going > to look at this now -- hopefully tomorrow night or Friday, if no one > beats me to it. I don't have time for this either now, I just wanted to quickly try it on my project yesterday before going to sleep, and give you guys some feedback. In general, I am impressed with this, it makes wrapping C++ way easier. Ondrej From ondrej at certik.cz Thu Mar 11 20:46:54 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 11 Mar 2010 11:46:54 -0800 Subject: [Cython] GCC warnings In-Reply-To: References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> Message-ID: <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> On Thu, Mar 11, 2010 at 6:53 AM, Lisandro Dalcin wrote: > On 11 March 2010 03:38, Ondrej Certik wrote: >> On Tue, Mar 9, 2010 at 5:03 PM, Lisandro Dalcin wrote: >>> Folks, I've been doing some housekeeping work in the testsuite to make >>> GCC warnings under control. In the process, I've added a few hacks to >>> remove warnings from unused stuff coming from NumPy headers. My fixes >>> work for NumPy 1.2.x and above, but I've not tested with older >>> versions... If anyone is running Cython testsuite with an older NumPy >>> and have any trouble, let me know and I'll fix things. >> >> This is the output of my tests: >> > > Many thanks... > >> >> warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:90:5: >> Unraisable exception in function 'refnanny.GOTREF'. >> warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:103:5: >> Unraisable exception in function 'refnanny.GIVEREF_and_report'. >> warning: /home/ondrej/repos/cython-devel/Cython/Runtime/refnanny.pyx:129:5: >> Unraisable exception in function 'refnanny.FinishContext'. > > This one I'm not sure about (since long time ago).. Looking at the > code, it seems that exception handling is done the right way, but > perhaps I'm missing something.. The idea is that these functions > absolutely cannot exit with an exception. > >> specmethdocstring.c:424: warning: >> ?__pyx_doc_17specmethdocstring_1C___init__? defined but not used >> specmethdocstring.c:446: warning: >> ?__pyx_doc_17specmethdocstring_1C_3foo___get__? defined but not used >> specmethdocstring.c:465: warning: >> ?__pyx_doc_17specmethdocstring_1C_3foo___set__? defined but not used >> specmethdocstring.cpp:424: warning: >> ?__pyx_doc_17specmethdocstring_1C___init__? defined but not used >> specmethdocstring.cpp:446: warning: >> ?__pyx_doc_17specmethdocstring_1C_3foo___get__? defined but not used >> specmethdocstring.cpp:465: warning: >> ?__pyx_doc_17specmethdocstring_1C_3foo___set__? defined but not used >> autotestdict.c:966: warning: >> ?__pyx_doc_12autotestdict_11MyCdefClass___cinit__? defined but not >> used >> autotestdict.c:988: warning: >> ?__pyx_doc_12autotestdict_11MyCdefClass___dealloc__? defined but not >> used >> autotestdict.c:1004: warning: >> ?__pyx_doc_12autotestdict_11MyCdefClass___richcmp__? defined but not >> used >> autotestdict.c:1024: warning: >> ?__pyx_doc_12autotestdict_11MyCdefClass___nonzero__? defined but not >> used >> autotestdict.cpp:966: warning: >> ?__pyx_doc_12autotestdict_11MyCdefClass___cinit__? defined but not >> used >> autotestdict.cpp:988: warning: >> ?__pyx_doc_12autotestdict_11MyCdefClass___dealloc__? defined but not >> used >> autotestdict.cpp:1004: warning: >> ?__pyx_doc_12autotestdict_11MyCdefClass___richcmp__? defined but not >> used >> autotestdict.cpp:1024: warning: >> ?__pyx_doc_12autotestdict_11MyCdefClass___nonzero__? defined but not >> used >> extpropertyref.c:416: warning: >> ?__pyx_doc_14extpropertyref_4Spam_4eggs___get__? defined but not used >> extpropertyref.cpp:416: warning: >> ?__pyx_doc_14extpropertyref_4Spam_4eggs___get__? defined but not used >> ---------------------------------------------------------------------- > > These are innocent warnings... These are docstrings from special > methods... Cython generates them, but as Python C-API does not have a > place to put them, then they are unused in the C code... > > Mmm... perhaps we should not emit these strings in the C code? After > all, we know they will not be used... What's the point of emitting > them? They should not be emitted imho. All gcc warnings suck. Ondrej From dalcinl at gmail.com Thu Mar 11 21:23:59 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 11 Mar 2010 17:23:59 -0300 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: <4B99033E.4090702@behnel.de> References: <4B989956.7040601@behnel.de> <4B99033E.4090702@behnel.de> Message-ID: On 11 March 2010 11:50, Stefan Behnel wrote: > Lisandro Dalcin, 11.03.2010 15:40: >> On 11 March 2010 04:18, Stefan Behnel wrote: >>> ? ? ?"ATTRNAME : CTYPE [ = DEFAULT]" >> >> PS: Are you aware that default values does not apply here? Cython does >> not support default values for cdef members... I cannot remember if it >> was decided that this support should be in (after all, it is sugar for >> saving lines in __cinit__). > > I know that they are not currently supported, but if we think about an > automatically generated docstring here, we should try to catch all cases > before hand, especially the IMHO not so uninteresting one of documenting a > default value. If we ever decide to support that feature, the generated > docstring would make it more than just syntactic sugar. > > Stefan > _______________________________________________ > Cython-dev mailing list > Cython-dev at codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev > http://hg.cython.org/cython-devel/rev/1a2e04bc1395 -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From stefan_ml at behnel.de Thu Mar 11 21:38:56 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 11 Mar 2010 21:38:56 +0100 Subject: [Cython] getting rid of CPython's structmember.h In-Reply-To: References: <4B989956.7040601@behnel.de> <4B99033E.4090702@behnel.de> Message-ID: <4B9954E0.4080900@behnel.de> Lisandro Dalcin, 11.03.2010 21:23: > http://hg.cython.org/cython-devel/rev/1a2e04bc1395 Looks like a huge cleanup action to me. Stefan From robertwb at math.washington.edu Thu Mar 11 21:42:39 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Thu, 11 Mar 2010 12:42:39 -0800 Subject: [Cython] GCC warnings In-Reply-To: <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> Message-ID: On Mar 11, 2010, at 11:46 AM, Ondrej Certik wrote: > On Thu, Mar 11, 2010 at 6:53 AM, Lisandro Dalcin > wrote: >> On 11 March 2010 03:38, Ondrej Certik wrote: >>> On Tue, Mar 9, 2010 at 5:03 PM, Lisandro Dalcin >>> wrote: >>>> Folks, I've been doing some housekeeping work in the testsuite to >>>> make >>>> GCC warnings under control. Thanks! [...] >> These are innocent warnings... These are docstrings from special >> methods... Cython generates them, but as Python C-API does not have a >> place to put them, then they are unused in the C code... >> >> Mmm... perhaps we should not emit these strings in the C code? After >> all, we know they will not be used... What's the point of emitting >> them? > > They should not be emitted imho. All gcc warnings suck. Yep, I agree. IIRC, we're able to attach some of the special method docstrings, but the others should possibly be stuck somewhere on the class rather than simply discarded. As for the refnanny ones, http://hg.cython.org/cython-devel/rev/3045d63c5f14 - Robert From dalcinl at gmail.com Thu Mar 11 21:58:10 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 11 Mar 2010 17:58:10 -0300 Subject: [Cython] GCC warnings In-Reply-To: References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> Message-ID: On 11 March 2010 17:42, Robert Bradshaw wrote: > > As for the refnanny ones, > > http://hg.cython.org/cython-devel/rev/3045d63c5f14 > Can you explain me the magics behind: try: .... except object, e: ..... I cannot get it ... -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From robertwb at math.washington.edu Thu Mar 11 22:05:05 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Thu, 11 Mar 2010 13:05:05 -0800 Subject: [Cython] GCC warnings In-Reply-To: References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> Message-ID: On Mar 11, 2010, at 12:58 PM, Lisandro Dalcin wrote: > On 11 March 2010 17:42, Robert Bradshaw > wrote: >> >> As for the refnanny ones, >> >> http://hg.cython.org/cython-devel/rev/3045d63c5f14 >> > > Can you explain me the magics behind: > > try: > .... > except object, e: > ..... > > I cannot get it ... In Python 2.5+, all errors that are raised are subclasses of BaseException, but that doesn't exist in 2.3 or 2.4, hence "object" which will catch anything (and still give us the e). The nested try is because the action of getting and storing e may cause an exception (though this shouldn't ever come up in practice). - Robert From greg.ewing at canterbury.ac.nz Thu Mar 11 22:33:47 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Fri, 12 Mar 2010 10:33:47 +1300 Subject: [Cython] GCC warnings In-Reply-To: References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> Message-ID: <4B9961BB.1060002@canterbury.ac.nz> Robert Bradshaw wrote: > The nested try is > because the action of getting and storing e may cause an exception Wow, that *is* paranoid! If that ever happened, I think things would be pretty well screwed for good, and it wouldn't matter much what you did afterwards. -- Greg From dalcinl at gmail.com Thu Mar 11 22:20:02 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 11 Mar 2010 18:20:02 -0300 Subject: [Cython] GCC warnings In-Reply-To: References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> Message-ID: On 11 March 2010 18:05, Robert Bradshaw wrote: > On Mar 11, 2010, at 12:58 PM, Lisandro Dalcin wrote: > >> On 11 March 2010 17:42, Robert Bradshaw >> wrote: >>> >>> As for the refnanny ones, >>> >>> http://hg.cython.org/cython-devel/rev/3045d63c5f14 >>> >> >> Can you explain me the magics behind: >> >> try: >> ? ?.... >> except object, e: >> ? ..... >> >> I cannot get it ... > > > In Python 2.5+, all errors that are raised are subclasses of > BaseException, but that doesn't exist in 2.3 or 2.4, hence "object" > which will catch anything (and still give us the e). What are you talking about? See this: [dalcinl at trantor tmp]$ cat exc.pyx def main(): try: raise ValueError except object, e: pass [dalcinl at trantor tmp]$ python -c 'import pyximport; pyximport.install(); import exc; exc.main()' Traceback (most recent call last): File "", line 1, in File "exc.pyx", line 3, in exc.main (/u/dalcinl/.pyxbld/temp.linux-i686-2.6/pyrex/exc.c:430) raise ValueError ValueError > The nested try is > because the action of getting and storing e may cause an exception > (though this shouldn't ever come up in practice). > Yes, I understand that part. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From robertwb at math.washington.edu Thu Mar 11 22:51:55 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Thu, 11 Mar 2010 13:51:55 -0800 Subject: [Cython] GCC warnings In-Reply-To: <4B9961BB.1060002@canterbury.ac.nz> References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> <4B9961BB.1060002@canterbury.ac.nz> Message-ID: <7690C9A4-D6F9-42B8-B12A-19BA68448695@math.washington.edu> On Mar 11, 2010, at 1:33 PM, Greg Ewing wrote: > Robert Bradshaw wrote: >> The nested try is >> because the action of getting and storing e may cause an exception > > Wow, that *is* paranoid! > > If that ever happened, I think things would be pretty well > screwed for good, and it wouldn't matter much what you > did afterwards. It's mostly to suppress warnings in the test runs. - Robert From robertwb at math.washington.edu Thu Mar 11 22:49:28 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Thu, 11 Mar 2010 13:49:28 -0800 Subject: [Cython] GCC warnings In-Reply-To: References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> Message-ID: <1B795BDA-338C-4EC0-963F-26220A56B200@math.washington.edu> On Mar 11, 2010, at 1:20 PM, Lisandro Dalcin wrote: > On 11 March 2010 18:05, Robert Bradshaw > wrote: >> On Mar 11, 2010, at 12:58 PM, Lisandro Dalcin wrote: >> >>> On 11 March 2010 17:42, Robert Bradshaw >>> wrote: >>>> >>>> As for the refnanny ones, >>>> >>>> http://hg.cython.org/cython-devel/rev/3045d63c5f14 >>>> >>> >>> Can you explain me the magics behind: >>> >>> try: >>> .... >>> except object, e: >>> ..... >>> >>> I cannot get it ... >> >> >> In Python 2.5+, all errors that are raised are subclasses of >> BaseException, but that doesn't exist in 2.3 or 2.4, hence "object" >> which will catch anything (and still give us the e). > > What are you talking about? See this: > > [dalcinl at trantor tmp]$ cat exc.pyx > def main(): > try: > raise ValueError > except object, e: > pass > [dalcinl at trantor tmp]$ python -c 'import pyximport; > pyximport.install(); import exc; exc.main()' > Traceback (most recent call last): > File "", line 1, in > File "exc.pyx", line 3, in exc.main > (/u/dalcinl/.pyxbld/temp.linux-i686-2.6/pyrex/exc.c:430) > raise ValueError > ValueError Hmm... I thought that "except X" would catch all exceptions that were subclasses of X, but it turns out that it doesn't check inheritance unless X is an exception type... I'll fix this. - Robert From ondrej at certik.cz Fri Mar 12 01:13:02 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 11 Mar 2010 16:13:02 -0800 Subject: [Cython] .pxd for some STL classes In-Reply-To: <45239151003101144g18c3c3dchd4710cfb05614e04@mail.gmail.com> References: <45239151003101144g18c3c3dchd4710cfb05614e04@mail.gmail.com> Message-ID: <85b5c3131003111613h791e0f93u5484dfe741da555d@mail.gmail.com> On Wed, Mar 10, 2010 at 11:44 AM, Danilo Freitas wrote: > I wrote some .pxd for use. They are on Cython/Includes/libcpp > (http://hg.cython.org/cython-devel/rev/d7b27e929888) > > There are only 5 wrapped classes (queue, deque, stack, vector and > set). Also there is the struct pair (here, declared as a class), that > aux the STL. > > There aren't secific tests for it yet, but we'll have soon. > > Also, there are still some issues for wrapping them (like template > declarations inside a templated class). > > Please report if you find some bug. There is also vector and deque already wrapped in Cython/Includes/stl.pxd. Maybe let's delete this file? Ondrej From ondrej at certik.cz Fri Mar 12 01:24:08 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 11 Mar 2010 16:24:08 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> Message-ID: <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> On Wed, Mar 10, 2010 at 6:23 PM, Lisandro Dalcin wrote: > On 10 March 2010 21:03, Ondrej Certik wrote: >> >> After I get this working, and test that it indeed works, I'll try to >> put it into cython somehow, so that anyone can use it. >> > > Or start a new project. IMO, such a tool has a broader scope. BTW, you > can get some ideas from here > http://www.boost.org/doc/libs/1_42_0/libs/python/doc/tutorial/doc/html/python/embedding.html Thanks for the link. I'll rename my cmd() to eval(), that makes more sense. > >> One of my friends told me that he prefers Lua because it's super easy >> to use from C/C++. > > Really? Looking here http://www.lua.org/pil/24.1.html, I cannot buy > such assertion. Is your friend talking about the stack-based C<->Lua > flow? I did use Lua about 5 years ago myself from C++ and it was super easy to export my C++ functions in there and use Lua as a plugin/extension language to my C++ program (e.g. exposing C++ functions as well as calling lua functions). Doing the same using Python C/API was way more complex (talking for myself). Cython made it easy, but not easy enough, but the Python() class approach above, I think that will rock. I am implementing it now. Ondrej From dsurviver at gmail.com Fri Mar 12 03:11:43 2010 From: dsurviver at gmail.com (Danilo Freitas) Date: Thu, 11 Mar 2010 23:11:43 -0300 Subject: [Cython] .pxd for some STL classes In-Reply-To: <85b5c3131003111613h791e0f93u5484dfe741da555d@mail.gmail.com> References: <45239151003101144g18c3c3dchd4710cfb05614e04@mail.gmail.com> <85b5c3131003111613h791e0f93u5484dfe741da555d@mail.gmail.com> Message-ID: <45239151003111811s5c29b7f3qfe72f81584d66b2d@mail.gmail.com> Yes, I forgot to remove it. It's not useful anymore. 2010/3/11 Ondrej Certik : > On Wed, Mar 10, 2010 at 11:44 AM, Danilo Freitas wrote: >> I wrote some .pxd for use. They are on Cython/Includes/libcpp >> (http://hg.cython.org/cython-devel/rev/d7b27e929888) >> >> There are only 5 wrapped classes (queue, deque, stack, vector and >> set). Also there is the struct pair (here, declared as a class), that >> aux the STL. >> >> There aren't secific tests for it yet, but we'll have soon. >> >> Also, there are still some issues for wrapping them (like template >> declarations inside a templated class). >> >> Please report if you find some bug. > > There is also vector and deque already wrapped in > Cython/Includes/stl.pxd. Maybe let's delete this file? > > Ondrej > _______________________________________________ > Cython-dev mailing list > Cython-dev at codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev > -- - Danilo Freitas From ondrej at certik.cz Fri Mar 12 03:14:50 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 11 Mar 2010 18:14:50 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031701s1ed40e5ay5e0081e8849e1a05@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> Message-ID: <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> Hi, I am attaching the two files that I want Cython to generate. It seem to do the job. So far, I just did them by hand and I will test my code more in the coming days. If all is fine, I will try to modify Cython to do that automatically. The static->extern is easy, this patch does it: ondrej at raven:~/repos/cython-devel$ hg diff diff -r b8f0fc5153e0 Cython/Compiler/ModuleNode.py --- a/Cython/Compiler/ModuleNode.py Thu Mar 11 14:06:32 2010 -0800 +++ b/Cython/Compiler/ModuleNode.py Thu Mar 11 18:13:19 2010 -0800 @@ -184,7 +184,7 @@ h_code.putln("") for entry in api_funcs: type = CPtrType(entry.type) - h_code.putln("static %s;" % type.declaration_code(entry.cname)) + h_code.putln("extern %s;" % type.declaration_code(entry.cname)) h_code.putln("") h_code.put_h_guard(Naming.api_func_guard + "import_module") h_code.put(import_module_utility_code.impl) but the rest will be a bit more complicated. Let me know if you think this is the right approach. I guess I could add some cython cmdline option to control the global/local api. Ondrej -------------- next part -------------- A non-text attachment was scrubbed... Name: _hermes_common_api.h Type: text/x-chdr Size: 1193 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100311/9554d0ad/attachment-0001.h -------------- next part -------------- A non-text attachment was scrubbed... Name: _hermes_common_api.cpp Type: text/x-c++src Size: 5231 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100311/9554d0ad/attachment-0001.cpp From ondrej at certik.cz Fri Mar 12 03:16:48 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 11 Mar 2010 18:16:48 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> Message-ID: <85b5c3131003111816q7f7e53d1m2964e38b36f42191@mail.gmail.com> On Thu, Mar 11, 2010 at 6:14 PM, Ondrej Certik wrote: > Hi, > > I am attaching the two files that I want Cython to generate. > > It seem to do the job. So far, I just did them by hand and I will test > my code more in the coming days. If all is fine, I will try to modify > Cython to do that automatically. The static->extern is easy, this Btw, and here is my Python() class so far: class Python { public: Python(); Python(int argc, char* argv[]); ~Python(); void eval(const char *text); void insert_object(const char *name, PyObject *o); PyObject *get_object(const char *name); }; and implementation: #include #include "python_api.h" static int python_count=0; Python::Python() { Python::Python(-1, NULL); } Python::Python(int argc, char* argv[]) { python_count++; if (python_count == 1) { // This is a hack: putenv((char *)"PYTHONPATH=../.."); Py_Initialize(); if (argc >= 0) PySys_SetArgv(argc, argv); if (import__hermes_common()) throw std::runtime_error("hermes_common failed to import."); } } Python::~Python() { python_count--; if (python_count == 0) { Py_Finalize(); } } void Python::eval(const char *text) { cmd(text); } void Python::insert_object(const char *name, PyObject *o) { ::insert_object(name, o); } PyObject *Python::get_object(const char *name) { ::get_object(name); } It's still a work in progress. I'll rename insert_object to push(), get_object to pull(), eval() to exec() and also implement local namespace for each instance. Ondrej From ondrej at certik.cz Fri Mar 12 03:17:42 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 11 Mar 2010 18:17:42 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003111816q7f7e53d1m2964e38b36f42191@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> <85b5c3131003111816q7f7e53d1m2964e38b36f42191@mail.gmail.com> Message-ID: <85b5c3131003111817h5be52b54y54f0b40ee4f0cf83@mail.gmail.com> On Thu, Mar 11, 2010 at 6:16 PM, Ondrej Certik wrote: > On Thu, Mar 11, 2010 at 6:14 PM, Ondrej Certik wrote: >> Hi, >> >> I am attaching the two files that I want Cython to generate. >> >> It seem to do the job. So far, I just did them by hand and I will test >> my code more in the coming days. If all is fine, I will try to modify >> Cython to do that automatically. The static->extern is easy, this > > Btw, and here is my Python() class so far: > > class Python { > public: > ? ?Python(); > ? ?Python(int argc, char* argv[]); > ? ?~Python(); > ? ?void eval(const char *text); > ? ?void insert_object(const char *name, PyObject *o); > ? ?PyObject *get_object(const char *name); > }; > > > and implementation: > > > > #include > > #include "python_api.h" > > static int python_count=0; > > Python::Python() > { > ? ?Python::Python(-1, NULL); > } > > Python::Python(int argc, char* argv[]) > { > ? ?python_count++; > ? ?if (python_count == 1) { > ? ? ? ?// This is a hack: > ? ? ? ?putenv((char *)"PYTHONPATH=../.."); > ? ? ? ?Py_Initialize(); > ? ? ? ?if (argc >= 0) > ? ? ? ? ? ?PySys_SetArgv(argc, argv); > ? ? ? ?if (import__hermes_common()) > ? ? ? ? ? ?throw std::runtime_error("hermes_common failed to import."); > ? ?} > } > > Python::~Python() > { > ? ?python_count--; > ? ?if (python_count == 0) { > ? ? ? ?Py_Finalize(); > ? ?} > } > > void Python::eval(const char *text) > { > ? ?cmd(text); > } > > void Python::insert_object(const char *name, PyObject *o) > { > ? ?::insert_object(name, o); > } > > PyObject *Python::get_object(const char *name) > { > ? ?::get_object(name); > } > > > It's still a work in progress. I'll rename insert_object to push(), > get_object to pull(), eval() to exec() and also implement local > namespace for each instance. And here is how to use it: void test_basic2() { Python *p = new Python(); p->insert_object("i", c2py_int(5)); p->eval("i = i*2"); int i = py2c_int(p->get_object("i")); _assert(i == 10); delete p; } All I need to do is to link with libhermes_common.so and that's it. Ondrej From dalcinl at gmail.com Fri Mar 12 15:32:12 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 12 Mar 2010 11:32:12 -0300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003111817h5be52b54y54f0b40ee4f0cf83@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> <85b5c3131003111816q7f7e53d1m2964e38b36f42191@mail.gmail.com> <85b5c3131003111817h5be52b54y54f0b40ee4f0cf83@mail.gmail.com> Message-ID: On 11 March 2010 23:17, Ondrej Certik wrote: > > ? ?p->insert_object("i", c2py_int(5)); > I would prefer p->put("i", c2py_int(5)) > > ? ?int i = py2c_int(p->get_object("i")); > Can you explain me how are you managing reference counting? I think that you will need a proxy C++ class to manage incref()/decref() -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dalcinl at gmail.com Fri Mar 12 15:28:58 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 12 Mar 2010 11:28:58 -0300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> Message-ID: On 11 March 2010 23:14, Ondrej Certik wrote: > Hi, > > I am attaching the two files that I want Cython to generate. > > It seem to do the job. So far, I just did them by hand and I will test > my code more in the coming days. If all is fine, I will try to modify > Cython to do that automatically. The static->extern is easy, this > patch does it: > > ondrej at raven:~/repos/cython-devel$ hg diff > diff -r b8f0fc5153e0 Cython/Compiler/ModuleNode.py > --- a/Cython/Compiler/ModuleNode.py ? ? Thu Mar 11 14:06:32 2010 -0800 > +++ b/Cython/Compiler/ModuleNode.py ? ? Thu Mar 11 18:13:19 2010 -0800 > @@ -184,7 +184,7 @@ > ? ? ? ? ? ? ? ? h_code.putln("") > ? ? ? ? ? ? ? ? for entry in api_funcs: > ? ? ? ? ? ? ? ? ? ? type = CPtrType(entry.type) > - ? ? ? ? ? ? ? ? ? ?h_code.putln("static %s;" % > type.declaration_code(entry.cname)) > + ? ? ? ? ? ? ? ? ? ?h_code.putln("extern %s;" % > type.declaration_code(entry.cname)) > ? ? ? ? ? ? h_code.putln("") > ? ? ? ? ? ? h_code.put_h_guard(Naming.api_func_guard + "import_module") > ? ? ? ? ? ? h_code.put(import_module_utility_code.impl) > > > but the rest will be a bit more complicated. Let me know if you think > this is the right approach. I guess I could add some cython cmdline > option to control the global/local api. > How about controling global/local api at C compile time, using preprocessor definitions? -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dagss at student.matnat.uio.no Fri Mar 12 13:00:25 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 12 Mar 2010 13:00:25 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B97F580.7040904@behnel.de> References: <4B97890E.5060407@student.matnat.uio.no> <4B97F580.7040904@behnel.de> Message-ID: <4B9A2CD9.9080307@student.matnat.uio.no> Stefan Behnel wrote: > Robert Bradshaw, 10.03.2010 19:38: > >> On Mar 10, 2010, at 3:57 AM, Dag Sverre Seljebotn wrote: >> >> >>> It was agreed earlier to put pxds for the CPython API into the >>> namespace >>> "python". However I'm wondering if "cpython" is more appropriate, >>> given >>> that the CPython API is very CPython-specific and has very little to >>> do >>> with Python as a language. "python" seems a little to generic to me, >>> while "cpython" seems specific and to the point. >>> >> I like that idea. It makes it more clear that we're really talking >> about the C/Python API here. >> > > Fine with me. > > Done. The web commit visualization gives a full diff and duplicates the files contents, although I am sure I used "hg mv". Perhaps just a weakness of "hg serve". Dag Sverre From dalcinl at gmail.com Fri Mar 12 07:21:58 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 12 Mar 2010 03:21:58 -0300 Subject: [Cython] Pyhon 3 exception clauses Message-ID: What do you think? I'd be surprised if I'm not missing something, but this is an starter... diff -r 3932a0be3b6b Cython/Compiler/Parsing.py --- a/Cython/Compiler/Parsing.py Thu Mar 11 18:57:06 2010 -0300 +++ b/Cython/Compiler/Parsing.py Fri Mar 12 03:18:28 2010 -0300 @@ -1334,6 +1334,9 @@ if s.sy == ',': s.next() exc_value = p_simple_expr(s) + elif s.sy == 'IDENT' and s.systring == 'as': + s.next() + exc_value = p_simple_expr(s) body = p_suite(s) return Nodes.ExceptClauseNode(pos, pattern = exc_type, target = exc_value, body = body) -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From stefan_ml at behnel.de Fri Mar 12 18:52:58 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 12 Mar 2010 18:52:58 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B9A2CD9.9080307@student.matnat.uio.no> References: <4B97890E.5060407@student.matnat.uio.no> <4B97F580.7040904@behnel.de> <4B9A2CD9.9080307@student.matnat.uio.no> Message-ID: <4B9A7F7A.4000407@behnel.de> Dag Sverre Seljebotn, 12.03.2010 13:00: > Done. The web commit visualization gives a full diff and duplicates the > files contents, although I am sure I used "hg mv". Perhaps just a > weakness of "hg serve". Looks like you just killed the refnanny. Stefan From stefan_ml at behnel.de Fri Mar 12 19:01:55 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 12 Mar 2010 19:01:55 +0100 Subject: [Cython] Pyhon 3 exception clauses In-Reply-To: References: Message-ID: <4B9A8193.1070406@behnel.de> Lisandro Dalcin, 12.03.2010 07:21: > What do you think? I'd be surprised if I'm not missing something, but > this is an starter... Hmm, interesting. Looks like I forgot to open a ticket for that when I went through the Py3 syntax differences lately. So you'll have to do that yourself (just for documentation purposes, pushing this fix is fine with me, as long as you add a couple of tests). > diff -r 3932a0be3b6b Cython/Compiler/Parsing.py > --- a/Cython/Compiler/Parsing.py Thu Mar 11 18:57:06 2010 -0300 > +++ b/Cython/Compiler/Parsing.py Fri Mar 12 03:18:28 2010 -0300 > @@ -1334,6 +1334,9 @@ > if s.sy == ',': > s.next() > exc_value = p_simple_expr(s) > + elif s.sy == 'IDENT' and s.systring == 'as': > + s.next() > + exc_value = p_simple_expr(s) > body = p_suite(s) > return Nodes.ExceptClauseNode(pos, > pattern = exc_type, target = exc_value, body = body) Sure, why not. That's basically all there is to it syntaxwise. PEP 3110 also states that multiple exceptions must be put into a tuple (as before), so all that really changes is the 'as'. http://www.python.org/dev/peps/pep-3110/ Personally, I'd merge the two conditions into one, but that's just aesthetics. Stefan From dagss at student.matnat.uio.no Fri Mar 12 19:13:40 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 12 Mar 2010 19:13:40 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B9A7F7A.4000407@behnel.de> References: <4B97890E.5060407@student.matnat.uio.no> <4B97F580.7040904@behnel.de> <4B9A2CD9.9080307@student.matnat.uio.no> <4B9A7F7A.4000407@behnel.de> Message-ID: <4B9A8454.4000901@student.matnat.uio.no> Stefan Behnel wrote: > Dag Sverre Seljebotn, 12.03.2010 13:00: >> Done. The web commit visualization gives a full diff and duplicates the >> files contents, although I am sure I used "hg mv". Perhaps just a >> weakness of "hg serve". > > Looks like you just killed the refnanny. > Whoops. I'll clean up after myself as fast as I can get to it, tonight or tomorrow. -- Dag Sverre From dalcinl at gmail.com Fri Mar 12 19:17:50 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 12 Mar 2010 15:17:50 -0300 Subject: [Cython] Pyhon 3 exception clauses In-Reply-To: <4B9A8193.1070406@behnel.de> References: <4B9A8193.1070406@behnel.de> Message-ID: On 12 March 2010 15:01, Stefan Behnel wrote: > Lisandro Dalcin, 12.03.2010 07:21: >> What do you think? I'd be surprised if I'm not missing something, but >> this is an starter... > > Hmm, interesting. Looks like I forgot to open a ticket for that when I went > through the Py3 syntax differences lately. So you'll have to do that > yourself (just for documentation purposes, pushing this fix is fine with > me, as long as you add a couple of tests). > OK... I'll do it (though likely the next week) > >> ? ? ? ? ? if s.sy == ',': >> ? ? ? ? ? ? ? s.next() >> ? ? ? ? ? ? ? exc_value = p_simple_expr(s) >> + ? ? ? ?elif s.sy == 'IDENT' and s.systring == 'as': >> + ? ? ? ? ? ?s.next() >> + ? ? ? ? ? ?exc_value = p_simple_expr(s) > > Personally, I'd merge the two conditions into one, but that's just aesthetics. > Of course... -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dagss at student.matnat.uio.no Fri Mar 12 19:28:15 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 12 Mar 2010 19:28:15 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B9A7F7A.4000407@behnel.de> References: <4B97890E.5060407@student.matnat.uio.no> <4B97F580.7040904@behnel.de> <4B9A2CD9.9080307@student.matnat.uio.no> <4B9A7F7A.4000407@behnel.de> Message-ID: <4B9A87BF.2060509@student.matnat.uio.no> Stefan Behnel wrote: > Dag Sverre Seljebotn, 12.03.2010 13:00: >> Done. The web commit visualization gives a full diff and duplicates the >> files contents, although I am sure I used "hg mv". Perhaps just a >> weakness of "hg serve". > > Looks like you just killed the refnanny. > I can't reproduce this (or I need a better description of what goes wrong for you). Note that there's a seperate commit adding backwards-compatability pxds which the refnanny can use. (Of course, the refnanny should be changed for aesthetics regardless.) -- Dag Sverre From stefan_ml at behnel.de Fri Mar 12 20:14:09 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 12 Mar 2010 20:14:09 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B9A87BF.2060509@student.matnat.uio.no> References: <4B97890E.5060407@student.matnat.uio.no> <4B97F580.7040904@behnel.de> <4B9A2CD9.9080307@student.matnat.uio.no> <4B9A7F7A.4000407@behnel.de> <4B9A87BF.2060509@student.matnat.uio.no> Message-ID: <4B9A9281.7030603@behnel.de> Dag Sverre Seljebotn, 12.03.2010 19:28: > Stefan Behnel wrote: >> Dag Sverre Seljebotn, 12.03.2010 13:00: >>> Done. The web commit visualization gives a full diff and duplicates the >>> files contents, although I am sure I used "hg mv". Perhaps just a >>> weakness of "hg serve". >> >> Looks like you just killed the refnanny. > > I can't reproduce this (or I need a better description of what goes > wrong for you). Note that there's a seperate commit adding > backwards-compatability pxds which the refnanny can use. > > (Of course, the refnanny should be changed for aesthetics regardless.) Hmm, it looks like there is a problem with packaging up the new Cython/Includes subdirectories. When building a bdist, I get tons of errors like this: Error converting Pyrex file to C: ------------------------------------------------------------ ... from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF, Py_XDECREF ^ ------------------------------------------------------------ .../Cython/Runtime/refnanny.pyx:1:0: 'cpython.ref.pxd' not found I can always reproduce this with Py3.2 and not always with other python versions. I don't have the time to look into this right now, but unless someone else can take a look, I should get around to do so this weekend. Stefan From dalcinl at gmail.com Fri Mar 12 20:49:18 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 12 Mar 2010 16:49:18 -0300 Subject: [Cython] Namespace for CPython API? In-Reply-To: <4B9A9281.7030603@behnel.de> References: <4B97890E.5060407@student.matnat.uio.no> <4B97F580.7040904@behnel.de> <4B9A2CD9.9080307@student.matnat.uio.no> <4B9A7F7A.4000407@behnel.de> <4B9A87BF.2060509@student.matnat.uio.no> <4B9A9281.7030603@behnel.de> Message-ID: On 12 March 2010 16:14, Stefan Behnel wrote: > Dag Sverre Seljebotn, 12.03.2010 19:28: >> Stefan Behnel wrote: >>> Dag Sverre Seljebotn, 12.03.2010 13:00: >>>> Done. The web commit visualization gives a full diff and duplicates the >>>> files contents, although I am sure I used "hg mv". Perhaps just a >>>> weakness of "hg serve". >>> >>> Looks like you just killed the refnanny. >> >> I can't reproduce this (or I need a better description of what goes >> wrong for you). Note that there's a seperate commit adding >> backwards-compatability pxds which the refnanny can use. >> >> (Of course, the refnanny should be changed for aesthetics regardless.) > > Hmm, it looks like there is a problem with packaging up the new > Cython/Includes subdirectories. When building a bdist, I get tons of errors > like this: > > > Error converting Pyrex file to C: > ------------------------------------------------------------ > ... > from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF, Py_XDECREF > ^ > ------------------------------------------------------------ > > .../Cython/Runtime/refnanny.pyx:1:0: 'cpython.ref.pxd' not found > > > I can always reproduce this with Py3.2 and not always with other python > versions. I don't have the time to look into this right now, but unless > someone else can take a look, I should get around to do so this weekend. > > Stefan > _______________________________________________ > Cython-dev mailing list > Cython-dev at codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev > Try to add a __init__.pxd (alongside the __init__.pyx ones) in each directory... This is likely the long standing issue I commented a couple of times ago. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dagss at student.matnat.uio.no Fri Mar 12 21:08:40 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 12 Mar 2010 21:08:40 +0100 Subject: [Cython] Namespace for CPython API? In-Reply-To: References: <4B97890E.5060407@student.matnat.uio.no> <4B97F580.7040904@behnel.de> <4B9A2CD9.9080307@student.matnat.uio.no> <4B9A7F7A.4000407@behnel.de> <4B9A87BF.2060509@student.matnat.uio.no> <4B9A9281.7030603@behnel.de> Message-ID: <4B9A9F48.2030508@student.matnat.uio.no> Lisandro Dalcin wrote: > On 12 March 2010 16:14, Stefan Behnel wrote: > >> Dag Sverre Seljebotn, 12.03.2010 19:28: >> >>> Stefan Behnel wrote: >>> >>>> Dag Sverre Seljebotn, 12.03.2010 13:00: >>>> >>>>> Done. The web commit visualization gives a full diff and duplicates the >>>>> files contents, although I am sure I used "hg mv". Perhaps just a >>>>> weakness of "hg serve". >>>>> >>>> Looks like you just killed the refnanny. >>>> >>> I can't reproduce this (or I need a better description of what goes >>> wrong for you). Note that there's a seperate commit adding >>> backwards-compatability pxds which the refnanny can use. >>> >>> (Of course, the refnanny should be changed for aesthetics regardless.) >>> >> Hmm, it looks like there is a problem with packaging up the new >> Cython/Includes subdirectories. When building a bdist, I get tons of errors >> like this: >> >> >> Error converting Pyrex file to C: >> ------------------------------------------------------------ >> ... >> from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF, Py_XDECREF >> ^ >> ------------------------------------------------------------ >> >> .../Cython/Runtime/refnanny.pyx:1:0: 'cpython.ref.pxd' not found >> >> >> I can always reproduce this with Py3.2 and not always with other python >> versions. I don't have the time to look into this right now, but unless >> someone else can take a look, I should get around to do so this weekend. >> >> Stefan >> _______________________________________________ >> Cython-dev mailing list >> Cython-dev at codespeak.net >> http://codespeak.net/mailman/listinfo/cython-dev >> >> > > Try to add a __init__.pxd (alongside the __init__.pyx ones) in each > directory... This is likely the long standing issue I commented a > couple of times ago. > That's not it, it was already there. It's the streamlined beauty of distutils hitting again. http://hg.cython.org/cython-devel/rev/9c5bc24ef233 If someone wants to make it more pretty feel free; having to hard-code in the packages is kind of ugly I suppose. Also there might be more cases like this but this at least should have got the "bdist" one. Dag Sverre From greg.ewing at canterbury.ac.nz Fri Mar 12 12:02:25 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Sat, 13 Mar 2010 00:02:25 +1300 Subject: [Cython] GCC warnings In-Reply-To: <1B795BDA-338C-4EC0-963F-26220A56B200@math.washington.edu> References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> <1B795BDA-338C-4EC0-963F-26220A56B200@math.washington.edu> Message-ID: <4B9A1F41.4010509@canterbury.ac.nz> Robert Bradshaw wrote: > Hmm... I thought that "except X" would catch all exceptions that were > subclasses of X, but it turns out that it doesn't check inheritance > unless X is an exception type... I'll fix this. Also remember that before 2.5, exceptions were old-style classes, which are *not* subclasses of object. So this would never have worked in any version of Python. -- Greg From dalcinl at gmail.com Fri Mar 12 22:33:27 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 12 Mar 2010 18:33:27 -0300 Subject: [Cython] [HELP] broken except A,B Message-ID: See the testcase below. If the body of the except clause is empty, 'e' never gets assigned to the exception value, ie... """ >>> repr(e) 'ValueError()' """ e = None try: raise ValueError except ValueError, e: #a = 1 pass Could any of you fix this in order I can properly implement and test "except A as B" ?? The fix should be done in "Nodes.py", at "generate_handling_code()" ... The obvious patch is the one below, but perhaps you can imagine a better way? diff -r 3932a0be3b6b Cython/Compiler/Nodes.py --- a/Cython/Compiler/Nodes.py Thu Mar 11 18:57:06 2010 -0300 +++ b/Cython/Compiler/Nodes.py Fri Mar 12 18:30:43 2010 -0300 @@ -4497,14 +4497,6 @@ else: code.putln("/*except:*/ {") - if not getattr(self.body, 'stats', True): - # most simple case: no exception variable, empty body (pass) - # => reset the exception state, done - code.putln("PyErr_Restore(0,0,0);") - code.put_goto(end_label) - code.putln("}") - return - exc_vars = [code.funcstate.allocate_temp(py_object_type, manage_ref=True) for i in xrange(3)] PS: Is this an instance of "premature optimization is the root of all evil" ?? -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From ondrej at certik.cz Sat Mar 13 01:57:16 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Fri, 12 Mar 2010 16:57:16 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> Message-ID: <85b5c3131003121657u2c3623abjf835f230ee658b3e@mail.gmail.com> On Fri, Mar 12, 2010 at 6:28 AM, Lisandro Dalcin wrote: > On 11 March 2010 23:14, Ondrej Certik wrote: [...] >> but the rest will be a bit more complicated. Let me know if you think >> this is the right approach. I guess I could add some cython cmdline >> option to control the global/local api. >> > > How about controling global/local api at C compile time, using > preprocessor definitions? But how about the generation of the .cpp file? I need cython to generate it. But most users imho just need the .h file. >> >> ? ?p->insert_object("i", c2py_int(5)); >> > > I would prefer p->put("i", c2py_int(5)) As I wrote, I would actually prefer push. That's what ipython uses (Brian suggested this to me) and I like it. E.g. push/pull. Yours idea would be put/get. I guess that's cool too. I am not a native speaker, so my intuition might be wrong, but I like to think of pushing the variables in there, rather than just putting them there. :) > >> >> ? ?int i = py2c_int(p->get_object("i")); >> > > Can you explain me how are you managing reference counting? I think > that you will need a proxy C++ class to manage incref()/decref() Here are the definitions of py2c_int and c2py_int: cdef api object c2py_int(int i): return i cdef api int py2c_int(object i): return i So what exactly do you mean by reference counting? Isn't this safe? Ondrej From dalcinl at gmail.com Sat Mar 13 04:25:42 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Sat, 13 Mar 2010 00:25:42 -0300 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003121657u2c3623abjf835f230ee658b3e@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> <85b5c3131003121657u2c3623abjf835f230ee658b3e@mail.gmail.com> Message-ID: On 12 March 2010 21:57, Ondrej Certik wrote: > On Fri, Mar 12, 2010 at 6:28 AM, Lisandro Dalcin wrote: >> On 11 March 2010 23:14, Ondrej Certik wrote: > [...] >>> but the rest will be a bit more complicated. Let me know if you think >>> this is the right approach. I guess I could add some cython cmdline >>> option to control the global/local api. >>> >> >> How about controling global/local api at C compile time, using >> preprocessor definitions? > > But how about the generation of the .cpp file? I need cython to > generate it. But most users imho just need the .h file. > I'm thinking about and additional C macro.. then you just write a "hermes_api.cpp" with #define EMIT_CAPI_SYMBOLS_HERE #include "hermes_api.h" After compiling this file, you are done... >>> >>> ? ?p->insert_object("i", c2py_int(5)); >>> >> >> I would prefer p->put("i", c2py_int(5)) > > As I wrote, I would actually prefer push. That's what ipython uses > (Brian suggested this to me) and I like it. E.g. push/pull. > Looks good, too.. > Yours idea would be put/get. I guess that's cool too. I am not a > native speaker, so my intuition might be wrong, but I like to think of > pushing the variables in there, rather than just putting them there. > :) > I?m not a native speaker, too.. So let's follow Brian... >> >>> >>> ? ?int i = py2c_int(p->get_object("i")); >>> >> >> Can you explain me how are you managing reference counting? I think >> that you will need a proxy C++ class to manage incref()/decref() > > Here are the definitions of py2c_int and c2py_int: > > cdef api object c2py_int(int i): > ? ?return i > This function will return a brand new object (or a new reference of a caches Python intinstance, this is a CPython implementation detail)... Anyway, if you call this from C/C++, you have to somehow take ownership of that object, of use it and throw it away (i.e. decref) > > cdef api int py2c_int(object i): > ? ?return i > This functions just take a (borrowed) object as its argument... So if you do: py2c_int( c2py_int(7) ) you have just leaked a reference :-) ... > > So what exactly do you mean by reference counting? Isn't this safe? > When you code in Cython, all is safe, and nice, and all gory details are taken into account... But when you move to C and start using C functions (it does not actually matter if they are auto-generated by Cython) taking and returning Cython objects, then YOU are in charge of following the rules and managing refcounting. In short, you have to be VERY careful about what your methods/functions do regarding ownership of Python references... BTW, Boost.Python solves this more or less nicely using proxy, smart-pointer-like classes for holding PyObject* references (e.g. you incref() in copy constructors and operator=(), decref() in destructors). Moral: Once you get a decent knowledge of Python's C-API, you start to REALLY appreciate the Cython's (and Pyrex's) magic... This is the reason I've switched all my projects to Cython, starting from scratch and thorowing away all the previous hand-written C crap! Of course, working in C++ is other story... because the compiler can help you to manage these references (using those proxy classes). -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From ondrej at certik.cz Sat Mar 13 04:46:15 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Fri, 12 Mar 2010 19:46:15 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> <85b5c3131003121657u2c3623abjf835f230ee658b3e@mail.gmail.com> Message-ID: <85b5c3131003121946s21a67aa6i742ba6b592bcc199@mail.gmail.com> Hi Lisandro! On Fri, Mar 12, 2010 at 7:25 PM, Lisandro Dalcin wrote: >>> >>> Can you explain me how are you managing reference counting? I think >>> that you will need a proxy C++ class to manage incref()/decref() >> >> Here are the definitions of py2c_int and c2py_int: >> >> cdef api object c2py_int(int i): >> ? ?return i >> > > This function will return a brand new object (or a new reference of a > caches Python intinstance, this is a CPython implementation detail)... > Anyway, if you call this from C/C++, you have to somehow take > ownership of that object, of use it and throw it away (i.e. decref) > >> >> cdef api int py2c_int(object i): >> ? ?return i >> > > This functions just take a (borrowed) object as its argument... > > So if you do: > > py2c_int( c2py_int(7) ) > > you have just leaked a reference :-) ... Yep. But the c2py_int() is only meant to use with insert_object(), (that I'll rename to push): cdef api void insert_object(const_char_p name, object o): global_namespace.update({name: o}) where global_namespace is a dictionary. All in Cython. Example: insert_object("a", c2py_int(3)); will this create a leak? e.g. if I do this in Cython: insert_object("a", c2py_int(3)) there should be no leak, right? Will it create a leak when executed from C++? I am not sure now, I'd have to test it. > > >> >> So what exactly do you mean by reference counting? Isn't this safe? >> > > When you code in Cython, all is safe, and nice, and all gory details > are taken into account... But when you move to C and start using C > functions (it does not actually matter if they are auto-generated by > Cython) taking and returning Cython objects, then YOU are in charge of > following the rules and managing refcounting. > > In short, you have to be VERY careful about what your > methods/functions do regarding ownership of Python references... BTW, > Boost.Python solves this more or less nicely using proxy, > smart-pointer-like classes for holding PyObject* references (e.g. you > incref() in copy constructors and operator=(), decref() in > destructors). Right. I have to test this line in C++: insert_object("a", c2py_int(3)); if it's leaking, then I have a problem. Well, if it's leaking, then I can fix insert_object to steal ownership of the PyObject and I should be fine hopefully. > > > Moral: Once you get a decent knowledge of Python's C-API, you start to > REALLY appreciate the Cython's (and Pyrex's) magic... This is the > reason I've switched all my projects to Cython, starting from scratch > and thorowing away all the previous hand-written C crap! > > Of course, working in C++ is other story... because the compiler can > help you to manage these references (using those proxy classes). I'd like to avoid proxy classes if possible. Ondrej From ondrej at certik.cz Sat Mar 13 05:19:26 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Fri, 12 Mar 2010 20:19:26 -0800 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003121946s21a67aa6i742ba6b592bcc199@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> <85b5c3131003121657u2c3623abjf835f230ee658b3e@mail.gmail.com> <85b5c3131003121946s21a67aa6i742ba6b592bcc199@mail.gmail.com> Message-ID: <85b5c3131003122019q47332ac5i7934db7a18df66a7@mail.gmail.com> On Fri, Mar 12, 2010 at 7:46 PM, Ondrej Certik wrote: > Hi Lisandro! > > On Fri, Mar 12, 2010 at 7:25 PM, Lisandro Dalcin wrote: >>>> >>>> Can you explain me how are you managing reference counting? I think >>>> that you will need a proxy C++ class to manage incref()/decref() >>> >>> Here are the definitions of py2c_int and c2py_int: >>> >>> cdef api object c2py_int(int i): >>> ? ?return i >>> >> >> This function will return a brand new object (or a new reference of a >> caches Python intinstance, this is a CPython implementation detail)... >> Anyway, if you call this from C/C++, you have to somehow take >> ownership of that object, of use it and throw it away (i.e. decref) >> >>> >>> cdef api int py2c_int(object i): >>> ? ?return i >>> >> >> This functions just take a (borrowed) object as its argument... >> >> So if you do: >> >> py2c_int( c2py_int(7) ) >> >> you have just leaked a reference :-) ... > > Yep. But the c2py_int() is only meant to use with insert_object(), > (that I'll rename to push): > > cdef api void insert_object(const_char_p name, object o): > ? ? global_namespace.update({name: o}) > > where global_namespace is a dictionary. All in Cython. Example: > > insert_object("a", c2py_int(3)); > > will this create a leak? e.g. if I do this in Cython: > > insert_object("a", c2py_int(3)) > > there should be no leak, right? Will it create a leak when executed > from C++? I am not sure now, I'd have to test it. the Cython version produces this C code: __pyx_t_1 = c2py_int(7); insert_object(__pyx_k__a, __pyx_t_1); __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; So indeed, I think I have a leak if I just do: insert_object(__pyx_k__a, c2py_int(7)); What's the best way to fix it? Calling Py_DECREF() by hand in insert_object and adding a strong warning in the comments, that it is ok to call "insert_object(__pyx_k__a, c2py_int(7))" from C, but one should never do that in Cython? Ondrej From craigcitro at gmail.com Sat Mar 13 06:52:36 2010 From: craigcitro at gmail.com (Craig Citro) Date: Fri, 12 Mar 2010 21:52:36 -0800 Subject: [Cython] new C++ syntax error In-Reply-To: References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> Message-ID: >> I am trying the new C++ syntax. I got the following error (all files attached): >> >> $ cython _hermes_common.pyx >> ... >> ? ?if len(self.attributes) != 0: >> TypeError: object of type 'NoneType' has no len() >> > > Yep, that's a bug. I'm pretty sure you just want to change that line > in Cython/Compiler/Nodes.py from "if len(self.attributes) != 0:" to > just "if self.attributes:" and you'll be good. > > Amusingly, I tried to grab the source files you attached and test this > -- and hit a different bug (a problem in parsing). I don't know why -- > you just posted and confirmed that we're using the same version > (before I even asked!). Is there any chance your source files are > slightly out of sync with the ones you posted? (Maybe forgot to save > changes in your editor before posting?) Has anyone else tried and hit > the same bug, or is my setup wonky? > So I finally got a chance to sit down and look at this today. The first bug was easy, and luckily Robert Bradshaw was sitting right next to me for the second -- he immediately called the function where the error was going to be, and it was just a question of spotting it. I'm running tests right now, and I'll push as soon as those are done. I also had to make a few changes to your .pyx and .pxd files -- mostly little things. I'm attaching the versions I used, which now compile just fine. (I didn't actually test them at all, not having any of the code they're written against, just checked that they seem to be producing sensible .cpp files.) Definitely let us know if/when you run into more trouble -- a lot of this new C++ code just hasn't been run through its paces, and it's good to work the little bugs out. -cc -------------- next part -------------- A non-text attachment was scrubbed... Name: _hermes_common.pxd Type: application/octet-stream Size: 2672 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100312/9db389cd/attachment.obj -------------- next part -------------- A non-text attachment was scrubbed... Name: _hermes_common.pyx Type: application/octet-stream Size: 10792 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100312/9db389cd/attachment-0001.obj From dagss at student.matnat.uio.no Sat Mar 13 07:45:02 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Sat, 13 Mar 2010 07:45:02 +0100 Subject: [Cython] using the C API Declarations in several C++ files In-Reply-To: <85b5c3131003121657u2c3623abjf835f230ee658b3e@mail.gmail.com> References: <85b5c3131003031222n26bdd2beo983448ea6ce35a2d@mail.gmail.com> <85b5c3131003031808i48f71b0al2f08b49793c65a5b@mail.gmail.com> <85b5c3131003101549n74dfc53eo3ab45d5ece08ec43@mail.gmail.com> <85b5c3131003101601i4775512ev9094e49f950a5b66@mail.gmail.com> <85b5c3131003101603i7f4a4845i2a6beeface441e28@mail.gmail.com> <85b5c3131003111624n647dc0dahfc7e3c5ce6561e35@mail.gmail.com> <85b5c3131003111814t4ff56fe9t88caca6f44c7c203@mail.gmail.com> <85b5c3131003121657u2c3623abjf835f230ee658b3e@mail.gmail.com> Message-ID: <4B9B346E.5030104@student.matnat.uio.no> Ondrej Certik wrote: > On Fri, Mar 12, 2010 at 6:28 AM, Lisandro Dalcin wrote: >> On 11 March 2010 23:14, Ondrej Certik wrote: > [...] >>> but the rest will be a bit more complicated. Let me know if you think >>> this is the right approach. I guess I could add some cython cmdline >>> option to control the global/local api. >>> >> How about controling global/local api at C compile time, using >> preprocessor definitions? > > But how about the generation of the .cpp file? I need cython to > generate it. But most users imho just need the .h file. > >>> p->insert_object("i", c2py_int(5)); >>> >> I would prefer p->put("i", c2py_int(5)) > > As I wrote, I would actually prefer push. That's what ipython uses > (Brian suggested this to me) and I like it. E.g. push/pull. > > Yours idea would be put/get. I guess that's cool too. I am not a > native speaker, so my intuition might be wrong, but I like to think of > pushing the variables in there, rather than just putting them there. > :) > >>> int i = py2c_int(p->get_object("i")); >>> >> Can you explain me how are you managing reference counting? I think >> that you will need a proxy C++ class to manage incref()/decref() > > Here are the definitions of py2c_int and c2py_int: > > cdef api object c2py_int(int i): > return i > > cdef api int py2c_int(object i): > return i This sounds like the kind of stuff Convert-XY is doing, except Convert-XY supports many more types... http://conference.scipy.org/proceedings/SciPy2009/paper_4/ """ We present Convert-XY: a new, header-only template library for converting containers between C++ and Python with a simple, succinct syntax. At compile-time, template-based recursive pattern matching is performed on the static structure of the C++ type to build dynamic type checkers and conversion functions. """ -- Dag Sverre From stefan_ml at behnel.de Sat Mar 13 08:39:49 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 13 Mar 2010 08:39:49 +0100 Subject: [Cython] [HELP] broken except A,B In-Reply-To: References: Message-ID: <4B9B4145.1050203@behnel.de> Lisandro Dalcin, 12.03.2010 22:33: > See the testcase below. If the body of the except clause is empty, 'e' > never gets assigned to the exception value, ie... > > """ >>>> repr(e) > 'ValueError()' > """ > > e = None > try: > raise ValueError > except ValueError, e: > #a = 1 > pass > > Could any of you fix this in order I can properly implement and test > "except A as B" ?? The fix should be done in "Nodes.py", at > "generate_handling_code()" ... The obvious patch is the one below, but > perhaps you can imagine a better way? > > diff -r 3932a0be3b6b Cython/Compiler/Nodes.py > --- a/Cython/Compiler/Nodes.py Thu Mar 11 18:57:06 2010 -0300 > +++ b/Cython/Compiler/Nodes.py Fri Mar 12 18:30:43 2010 -0300 > @@ -4497,14 +4497,6 @@ > else: > code.putln("/*except:*/ {") > > - if not getattr(self.body, 'stats', True): > - # most simple case: no exception variable, empty body (pass) > - # => reset the exception state, done > - code.putln("PyErr_Restore(0,0,0);") > - code.put_goto(end_label) > - code.putln("}") > - return > - > exc_vars = [code.funcstate.allocate_temp(py_object_type, > manage_ref=True) > for i in xrange(3)] Funny. I just tried this with Py3 and it gives me this: Python 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> e = None >>> try: raise ValueError ... except ValueError as e: pass ... >>> e Traceback (most recent call last): File "", line 1, in NameError: name 'e' is not defined So you could argue that Cython is better behaved than Py3 here. ;) I files a bug report on Py3 for now. http://bugs.python.org/issue8130 Anyway, given that keeping the variable inside of the except block is the expected behaviour in Py3, I think this should not be 'fixed'. > PS: Is this an instance of "premature optimization is the root of all > evil" ?? I actually implemented it at the time because Python 3 was supposed to behave like this, and the reason for the change in Py3 was that exceptions (and their tracebacks, i.e. all impacted frames!) should never be kept alive any longer than strictly necessary. The official way to get the behaviour you want in Py3 is this: e = None try: raise ValueError except ValueError as err: e = err with 'err' not being defined outside of the except block. Stefan From dagss at student.matnat.uio.no Sat Mar 13 09:13:40 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Sat, 13 Mar 2010 09:13:40 +0100 Subject: [Cython] [HELP] broken except A,B In-Reply-To: <4B9B4145.1050203@behnel.de> References: <4B9B4145.1050203@behnel.de> Message-ID: <4B9B4934.4020803@student.matnat.uio.no> Stefan Behnel wrote: > Lisandro Dalcin, 12.03.2010 22:33: >> See the testcase below. If the body of the except clause is empty, 'e' >> never gets assigned to the exception value, ie... >> >> """ >>>>> repr(e) >> 'ValueError()' >> """ >> >> e = None >> try: >> raise ValueError >> except ValueError, e: >> #a = 1 >> pass >> >> Could any of you fix this in order I can properly implement and test >> "except A as B" ?? The fix should be done in "Nodes.py", at >> "generate_handling_code()" ... The obvious patch is the one below, but >> perhaps you can imagine a better way? >> >> diff -r 3932a0be3b6b Cython/Compiler/Nodes.py >> --- a/Cython/Compiler/Nodes.py Thu Mar 11 18:57:06 2010 -0300 >> +++ b/Cython/Compiler/Nodes.py Fri Mar 12 18:30:43 2010 -0300 >> @@ -4497,14 +4497,6 @@ >> else: >> code.putln("/*except:*/ {") >> >> - if not getattr(self.body, 'stats', True): >> - # most simple case: no exception variable, empty body (pass) >> - # => reset the exception state, done >> - code.putln("PyErr_Restore(0,0,0);") >> - code.put_goto(end_label) >> - code.putln("}") >> - return >> - >> exc_vars = [code.funcstate.allocate_temp(py_object_type, >> manage_ref=True) >> for i in xrange(3)] > > Funny. I just tried this with Py3 and it gives me this: > > > Python 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00) > [GCC 4.4.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> e = None > >>> try: raise ValueError > ... except ValueError as e: pass > ... > >>> e > Traceback (most recent call last): > File "", line 1, in > NameError: name 'e' is not defined > > > So you could argue that Cython is better behaved than Py3 here. ;) > > I files a bug report on Py3 for now. > > http://bugs.python.org/issue8130 > > Anyway, given that keeping the variable inside of the except block is the > expected behaviour in Py3, I think this should not be 'fixed'. > > > > PS: Is this an instance of "premature optimization is the root of all > > evil" ?? > > I actually implemented it at the time because Python 3 was supposed to > behave like this, and the reason for the change in Py3 was that exceptions > (and their tracebacks, i.e. all impacted frames!) should never be kept > alive any longer than strictly necessary. The official way to get the > behaviour you want in Py3 is this: > > e = None > try: raise ValueError > except ValueError as err: > e = err > > with 'err' not being defined outside of the except block. Hmm. Reading the response to that bug, I'm worried as to how Cython can behave as to not mess up either Python 2 or Python 3 code. Proposal: We simply don't allow variable names in an "as" clause (or ","-claus, obviously) which are also found elsewhere in the local scope (other as-clauses is OK, obviously). This makes sure that Cython users write code which complies both with Python 2 and 3, and can always be backwards-compatibly lifted when Python 2 is irrelevant. Any Cython code which breaks because of this would break with Python 3 semantics as well, so I guess that breakage is already made. Not sure how easy it is to implement though... -- Dag Sverre From robertwb at math.washington.edu Sat Mar 13 09:25:06 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Sat, 13 Mar 2010 00:25:06 -0800 Subject: [Cython] [HELP] broken except A,B In-Reply-To: <4B9B4145.1050203@behnel.de> References: <4B9B4145.1050203@behnel.de> Message-ID: <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> On Mar 12, 2010, at 11:39 PM, Stefan Behnel wrote: > Lisandro Dalcin, 12.03.2010 22:33: >> See the testcase below. If the body of the except clause is empty, >> 'e' >> never gets assigned to the exception value, ie... >> >> """ >>>>> repr(e) >> 'ValueError()' >> """ >> >> e = None >> try: >> raise ValueError >> except ValueError, e: >> #a = 1 >> pass >> >> Could any of you fix this in order I can properly implement and test >> "except A as B" ?? The fix should be done in "Nodes.py", at >> "generate_handling_code()" ... The obvious patch is the one below, >> but >> perhaps you can imagine a better way? >> >> diff -r 3932a0be3b6b Cython/Compiler/Nodes.py >> --- a/Cython/Compiler/Nodes.py Thu Mar 11 18:57:06 2010 -0300 >> +++ b/Cython/Compiler/Nodes.py Fri Mar 12 18:30:43 2010 -0300 >> @@ -4497,14 +4497,6 @@ >> else: >> code.putln("/*except:*/ {") >> >> - if not getattr(self.body, 'stats', True): >> - # most simple case: no exception variable, empty body >> (pass) >> - # => reset the exception state, done >> - code.putln("PyErr_Restore(0,0,0);") >> - code.put_goto(end_label) >> - code.putln("}") >> - return >> - >> exc_vars = [code.funcstate.allocate_temp(py_object_type, >> manage_ref=True) >> for i in xrange(3)] > > Funny. I just tried this with Py3 and it gives me this: > > > Python 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00) > [GCC 4.4.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> e = None >>>> try: raise ValueError > ... except ValueError as e: pass > ... >>>> e > Traceback (most recent call last): > File "", line 1, in > NameError: name 'e' is not defined > > > So you could argue that Cython is better behaved than Py3 here. ;) > > I files a bug report on Py3 for now. > > http://bugs.python.org/issue8130 > > Anyway, given that keeping the variable inside of the except block > is the > expected behaviour in Py3, I think this should not be 'fixed'. In the face of differing behavior Cython follows Python 2.x scoping conventions, e.g. the same issue arises for list comprehensions. In any case, it's bad that the empty except block behaves differently than the non-empty one. >> PS: Is this an instance of "premature optimization is the root of all >> evil" ?? > > I actually implemented it at the time because Python 3 was supposed to > behave like this, and the reason for the change in Py3 was that > exceptions > (and their tracebacks, i.e. all impacted frames!) should never be kept > alive any longer than strictly necessary. The official way to get the > behaviour you want in Py3 is this: > > e = None > try: raise ValueError > except ValueError as err: > e = err > > with 'err' not being defined outside of the except block. The problem is this comment: >> # most simple case: no exception variable, empty body (pass) which is *not* what is being tested. Anyone opposed to diff -r 587136bd1d75 Cython/Compiler/Nodes.py --- a/Cython/Compiler/Nodes.py Sat Mar 13 08:24:13 2010 +0100 +++ b/Cython/Compiler/Nodes.py Sat Mar 13 00:24:33 2010 -0800 @@ -4497,7 +4497,8 @@ else: code.putln("/*except:*/ {") - if not getattr(self.body, 'stats', True): + if not getattr(self.body, 'stats', True) and + self.excinfo_target is None and self.target is None): # most simple case: no exception variable, empty body (pass) # => reset the exception state, done code.putln("PyErr_Restore(0,0,0);") - Robert From robertwb at math.washington.edu Sat Mar 13 09:26:25 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Sat, 13 Mar 2010 00:26:25 -0800 Subject: [Cython] GCC warnings In-Reply-To: <4B9A1F41.4010509@canterbury.ac.nz> References: <85b5c3131003102238m6e48f8dfyf8c1239a36521746@mail.gmail.com> <85b5c3131003111146k5e2489dg976b6e0dc8cdb50d@mail.gmail.com> <1B795BDA-338C-4EC0-963F-26220A56B200@math.washington.edu> <4B9A1F41.4010509@canterbury.ac.nz> Message-ID: <0992DB2A-6D1F-43D6-81D8-E9F1ECD58BFF@math.washington.edu> On Mar 12, 2010, at 3:02 AM, Greg Ewing wrote: > Robert Bradshaw wrote: > >> Hmm... I thought that "except X" would catch all exceptions that were >> subclasses of X, but it turns out that it doesn't check inheritance >> unless X is an exception type... I'll fix this. > > Also remember that before 2.5, exceptions were old-style > classes, which are *not* subclasses of object. So this would > never have worked in any version of Python. Yep. In any case, a better fix is in now. - Robert From stefan_ml at behnel.de Sat Mar 13 10:25:59 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 13 Mar 2010 10:25:59 +0100 Subject: [Cython] [HELP] broken except A,B In-Reply-To: <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> Message-ID: <4B9B5A27.6090708@behnel.de> Robert Bradshaw, 13.03.2010 09:25: >> >>> e = None >> >>> try: raise ValueError >> ... except ValueError as e: pass >> ... >> >>> e > > In the face of differing behavior Cython follows Python 2.x scoping > conventions, e.g. the same issue arises for list comprehensions. It's actually very easy for the exception case. You could have Py2 scoping for except ValueError, e: and Py3 scoping for except ValueError as e: Although I would prefer having the same (Py3) rules for both. I think Cython should generally try to follow Py3 also for the corner case of list comps. The use cases where the behaviour differs (i.e. when the internal names are used outside of the comp scope) are rarely used, generally known to be broken by Py3 anyway, and easy to fix (and clean up) by not using a list-comp in the first place. So I propose that Cython code that relies on them now should be considered relying on undefined behaviour and allowed to be broken in a future Cython version. What do you think? > In any case, it's bad that the empty except block behaves differently > than the non-empty one. Sure. > The problem is this comment: > >>> # most simple case: no exception variable, empty body (pass) > > which is *not* what is being tested. Agreed. > Anyone opposed to > > diff -r 587136bd1d75 Cython/Compiler/Nodes.py > --- a/Cython/Compiler/Nodes.py Sat Mar 13 08:24:13 2010 +0100 > +++ b/Cython/Compiler/Nodes.py Sat Mar 13 00:24:33 2010 -0800 > @@ -4497,7 +4497,8 @@ > else: > code.putln("/*except:*/ {") > > - if not getattr(self.body, 'stats', True): > + if not getattr(self.body, 'stats', True) and > + self.excinfo_target is None and self.target is None): > # most simple case: no exception variable, empty body > (pass) > # => reset the exception state, done > code.putln("PyErr_Restore(0,0,0);") Fine with me. It doesn't make much sense to assign the exception to a variable on an empty except block, unless you explicitly want it to stay available after the block (thus depending on Py2 semantics). Stefan From dagss at student.matnat.uio.no Sat Mar 13 10:51:31 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Sat, 13 Mar 2010 10:51:31 +0100 Subject: [Cython] [HELP] broken except A,B In-Reply-To: <4B9B5A27.6090708@behnel.de> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> Message-ID: <4B9B6023.2070703@student.matnat.uio.no> Stefan Behnel wrote: > Robert Bradshaw, 13.03.2010 09:25: >>>>>> e = None >>>>>> try: raise ValueError >>> ... except ValueError as e: pass >>> ... >>>>>> e >> In the face of differing behavior Cython follows Python 2.x scoping >> conventions, e.g. the same issue arises for list comprehensions. > > It's actually very easy for the exception case. You could have Py2 scoping for > > except ValueError, e: > > and Py3 scoping for > > except ValueError as e: -1 from me on that one -- way too obscure. -- Dag Sverre From stefan_ml at behnel.de Sat Mar 13 11:00:53 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 13 Mar 2010 11:00:53 +0100 Subject: [Cython] [HELP] broken except A,B In-Reply-To: <4B9B6023.2070703@student.matnat.uio.no> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <4B9B6023.2070703@student.matnat.uio.no> Message-ID: <4B9B6255.9090606@behnel.de> Dag Sverre Seljebotn, 13.03.2010 10:51: > Stefan Behnel wrote: >> Robert Bradshaw, 13.03.2010 09:25: >>>> >>> e = None >>>> >>> try: raise ValueError >>>> ... except ValueError as e: pass >>>> ... >>>> >>> e >>> In the face of differing behavior Cython follows Python 2.x scoping >>> conventions, e.g. the same issue arises for list comprehensions. >> >> It's actually very easy for the exception case. You could have Py2 scoping for >> >> except ValueError, e: >> >> and Py3 scoping for >> >> except ValueError as e: > > -1 from me on that one -- way too obscure. I can't really share your concern here. We are talking about two different spellings that are well defined as having different semantics. If you know about the second, you'll expect Py3 semantics when you use it. If you use the first, you'll expect Py2 semantics. For both, you likely won't care about the difference in most cases. Anyway, I could certainly live with Py3 semantics for both. :) Stefan From robertwb at math.washington.edu Sat Mar 13 11:26:56 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Sat, 13 Mar 2010 02:26:56 -0800 Subject: [Cython] [HELP] broken except A,B In-Reply-To: <4B9B5A27.6090708@behnel.de> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> Message-ID: <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> On Mar 13, 2010, at 1:25 AM, Stefan Behnel wrote: > Robert Bradshaw, 13.03.2010 09:25: >>>>>> e = None >>>>>> try: raise ValueError >>> ... except ValueError as e: pass >>> ... >>>>>> e >> >> In the face of differing behavior Cython follows Python 2.x scoping >> conventions, e.g. the same issue arises for list comprehensions. > > It's actually very easy for the exception case. You could have Py2 > scoping for > > except ValueError, e: > > and Py3 scoping for > > except ValueError as e: > > Although I would prefer having the same (Py3) rules for both. This may be a bit confusing for some, but I could live with that. > I think Cython should generally try to follow Py3 also for the > corner case > of list comps. The use cases where the behaviour differs (i.e. when > the > internal names are used outside of the comp scope) are rarely used, > generally known to be broken by Py3 anyway, and easy to fix (and > clean up) > by not using a list-comp in the first place. So I propose that > Cython code > that relies on them now should be considered relying on undefined > behaviour > and allowed to be broken in a future Cython version. > > What do you think? The stated goal for Cython 1.0 is to compile and (correctly) run any Python code. In practice, this means I should be able to take any piece of Python code and safely compile and run it without any changes (perhaps with a --pedantic flag if some corner cases are considered to expensive to not be worth handling, though none come to mind at the moment). This can't be the case if the Cython language (which is also too ill-defined) is a mixture of Py2 and Py3 semantics (where the syntaxes overlap). Given that the we currently have Py2 semantics for nearly everything, and have the __future__ directives, until we hit 1.0 at least it makes the most sense to stick with that. (It's also inline with the "easiest way to port code to 3.x :)). Thus the refined goal would be be supporting any Python 2.6 code compiled against Python 2.6 to have exactly the same behavior (modulo obvious differences like introspection) as if it were interpreted. I think it would be a good idea to have flag/directive for compiling code with Python 3 semantics, and perhaps eventually switch over to that by default (or in a 3.x context?) after throwing in some warning/ deprecation code in there, but the time to make such backwards incompatible changes is not now. - Robert From robertwb at math.washington.edu Sat Mar 13 11:36:58 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Sat, 13 Mar 2010 02:36:58 -0800 Subject: [Cython] [HELP] broken except A,B In-Reply-To: <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> Message-ID: <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> On Mar 13, 2010, at 2:26 AM, Robert Bradshaw wrote: > The stated goal for Cython 1.0 is to compile and (correctly) run any > Python code. In practice, this means I should be able to take any > piece of Python code and safely compile and run it without any changes > (perhaps with a --pedantic flag if some corner cases are considered to > expensive to not be worth handling, though none come to mind at the > moment). This can't be the case if the Cython language (which is also > too ill-defined) is a mixture of Py2 and Py3 semantics (where the > syntaxes overlap). Given that the we currently have Py2 semantics for > nearly everything, and have the __future__ directives, until we hit > 1.0 at least it makes the most sense to stick with that. (It's also > inline with the "easiest way to port code to 3.x :)). Thus the refined > goal would be be supporting any Python 2.6 code compiled against > Python 2.6 to have exactly the same behavior (modulo obvious > differences like introspection) as if it were interpreted. > > I think it would be a good idea to have flag/directive for compiling > code with Python 3 semantics, and perhaps eventually switch over to > that by default (or in a 3.x context?) after throwing in some warning/ > deprecation code in there, but the time to make such backwards > incompatible changes is not now. That was a bit rambly. My main points are - With closures around the corner, and generators soon after, we're getting to the point that we need a very specific goal and metric for "total Python compatability." - With all the other changes going on, I don't think switching things over to Py3 semantics by default is a good move right now. Better to do it when things are more stable. - Robert From stefan_ml at behnel.de Sat Mar 13 14:30:46 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 13 Mar 2010 14:30:46 +0100 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> Message-ID: <4B9B9386.8040606@behnel.de> Robert Bradshaw, 13.03.2010 11:36: > On Mar 13, 2010, at 2:26 AM, Robert Bradshaw wrote: > >> The stated goal for Cython 1.0 is to compile and (correctly) run any >> Python code. In practice, this means I should be able to take any >> piece of Python code and safely compile and run it without any changes >> (perhaps with a --pedantic flag if some corner cases are considered to >> expensive to not be worth handling, though none come to mind at the >> moment). This can't be the case if the Cython language (which is also >> too ill-defined) is a mixture of Py2 and Py3 semantics (where the >> syntaxes overlap). Given that the we currently have Py2 semantics for >> nearly everything, and have the __future__ directives, until we hit >> 1.0 at least it makes the most sense to stick with that. (It's also >> inline with the "easiest way to port code to 3.x :)). Thus the refined >> goal would be be supporting any Python 2.6 code compiled against >> Python 2.6 to have exactly the same behavior (modulo obvious >> differences like introspection) as if it were interpreted. >> >> I think it would be a good idea to have flag/directive for compiling >> code with Python 3 semantics, and perhaps eventually switch over to >> that by default (or in a 3.x context?) after throwing in some warning/ >> deprecation code in there, but the time to make such backwards >> incompatible changes is not now. > > That was a bit rambly. My main points are > > - With closures around the corner, and generators soon after, > we're getting to the point that we need a very specific goal and > metric for "total Python compatability." > - With all the other changes going on, I don't think switching > things over to Py3 semantics by default is a good move right now. > Better to do it when things are more stable. With Py2.7 ahead, which will have most of the features of Py3 except for the really backwards incompatible stuff (and potentially some things that no-one cares to port), it'll be hard to claim compatibility even with "Py2.x". Then, there's actually three things to consider here: a) compatibility with Py2 code (-> when compiling .py files) b) compatibility with Py3 code (-> .py files + "-3" option or "cython3") c) the Cython language (-> .pyx files) with the obvious gray area of future imports between a)/c) and b). We don't currently care about the file extension, for example. Should we generally stop supporting the 'cdef' keyword in .py files? I think we need to do that if we really want to introduce a well defined notion of "language level compatibility". Stefan From robertwb at math.washington.edu Sat Mar 13 21:07:52 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Sat, 13 Mar 2010 12:07:52 -0800 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: <4B9B9386.8040606@behnel.de> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> Message-ID: On Mar 13, 2010, at 5:30 AM, Stefan Behnel wrote: > Robert Bradshaw, 13.03.2010 11:36: >> On Mar 13, 2010, at 2:26 AM, Robert Bradshaw wrote: >> >>> The stated goal for Cython 1.0 is to compile and (correctly) run any >>> Python code. In practice, this means I should be able to take any >>> piece of Python code and safely compile and run it without any >>> changes >>> (perhaps with a --pedantic flag if some corner cases are >>> considered to >>> expensive to not be worth handling, though none come to mind at the >>> moment). This can't be the case if the Cython language (which is >>> also >>> too ill-defined) is a mixture of Py2 and Py3 semantics (where the >>> syntaxes overlap). Given that the we currently have Py2 semantics >>> for >>> nearly everything, and have the __future__ directives, until we hit >>> 1.0 at least it makes the most sense to stick with that. (It's also >>> inline with the "easiest way to port code to 3.x :)). Thus the >>> refined >>> goal would be be supporting any Python 2.6 code compiled against >>> Python 2.6 to have exactly the same behavior (modulo obvious >>> differences like introspection) as if it were interpreted. >>> >>> I think it would be a good idea to have flag/directive for compiling >>> code with Python 3 semantics, and perhaps eventually switch over to >>> that by default (or in a 3.x context?) after throwing in some >>> warning/ >>> deprecation code in there, but the time to make such backwards >>> incompatible changes is not now. >> >> That was a bit rambly. My main points are >> >> - With closures around the corner, and generators soon after, >> we're getting to the point that we need a very specific goal and >> metric for "total Python compatability." >> - With all the other changes going on, I don't think switching >> things over to Py3 semantics by default is a good move right now. >> Better to do it when things are more stable. > > With Py2.7 ahead, which will have most of the features of Py3 except > for > the really backwards incompatible stuff (and potentially some things > that > no-one cares to port), it'll be hard to claim compatibility even > with "Py2.x". > > Then, there's actually three things to consider here: > > a) compatibility with Py2 code (-> when compiling .py files) > b) compatibility with Py3 code (-> .py files + "-3" option or > "cython3") > c) the Cython language (-> .pyx files) > > with the obvious gray area of future imports between a)/c) and b). > > We don't currently care about the file extension, for example. > Should we > generally stop supporting the 'cdef' keyword in .py files? The only place that the extension currently matters is that with a .py file, signatures and other declarations don't have to match what they are in the .pxd file (the ones from the .pxd file get pulled over). This may result in compile time errors, when renaming a .py file to a .pyx file, which I'm OK with, but we should definitely avoid any behavioral changes due to changing the file extension. The -3 option would be completely orthogonal to the file extension, and could be used with both .py and .pyx files. > I think we need to do that if we really want to introduce a well > defined > notion of "language level compatibility". What I want is that Cython is a superset of Python. In particular, for widespread use it should be safe (barring bugs) to just compile a .py file and run it. I don't think our "language level compatibility" needs to be more granular than Py2 (meaning specifically 2.6, or maybe 2.7) and Py3 (meaning 3.1?), at least not right now (there are bigger fish to fry). - Robert From dalcinl at gmail.com Sat Mar 13 23:46:09 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Sat, 13 Mar 2010 19:46:09 -0300 Subject: [Cython] Pyhon 3 exception clauses In-Reply-To: References: <4B9A8193.1070406@behnel.de> Message-ID: On 12 March 2010 15:17, Lisandro Dalcin wrote: > On 12 March 2010 15:01, Stefan Behnel wrote: >> Lisandro Dalcin, 12.03.2010 07:21: >>> What do you think? I'd be surprised if I'm not missing something, but >>> this is an starter... >> >> Hmm, interesting. Looks like I forgot to open a ticket for that when I went >> through the Py3 syntax differences lately. So you'll have to do that >> yourself (just for documentation purposes, pushing this fix is fine with >> me, as long as you add a couple of tests). >> http://trac.cython.org/cython_trac/ticket/519 http://hg.cython.org/cython-devel/rev/6834b6f640ae >> >>> ? ? ? ? ? if s.sy == ',': >>> ? ? ? ? ? ? ? s.next() >>> ? ? ? ? ? ? ? exc_value = p_simple_expr(s) >>> + ? ? ? ?elif s.sy == 'IDENT' and s.systring == 'as': >>> + ? ? ? ? ? ?s.next() >>> + ? ? ? ? ? ?exc_value = p_simple_expr(s) >> >> Personally, I'd merge the two conditions into one, but that's just aesthetics. >> I did not merged the conditions, just in case we later decide on a -3 switch for Python 3 semantics... BTW, I've left a commented block with what I understand it should be the semantics for Python 3 (please, review). -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From robertwb at math.washington.edu Sun Mar 14 03:12:53 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Sat, 13 Mar 2010 18:12:53 -0800 Subject: [Cython] new C++ syntax error In-Reply-To: <85b5c3131003111145g239cbae6tc927b9740f7153c1@mail.gmail.com> References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> <4B989F4F.9030606@behnel.de> <85b5c3131003111145g239cbae6tc927b9740f7153c1@mail.gmail.com> Message-ID: <1C48C1FB-0471-407A-9DBE-A29CD0786E41@math.washington.edu> On Mar 11, 2010, at 11:45 AM, Ondrej Certik wrote: > On Thu, Mar 11, 2010 at 12:00 AM, Craig Citro > wrote: >>> Did you run "python setup.py build_ext -i" locally? The compiled >>> Cython >>> parser is not automatically rebuilt on source changes. Try "make >>> clean". >>> >> >> Oh, it was just PEBKAC -- I had already made the fix I suggested to >> Ondrej in my local copy of Cython before I even downloaded the >> example. :) >> >> Ondrej -- even with that fix, you hit some other issues. I'm not >> going >> to look at this now -- hopefully tomorrow night or Friday, if no one >> beats me to it. > > I don't have time for this either now, I just wanted to quickly try it > on my project yesterday before going to sleep, and give you guys some > feedback. In general, I am impressed with this, it makes wrapping C++ > way easier. Yes, it's so much cleaner. It's a lot of new code, and has far too few tests yet, so thanks for being adventurous and trying this out on real world code so we can at least get rid of some bugs that we didn't think of before we release. Let us know if (I should probably just say when) you find anything else. - Robert From stefan_ml at behnel.de Sun Mar 14 08:10:47 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 14 Mar 2010 08:10:47 +0100 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> Message-ID: <4B9C8BF7.8000200@behnel.de> Robert Bradshaw, 13.03.2010 21:07: > On Mar 13, 2010, at 5:30 AM, Stefan Behnel wrote: >> a) compatibility with Py2 code (-> when compiling .py files) >> b) compatibility with Py3 code (-> .py files + "-3" option or >> "cython3") >> c) the Cython language (-> .pyx files) >> >> We don't currently care about the file extension, for example. >> Should we generally stop supporting the 'cdef' keyword in .py files? > > The only place that the extension currently matters is that with a .py > file, signatures and other declarations don't have to match what they > are in the .pxd file (the ones from the .pxd file get pulled over). > This may result in compile time errors, when renaming a .py file to > a .pyx file, which I'm OK with, but we should definitely avoid any > behavioral changes due to changing the file extension. The only reason why a user would choose the .py extension is because the code should work also in plain CPython, i.e. purely pure mode. So, what I meant was: when compiling a .py file, we should keep Cython specific syntax (especially keywords) out of the way, so that Cython can compile as much Python code as possible. I agree that it shouldn't result in any semantic differences for Python constructs. > The -3 option would be completely orthogonal to the file extension, > and could be used with both .py and .pyx files. Makes sense. >> I think we need to do that if we really want to introduce a well >> defined notion of "language level compatibility". > > What I want is that Cython is a superset of Python. In particular, for > widespread use it should be safe (barring bugs) to just compile a .py > file and run it. I don't think our "language level compatibility" > needs to be more granular than Py2 (meaning specifically 2.6, or maybe > 2.7) and Py3 (meaning 3.1?), at least not right now (there are bigger > fish to fry). Ok, let's assume that most Py3 syntax that we support will make it into Py2.7 anyway, so targeting something close to 2.7 should work. Stefan From robertwb at math.washington.edu Sun Mar 14 09:42:18 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Sun, 14 Mar 2010 00:42:18 -0800 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: <4B9C8BF7.8000200@behnel.de> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> <4B9C8BF7.8000200@behnel.de> Message-ID: On Mar 13, 2010, at 11:10 PM, Stefan Behnel wrote: > Robert Bradshaw, 13.03.2010 21:07: >> On Mar 13, 2010, at 5:30 AM, Stefan Behnel wrote: >>> a) compatibility with Py2 code (-> when compiling .py files) >>> b) compatibility with Py3 code (-> .py files + "-3" option or >>> "cython3") >>> c) the Cython language (-> .pyx files) >>> >>> We don't currently care about the file extension, for example. >>> Should we generally stop supporting the 'cdef' keyword in .py files? >> >> The only place that the extension currently matters is that with >> a .py >> file, signatures and other declarations don't have to match what they >> are in the .pxd file (the ones from the .pxd file get pulled over). >> This may result in compile time errors, when renaming a .py file to >> a .pyx file, which I'm OK with, but we should definitely avoid any >> behavioral changes due to changing the file extension. > > The only reason why a user would choose the .py extension is because > the > code should work also in plain CPython, i.e. purely pure mode. So, > what I > meant was: when compiling a .py file, we should keep Cython specific > syntax > (especially keywords) out of the way, so that Cython can compile as > much > Python code as possible. Ah, yes, it would be good to support cdef = 4 in a .py file. >>> I think we need to do that if we really want to introduce a well >>> defined notion of "language level compatibility". >> >> What I want is that Cython is a superset of Python. In particular, >> for >> widespread use it should be safe (barring bugs) to just compile a .py >> file and run it. I don't think our "language level compatibility" >> needs to be more granular than Py2 (meaning specifically 2.6, or >> maybe >> 2.7) and Py3 (meaning 3.1?), at least not right now (there are bigger >> fish to fry). > > Ok, let's assume that most Py3 syntax that we support will make it > into > Py2.7 anyway, so targeting something close to 2.7 should work. Yes. 2.7 should be a backwards compatible superset of 2.6. Whichever one we pick I want to be able to say if it runs differently (or not at all) in Cython compared to Python 2.(6|7), that's a bug. (Likewise, if it runs differently in Cython with the -3 flag compared to a choice of Python 3.x, that's a bug.) - Robert From dagss at student.matnat.uio.no Sun Mar 14 10:27:18 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Sun, 14 Mar 2010 10:27:18 +0100 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> <4B9C8BF7.8000200@behnel.de> Message-ID: <4B9CABF6.6050203@student.matnat.uio.no> Robert Bradshaw wrote: > On Mar 13, 2010, at 11:10 PM, Stefan Behnel wrote: > >> Robert Bradshaw, 13.03.2010 21:07: >>> On Mar 13, 2010, at 5:30 AM, Stefan Behnel wrote: >>>> a) compatibility with Py2 code (-> when compiling .py files) >>>> b) compatibility with Py3 code (-> .py files + "-3" option or >>>> "cython3") >>>> c) the Cython language (-> .pyx files) >>>> >>>> We don't currently care about the file extension, for example. >>>> Should we generally stop supporting the 'cdef' keyword in .py files? >>> The only place that the extension currently matters is that with >>> a .py >>> file, signatures and other declarations don't have to match what they >>> are in the .pxd file (the ones from the .pxd file get pulled over). >>> This may result in compile time errors, when renaming a .py file to >>> a .pyx file, which I'm OK with, but we should definitely avoid any >>> behavioral changes due to changing the file extension. >> The only reason why a user would choose the .py extension is because >> the >> code should work also in plain CPython, i.e. purely pure mode. So, >> what I >> meant was: when compiling a .py file, we should keep Cython specific >> syntax >> (especially keywords) out of the way, so that Cython can compile as >> much >> Python code as possible. > > Ah, yes, it would be good to support > > cdef = 4 > > in a .py file. Perhaps the easiest thing here (once we start on this, which IMO is past 1.0) could be to switch to using the Python parser for .py files and transform the resulting tree to a Cython tree. Rather than litter Parsing.py with special cases. -- Dag Sverre From stefan_ml at behnel.de Sun Mar 14 10:52:13 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 14 Mar 2010 10:52:13 +0100 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: <4B9CABF6.6050203@student.matnat.uio.no> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> <4B9C8BF7.8000200@behnel.de> <4B9CABF6.6050203@student.matnat.uio.no> Message-ID: <4B9CB1CD.60406@behnel.de> Dag Sverre Seljebotn, 14.03.2010 10:27: > Robert Bradshaw wrote: >> Ah, yes, it would be good to support >> >> cdef = 4 >> >> in a .py file. > > Perhaps the easiest thing here (once we start on this, which IMO is past > 1.0) could be to switch to using the Python parser for .py files and > transform the resulting tree to a Cython tree. Rather than litter > Parsing.py with special cases. Oh, the above shouldn't be hard to add at all, I did this for the print_function future import already. All you have to do is make the scanner more configurable so that it changes the set of keywords based on the thing it's scanning through. If it returns 'cdef' as an identifier rather than a keyword, the parser will do the right thing (minus a couple of expected bugs, maybe). I would have expected the same for the C++ support, BTW, although I'm ok with the way it currently works. Stefan From stefan_ml at behnel.de Sun Mar 14 11:12:59 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 14 Mar 2010 11:12:59 +0100 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> <4B9C8BF7.8000200@behnel.de> Message-ID: <4B9CB6AB.7080708@behnel.de> Robert Bradshaw, 14.03.2010 09:42: > On Mar 13, 2010, at 11:10 PM, Stefan Behnel wrote: >> The only reason why a user would choose the .py extension is because >> the code should work also in plain CPython, i.e. purely pure mode. So, >> what I meant was: when compiling a .py file, we should keep Cython >> specific syntax (especially keywords) out of the way, so that Cython >> can compile as much Python code as possible. > > Ah, yes, it would be good to support > > cdef = 4 > > in a .py file. Actually, that's ticket #128 already: http://trac.cython.org/cython_trac/ticket/128 Stefan From stefan_ml at behnel.de Sun Mar 14 13:58:28 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 14 Mar 2010 13:58:28 +0100 Subject: [Cython] Pyhon 3 exception clauses In-Reply-To: References: <4B9A8193.1070406@behnel.de> Message-ID: <4B9CDD74.2050504@behnel.de> Lisandro Dalcin, 13.03.2010 23:46: >>>> if s.sy == ',': >>>> s.next() >>>> exc_value = p_simple_expr(s) >>>> + elif s.sy == 'IDENT' and s.systring == 'as': >>>> + s.next() >>>> + exc_value = p_simple_expr(s) > > I did not merged the conditions, just in case we later decide on a -3 > switch for Python 3 semantics... BTW, I've left a commented block with > what I understand it should be the semantics for Python 3 (please, > review). If we allow Py3 syntax, we should enforce the Py3 grammar correctly at this point, i.e. not allow arbitrary expressions as target but only names. I changed it to use the code that you provided as alternative. Stefan From stefan_ml at behnel.de Sun Mar 14 14:23:33 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 14 Mar 2010 14:23:33 +0100 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> Message-ID: <4B9CE355.2010001@behnel.de> Robert Bradshaw, 13.03.2010 21:07: > On Mar 13, 2010, at 5:30 AM, Stefan Behnel wrote: >> b) compatibility with Py3 code (-> .py files + "-3" option or >> "cython3") >> [...] >> with the obvious gray area of future imports between a)/c) and b). >> >> We don't currently care about the file extension, for example. >> Should we generally stop supporting the 'cdef' keyword in .py files? I disabled all Cython specific keywords when compiling .py files. http://hg.cython.org/cython-devel/rev/198e42d128dd > The -3 option would be completely orthogonal to the file extension, > and could be used with both .py and .pyx files. Actually, I'd even prefer having it a top-level compiler directive instead of a command line switch. If a source file is written in Py3 syntax, there's no use in making it optional to compile it as Py2 code and vice versa. So putting # cython: python3=True at the top is a lot safer and simpler. Maybe we could even be smart and pick up a shebang line like #!...python3 as Python 3.x installs its interpreter as 'python3' by default, but I guess that's hard to do reliably. Stefan From greg.ewing at canterbury.ac.nz Mon Mar 15 00:01:03 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Mon, 15 Mar 2010 12:01:03 +1300 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: <4B9CB1CD.60406@behnel.de> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> <4B9C8BF7.8000200@behnel.de> <4B9CABF6.6050203@student.matnat.uio.no> <4B9CB1CD.60406@behnel.de> Message-ID: <4B9D6AAF.4050103@canterbury.ac.nz> Stefan Behnel wrote: > All you have to do is make the > scanner more configurable so that it changes the set of keywords based on > the thing it's scanning through. There are also a few places in the Pyrex parser where it context-dependently recognises an identifier as a keyword. You might want to check on those to make sure they aren't going to cause a problem. -- Greg From ondrej at certik.cz Mon Mar 15 09:12:17 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Mon, 15 Mar 2010 01:12:17 -0700 Subject: [Cython] new C++ syntax error In-Reply-To: References: <85b5c3131003102315rdd9ff09g71cf61c7aa01c506@mail.gmail.com> Message-ID: <85b5c3131003150112ja667667n919cdfe6e2e81bb4@mail.gmail.com> On Fri, Mar 12, 2010 at 10:52 PM, Craig Citro wrote: >>> I am trying the new C++ syntax. I got the following error (all files attached): >>> >>> $ cython _hermes_common.pyx >>> ... >>> ? ?if len(self.attributes) != 0: >>> TypeError: object of type 'NoneType' has no len() >>> >> >> Yep, that's a bug. I'm pretty sure you just want to change that line >> in Cython/Compiler/Nodes.py from "if len(self.attributes) != 0:" to >> just "if self.attributes:" and you'll be good. >> >> Amusingly, I tried to grab the source files you attached and test this >> -- and hit a different bug (a problem in parsing). I don't know why -- >> you just posted and confirmed that we're using the same version >> (before I even asked!). Is there any chance your source files are >> slightly out of sync with the ones you posted? (Maybe forgot to save >> changes in your editor before posting?) Has anyone else tried and hit >> the same bug, or is my setup wonky? >> > > So I finally got a chance to sit down and look at this today. The > first bug was easy, and luckily Robert Bradshaw was sitting right next > to me for the second -- he immediately called the function where the > error was going to be, and it was just a question of spotting it. I'm > running tests right now, and I'll push as soon as those are done. > > I also had to make a few changes to your .pyx and .pxd files -- mostly > little things. I'm attaching the versions I used, which now compile > just fine. (I didn't actually test them at all, not having any of the > code they're written against, just checked that they seem to be > producing sensible .cpp files.) Definitely let us know if/when you run > into more trouble -- a lot of this new C++ code just hasn't been run > through its paces, and it's good to work the little bugs out. Thanks Craig for fixing it! I tried your files with new cython and all works fine and things compile just fine too (and all my tests run), so it seems to work. I guess my (broken) pyx files just triggered some bugs, that regular (correct) files don't trigger. Thanks again I'll try to use this new functionality in my projects and keep reporting bugs. Ondrej From robertwb at math.washington.edu Mon Mar 15 17:48:38 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Mon, 15 Mar 2010 09:48:38 -0700 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: <4B9CE355.2010001@behnel.de> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> <4B9CE355.2010001@behnel.de> Message-ID: <9E5F3E45-9097-4244-A6F8-51D29693F3A5@math.washington.edu> On Mar 14, 2010, at 6:23 AM, Stefan Behnel wrote: > Robert Bradshaw, 13.03.2010 21:07: >> On Mar 13, 2010, at 5:30 AM, Stefan Behnel wrote: >>> b) compatibility with Py3 code (-> .py files + "-3" option or >>> "cython3") >>> [...] >>> with the obvious gray area of future imports between a)/c) and b). >>> >>> We don't currently care about the file extension, for example. >>> Should we generally stop supporting the 'cdef' keyword in .py files? > > I disabled all Cython specific keywords when compiling .py files. > > http://hg.cython.org/cython-devel/rev/198e42d128dd Cool. As Greg pointed out, this may not be everything, but I bet this'll handle 95% of what's there. (The other 10% will be harder, and the idea of leveraging the Python parser is an interesting one.) >> The -3 option would be completely orthogonal to the file extension, >> and could be used with both .py and .pyx files. > > Actually, I'd even prefer having it a top-level compiler directive > instead > of a command line switch. If a source file is written in Py3 syntax, > there's no use in making it optional to compile it as Py2 code and > vice > versa. So putting > > # cython: python3=True > > at the top is a lot safer and simpler. Maybe we could even be smart > and > pick up a shebang line like > > #!...python3 > > as Python 3.x installs its interpreter as 'python3' by default, but > I guess > that's hard to do reliably. We should certainly have a dirctive. One advantage of a first-level command line option is that I would guess it would be a more global option (i.e. it will probably be most common for people will have all or no Py3 files, or none, except during a possible transition phase) and I imagine eventually the default will be switched. - Robert From stefan_ml at behnel.de Tue Mar 16 15:40:09 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 16 Mar 2010 15:40:09 +0100 Subject: [Cython] language differences in Cython/Py2/Py3 In-Reply-To: <9E5F3E45-9097-4244-A6F8-51D29693F3A5@math.washington.edu> References: <4B9B4145.1050203@behnel.de> <045F4AC0-DB6A-463B-B2D1-B18820A060A7@math.washington.edu> <4B9B5A27.6090708@behnel.de> <6690B2CF-56D9-4D94-9B62-F910413757AF@math.washington.edu> <7C81345A-F0DF-4775-9BAB-8250EBAD0014@math.washington.edu> <4B9B9386.8040606@behnel.de> <4B9CE355.2010001@behnel.de> <9E5F3E45-9097-4244-A6F8-51D29693F3A5@math.washington.edu> Message-ID: <4B9F9849.8040804@behnel.de> Robert Bradshaw, 15.03.2010 17:48: >> I disabled all Cython specific keywords when compiling .py files. >> >> http://hg.cython.org/cython-devel/rev/198e42d128dd > > Cool. As Greg pointed out, this may not be everything, but I bet > this'll handle 95% of what's there. (The other 10% will be harder Note that I already disabled typing in function signatures (to let users write code like "def func(int)"), so there are only a couple of other places left to fix that will not interfere that easily anymore. > and the idea of leveraging the Python parser is an interesting one.) It's a lot more work, though, given that all work that you put into Cython's parser automatically extends the Cython language to match Python better, whereas using CPython's parser will only work as an alternative to Cython's own parser, given how smart Cython's parser still is. >>> The -3 option would be completely orthogonal to the file extension, >>> and could be used with both .py and .pyx files. >> >> Actually, I'd even prefer having it a top-level compiler directive >> instead >> of a command line switch. If a source file is written in Py3 syntax, >> there's no use in making it optional to compile it as Py2 code and >> vice >> versa. So putting >> >> # cython: python3=True >> >> at the top is a lot safer and simpler. Maybe we could even be smart >> and >> pick up a shebang line like >> >> #!...python3 >> >> as Python 3.x installs its interpreter as 'python3' by default, but >> I guess >> that's hard to do reliably. > > We should certainly have a dirctive. One advantage of a first-level > command line option is that I would guess it would be a more global > option (i.e. it will probably be most common for people will have all > or no Py3 files, or none, except during a possible transition phase) > and I imagine eventually the default will be switched. That sounds like a case for a "cython3" script, as well as a general switch for the distutils Extension, e.g. cython_language_level="3" Stefan From zidibik at yahoo.com Tue Mar 16 17:36:09 2010 From: zidibik at yahoo.com (Jack Brown) Date: Tue, 16 Mar 2010 09:36:09 -0700 (PDT) Subject: [Cython] (no subject) Message-ID: <611906.90637.qm@web63907.mail.re1.yahoo.com> hi, the "getting multiple cython packages to work together" part of the cython's documentation is a bit lacking. so i prepared the attached test case. it works w/o problems in cpython. (cd src/ && python main.py) but the cython-compiled part (python setup.py build_ext && cd build/lib.* && ./???) does not. i think if you get this test case to work and put it somewhere, it'll be a nice sample project to get people started using cython with complex projects. so, here are the questions: 1) how do we set the pxd files? 2) how should the setup.py be modified so that : a) the main.py has python interpreter embedded b) others are compiled as shared objects? thanks in advance, all the best. jack -------------- next part -------------- A non-text attachment was scrubbed... Name: cython-test.tar.gz Type: application/x-gzip Size: 769 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100316/4ab5cd9a/attachment.bin From robertwb at math.washington.edu Tue Mar 16 18:49:20 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Tue, 16 Mar 2010 10:49:20 -0700 Subject: [Cython] (no subject) In-Reply-To: <611906.90637.qm@web63907.mail.re1.yahoo.com> References: <611906.90637.qm@web63907.mail.re1.yahoo.com> Message-ID: <841A7D10-67E0-4C36-A79C-3CE05565E0BB@math.washington.edu> On Mar 16, 2010, at 9:36 AM, Jack Brown wrote: > hi, > > the "getting multiple cython packages to work together" part of the > cython's documentation is a bit lacking. so i prepared the attached > test case. it works w/o problems in cpython. (cd src/ && python > main.py) but the cython-compiled part (python setup.py build_ext && > cd build/lib.* && ./???) does not. > > i think if you get this test case to work and put it somewhere, > it'll be a nice sample project to get people started using cython > with complex projects. > > so, here are the questions: > 1) how do we set the pxd files? > 2) how should the setup.py be modified so that : > a) the main.py has python interpreter embedded > b) others are compiled as shared objects? I'm not sure if this can be done with disutils, but you may want to see Cython freeze http://hg.cython.org/cython-devel/file/7232eb0b1cd9/Demos/freeze/README.txt - Robert From ondrej at certik.cz Sun Mar 21 07:27:44 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Sat, 20 Mar 2010 23:27:44 -0700 Subject: [Cython] how to do reference counting in C++ Message-ID: <85b5c3131003202327p7318d94cy7fc333622b28701f@mail.gmail.com> Hi, here is the relevant code of my C++ Python class, that encapsulates Python: void Python::exec(const char *text) { run_cmd(text, this->_namespace); } void Python::push(const char *name, PyObject *o) { namespace_push(this->_namespace, name, o); Py_DECREF(o); } PyObject *Python::pull(const char *name) { return namespace_pull(this->_namespace, name); } and here is how to use it: python->push("i", c2py_int(5)); python->exec("i = i*2"); int i = py2c_int(python->pull("i")); _assert(i == 10); where the namespace_pull/push functions as well as c2py_int and py2c_int are defined in Cython as: cdef api void namespace_push(object namespace, const_char_p name, object o): namespace.update({name: o}) cdef api object namespace_pull(object namespace, const_char_p name): return namespace.get(name) cdef api object c2py_int(int i): return i cdef api int py2c_int(object i): return i Everything works fine. This line is ok too: python->push("i", c2py_int(5)); because c2py_int(5) increfs the object 5, but push() decrefs it, so all is fine. This line however is leaking: int i = py2c_int(python->pull("i")); because pull("i") increfs "i", but py2c_int() doesn't do anything with the reference, so nothing decrefs it. What is the best way to fix this? Here is one way: PyObject *tmp = python->pull("i"); int i = py2c_int(tmp); Py_DECREF(tmp); But this is super annoying from the user experience, because this is the code that the user has to write by hand and I want to make this as easy as possible. Another option is to create a function pull_int(): int Python::pull_int(const char* name) { PyObject *tmp = python->pull("i"); int i = py2c_int(tmp); Py_DECREF(tmp); return i; } and the user would use: int i = p->pull_int("i"); there would be no leak and it would be very easy to use. However, this means that I have to manually write C++ methods like pull_int() for every single of my py2c_* conversion functions, which is annoying too. Yet another option seems to be to use some macro/function to do the above, e.g. usage would be something like: int i = F(py2c_int, p->pull_int("i")); but this is still too complex from the user perspective. I might figure out some way to construct the pull_int() methods semiautomatically using some macros. What would you suggest me? Ondrej From robertwb at math.washington.edu Sun Mar 21 07:40:44 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Sat, 20 Mar 2010 23:40:44 -0700 Subject: [Cython] how to do reference counting in C++ In-Reply-To: <85b5c3131003202327p7318d94cy7fc333622b28701f@mail.gmail.com> References: <85b5c3131003202327p7318d94cy7fc333622b28701f@mail.gmail.com> Message-ID: <45D9D986-D6F6-4FA3-A80A-8C7A2CFC772C@math.washington.edu> On Mar 20, 2010, at 11:27 PM, Ondrej Certik wrote: [...] > This line however is leaking: > > int i = py2c_int(python->pull("i")); > > because pull("i") increfs "i", but py2c_int() doesn't do anything with > the reference, so nothing decrefs it. What is the best way to fix > this? > > > Here is one way: > > PyObject *tmp = python->pull("i"); > int i = py2c_int(tmp); > Py_DECREF(tmp); > > But this is super annoying from the user experience, because this is > the code that the user has to write by hand and I want to make this as > easy as possible. This is why Cython is so nice :). > Another option is to create a function pull_int(): > > int Python::pull_int(const char* name) > { > PyObject *tmp = python->pull("i"); > int i = py2c_int(tmp); > Py_DECREF(tmp); > return i; > } > > and the user would use: > > int i = p->pull_int("i"); > > there would be no leak and it would be very easy to use. However, this > means that I have to manually write C++ methods like pull_int() for > every single of my py2c_* conversion functions, which is annoying too. > Yet another option seems to be to use some macro/function to do the > above, e.g. usage would be something like: > > int i = F(py2c_int, p->pull_int("i")); > > but this is still too complex from the user perspective. I might > figure out some way to construct the pull_int() methods > semiautomatically using some macros. > > What would you suggest me? I see two options. The first, is to make a "pull_borrowed" which does a decref (or skips an incref) before returning the value. This will work if what you're pulling from is actually a dictionary, as the object will be pointed to elsewhere so won't go away (until the dictionary is modified). The other option is to have your py2c_int function "steal" a reference. (How often do you need the old one around once you've converted it to an int?) Unfortunately, both require the user to be reference-count aware. I think the nicest interface is pull_T for several common T. - Robert From ondrej at certik.cz Sun Mar 21 08:52:25 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Sun, 21 Mar 2010 00:52:25 -0700 Subject: [Cython] how to do reference counting in C++ In-Reply-To: <45D9D986-D6F6-4FA3-A80A-8C7A2CFC772C@math.washington.edu> References: <85b5c3131003202327p7318d94cy7fc333622b28701f@mail.gmail.com> <45D9D986-D6F6-4FA3-A80A-8C7A2CFC772C@math.washington.edu> Message-ID: <85b5c3131003210052g1c1c088ayceb0829d99cc40a1@mail.gmail.com> On Sat, Mar 20, 2010 at 11:40 PM, Robert Bradshaw wrote: > On Mar 20, 2010, at 11:27 PM, Ondrej Certik wrote: [...] >> What would you suggest me? > > > I see two options. The first, is to make a "pull_borrowed" which does > a decref (or skips an incref) before returning the value. This will > work if what you're pulling from is actually a dictionary, as the > object will be pointed to elsewhere so won't go away (until the That's right, didn't occur to me that I can decref it in the pull() method, thanks! Instead of introducing pull_borrowed(), I'll just decref it in the pull() method, since I am decrefing it in the push() method too. E.g. push() method steals and pull() method borrows. That way it is reasonably simple for the user. > dictionary is modified). The other option is to have your py2c_int > function "steal" a reference. (How often do you need the old one > around once you've converted it to an int?) (I don't need it around.) The problem though is that I would need to add the decref in the Cython code of py2c_int, and thus I would not be able to use it in Cython. I use it in Cython though too, mainly numpy2c_int functions, that convert numpy array to a C array. So I would have to introduce two kinds of functions etc. > > Unfortunately, both require the user to be reference-count aware. I > think the nicest interface is pull_T for several common T. Actually, both int i = py2c_int(p->pull("i")); int i = p->pull_int("i")); look simple enough for me. I like the general way of p->pull(), since when the user gets used to it, he can use it for any other type that he interfaces from Cython, so I like that. Thanks again, the key idea was that pull() can returned a borrowed reference, I didn't realize that. I'll keep you posted how it goes. Ondrej From baihaoyu at gmail.com Sun Mar 21 12:31:06 2010 From: baihaoyu at gmail.com (Haoyu Bai) Date: Sun, 21 Mar 2010 19:31:06 +0800 Subject: [Cython] GSoC project for improving Python compatibility Message-ID: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> Hi, I'm Haoyu Bai, a graduate student from National University of Singapore. I'm quite interested in help Cython for improving Python compatibility as a GSoC project. I have some experience of working with Boost.Python and SWIG, and have use Python for several years, so I'm familiar with Python internals and C-API. Also, I have used Cython several times and pretty love its user-friendliness. Thanks your great works! :) I have read the relevant CEPs, and from the posts on the mailing list I see that the work on closure is almost finished, and the next thing would be generator(coroutines). So could generator support be a good GSoC project? I'd like to work on that because coroutines is a powerful thing in Python and support of that in Cython would be amazing. Another thing mentioned on the idea page is proper scoping. With the approach described in the CEP page, this seems easier to do. However, I'm aware that the described approach is incomplete. I'll do some more investigation about this. Anyway, I'm more interested in generator support. I want to have a better understanding of Cython and have more concrete idea of how these could be implemented. So where could be a good start point? Thank you! -- Haoyu BAI School of Computing, National University of Singapore. From dagss at student.matnat.uio.no Mon Mar 22 08:50:49 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Mon, 22 Mar 2010 08:50:49 +0100 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> Message-ID: <4BA72159.6010007@student.matnat.uio.no> Haoyu Bai wrote: > Hi, > > I'm Haoyu Bai, a graduate student from National University of > Singapore. I'm quite interested in help Cython for improving Python > compatibility as a GSoC project. I have some experience of working > with Boost.Python and SWIG, and have use Python for several years, so > I'm familiar with Python internals and C-API. Also, I have used Cython > several times and pretty love its user-friendliness. Thanks your great > works! :) > > I have read the relevant CEPs, and from the posts on the mailing list > I see that the work on closure is almost finished, and the next thing > would be generator(coroutines). So could generator support be a good > GSoC project? I'd like to work on that because coroutines is a > powerful thing in Python and support of that in Cython would be > amazing. > > Another thing mentioned on the idea page is proper scoping. With the > approach described in the CEP page, this seems easier to do. However, > > I'm aware that the described approach is incomplete. I'll do some more > investigation about this. Anyway, I'm more interested in generator > support. > > I want to have a better understanding of Cython and have more concrete > idea of how these could be implemented. So where could be a good start > point? > I have to say this sounds very promising. The next step is to get in touch with possible mentors on this mailing list, in order to work towards an application (we usually match up possible mentors and students up-front to the selection process, because there's so few possible mentors). Basically you have to wait and see if anyone volunteers to be your primary mentor, should you be selected. I'm not sure how many is able to participate as GSoC mentors this year. Myself I think I might not be able to be a GSoC mentor at all this year because of finishing my MSc thesis. PS. The Python software foundation will give a strong priority to Python 3 related projects this year, that's something worth thinking about. E.g. one could target Cython's compatability with Python 3 specifically. Dag Sverre From baihaoyu at gmail.com Mon Mar 22 17:39:43 2010 From: baihaoyu at gmail.com (Haoyu Bai) Date: Tue, 23 Mar 2010 00:39:43 +0800 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <4BA72159.6010007@student.matnat.uio.no> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> Message-ID: <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> On Mon, Mar 22, 2010 at 3:50 PM, Dag Sverre Seljebotn wrote: > Haoyu Bai wrote: >> Hi, >> >> I'm Haoyu Bai, a graduate student from National University of >> Singapore. I'm quite interested in help Cython for improving Python >> compatibility as a GSoC project. I have some experience of working >> with Boost.Python and SWIG, and have use Python for several years, so >> I'm familiar with Python internals and C-API. Also, I have used Cython >> several times and pretty love its user-friendliness. Thanks your great >> works! :) >> >> I have read the relevant CEPs, and from the posts on the mailing list >> I see that the work on closure is almost finished, and the next thing >> would be generator(coroutines). So could generator support be a good >> GSoC project? I'd like to work on that because coroutines is a >> powerful thing in Python and support of that in Cython would be >> amazing. >> >> Another thing mentioned on the idea page is proper scoping. With the >> approach described in the CEP page, this seems easier to do. However, >> >> I'm aware that the described approach is incomplete. I'll do some more >> investigation about this. Anyway, I'm more interested in generator >> support. >> >> I want to have a better understanding of Cython and have more concrete >> idea of how these could be implemented. So where could be a good start >> point? >> > I have to say this sounds very promising. The next step is to get in > touch with possible mentors on this mailing list, in order to work > towards an application (we usually match up possible mentors and > students up-front to the selection process, because there's so few > possible mentors). Basically you have to wait and see if anyone > volunteers to be your primary mentor, should you be selected. I'm not > sure how many is able to participate as GSoC mentors this year. > > Myself I think I might not be able to be a GSoC mentor at all this year > because of finishing my MSc thesis. > > PS. The Python software foundation will give a strong priority to Python > 3 related projects this year, that's something worth thinking about. > E.g. one could target Cython's compatability with Python 3 specifically. > > Dag Sverre Thanks for your advice, Dag. Your suggestion about Python 3 support also lead to an interesting idea. That is, to make use of the Python 3 function annotation syntax to have a more convenient pure Python mode in Cython. For example, this: cdef str foo(int x): ... could be written as: def foo(x:int) -> str: ... or maybe more verbose: def foo(x:cython.int) -> cython.str: ... This could make the pure Python mode more easier and attractive. There's also some minor improvement regarding Python 3 support could be done in Cython. Such as support for "nonlocal" keyword, and float division "1/2" (which evaluated as 0.5 in Python 3 but 0 in Cython). These could be a project easier than generator support. But quite useful and could attract more people to use Python 3. Any comments? Thanks! -- Haoyu BAI School of Computing, National University of Singapore. From dagss at student.matnat.uio.no Mon Mar 22 20:01:59 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Mon, 22 Mar 2010 20:01:59 +0100 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> Message-ID: <4BA7BEA7.3090809@student.matnat.uio.no> Haoyu Bai wrote: > On Mon, Mar 22, 2010 at 3:50 PM, Dag Sverre Seljebotn > wrote: >> Haoyu Bai wrote: >>> Hi, >>> >>> I'm Haoyu Bai, a graduate student from National University of >>> Singapore. I'm quite interested in help Cython for improving Python >>> compatibility as a GSoC project. I have some experience of working >>> with Boost.Python and SWIG, and have use Python for several years, so >>> I'm familiar with Python internals and C-API. Also, I have used Cython >>> several times and pretty love its user-friendliness. Thanks your great >>> works! :) >>> >>> I have read the relevant CEPs, and from the posts on the mailing list >>> I see that the work on closure is almost finished, and the next thing >>> would be generator(coroutines). So could generator support be a good >>> GSoC project? I'd like to work on that because coroutines is a >>> powerful thing in Python and support of that in Cython would be >>> amazing. >>> >>> Another thing mentioned on the idea page is proper scoping. With the >>> approach described in the CEP page, this seems easier to do. However, >>> >>> I'm aware that the described approach is incomplete. I'll do some more >>> investigation about this. Anyway, I'm more interested in generator >>> support. >>> >>> I want to have a better understanding of Cython and have more concrete >>> idea of how these could be implemented. So where could be a good start >>> point? >>> >> I have to say this sounds very promising. The next step is to get in >> touch with possible mentors on this mailing list, in order to work >> towards an application (we usually match up possible mentors and >> students up-front to the selection process, because there's so few >> possible mentors). Basically you have to wait and see if anyone >> volunteers to be your primary mentor, should you be selected. I'm not >> sure how many is able to participate as GSoC mentors this year. >> >> Myself I think I might not be able to be a GSoC mentor at all this year >> because of finishing my MSc thesis. >> >> PS. The Python software foundation will give a strong priority to Python >> 3 related projects this year, that's something worth thinking about. >> E.g. one could target Cython's compatability with Python 3 specifically. >> >> Dag Sverre > > Thanks for your advice, Dag. Your suggestion about Python 3 support > also lead to an interesting idea. That is, to make use of the Python 3 > function annotation syntax to have a more convenient pure Python mode > in Cython. > > For example, this: > > cdef str foo(int x): > ... > > could be written as: > > def foo(x:int) -> str: > ... > > or maybe more verbose: > > def foo(x:cython.int) -> cython.str: > ... > > This could make the pure Python mode more easier and attractive. +1. I think there should be a decorator though: @cython.compile # or whatever def foo(x: cython.int) -> cython.int which has the behaviour that in pure Python mode the argument decorators are stripped away (or perhaps stuck into an attribute on the function instead). Since decorated arguments don't have a meaning in themselves, I think it is polite to somehow declare their intent. > There's also some minor improvement regarding Python 3 support could > be done in Cython. Such as support for "nonlocal" keyword, and float > division "1/2" (which evaluated as 0.5 in Python 3 but 0 in Cython). from __future__ import division is fully supported in Cython. -- Dag Sverre From craigcitro at gmail.com Mon Mar 22 20:07:40 2010 From: craigcitro at gmail.com (Craig Citro) Date: Mon, 22 Mar 2010 12:07:40 -0700 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> Message-ID: > Thanks for your advice, Dag. Your suggestion about Python 3 support > also lead to an interesting idea. That is, to make use of the Python 3 > function annotation syntax to have a more convenient pure Python mode > in Cython. > I really like this idea -- I just ran into PEP 3107 (function annotations) for the first time recently, and I was thinking exactly the same thing. I'd definitely be interested in mentoring a project on this front, and the Py3 angle probably gives us a good shot at getting accepted by PSF. -cc From stefan_ml at behnel.de Mon Mar 22 21:43:35 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 22 Mar 2010 21:43:35 +0100 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> Message-ID: <4BA7D677.3020807@behnel.de> Haoyu Bai, 22.03.2010 17:39: > Your suggestion about Python 3 support > also lead to an interesting idea. That is, to make use of the Python 3 > function annotation syntax to have a more convenient pure Python mode > in Cython. > > For example, this: > > cdef str foo(int x): > ... > > could be written as: > > def foo(x:int) -> str: > ... > > or maybe more verbose: > > def foo(x:cython.int) -> cython.str: > ... > > This could make the pure Python mode more easier and attractive. Note that I already added support for this syntax to the parser. All that's missing is the recognition of the annotations as type declarations, should be doable in a day or so, given that most of this is implemented in the pure Python mode anyway. > There's also some minor improvement regarding Python 3 support could > be done in Cython. Such as support for "nonlocal" keyword Take a look at the Py3 related tickets in our bug tracker. > and float > division "1/2" (which evaluated as 0.5 in Python 3 but 0 in Cython). IIRC, there's a future import for this. Stefan From stefan_ml at behnel.de Mon Mar 22 21:51:11 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 22 Mar 2010 21:51:11 +0100 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <4BA7BEA7.3090809@student.matnat.uio.no> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> <4BA7BEA7.3090809@student.matnat.uio.no> Message-ID: <4BA7D83F.6030103@behnel.de> Dag Sverre Seljebotn, 22.03.2010 20:01: > I think there should be a decorator though: > > @cython.compile # or whatever > def foo(x: cython.int) -> cython.int > > which has the behaviour that in pure Python mode the argument decorators > are stripped away (or perhaps stuck into an attribute on the function > instead). Since decorated arguments don't have a meaning in themselves, > I think it is polite to somehow declare their intent. I don't think that should be required. In pure mode, "import cython" makes Cython declarations available in the module namespace, so using them in a function signature is a rather clear expression of intention when the module is passed into the Cython compiler. Note that annotations are available through introspection, and I think they should stay visible from Python space after compilation. However, we might want to extend the annotations somewhat to allow semantics like "list but not None", as we do in Cython code. Maybe something like "l: cython.notnone(list)" or "l: cython.declare(list, not_none=True)". IIRC, the PEP suggests the use of tuples to provide multiple annotations. Stefan From ondrej at certik.cz Tue Mar 23 02:28:11 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Mon, 22 Mar 2010 18:28:11 -0700 Subject: [Cython] how to initialize a class without calling it's constructor In-Reply-To: <26B8DFAA-3CB3-4D73-9735-32709CA76C24@math.washington.edu> References: <85b5c3130902162026g7a2bfaa7qe4af928f105b9e02@mail.gmail.com> <26B8DFAA-3CB3-4D73-9735-32709CA76C24@math.washington.edu> Message-ID: <85b5c3131003221828r79e4a49fs17d860bd65cad8d2@mail.gmail.com> On Tue, Feb 17, 2009 at 2:32 AM, Robert Bradshaw wrote: > On Feb 16, 2009, at 8:26 PM, Ondrej Certik wrote: > >> Currently I am using the following code and everything works for me: > > [...] > >> But it uses the PY_NEW macro: >> >> #define PY_NEW(zzz_type) \ >> ? ? (((PyTypeObject*)(zzz_type))->tp_new((PyTypeObject*)(zzz_type), >> global_empty_tuple, NULL)) >> >> and global_empty_tuple() that I had to initialize, etc. I copied this >> from Sage. Is there some better way to do the above, so that I don't >> have to include the special macro above and the corresponding .h and >> .c files in my project? > > How about > > cdef extern from *: > ? ? ctypedef struct RichPyTypeObject "PyTypeObject": > ? ? ? ? object tp_new(object, object, PyObject*) > > cdef inline PY_NEW(T): > ? ? return (x).tp_new(T, (), NULL) This works, thanks! There were couple minor typos, e.g.: object -> RichPyTypeObject* T -> (T) x -> T So this is the correct code that works for me: cdef extern from *: ctypedef struct RichPyTypeObject "PyTypeObject": object tp_new(RichPyTypeObject*, object, PyObject*) cdef inline PY_NEW(T): return (T).tp_new((T), (), NULL) Ondrej From ondrej at certik.cz Tue Mar 23 03:44:59 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Mon, 22 Mar 2010 19:44:59 -0700 Subject: [Cython] hermes_common update In-Reply-To: <1501b1c41003102256x2ccb97e6xc6441aa9fe350ea3@mail.gmail.com> References: <85b5c3131003030217y4747515en6a45313952d88dbb@mail.gmail.com> <85b5c3131003032022s719ea8d1t3a7ba5aa168b52a5@mail.gmail.com> <1501b1c41003032043q49ae3fcdk8ec9e87770b62ec7@mail.gmail.com> <85b5c3131003041657o5c17b439ja20afab3f428eea@mail.gmail.com> <1501b1c41003041702u3cffe2bdj1d4cc0fb85b0697b@mail.gmail.com> <85b5c3131003092000r6d0f17cdg13d801066391cc6a@mail.gmail.com> <1501b1c41003092041q72874b66k250b0710e013528e@mail.gmail.com> <85b5c3131003101455i70cb6c4egb6a76044801e2443@mail.gmail.com> <85b5c3131003102018o34ee3bc9g5aab643347c39f28@mail.gmail.com> <1501b1c41003102256x2ccb97e6xc6441aa9fe350ea3@mail.gmail.com> Message-ID: <85b5c3131003221944w38172454mb97c135c20e19284@mail.gmail.com> Hi, today I fixed hopefully the remaining bugs in Python <-> C++ interface: * Use the Python() C++ class as the only interface to Python * properly deallocated all memory when the Python() class is deleted * fixed numerous segfaults related to that * figured out how to pass your own (derived) C++ class to Python and get its virtual methods called properly from Python * figured out how to write the PY_NEW() function in Cython only, without the need of the stdcython.h/c files (that we currently have in hermes2d) * wrote test cases to all of the above (that could also serve as examples of usage, both in Python and in C++) The code is here: http://github.com/certik/hermes_common Here is a (complete) example how to use scipy to print the sparse matrices nicely (from C++): Python *p = new Python(); p->push("m", c2py_CSRMatrix(this)); p->exec("S = str(m.to_scipy_csr())"); printf("%s\n", py2c_str(p->pull("S"))); delete p; It initializes the wrappers/python (so the user doesn't have to worry about this at all --- this used to be quite nontrivial!), pushes the C++ matrix in (as a Python class, that's the "m" symbol), then converts it to scipy, calls str() on the scipy matrix (so scipy prints it), saves it to "S", then pulls the string "S" back to C++ and prints it using C++ printf(). Finally it frees the Python and the namespace (e.g. both "m" and "S" symbols will be freed). Notice that you don't have to worry about memory allocation, everything should just work. The py2c_* and c2py_* methods are conveniently defined in Cython, e.g.: cdef api char* py2c_str(object s): return s cdef api object c2py_CSRMatrix(c_CSRMatrix *m): cdef CSRMatrix c c = PY_NEW(CSRMatrix) c.thisptr = m return c and that's it, you don't have to redeclare it in C++, as Cython will do that for you automatically. If one changes these api functions, one has to run the convert_api.py script: http://github.com/certik/hermes_common/blob/master/convert_api.py that takes the standard Cython generated header file (that uses static/local API) and creates a new .h and .cpp files that use the global API, so that the user doesn't have to worry about this at all in C++. So all is automatic, and later on, after we use it more in hermes2d and hermes1d, and if all is ok, I'll see how cython could be modified, so that it can produce these files itself. It took me quite some time to figure everything out, but it's worthy. It used to be a big pain to use Python from C++ (for nontrivial things like the above, where you had to initialize python and the wrappers in each cpp file...), but with this Python() C++ class, everything is super easy now. Let's say you have this array (in practice way longer of course): double a[3] = {1., 5., 3.}; and you want to print it nicely for debugging purposes. Here is the complete code that you have to put in your program: Python p=Python(); p.push("A", c2numpy_double(a, 3)); p.exec("print A"); Which prints: [ 1. 5. 3.] Now compare this to the old way by hand: for (int i=0; i<3; i ++) printf("%f ", a[i]); printf("\n"); which prints: 1.000000 5.000000 3.000000 now imagine I want to just print the first 5, or last 5 numbers, or just taking advantage of the nice way numpy prints large matrices (it puts ... in the middle), then I can just use: p.exec("print 'Last two numbers:', A[-2:]"); etc. It is all pretty trivial now, but it seems noone has done this before, so it took me quite some time to figure everything out. So I am happy with the results, thanks Robert, Lisandro and others for the help. Ondrej From ondrej at certik.cz Tue Mar 23 03:48:11 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Mon, 22 Mar 2010 19:48:11 -0700 Subject: [Cython] hermes_common update In-Reply-To: <85b5c3131003221944w38172454mb97c135c20e19284@mail.gmail.com> References: <85b5c3131003030217y4747515en6a45313952d88dbb@mail.gmail.com> <1501b1c41003032043q49ae3fcdk8ec9e87770b62ec7@mail.gmail.com> <85b5c3131003041657o5c17b439ja20afab3f428eea@mail.gmail.com> <1501b1c41003041702u3cffe2bdj1d4cc0fb85b0697b@mail.gmail.com> <85b5c3131003092000r6d0f17cdg13d801066391cc6a@mail.gmail.com> <1501b1c41003092041q72874b66k250b0710e013528e@mail.gmail.com> <85b5c3131003101455i70cb6c4egb6a76044801e2443@mail.gmail.com> <85b5c3131003102018o34ee3bc9g5aab643347c39f28@mail.gmail.com> <1501b1c41003102256x2ccb97e6xc6441aa9fe350ea3@mail.gmail.com> <85b5c3131003221944w38172454mb97c135c20e19284@mail.gmail.com> Message-ID: <85b5c3131003221948u7898ff8dnf3286acf982625cb@mail.gmail.com> On Mon, Mar 22, 2010 at 7:44 PM, Ondrej Certik wrote: > Hi, > > today I fixed hopefully the remaining bugs in Python <-> C++ interface: > > * Use the Python() C++ class as the only interface to Python > * properly deallocated all memory when the Python() class is deleted > * fixed numerous segfaults related to that > * figured out how to pass your own (derived) C++ class to Python and > get its virtual methods called properly from Python > * figured out how to write the PY_NEW() function in Cython only, > without the need of the stdcython.h/c files (that we currently have in > hermes2d) > * wrote test cases to all of the above (that could also serve as > examples of usage, both in Python and in C++) I forgot to add, that the namespace is local to the Python() instance, so the following works (see the tests): void test_basic4() { Python *p1 = new Python(); Python *p2 = new Python(); p1->push("i", c2py_int(5)); p2->push("i", c2py_int(6)); p1->exec("i = i*2"); p2->exec("i = i*2"); int i1 = py2c_int(p1->pull("i")); int i2 = py2c_int(p2->pull("i")); _assert(i1 == 10); _assert(i2 == 12); delete p1; delete p2; } Ondrej From ondrej at certik.cz Tue Mar 23 03:56:15 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Mon, 22 Mar 2010 19:56:15 -0700 Subject: [Cython] hermes_common update In-Reply-To: <1501b1c41003221949v714667f6h5526b4459e33cd4b@mail.gmail.com> References: <85b5c3131003030217y4747515en6a45313952d88dbb@mail.gmail.com> <85b5c3131003041657o5c17b439ja20afab3f428eea@mail.gmail.com> <1501b1c41003041702u3cffe2bdj1d4cc0fb85b0697b@mail.gmail.com> <85b5c3131003092000r6d0f17cdg13d801066391cc6a@mail.gmail.com> <1501b1c41003092041q72874b66k250b0710e013528e@mail.gmail.com> <85b5c3131003101455i70cb6c4egb6a76044801e2443@mail.gmail.com> <85b5c3131003102018o34ee3bc9g5aab643347c39f28@mail.gmail.com> <1501b1c41003102256x2ccb97e6xc6441aa9fe350ea3@mail.gmail.com> <85b5c3131003221944w38172454mb97c135c20e19284@mail.gmail.com> <1501b1c41003221949v714667f6h5526b4459e33cd4b@mail.gmail.com> Message-ID: <85b5c3131003221956l63fef1c2m2cc2de71845a1c8f@mail.gmail.com> On Mon, Mar 22, 2010 at 7:49 PM, Pavel Solin wrote: > Hi Ondrej, this is fantastic! What are the next steps > you plan to do? Right now I am testing hermes2d with hermes_common, last time there were some little problems. I'll see how difficult is to get a patch working, that other people can review. If it takes quite some time, I'll try to do hermes1d first, as that should be easier. If it doesn't, I'll do hermes2d first, as I did most of the work already for it. > > My only question is whether the class should > be called Python. Python is the name of > a programming language, and this class does > something slightly different. That's a good point. I CCed cython-dev. What do people think? I quite like Python(), as it's short and it initializes the Python interpreter, and Python::push(), Python::pull() and Python::exec() are all methods of the class, that push, pull a symbol or execute some statement in Python, so speaking for myself, the naming is natural. But I can change it, that's not a problem. Ondrej From stefan_ml at behnel.de Tue Mar 23 07:46:19 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 23 Mar 2010 07:46:19 +0100 Subject: [Cython] how to initialize a class without calling it's constructor In-Reply-To: <85b5c3130902162026g7a2bfaa7qe4af928f105b9e02@mail.gmail.com> References: <85b5c3130902162026g7a2bfaa7qe4af928f105b9e02@mail.gmail.com> Message-ID: <4BA863BB.2050703@behnel.de> Ondrej Certik, 17.02.2009 05:26: > I have the following class: > > cdef class H1Space: > > def __init__(self, Mesh m, H1Shapeset s): > self.thisptr = new_H1Space(m.thisptr, s.thisptr) > > and it is initialized from 2 other classes. Sometimes however, I would > like to initialize it directly from a C++ instance of c_H1Space, e.g. > I would like to create the H1Space class *without* calling it's > constructor and just set the self.thisptr object to the C++ instance. > > Currently I am using the following code and everything works for me: > > cdef H1Space H1Space_from_c_H1Space(c_H1Space *h): > cdef H1Space n > n =PY_NEW(H1Space) > n.thisptr = h > return n > > But it uses the PY_NEW macro: > > #define PY_NEW(zzz_type) \ > (((PyTypeObject*)(zzz_type))->tp_new((PyTypeObject*)(zzz_type), > global_empty_tuple, NULL)) > > and global_empty_tuple() that I had to initialize, etc. I copied this > from Sage. Is there some better way to do the above, so that I don't > have to include the special macro above and the corresponding .h and > .c files in my project? This should work with the latest Cython: cdef H1Space n = H1Space.__new__(H1Space) Stefan From stefan_ml at behnel.de Tue Mar 23 07:47:30 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 23 Mar 2010 07:47:30 +0100 Subject: [Cython] how to initialize a class without calling it's constructor In-Reply-To: <85b5c3130902162026g7a2bfaa7qe4af928f105b9e02@mail.gmail.com> References: <85b5c3130902162026g7a2bfaa7qe4af928f105b9e02@mail.gmail.com> Message-ID: <4BA86402.80706@behnel.de> Ondrej Certik, 17.02.2009 05:26: > I have the following class: > > cdef class H1Space: > > def __init__(self, Mesh m, H1Shapeset s): > self.thisptr = new_H1Space(m.thisptr, s.thisptr) > > and it is initialized from 2 other classes. Sometimes however, I would > like to initialize it directly from a C++ instance of c_H1Space, e.g. > I would like to create the H1Space class *without* calling it's > constructor and just set the self.thisptr object to the C++ instance. BTW, note that this kind of question is best suited for the cython-users mailing list, not the cython-dev mailing list. Stefan From stefan_ml at behnel.de Tue Mar 23 07:52:53 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 23 Mar 2010 07:52:53 +0100 Subject: [Cython] hermes_common update In-Reply-To: <85b5c3131003221956l63fef1c2m2cc2de71845a1c8f@mail.gmail.com> References: <85b5c3131003030217y4747515en6a45313952d88dbb@mail.gmail.com> <85b5c3131003041657o5c17b439ja20afab3f428eea@mail.gmail.com> <1501b1c41003041702u3cffe2bdj1d4cc0fb85b0697b@mail.gmail.com> <85b5c3131003092000r6d0f17cdg13d801066391cc6a@mail.gmail.com> <1501b1c41003092041q72874b66k250b0710e013528e@mail.gmail.com> <85b5c3131003101455i70cb6c4egb6a76044801e2443@mail.gmail.com> <85b5c3131003102018o34ee3bc9g5aab643347c39f28@mail.gmail.com> <1501b1c41003102256x2ccb97e6xc6441aa9fe350ea3@mail.gmail.com> <85b5c3131003221944w38172454mb97c135c20e19284@mail.gmail.com> <1501b1c41003221949v714667f6h5526b4459e33cd4b@mail.gmail.com> <85b5c3131003221956l63fef1c2m2cc2de71845a1c8f@mail.gmail.com> Message-ID: <4BA86545.9050905@behnel.de> Ondrej Certik, 23.03.2010 03:56: > On Mon, Mar 22, 2010 at 7:49 PM, Pavel Solin wrote: >> My only question is whether the class should >> be called Python. Python is the name of >> a programming language, and this class does >> something slightly different. > > That's a good point. I CCed cython-dev. What do people think? I think the cython-users list would be a better match. > I quite like Python(), as it's short and it initializes the Python > interpreter, and Python::push(), Python::pull() and Python::exec() are > all methods of the class, that push, pull a symbol or execute some > statement in Python Sounds like "PythonInterpreter" would be a better name. Stefan From ondrej at certik.cz Tue Mar 23 08:04:39 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Tue, 23 Mar 2010 00:04:39 -0700 Subject: [Cython] how to initialize a class without calling it's constructor In-Reply-To: <4BA86402.80706@behnel.de> References: <85b5c3130902162026g7a2bfaa7qe4af928f105b9e02@mail.gmail.com> <4BA86402.80706@behnel.de> Message-ID: <85b5c3131003230004m1085acf4sc1f3dd9a15078735@mail.gmail.com> On Mon, Mar 22, 2010 at 11:47 PM, Stefan Behnel wrote: [...] > This should work with the latest Cython: > > ? ? cdef H1Space n = H1Space.__new__(H1Space) Wow, this is cool! > > BTW, note that this kind of question is best suited for the cython-users > mailing list, not the cython-dev mailing list. Yes, notice the date though (a year old), I wasn't aware of cython-users back then. Ondrej From stefan_ml at behnel.de Tue Mar 23 08:22:19 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 23 Mar 2010 08:22:19 +0100 Subject: [Cython] how to initialize a class without calling it's constructor In-Reply-To: <85b5c3131003230004m1085acf4sc1f3dd9a15078735@mail.gmail.com> References: <85b5c3130902162026g7a2bfaa7qe4af928f105b9e02@mail.gmail.com> <4BA86402.80706@behnel.de> <85b5c3131003230004m1085acf4sc1f3dd9a15078735@mail.gmail.com> Message-ID: <4BA86C2B.2060203@behnel.de> Ondrej Certik, 23.03.2010 08:04: > On Mon, Mar 22, 2010 at 11:47 PM, Stefan Behnel wrote: >> This should work with the latest Cython: >> >> cdef H1Space n = H1Space.__new__(H1Space) > > Wow, this is cool! Thanks. :) >> BTW, note that this kind of question is best suited for the cython-users >> mailing list, not the cython-dev mailing list. > > Yes, notice the date though (a year old), I wasn't aware of > cython-users back then. Ah, right, sorry. I just saw this thread popping up at the top due to your response and apparently hadn't read any of the mails in it yet, so I just considered it all new from tonight and didn't look at the date. Stefan From stefan_ml at behnel.de Wed Mar 24 08:00:36 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 24 Mar 2010 08:00:36 +0100 Subject: [Cython] bug #520: scanner broken for DOS line endings Message-ID: <4BA9B894.2070701@behnel.de> Hi, I only noticed recently that the codecs module doesn't support universal newline parsing. This means that parsing source files with DOS line endings will currently lead to this error: C:\Python31\hello.pyx:1:23: Unrecognized character I'm not sure if this also impacts Mac users, but given that Robert didn't seem to have any problems so far, I assume not (and wonder why ...). Maybe it depends on more than just the system platform, perhaps including the Python build or something... I fixed this for 0.13 by switching to the new 'io' module in Py2.6, plus a work-around for older Python versions. http://trac.cython.org/cython_trac/ticket/520 http://hg.cython.org/cython-devel/rev/751bdd38b55c However, given that many users on Windows will end up experiencing this problem, would this fix qualify for an emergency 0.12.2 release? Stefan From dagss at student.matnat.uio.no Wed Mar 24 08:20:34 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Wed, 24 Mar 2010 08:20:34 +0100 Subject: [Cython] bug #520: scanner broken for DOS line endings In-Reply-To: <4BA9B894.2070701@behnel.de> References: <4BA9B894.2070701@behnel.de> Message-ID: <4BA9BD42.2090207@student.matnat.uio.no> Stefan Behnel wrote: > Hi, > > I only noticed recently that the codecs module doesn't support universal > newline parsing. This means that parsing source files with DOS line endings > will currently lead to this error: > > C:\Python31\hello.pyx:1:23: Unrecognized character > Totally amazing that we haven't got complaints about this before from Windows users. Is this something that's been there all the time or the result of a recent change? > I'm not sure if this also impacts Mac users, but given that Robert didn't > seem to have any problems so far, I assume not (and wonder why ...). Maybe > it depends on more than just the system platform, perhaps including the > Python build or something... Perhaps newer Macs use Unix-style line endings, given the change to a BSD kernel? At least much of Robert's work is on Sage and I'd assume Sage files to be mostly Unix line-ended (much editing going on on math.washington.edu). Certainly, Emacs will deal with Unix line endings also on Macs. -- Dag Sverre From stefan_ml at behnel.de Wed Mar 24 08:40:50 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 24 Mar 2010 08:40:50 +0100 Subject: [Cython] bug #520: scanner broken for DOS line endings In-Reply-To: <4BA9BD42.2090207@student.matnat.uio.no> References: <4BA9B894.2070701@behnel.de> <4BA9BD42.2090207@student.matnat.uio.no> Message-ID: <4BA9C202.5050801@behnel.de> Dag Sverre Seljebotn, 24.03.2010 08:20: > Stefan Behnel wrote: >> I only noticed recently that the codecs module doesn't support universal >> newline parsing. This means that parsing source files with DOS line endings >> will currently lead to this error: >> >> C:\Python31\hello.pyx:1:23: Unrecognized character >> > > Totally amazing that we haven't got complaints about this before from > Windows users. Same from here. > Is this something that's been there all the time or the > result of a recent change? Ever since I implemented source encoding support, and that was long ago, certainly before 0.12 came out. The funny thing is that I only became aware of this due to a test failure in Hudson on the msvc docstrings test, which had been added with DOS line endings. I was puzzled enough to see this test fail on a Linux machine, while I can't reproduce this on my (Linux-)laptop at all. So I can only guess that this depends more on the Python build or some other environmental factor. Anyway, the error message above was taken from a recent post on c.l.py, pointing at the end of a totally innocent looking source line (first in the file), so for now I assume that this really impacts at least some users. Stefan From robertwb at math.washington.edu Wed Mar 24 15:34:21 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Wed, 24 Mar 2010 07:34:21 -0700 Subject: [Cython] bug #520: scanner broken for DOS line endings In-Reply-To: <4BA9C202.5050801@behnel.de> References: <4BA9B894.2070701@behnel.de> <4BA9BD42.2090207@student.matnat.uio.no> <4BA9C202.5050801@behnel.de> Message-ID: <7EEC9143-BBDD-41ED-B608-509479F47CA6@math.washington.edu> On Mar 24, 2010, at 12:40 AM, Stefan Behnel wrote: > Dag Sverre Seljebotn, 24.03.2010 08:20: >> Stefan Behnel wrote: >>> I only noticed recently that the codecs module doesn't support >>> universal >>> newline parsing. This means that parsing source files with DOS >>> line endings >>> will currently lead to this error: >>> >>> C:\Python31\hello.pyx:1:23: Unrecognized character >>> >> >> Totally amazing that we haven't got complaints about this before from >> Windows users. > > Same from here. Given we haven't (even now, once you've brought it up) it seems it isn't a big issue (though should still be fixed). >> Is this something that's been there all the time or the >> result of a recent change? > > Ever since I implemented source encoding support, and that was long > ago, > certainly before 0.12 came out. > > The funny thing is that I only became aware of this due to a test > failure > in Hudson on the msvc docstrings test, which had been added with DOS > line > endings. I was puzzled enough to see this test fail on a Linux > machine, > while I can't reproduce this on my (Linux-)laptop at all. So I can > only > guess that this depends more on the Python build or some other > environmental factor. I didn't ever see any failures on my box either. (FYI, OS X apps typically default to (or at least handle seamlessly) Unix line endings, at least the ones I ever use.) It > Anyway, the error message above was taken from a recent post on > c.l.py, > pointing at the end of a totally innocent looking source line (first > in the > file), so for now I assume that this really impacts at least some > users. Can anyone actually reproduce this on Windows. The example in question was provided by a Windows user who clearly had no issues with it. If you want to do an emergency release, feel free to do so, but I don't see any evidence that it's necessary. - Robert From robertwb at math.washington.edu Wed Mar 24 15:40:52 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Wed, 24 Mar 2010 07:40:52 -0700 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <4BA7D83F.6030103@behnel.de> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> <4BA7BEA7.3090809@student.matnat.uio.no> <4BA7D83F.6030103@behnel.de> Message-ID: <55283193-093B-49D3-8BEC-25BA40F6FA69@math.washington.edu> On Mar 22, 2010, at 1:51 PM, Stefan Behnel wrote: > Dag Sverre Seljebotn, 22.03.2010 20:01: >> I think there should be a decorator though: >> >> @cython.compile # or whatever >> def foo(x: cython.int) -> cython.int >> >> which has the behaviour that in pure Python mode the argument >> decorators >> are stripped away (or perhaps stuck into an attribute on the function >> instead). Since decorated arguments don't have a meaning in >> themselves, >> I think it is polite to somehow declare their intent. > > I don't think that should be required. In pure mode, "import cython" > makes > Cython declarations available in the module namespace, so using them > in a > function signature is a rather clear expression of intention when the > module is passed into the Cython compiler. My thoughts as well, especially if you're using cython.int (rather than just int). > Note that annotations are > available through introspection, and I think they should stay > visible from > Python space after compilation. > > However, we might want to extend the annotations somewhat to allow > semantics like "list but not None", as we do in Cython code. Maybe > something like "l: cython.notnone(list)" or "l: cython.declare(list, > not_none=True)". Sure. > IIRC, the PEP suggests the use of tuples to provide multiple > annotations. We should support that then. Supporting this (and other Py3 features) would make a great Py3 project (more so than generators, which would probably take a lot of background and familiarity with the code to get started on). Maybe making cython -3 (which use Py3 semantics), as discussed in a previous thread. Also, for anyone interested in working on Cython for GSoC, I would recommend finding a minor bug (e.g. on trac) and submitting a patch to get at least somewhat familiar with the codebase. - Robert From stefan_ml at behnel.de Wed Mar 24 16:02:33 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 24 Mar 2010 16:02:33 +0100 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <55283193-093B-49D3-8BEC-25BA40F6FA69@math.washington.edu> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> <4BA7BEA7.3090809@student.matnat.uio.no> <4BA7D83F.6030103@behnel.de> <55283193-093B-49D3-8BEC-25BA40F6FA69@math.washington.edu> Message-ID: <4BAA2989.2050307@behnel.de> Robert Bradshaw, 24.03.2010 15:40: > On Mar 22, 2010, at 1:51 PM, Stefan Behnel wrote: >> IIRC, the PEP suggests the use of tuples to provide multiple >> annotations. > > We should support that then. Actually, it doesn't. I don't remember where I read that, then. I couldn't find any reference to a preferred usage of multiple annotations now that I looked for it. I guess that'll just have to emerge over time then. So far, there's certainly too few people using it. Anyway, I think that at this time, supporting both a plain annotation as well as a tuple of several annotations is the only sensible way of supporting annotations in a cooperative way. New ways of providing more than one annotation can always be added to that later on. So I'd propose to let Cython walk through the annotations of each parameter, pick out those that come from the 'cython' namespace, and incorporate them into the parameter declarations (if they don't collide with other declarations, which would be an error). Especially strings would be ignored, as they can nicely be used to provide parameter documentation. I also think that *all* annotations should end up in the __annotations__ dict, including those interpreted by Cython. However, to avoid an actual dependency on Cython, should they be converted to their string names? Stefan From Chris.Barker at noaa.gov Wed Mar 24 17:38:44 2010 From: Chris.Barker at noaa.gov (Christopher Barker) Date: Wed, 24 Mar 2010 09:38:44 -0700 Subject: [Cython] bug #520: scanner broken for DOS line endings In-Reply-To: <7EEC9143-BBDD-41ED-B608-509479F47CA6@math.washington.edu> References: <4BA9B894.2070701@behnel.de> <4BA9BD42.2090207@student.matnat.uio.no> <4BA9C202.5050801@behnel.de> <7EEC9143-BBDD-41ED-B608-509479F47CA6@math.washington.edu> Message-ID: <4BAA4014.40702@noaa.gov> >>> Totally amazing that we haven't got complaints about this before from >>> Windows users. >> Same from here. that's probably because it handles "native" line endings OK -- so the problem only shows up if you process a file with DOS endings on a *nix machine. I imagine most of us have set up our version control system to do line ending translation. > I didn't ever see any failures on my box either. (FYI, OS X apps > typically default to (or at least handle seamlessly) Unix line > endings, right - the old mac-style '\r' is pretty much dead -- it will only show up if someone is working with old files generated by a "Classic" app. Or, I suppose, if they changed the defaults on their editor a while back when they were using Classic apps, and haven't changed it back. > Can anyone actually reproduce this on Windows. The example in question > was provided by a Windows user who clearly had no issues with it. We haven't had a problem, but then again, I think most of our Cython code is being written in vim on Linux in a VM, then compiled on Windows, so probably *nix line endings... -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker at noaa.gov From ondrej at certik.cz Wed Mar 24 19:38:47 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 24 Mar 2010 11:38:47 -0700 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> Message-ID: <85b5c3131003241138o5998ab87v96cc05b19cca7cf3@mail.gmail.com> Hi Haoyu, On Sun, Mar 21, 2010 at 4:31 AM, Haoyu Bai wrote: > Hi, > > I'm Haoyu Bai, a graduate student from National University of > Singapore. I'm quite interested in help Cython for improving Python > compatibility as a GSoC project. I have some experience of working > with Boost.Python and SWIG, and have use Python for several years, so > I'm familiar with Python internals and C-API. Also, I have used Cython > several times and pretty love its user-friendliness. Thanks your great > works! :) I just want to encourage you to apply for Cython. It's a great tool and things that you proposed would be awesome additions to it. Ondrej From dagss at student.matnat.uio.no Wed Mar 24 20:17:28 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Wed, 24 Mar 2010 20:17:28 +0100 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <4BAA2989.2050307@behnel.de> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> <4BA7BEA7.3090809@student.matnat.uio.no> <4BA7D83F.6030103@behnel.de> <55283193-093B-49D3-8BEC-25BA40F6FA69@math.washington.edu> <4BAA2989.2050307@behnel.de> Message-ID: <4BAA6548.2000900@student.matnat.uio.no> Stefan Behnel wrote: > Robert Bradshaw, 24.03.2010 15:40: >> On Mar 22, 2010, at 1:51 PM, Stefan Behnel wrote: >>> IIRC, the PEP suggests the use of tuples to provide multiple >>> annotations. >> We should support that then. > > Actually, it doesn't. I don't remember where I read that, then. I couldn't It might have been a cython-dev post by me (just as a proposal well over a year ago). > find any reference to a preferred usage of multiple annotations now that I > looked for it. I guess that'll just have to emerge over time then. So far, > there's certainly too few people using it. > > Anyway, I think that at this time, supporting both a plain annotation as > well as a tuple of several annotations is the only sensible way of > supporting annotations in a cooperative way. New ways of providing more > than one annotation can always be added to that later on. My proposal was that one would have a tuple (cython_type, inner), which would be replaced by inner after Cython had processed it, so that Cython could work with "bad citizens" which expected the annotations all for themselves. (Hence the decorator notation.) However I like your proposal much, much better -- better to encourage good practices; we can always add another, more inconvenient method if compatability with bad citizens are needed. Also documentation tools etc. could very well make use of Cython annotations run-time. I vote for making C style declarations available in __annotations__ as well... -- Dag Sverre From ondrej at certik.cz Wed Mar 24 21:08:53 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 24 Mar 2010 13:08:53 -0700 Subject: [Cython] fwrap status + source codes Message-ID: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> Hi, do you think that fwrap would be usable over the summer? I will be working with some fortran codes (f95), so I am curious whether I should stay with f2py, or invest some time into fwrap and use Cython, that I use anyway for C/C++. Is there some webpage for it? Bug tracker? I found this blog: http://fortrancython.wordpress.com/ and I found this: http://hg.cython.org/fwrap-dev/ Is this the official source code? How do I compile it and use it? How do I run tests? I tried: $ python fwrap.py Traceback (most recent call last): File "fwrap.py", line 4, in main() File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 12, in main wrap(options, args) File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 18, in wrap funcs_templ = [(fc_wrap.generate_fortran, "%s_c.f90"), AttributeError: 'module' object has no attribute 'generate_fortran' $ python runtests.py Traceback (most recent call last): File "runtests.py", line 799, in from fwrap_src.Main import wrap And I didn't find any setup.py, or README (I found NOTES.txt, but no build instructions). Sorry for the basic questions, I just want to give it a shot. Kurt and Dag seem to be working/interested in this, so if we can get something usable (90%) to get started and then once I can use it for my stuff (I just need simple wrappers for now), it'd be really cool. Thanks, Ondrej From kwmsmith at gmail.com Wed Mar 24 21:40:59 2010 From: kwmsmith at gmail.com (Kurt Smith) Date: Wed, 24 Mar 2010 14:40:59 -0600 Subject: [Cython] fwrap status + source codes In-Reply-To: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> Message-ID: On Wed, Mar 24, 2010 at 2:08 PM, Ondrej Certik wrote: > Hi, > > do you think that fwrap would be usable over the summer? I will be > working with some fortran codes (f95), so I am curious whether I > should stay with f2py, or invest some time into fwrap and use Cython, > that I use anyway for C/C++. > I'm working on the build system as we speak :-) > Is there some webpage for it? Bug tracker? > I've been putting my time in on coding lately, so fwrap's public face is sorely lacking. I need to remedy this soon. Thanks for the motivation -- you and others have made it clear that soon is 'now.' > I found this blog: > > http://fortrancython.wordpress.com/ > To be updated soon, too (I know, long on promises -- I'm very short on time lately). But the project is active. > and I found this: > > http://hg.cython.org/fwrap-dev/ > > Is this the official source code? > > How do I compile it and use it? How do I run tests? I tried: > > $ python fwrap.py > Traceback (most recent call last): > ?File "fwrap.py", line 4, in > ? ?main() > ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 12, in main > ? ?wrap(options, args) > ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 18, in wrap > ? ?funcs_templ = [(fc_wrap.generate_fortran, "%s_c.f90"), > AttributeError: 'module' object has no attribute 'generate_fortran' > > $ python runtests.py > Traceback (most recent call last): > ?File "runtests.py", line 799, in > ? ?from fwrap_src.Main import wrap > > > And I didn't find any setup.py, or README (I found NOTES.txt, but no > build instructions). Sorry for the basic questions, I just want to > give it a shot. > > > Kurt and Dag seem to be working/interested in this, so if we can get > something usable (90%) to get started and then once I can use it for > my stuff (I just need simple wrappers for now), it'd be really cool. I'm *really* aiming for something like 90% by the end of April, as unlikely as the above makes it seem. I'll put a post on the blog (fortrancython.wordpress.com/) about what 90% means. It will include all the basic support stuff that you mention above (where hosted (likely bitbucket), a mailing-list, and the features supported). So by summer (end of May) fwrap should have a couple of releases out, and be usable. It will certainly work for simple wrappers (no callback support). Although that depends on just what you mean by 'simple.' Your interest is very much appreciated. Thanks. Kurt From greg.ewing at canterbury.ac.nz Wed Mar 24 22:22:26 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Thu, 25 Mar 2010 09:22:26 +1200 Subject: [Cython] bug #520: scanner broken for DOS line endings In-Reply-To: <4BA9BD42.2090207@student.matnat.uio.no> References: <4BA9B894.2070701@behnel.de> <4BA9BD42.2090207@student.matnat.uio.no> Message-ID: <4BAA8292.3020806@canterbury.ac.nz> Dag Sverre Seljebotn wrote: > Perhaps newer Macs use Unix-style line endings, given the change to a > BSD kernel? Native MacOSX applications normally use Unix line endings. Apps carried over from the Classic era may still use \r, but they're becoming increasingly rare, so it's quite likely nobody has been tripped up by this yet. -- Greg From ondrej at certik.cz Thu Mar 25 07:06:56 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Wed, 24 Mar 2010 23:06:56 -0700 Subject: [Cython] fwrap status + source codes In-Reply-To: References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> Message-ID: <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> Hi Kurt! On Wed, Mar 24, 2010 at 1:40 PM, Kurt Smith wrote: > On Wed, Mar 24, 2010 at 2:08 PM, Ondrej Certik wrote: >> Hi, >> >> do you think that fwrap would be usable over the summer? I will be >> working with some fortran codes (f95), so I am curious whether I >> should stay with f2py, or invest some time into fwrap and use Cython, >> that I use anyway for C/C++. >> > > I'm working on the build system as we speak :-) > >> Is there some webpage for it? Bug tracker? >> > > I've been putting my time in on coding lately, so fwrap's public face > is sorely lacking. ?I need to remedy this soon. ?Thanks for the > motivation -- you and others have made it clear that soon is 'now.' > >> I found this blog: >> >> http://fortrancython.wordpress.com/ >> > > To be updated soon, too (I know, long on promises -- I'm very short on > time lately). ?But the project is active. > >> and I found this: >> >> http://hg.cython.org/fwrap-dev/ >> >> Is this the official source code? >> >> How do I compile it and use it? How do I run tests? I tried: >> >> $ python fwrap.py >> Traceback (most recent call last): >> ?File "fwrap.py", line 4, in >> ? ?main() >> ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 12, in main >> ? ?wrap(options, args) >> ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 18, in wrap >> ? ?funcs_templ = [(fc_wrap.generate_fortran, "%s_c.f90"), >> AttributeError: 'module' object has no attribute 'generate_fortran' >> >> $ python runtests.py >> Traceback (most recent call last): >> ?File "runtests.py", line 799, in >> ? ?from fwrap_src.Main import wrap >> >> >> And I didn't find any setup.py, or README (I found NOTES.txt, but no >> build instructions). Sorry for the basic questions, I just want to >> give it a shot. >> >> >> Kurt and Dag seem to be working/interested in this, so if we can get >> something usable (90%) to get started and then once I can use it for >> my stuff (I just need simple wrappers for now), it'd be really cool. > > I'm *really* aiming for something like 90% by the end of April, as > unlikely as the above makes it seem. ?I'll put a post on the blog > (fortrancython.wordpress.com/) about what 90% means. ?It will include > all the basic support stuff that you mention above (where hosted > (likely bitbucket), a mailing-list, and the features supported). > > So by summer (end of May) fwrap should have a couple of releases out, > and be usable. ?It will certainly work for simple wrappers (no > callback support). ?Although that depends on just what you mean by > 'simple.' That should be enough. I use cmake, so I'd appreciate if fwrap could just spit the C (or fortran or both) files, that I compile and link myself. Just like Cython does. (I read some issues with integrating fwrap into build tools like distutils etc. ---- I really don't need that at all). If I can wrap fortran subroutines, pass some arguments to them from Python and get results back (as numpy arrays), that's all I need. I will need passing numpy arrays in and out though --- I don't know if this is "simple" or not. I don't need callbacks. It seems to me, that fwrap is really close. Can you please ping me, when I can run some simple example with fwrap? I will need some time to learn it, so if you could just fix fwrap so that it runs, it'd be cool for now. :) I am excited, couldn't wait to see this working, at least a little bit. Thanks and looking forward! Ondrej From dagss at student.matnat.uio.no Thu Mar 25 07:52:12 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Thu, 25 Mar 2010 07:52:12 +0100 Subject: [Cython] fwrap status + source codes In-Reply-To: <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> Message-ID: <4BAB081C.6000208@student.matnat.uio.no> Ondrej Certik wrote: > Hi Kurt! > > On Wed, Mar 24, 2010 at 1:40 PM, Kurt Smith wrote: >> On Wed, Mar 24, 2010 at 2:08 PM, Ondrej Certik wrote: >>> Hi, >>> >>> do you think that fwrap would be usable over the summer? I will be >>> working with some fortran codes (f95), so I am curious whether I >>> should stay with f2py, or invest some time into fwrap and use Cython, >>> that I use anyway for C/C++. >>> >> I'm working on the build system as we speak :-) >> >>> Is there some webpage for it? Bug tracker? >>> >> I've been putting my time in on coding lately, so fwrap's public face >> is sorely lacking. I need to remedy this soon. Thanks for the >> motivation -- you and others have made it clear that soon is 'now.' >> >>> I found this blog: >>> >>> http://fortrancython.wordpress.com/ >>> >> To be updated soon, too (I know, long on promises -- I'm very short on >> time lately). But the project is active. >> >>> and I found this: >>> >>> http://hg.cython.org/fwrap-dev/ >>> >>> Is this the official source code? >>> >>> How do I compile it and use it? How do I run tests? I tried: >>> >>> $ python fwrap.py >>> Traceback (most recent call last): >>> File "fwrap.py", line 4, in >>> main() >>> File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 12, in main >>> wrap(options, args) >>> File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 18, in wrap >>> funcs_templ = [(fc_wrap.generate_fortran, "%s_c.f90"), >>> AttributeError: 'module' object has no attribute 'generate_fortran' >>> >>> $ python runtests.py >>> Traceback (most recent call last): >>> File "runtests.py", line 799, in >>> from fwrap_src.Main import wrap >>> >>> >>> And I didn't find any setup.py, or README (I found NOTES.txt, but no >>> build instructions). Sorry for the basic questions, I just want to >>> give it a shot. >>> >>> >>> Kurt and Dag seem to be working/interested in this, so if we can get >>> something usable (90%) to get started and then once I can use it for >>> my stuff (I just need simple wrappers for now), it'd be really cool. >> I'm *really* aiming for something like 90% by the end of April, as >> unlikely as the above makes it seem. I'll put a post on the blog >> (fortrancython.wordpress.com/) about what 90% means. It will include >> all the basic support stuff that you mention above (where hosted >> (likely bitbucket), a mailing-list, and the features supported). >> >> So by summer (end of May) fwrap should have a couple of releases out, >> and be usable. It will certainly work for simple wrappers (no >> callback support). Although that depends on just what you mean by >> 'simple.' > > That should be enough. I use cmake, so I'd appreciate if fwrap could > just spit the C (or fortran or both) files, that I compile and link > myself. Just like Cython does. (I read some issues with integrating Note that fwrap generates both C, Fortran and Cython code to make everything work together in a standard-compliant way... Also, unless things have changed, a configuration script must be run as part of the build process to detect which C types correspond to which Fortran types. f2py just makes blatant assumptions in this area and you could probably impose such blatant assumptions yourself too if you want to avoid complicating your build. (If you are building Cython code via cmake in a reasonably robust manner we would love to have the necesarry scripts contributed to the our Tools/ dir. Have you just hardcoded the C compilation flags or do you try to be portable and query distutils.sysconfig?) Dag Sverre From stefan_ml at behnel.de Thu Mar 25 07:54:14 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 25 Mar 2010 07:54:14 +0100 Subject: [Cython] type declarations as function annotations In-Reply-To: <4BAA6548.2000900@student.matnat.uio.no> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> <4BA7BEA7.3090809@student.matnat.uio.no> <4BA7D83F.6030103@behnel.de> <55283193-093B-49D3-8BEC-25BA40F6FA69@math.washington.edu> <4BAA2989.2050307@behnel.de> <4BAA6548.2000900@student.matnat.uio.no> Message-ID: <4BAB0896.9030302@behnel.de> Dag Sverre Seljebotn, 24.03.2010 20:17: > I vote for making C style declarations available in __annotations__ as > well... I assume you meant the normal Cython syntax declarations, i.e. type declarations that were not written as Py3 annotation, right? That's a good idea, too. My last question was rather *how* to make them available, though. If we use custom objects to represent them, the module will end up with a dependency on those objects. If we convert them to a string representation, they'll be much easier to handle by generic tools, but interpreting them requires a C parser. Also, how should code know that it was Cython that generated the annotations, and that they *can* be interpreted as C types? We could use a special string syntax, e.g. "C[...]", or "Cython(...)", or maybe "ctype:...". I think we should discuss this on c.l.py, so that other interested projects can jump in. Stefan From ondrej at certik.cz Thu Mar 25 08:37:36 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 25 Mar 2010 00:37:36 -0700 Subject: [Cython] fwrap status + source codes In-Reply-To: <4BAB081C.6000208@student.matnat.uio.no> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> Message-ID: <85b5c3131003250037g5798d6fcs84972ce9de35c16d@mail.gmail.com> On Wed, Mar 24, 2010 at 11:52 PM, Dag Sverre Seljebotn wrote: > Ondrej Certik wrote: >> Hi Kurt! >> >> On Wed, Mar 24, 2010 at 1:40 PM, Kurt Smith wrote: >>> On Wed, Mar 24, 2010 at 2:08 PM, Ondrej Certik wrote: >>>> Hi, >>>> >>>> do you think that fwrap would be usable over the summer? I will be >>>> working with some fortran codes (f95), so I am curious whether I >>>> should stay with f2py, or invest some time into fwrap and use Cython, >>>> that I use anyway for C/C++. >>>> >>> I'm working on the build system as we speak :-) >>> >>>> Is there some webpage for it? Bug tracker? >>>> >>> I've been putting my time in on coding lately, so fwrap's public face >>> is sorely lacking. ?I need to remedy this soon. ?Thanks for the >>> motivation -- you and others have made it clear that soon is 'now.' >>> >>>> I found this blog: >>>> >>>> http://fortrancython.wordpress.com/ >>>> >>> To be updated soon, too (I know, long on promises -- I'm very short on >>> time lately). ?But the project is active. >>> >>>> and I found this: >>>> >>>> http://hg.cython.org/fwrap-dev/ >>>> >>>> Is this the official source code? >>>> >>>> How do I compile it and use it? How do I run tests? I tried: >>>> >>>> $ python fwrap.py >>>> Traceback (most recent call last): >>>> ?File "fwrap.py", line 4, in >>>> ? ?main() >>>> ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 12, in main >>>> ? ?wrap(options, args) >>>> ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 18, in wrap >>>> ? ?funcs_templ = [(fc_wrap.generate_fortran, "%s_c.f90"), >>>> AttributeError: 'module' object has no attribute 'generate_fortran' >>>> >>>> $ python runtests.py >>>> Traceback (most recent call last): >>>> ?File "runtests.py", line 799, in >>>> ? ?from fwrap_src.Main import wrap >>>> >>>> >>>> And I didn't find any setup.py, or README (I found NOTES.txt, but no >>>> build instructions). Sorry for the basic questions, I just want to >>>> give it a shot. >>>> >>>> >>>> Kurt and Dag seem to be working/interested in this, so if we can get >>>> something usable (90%) to get started and then once I can use it for >>>> my stuff (I just need simple wrappers for now), it'd be really cool. >>> I'm *really* aiming for something like 90% by the end of April, as >>> unlikely as the above makes it seem. ?I'll put a post on the blog >>> (fortrancython.wordpress.com/) about what 90% means. ?It will include >>> all the basic support stuff that you mention above (where hosted >>> (likely bitbucket), a mailing-list, and the features supported). >>> >>> So by summer (end of May) fwrap should have a couple of releases out, >>> and be usable. ?It will certainly work for simple wrappers (no >>> callback support). ?Although that depends on just what you mean by >>> 'simple.' >> >> That should be enough. I use cmake, so I'd appreciate if fwrap could >> just spit the C (or fortran or both) files, that I compile and link >> myself. Just like Cython does. (I read some issues with integrating > > Note that fwrap generates both C, Fortran and Cython code to make > everything work together in a standard-compliant way... > > Also, unless things have changed, a configuration script must be run as > part of the build process to detect which C types correspond to which > Fortran types. f2py just makes blatant assumptions in this area and you > could probably impose such blatant assumptions yourself too if you want > to avoid complicating your build. That's fine. I just want some simple example of wrapping a simple fortran program, so that I can see this myself. I didn't manage to figure this out by myself using the fwrap hg repository and I didn't find any documentation. So I am waiting for Kurt's help. :) > > (If you are building Cython code via cmake in a reasonably robust manner > we would love to have the necesarry scripts contributed to the our > Tools/ dir. Have you just hardcoded the C compilation flags or do you > try to be portable and query distutils.sysconfig?) The cmake file that I wrote is attached, and the usage is in the comments at the top of the file. I don't query distutils.sysconfig. But I tested it on linux, Mac and windows using cygwin (and espeically mac has a way different way of compiling and linking stuff) and it works. Ondrej -------------- next part -------------- A non-text attachment was scrubbed... Name: UseCython.cmake Type: application/octet-stream Size: 1718 bytes Desc: not available Url : http://codespeak.net/pipermail/cython-dev/attachments/20100325/9b5b101e/attachment.obj From stefan_ml at behnel.de Thu Mar 25 08:52:50 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 25 Mar 2010 08:52:50 +0100 Subject: [Cython] C++ wrapping documentation in the Wiki Message-ID: <4BAB1652.6090409@behnel.de> Hi, I just noticed that the C++ wrapping documentation in the Wiki isn't all that complete and also somewhat misleading. There are (at least) three major pages, the most outdated (first) one of which is the easiest to find: http://wiki.cython.org/WrappingCPlusPlus http://wiki.cython.org/enhancements/cpp http://wiki.cython.org/gsoc09/daniloaf/progress The last page describes the current status, whereas the first one is outdated and misleading, and the second one is incomplete at best. It looks like the first page is also part of the official documentation: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html So this also needs an update. Would someone volunteer to invest a little time in cleaning those up and merging the content of the third page back into the first one (and preferably the official documentation), so that we get a useful tutorial for C++ users? Thanks! Stefan From dagss at student.matnat.uio.no Thu Mar 25 09:28:01 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Thu, 25 Mar 2010 09:28:01 +0100 Subject: [Cython] fwrap status + source codes In-Reply-To: <85b5c3131003250037g5798d6fcs84972ce9de35c16d@mail.gmail.com> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> <85b5c3131003250037g5798d6fcs84972ce9de35c16d@mail.gmail.com> Message-ID: <4BAB1E91.3010905@student.matnat.uio.no> Ondrej Certik wrote: > On Wed, Mar 24, 2010 at 11:52 PM, Dag Sverre Seljebotn > wrote: > >> Ondrej Certik wrote: >> >>> Hi Kurt! >>> >>> On Wed, Mar 24, 2010 at 1:40 PM, Kurt Smith wrote: >>> >>>> On Wed, Mar 24, 2010 at 2:08 PM, Ondrej Certik wrote: >>>> >>>>> Hi, >>>>> >>>>> do you think that fwrap would be usable over the summer? I will be >>>>> working with some fortran codes (f95), so I am curious whether I >>>>> should stay with f2py, or invest some time into fwrap and use Cython, >>>>> that I use anyway for C/C++. >>>>> >>>>> >>>> I'm working on the build system as we speak :-) >>>> >>>> >>>>> Is there some webpage for it? Bug tracker? >>>>> >>>>> >>>> I've been putting my time in on coding lately, so fwrap's public face >>>> is sorely lacking. I need to remedy this soon. Thanks for the >>>> motivation -- you and others have made it clear that soon is 'now.' >>>> >>>> >>>>> I found this blog: >>>>> >>>>> http://fortrancython.wordpress.com/ >>>>> >>>>> >>>> To be updated soon, too (I know, long on promises -- I'm very short on >>>> time lately). But the project is active. >>>> >>>> >>>>> and I found this: >>>>> >>>>> http://hg.cython.org/fwrap-dev/ >>>>> >>>>> Is this the official source code? >>>>> >>>>> How do I compile it and use it? How do I run tests? I tried: >>>>> >>>>> $ python fwrap.py >>>>> Traceback (most recent call last): >>>>> File "fwrap.py", line 4, in >>>>> main() >>>>> File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 12, in main >>>>> wrap(options, args) >>>>> File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 18, in wrap >>>>> funcs_templ = [(fc_wrap.generate_fortran, "%s_c.f90"), >>>>> AttributeError: 'module' object has no attribute 'generate_fortran' >>>>> >>>>> $ python runtests.py >>>>> Traceback (most recent call last): >>>>> File "runtests.py", line 799, in >>>>> from fwrap_src.Main import wrap >>>>> >>>>> >>>>> And I didn't find any setup.py, or README (I found NOTES.txt, but no >>>>> build instructions). Sorry for the basic questions, I just want to >>>>> give it a shot. >>>>> >>>>> >>>>> Kurt and Dag seem to be working/interested in this, so if we can get >>>>> something usable (90%) to get started and then once I can use it for >>>>> my stuff (I just need simple wrappers for now), it'd be really cool. >>>>> >>>> I'm *really* aiming for something like 90% by the end of April, as >>>> unlikely as the above makes it seem. I'll put a post on the blog >>>> (fortrancython.wordpress.com/) about what 90% means. It will include >>>> all the basic support stuff that you mention above (where hosted >>>> (likely bitbucket), a mailing-list, and the features supported). >>>> >>>> So by summer (end of May) fwrap should have a couple of releases out, >>>> and be usable. It will certainly work for simple wrappers (no >>>> callback support). Although that depends on just what you mean by >>>> 'simple.' >>>> >>> That should be enough. I use cmake, so I'd appreciate if fwrap could >>> just spit the C (or fortran or both) files, that I compile and link >>> myself. Just like Cython does. (I read some issues with integrating >>> >> Note that fwrap generates both C, Fortran and Cython code to make >> everything work together in a standard-compliant way... >> >> Also, unless things have changed, a configuration script must be run as >> part of the build process to detect which C types correspond to which >> Fortran types. f2py just makes blatant assumptions in this area and you >> could probably impose such blatant assumptions yourself too if you want >> to avoid complicating your build. >> > > That's fine. I just want some simple example of wrapping a simple > fortran program, so that I can see this myself. I didn't manage to > figure this out by myself using the fwrap hg repository and I didn't > find any documentation. So I am waiting for Kurt's help. :) > > >> (If you are building Cython code via cmake in a reasonably robust manner >> we would love to have the necesarry scripts contributed to the our >> Tools/ dir. Have you just hardcoded the C compilation flags or do you >> try to be portable and query distutils.sysconfig?) >> > > The cmake file that I wrote is attached, and the usage is in the > comments at the top of the file. I don't query distutils.sysconfig. > But I tested it on linux, Mac and windows using cygwin (and espeically > mac has a way different way of compiling and linking stuff) and it > works. > Thanks. You are aware of the -fno-strict-aliasing issue, right? Unless the C files are compiled with that they may suddenly segfault or corrupt data if the compiler does a certain optimization... this is a restriction of Python itself, not something Cython does. python -c 'from distutils.sysconfig import get_config_var as g; print g("CFLAGS")' Dag Sverre From stefan_ml at behnel.de Thu Mar 25 09:39:23 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 25 Mar 2010 09:39:23 +0100 Subject: [Cython] Deferring closures to 0.14? Message-ID: <4BAB213B.2010804@behnel.de> Hi, there hasn't been a major Cython release for a while now, but an immense aggregation of features. Given that there are still issues with the closures branch (ref-counting issues this time), would it be ok to push out a 0.13 from the current cython-devel, and to make closures the only big new feature of Cython 0.14? That would allow us to release 0.14 whenever we think closures are ready enough for a release (I don't mind if it happens within a week or another couple of months), but it would allow users to get started with simpler C++ wrapping, type inference, and tons of other great goodies right now, no matter how much more time it takes us to get it fixed up and merged. Objections? Stefan From robertwb at math.washington.edu Thu Mar 25 09:53:49 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Thu, 25 Mar 2010 01:53:49 -0700 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: <4BAB213B.2010804@behnel.de> References: <4BAB213B.2010804@behnel.de> Message-ID: <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> On Mar 25, 2010, at 1:39 AM, Stefan Behnel wrote: > Hi, > > there hasn't been a major Cython release for a while now, but an > immense > aggregation of features. Given that there are still issues with the > closures branch (ref-counting issues this time), would it be ok to > push out > a 0.13 from the current cython-devel, and to make closures the only > big new > feature of Cython 0.14? > > That would allow us to release 0.14 whenever we think closures are > ready > enough for a release (I don't mind if it happens within a week or > another > couple of months), but it would allow users to get started with > simpler C++ > wrapping, type inference, and tons of other great goodies right now, > no > matter how much more time it takes us to get it fixed up and merged. > > Objections? It's not the closures code that's holding anything up, as far as I know it's ready to go (with a small type inferencing issue, which could be easily resolved at first by disabling type inference for closures). It's the fact that Sage doesn't (didn't?) compile with the latest cython-devel tip. - Robert From stefan_ml at behnel.de Thu Mar 25 10:17:15 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 25 Mar 2010 10:17:15 +0100 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> References: <4BAB213B.2010804@behnel.de> <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> Message-ID: <4BAB2A1B.2020603@behnel.de> Robert Bradshaw, 25.03.2010 09:53: > On Mar 25, 2010, at 1:39 AM, Stefan Behnel wrote: > It's not the closures code that's holding anything up, as far as I > know it's ready to go (with a small type inferencing issue, which > could be easily resolved at first by disabling type inference for > closures). ... and which then lead to a reproducible, GC related crash in Hudson: Doctest: closure_tests_4.__test__.g1923 (line 205) ... python: Modules/gcmodule.c:277: visit_decref: Assertion `gc->gc.gc_refs != 0' failed. So, it's not ready for a merge yet, and we may still run into more problems while fixing it up. As I said, I don't mind if we get it fixed within a couple of weeks so that 0.14 follows 0.13 closely. We never kept an old branch alive for that much longer when we release a new one. > It's the fact that Sage doesn't (didn't?) compile with the > latest cython-devel tip. Ah, ok, I wasn't aware of that. Sounds like we should get the sagelib into Hudson rather sooner than later then. Does it look like big trouble all over the place or is it just a few things that look fixable at first glance? Stefan From ondrej at certik.cz Thu Mar 25 16:33:57 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Thu, 25 Mar 2010 08:33:57 -0700 Subject: [Cython] fwrap status + source codes In-Reply-To: <4BAB1E91.3010905@student.matnat.uio.no> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> <85b5c3131003250037g5798d6fcs84972ce9de35c16d@mail.gmail.com> <4BAB1E91.3010905@student.matnat.uio.no> Message-ID: <85b5c3131003250833i47fa2a68o6a414df83e64f1ff@mail.gmail.com> On Thu, Mar 25, 2010 at 1:28 AM, Dag Sverre Seljebotn wrote: > Ondrej Certik wrote: >> On Wed, Mar 24, 2010 at 11:52 PM, Dag Sverre Seljebotn >> wrote: >> >>> Ondrej Certik wrote: >>> >>>> Hi Kurt! >>>> >>>> On Wed, Mar 24, 2010 at 1:40 PM, Kurt Smith wrote: >>>> >>>>> On Wed, Mar 24, 2010 at 2:08 PM, Ondrej Certik wrote: >>>>> >>>>>> Hi, >>>>>> >>>>>> do you think that fwrap would be usable over the summer? I will be >>>>>> working with some fortran codes (f95), so I am curious whether I >>>>>> should stay with f2py, or invest some time into fwrap and use Cython, >>>>>> that I use anyway for C/C++. >>>>>> >>>>>> >>>>> I'm working on the build system as we speak :-) >>>>> >>>>> >>>>>> Is there some webpage for it? Bug tracker? >>>>>> >>>>>> >>>>> I've been putting my time in on coding lately, so fwrap's public face >>>>> is sorely lacking. ?I need to remedy this soon. ?Thanks for the >>>>> motivation -- you and others have made it clear that soon is 'now.' >>>>> >>>>> >>>>>> I found this blog: >>>>>> >>>>>> http://fortrancython.wordpress.com/ >>>>>> >>>>>> >>>>> To be updated soon, too (I know, long on promises -- I'm very short on >>>>> time lately). ?But the project is active. >>>>> >>>>> >>>>>> and I found this: >>>>>> >>>>>> http://hg.cython.org/fwrap-dev/ >>>>>> >>>>>> Is this the official source code? >>>>>> >>>>>> How do I compile it and use it? How do I run tests? I tried: >>>>>> >>>>>> $ python fwrap.py >>>>>> Traceback (most recent call last): >>>>>> ?File "fwrap.py", line 4, in >>>>>> ? ?main() >>>>>> ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 12, in main >>>>>> ? ?wrap(options, args) >>>>>> ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 18, in wrap >>>>>> ? ?funcs_templ = [(fc_wrap.generate_fortran, "%s_c.f90"), >>>>>> AttributeError: 'module' object has no attribute 'generate_fortran' >>>>>> >>>>>> $ python runtests.py >>>>>> Traceback (most recent call last): >>>>>> ?File "runtests.py", line 799, in >>>>>> ? ?from fwrap_src.Main import wrap >>>>>> >>>>>> >>>>>> And I didn't find any setup.py, or README (I found NOTES.txt, but no >>>>>> build instructions). Sorry for the basic questions, I just want to >>>>>> give it a shot. >>>>>> >>>>>> >>>>>> Kurt and Dag seem to be working/interested in this, so if we can get >>>>>> something usable (90%) to get started and then once I can use it for >>>>>> my stuff (I just need simple wrappers for now), it'd be really cool. >>>>>> >>>>> I'm *really* aiming for something like 90% by the end of April, as >>>>> unlikely as the above makes it seem. ?I'll put a post on the blog >>>>> (fortrancython.wordpress.com/) about what 90% means. ?It will include >>>>> all the basic support stuff that you mention above (where hosted >>>>> (likely bitbucket), a mailing-list, and the features supported). >>>>> >>>>> So by summer (end of May) fwrap should have a couple of releases out, >>>>> and be usable. ?It will certainly work for simple wrappers (no >>>>> callback support). ?Although that depends on just what you mean by >>>>> 'simple.' >>>>> >>>> That should be enough. I use cmake, so I'd appreciate if fwrap could >>>> just spit the C (or fortran or both) files, that I compile and link >>>> myself. Just like Cython does. (I read some issues with integrating >>>> >>> Note that fwrap generates both C, Fortran and Cython code to make >>> everything work together in a standard-compliant way... >>> >>> Also, unless things have changed, a configuration script must be run as >>> part of the build process to detect which C types correspond to which >>> Fortran types. f2py just makes blatant assumptions in this area and you >>> could probably impose such blatant assumptions yourself too if you want >>> to avoid complicating your build. >>> >> >> That's fine. I just want some simple example of wrapping a simple >> fortran program, so that I can see this myself. I didn't manage to >> figure this out by myself using the fwrap hg repository and I didn't >> find any documentation. So I am waiting for Kurt's help. :) >> >> >>> (If you are building Cython code via cmake in a reasonably robust manner >>> we would love to have the necesarry scripts contributed to the our >>> Tools/ dir. Have you just hardcoded the C compilation flags or do you >>> try to be portable and query distutils.sysconfig?) >>> >> >> The cmake file that I wrote is attached, and the usage is in the >> comments at the top of the file. I don't query distutils.sysconfig. >> But I tested it on linux, Mac and windows using cygwin (and espeically >> mac has a way different way of compiling and linking stuff) and it >> works. >> > Thanks. You are aware of the -fno-strict-aliasing issue, right? Unless > the C files are compiled with that they may suddenly segfault or corrupt > data if the compiler does a certain optimization... this is a > restriction of Python itself, not something Cython does. > > python -c 'from distutils.sysconfig import get_config_var as g; print > g("CFLAGS")' No, I wasn't aware of that! Thanks for pointing this out to me. I'll fix it. Ondrej From dsurviver at gmail.com Thu Mar 25 18:16:11 2010 From: dsurviver at gmail.com (Danilo Freitas) Date: Thu, 25 Mar 2010 14:16:11 -0300 Subject: [Cython] C++ wrapping documentation in the Wiki In-Reply-To: <4BAB1652.6090409@behnel.de> References: <4BAB1652.6090409@behnel.de> Message-ID: <45239151003251016i7a500306v1a924032db6a0142@mail.gmail.com> Hi, I'll take a look on it. Today maybe I don't have enough time, but I guess this weekend I'll have it done. 2010/3/25 Stefan Behnel : > Hi, > > I just noticed that the C++ wrapping documentation in the Wiki isn't all > that complete and also somewhat misleading. There are (at least) three > major pages, the most outdated (first) one of which is the easiest to find: > > http://wiki.cython.org/WrappingCPlusPlus > > http://wiki.cython.org/enhancements/cpp > > http://wiki.cython.org/gsoc09/daniloaf/progress > > The last page describes the current status, whereas the first one is > outdated and misleading, and the second one is incomplete at best. > > It looks like the first page is also part of the official documentation: > > http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html > > So this also needs an update. > > Would someone volunteer to invest a little time in cleaning those up and > merging the content of the third page back into the first one (and > preferably the official documentation), so that we get a useful tutorial > for C++ users? > > Thanks! > > Stefan > _______________________________________________ > Cython-dev mailing list > Cython-dev at codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev > -- - Danilo Freitas From craigcitro at gmail.com Thu Mar 25 18:21:28 2010 From: craigcitro at gmail.com (Craig Citro) Date: Thu, 25 Mar 2010 10:21:28 -0700 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> References: <4BAB213B.2010804@behnel.de> <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> Message-ID: > It's not the closures code that's holding anything up, as far as I > know it's ready to go (with a small type inferencing issue, which > could be easily resolved at first by disabling type inference for > closures). It's the fact that Sage doesn't (didn't?) compile with the > latest cython-devel tip. > Yep, there's an issue with type inferencing and sage that took me a little while to track down. It's a case where I'm not sure what the "right" fix is -- it's a question of the semantics of import and cimport. I've got half an email to this list drafted about it, which I've finally got time to sit down and finish up right now ... Honestly, the much bigger holdup is that my house has apparently become a bastion of illness -- today is shaping up to be the second day this month that my daughter doesn't have a cold. :) -cc From dalcinl at gmail.com Thu Mar 25 19:43:22 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Thu, 25 Mar 2010 15:43:22 -0300 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: <4BAB2A1B.2020603@behnel.de> References: <4BAB213B.2010804@behnel.de> <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> <4BAB2A1B.2020603@behnel.de> Message-ID: On 25 March 2010 06:17, Stefan Behnel wrote: > > ... and which then lead to a reproducible, GC related crash in Hudson: > > Doctest: closure_tests_4.__test__.g1923 (line 205) ... python: > Modules/gcmodule.c:277: visit_decref: Assertion `gc->gc.gc_refs != 0' failed. > BTW, I've previously commented on something related to this ... In CPython, classes defining __del__() are not GC'ed, because __del__ can invoke arbitrary Python code... However, this is not the case for Cython's __dealloc__ ... Am I missing something? -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From stefan_ml at behnel.de Thu Mar 25 20:04:50 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 25 Mar 2010 20:04:50 +0100 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: References: <4BAB213B.2010804@behnel.de> <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> <4BAB2A1B.2020603@behnel.de> Message-ID: <4BABB3D2.4040607@behnel.de> Lisandro Dalcin, 25.03.2010 19:43: > On 25 March 2010 06:17, Stefan Behnel wrote: >> >> ... and which then lead to a reproducible, GC related crash in Hudson: >> >> Doctest: closure_tests_4.__test__.g1923 (line 205) ... python: >> Modules/gcmodule.c:277: visit_decref: Assertion `gc->gc.gc_refs != 0' failed. >> > > BTW, I've previously commented on something related to this ... In > CPython, classes defining __del__() are not GC'ed, because __del__ > can invoke arbitrary Python code... However, this is not the case for > Cython's __dealloc__ ... Am I missing something? Well, yes, there is a difference. Python code can only do Python operations for cleanup, so this can lead to all sorts of complications. On the other hand, C code can run any kind of safe C operations, and this is often required for timely memory cleanup in wrapper code. The special __dealloc__ method serves the latter purpose. Although it certainly is not hard to write crashing code in __dealloc__, it's rather unlikely that someone does so a) on purpose or b) without recognising it somewhat soon due to a crash. Stefan From dagss at student.matnat.uio.no Thu Mar 25 20:08:53 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Thu, 25 Mar 2010 20:08:53 +0100 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: <4BABB3D2.4040607@behnel.de> References: <4BAB213B.2010804@behnel.de> <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> <4BAB2A1B.2020603@behnel.de> <4BABB3D2.4040607@behnel.de> Message-ID: <4BABB4C5.5080402@student.matnat.uio.no> Stefan Behnel wrote: > Lisandro Dalcin, 25.03.2010 19:43: > >> On 25 March 2010 06:17, Stefan Behnel wrote: >> >>> ... and which then lead to a reproducible, GC related crash in Hudson: >>> >>> Doctest: closure_tests_4.__test__.g1923 (line 205) ... python: >>> Modules/gcmodule.c:277: visit_decref: Assertion `gc->gc.gc_refs != 0' failed. >>> >>> >> BTW, I've previously commented on something related to this ... In >> CPython, classes defining __del__() are not GC'ed, because __del__ >> can invoke arbitrary Python code... However, this is not the case for >> Cython's __dealloc__ ... Am I missing something? >> > > Well, yes, there is a difference. Python code can only do Python operations > for cleanup, so this can lead to all sorts of complications. On the other > hand, C code can run any kind of safe C operations, and this is often > required for timely memory cleanup in wrapper code. > > The special __dealloc__ method serves the latter purpose. Although it > certainly is not hard to write crashing code in __dealloc__, it's rather > unlikely that someone does so a) on purpose or b) without recognising it > somewhat soon due to a crash. > Would __dealloc__ be implied "nogil" (that is, as in the signature, not as in "with nogil") solve anything which isn't already solved? Or is that already the case? Dag Sverre From dsurviver at gmail.com Thu Mar 25 20:09:25 2010 From: dsurviver at gmail.com (Danilo Freitas) Date: Thu, 25 Mar 2010 16:09:25 -0300 Subject: [Cython] C++ wrapping documentation in the Wiki In-Reply-To: <45239151003251016i7a500306v1a924032db6a0142@mail.gmail.com> References: <4BAB1652.6090409@behnel.de> <45239151003251016i7a500306v1a924032db6a0142@mail.gmail.com> Message-ID: <45239151003251209t696ac97bw81cf0bc034de52d@mail.gmail.com> I did a few changes on http://wiki.cython.org/WrappingCPlusPlus It's not complete yet, but already has the basic syntax for C++ Wrapping. If something is wrong or incomplete, please report here. 2010/3/25 Danilo Freitas : > Hi, > > I'll take a look on it. Today maybe I don't have enough time, but I > guess this weekend I'll have it done. > > 2010/3/25 Stefan Behnel : >> Hi, >> >> I just noticed that the C++ wrapping documentation in the Wiki isn't all >> that complete and also somewhat misleading. There are (at least) three >> major pages, the most outdated (first) one of which is the easiest to find: >> >> http://wiki.cython.org/WrappingCPlusPlus >> >> http://wiki.cython.org/enhancements/cpp >> >> http://wiki.cython.org/gsoc09/daniloaf/progress >> >> The last page describes the current status, whereas the first one is >> outdated and misleading, and the second one is incomplete at best. >> >> It looks like the first page is also part of the official documentation: >> >> http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html >> >> So this also needs an update. >> >> Would someone volunteer to invest a little time in cleaning those up and >> merging the content of the third page back into the first one (and >> preferably the official documentation), so that we get a useful tutorial >> for C++ users? >> >> Thanks! >> >> Stefan >> _______________________________________________ >> Cython-dev mailing list >> Cython-dev at codespeak.net >> http://codespeak.net/mailman/listinfo/cython-dev >> > > > > -- > - Danilo Freitas > -- - Danilo Freitas From stefan_ml at behnel.de Thu Mar 25 20:42:56 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 25 Mar 2010 20:42:56 +0100 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: <4BABB4C5.5080402@student.matnat.uio.no> References: <4BAB213B.2010804@behnel.de> <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> <4BAB2A1B.2020603@behnel.de> <4BABB3D2.4040607@behnel.de> <4BABB4C5.5080402@student.matnat.uio.no> Message-ID: <4BABBCC0.7030904@behnel.de> Dag Sverre Seljebotn, 25.03.2010 20:08: > Stefan Behnel wrote: >> Lisandro Dalcin, 25.03.2010 19:43: >>> BTW, I've previously commented on something related to this ... In >>> CPython, classes defining __del__() are not GC'ed, because __del__ >>> can invoke arbitrary Python code... However, this is not the case for >>> Cython's __dealloc__ ... Am I missing something? >> >> Well, yes, there is a difference. Python code can only do Python operations >> for cleanup, so this can lead to all sorts of complications. On the other >> hand, C code can run any kind of safe C operations, and this is often >> required for timely memory cleanup in wrapper code. >> >> The special __dealloc__ method serves the latter purpose. Although it >> certainly is not hard to write crashing code in __dealloc__, it's rather >> unlikely that someone does so a) on purpose or b) without recognising it >> somewhat soon due to a crash. >> > Would __dealloc__ be implied "nogil" (that is, as in the signature, not > as in "with nogil") solve anything which isn't already solved? Or is > that already the case? No, it's not, and Python operations are not evil per se. From the top of my head, I don't even know which Python operations are really dangerous in __dealloc__, and even those that are may not lead to an error in a particular case due to the way GC works. So I'd prefer allowing them in general, and just discourage them in the docs. If we ever find patterns that should better be disallowed, we can try to issue a warning, but detecting that programmatically may not be trivial. I think this is one of those parts of the language where we just have to accept that people can use it to shoot themselves in the foot. Stefan From dsurviver at gmail.com Fri Mar 26 01:30:00 2010 From: dsurviver at gmail.com (Danilo Freitas) Date: Thu, 25 Mar 2010 21:30:00 -0300 Subject: [Cython] C++ wrapping documentation in the Wiki In-Reply-To: <45239151003251209t696ac97bw81cf0bc034de52d@mail.gmail.com> References: <4BAB1652.6090409@behnel.de> <45239151003251016i7a500306v1a924032db6a0142@mail.gmail.com> <45239151003251209t696ac97bw81cf0bc034de52d@mail.gmail.com> Message-ID: <45239151003251730k410b971dq8f49f6ef4f53674d@mail.gmail.com> Worked a little more on it: http://wiki.cython.org/WrappingCPlusPlus Still missing a few stuff, I guess only inheritance and references, and also removing some old stuff. -- - Danilo Freitas From tjhnson at gmail.com Fri Mar 26 04:20:12 2010 From: tjhnson at gmail.com (T J) Date: Thu, 25 Mar 2010 20:20:12 -0700 Subject: [Cython] cdef function accepting numpy array Message-ID: Hi, I want to call x[start:stop:skip].sum() on a NumPy array within cython code. The problem with doing this is that the call is a Python call, which is handled in C, whose result is converted into a Python float. So, I'd have to convert this Python float back into a double. So it seems best if I just wrote the summation manually. I give it a NumPy array, a start index, stop index, and a skip index. I can write the loop, but I am not sure how I should define the cdef function signature. Can someone help me out? I need something like: cdef double sum(np.ndarray[np.float64_t, ndim=1] x, unsigned int start, unsigned int stop, int skip): cdef unsigned int i cdef double result for i in range(start, stop, skip): result += x[i] return result I'm actually hoping the cython can accept the above and do the magic for me. Looking at C which accesses NumPy arrays: (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float64_t *, __pyx_bstruct_a.buf, __pyx_t_8, __pyx_bstride_0_a) seems to suggest I might need something involving buffers and strides. Or is it possible to call the NumPy C function (assuming it doesn't convert to the Python float within that same function)? Thanks! From greg.ewing at canterbury.ac.nz Fri Mar 26 05:09:57 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Fri, 26 Mar 2010 17:09:57 +1300 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: <4BABBCC0.7030904@behnel.de> References: <4BAB213B.2010804@behnel.de> <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> <4BAB2A1B.2020603@behnel.de> <4BABB3D2.4040607@behnel.de> <4BABB4C5.5080402@student.matnat.uio.no> <4BABBCC0.7030904@behnel.de> Message-ID: <4BAC3395.60203@canterbury.ac.nz> Stefan Behnel wrote: > From the top of my > head, I don't even know which Python operations are really dangerous in > __dealloc__, and even those that are may not lead to an error in a > particular case due to the way GC works. The only thing I can think of offhand that might be a problem in the face of GC in particular is that when __dealloc__ gets called, some of the object's Python-valued attributes may have been set to None as a result of cycle-breaking. If you were to assume they were pointing to some particular type of object, a crash could result. More generally, the main thing to be careful of in __dealloc__ is that Python-valued attributes defined by a subclass may no longer contain valid references (not even to None) by the time the base class's __dealloc__ gets called. So if it does anything that directly or indirectly results in a method of the subclass getting called, and that method tries to use one of the subclass's attributes, a crash could result. But this has nothing to do with GC particularly. -- Greg From craigcitro at gmail.com Fri Mar 26 07:38:53 2010 From: craigcitro at gmail.com (Craig Citro) Date: Thu, 25 Mar 2010 23:38:53 -0700 Subject: [Cython] semantics of import and cimport Message-ID: I have a question about the semantics of mixing imports and cimports. I started drafting a long message with lots of details and examples, but I realized I should just start with a few simple questions: if you do from Foo import Bar from Foo cimport Bar in a .pyx file, is it safe to assume that the Bar we import at runtime is going to be coming from the same module whose .pxd we analyzed at compile-time? Is it at least safe to assume that the Bar we import at runtime will be the same *type* as the Bar we saw at compile-time? (The next obvious question: what other information can we assume is the same?) Of course, this sounds reasonable enough, but completely goes against the usual Python import semantics, since there are so many ways to change what will actually get imported at runtime. In general, it would be nice (specifically as our type inferencer becomes more sophisticated) to be able to provide Cython with as much information as possible about the module it's going to see at runtime -- not just what's in the .pxd. In fact, if you know at compile-time which module is going to be there at runtime, it would be helpful to be able to tell Cython "here's the module, feel free to analyze it however you'd like." This makes sense even for import statements without a corresponding cimport. Would people be opposed to a "@cython.staticimport" decorator or the like? -cc From dagss at student.matnat.uio.no Fri Mar 26 07:51:43 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 26 Mar 2010 07:51:43 +0100 Subject: [Cython] semantics of import and cimport In-Reply-To: References: Message-ID: <4BAC597F.1090702@student.matnat.uio.no> Craig Citro wrote: > I have a question about the semantics of mixing imports and cimports. > I started drafting a long message with lots of details and examples, > but I realized I should just start with a few simple questions: if you > do > > from Foo import Bar > from Foo cimport Bar > > in a .pyx file, is it safe to assume that the Bar we import at runtime > is going to be coming from the same module whose .pxd we analyzed at > compile-time? Is it at least safe to assume that the Bar we import at > runtime will be the same *type* as the Bar we saw at compile-time? > (The next obvious question: what other information can we assume is > the same?) > I can't help you, but: If not much is assumed at the moment, my vote is to be able to start assuming stuff (and that the backwards compatability breakage is a sacrifice one can make). Basically, if a pxd and a Python module has the same name, assume that the pxd file is written with detailed knowledge of what goes on in the Python module, and exists to provide hints to Cython about faster execution. > Of course, this sounds reasonable enough, but completely goes against > the usual Python import semantics, since there are so many ways to > change what will actually get imported at runtime. > But one can still do import tricks etc., as long as the pxd file written is written with knowledge about it. > In general, it would be nice (specifically as our type inferencer > becomes more sophisticated) to be able to provide Cython with as much > information as possible about the module it's going to see at runtime > -- not just what's in the .pxd. In fact, if you know at compile-time > which module is going to be there at runtime, it would be helpful to > be able to tell Cython "here's the module, feel free to analyze it > however you'd like." This makes sense even for import statements > without a corresponding cimport. Would people be opposed to a > "@cython.staticimport" decorator or the like? > Hmm. In my experience the return value of functions is much more important though, and how can one possibly get those through introspection? So I'm unsure about the gain... This is more in a grey zone, because there's a long tradition of making standalone C files which can be compiled on any system independent of the exact installed version of the Python library (at least, with a pxd file, there's a human to make conscious decisions about everything). But I'm usually not opposed to directives that are off by default... Dag Sverre From dagss at student.matnat.uio.no Fri Mar 26 08:00:18 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 26 Mar 2010 08:00:18 +0100 Subject: [Cython] cdef function accepting numpy array In-Reply-To: References: Message-ID: <4BAC5B82.7060606@student.matnat.uio.no> T J wrote: > Hi, > > I want to call x[start:stop:skip].sum() on a NumPy array within cython > code. The problem with doing this is that the call is a Python call, > which is handled in C, whose result is converted into a Python float. > So, I'd have to convert this Python float back into a double. So it > seems best if I just wrote the summation manually. I give it a NumPy > The conversion to float isn't the problem, that's almost free compared to taking that x[start:stop:skip] call and doing the sum() itself. I expect you are doing lots and lots of really small summations? Otherwise there's no real point. > array, a start index, stop index, and a skip index. I can write the > loop, but I am not sure how I should define the cdef function > signature. > > Can someone help me out? I need something like: > > cdef double sum(np.ndarray[np.float64_t, ndim=1] x, unsigned int > start, unsigned int stop, int skip): > You unfortunately need to do cdef double sum(object x, unsigned int start, unsigned int stop, int skip): cdef np.ndarray[np.float64_t, ndim=1] x_buf = x then use x_buf. There's a slight speed penalty with that assignment which Also, please use Py_ssize_t instead of unsigned int. unsigned ints are dangerous in Cython. > cdef unsigned int i > cdef double result > You need to initialize result to 0. Dag Sverre From dagss at student.matnat.uio.no Fri Mar 26 08:03:05 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 26 Mar 2010 08:03:05 +0100 Subject: [Cython] semantics of import and cimport In-Reply-To: <4BAC597F.1090702@student.matnat.uio.no> References: <4BAC597F.1090702@student.matnat.uio.no> Message-ID: <4BAC5C29.3090709@student.matnat.uio.no> Dag Sverre Seljebotn wrote: > Craig Citro wrote: > >> I have a question about the semantics of mixing imports and cimports. >> I started drafting a long message with lots of details and examples, >> but I realized I should just start with a few simple questions: if you >> do >> >> from Foo import Bar >> from Foo cimport Bar >> >> in a .pyx file, is it safe to assume that the Bar we import at runtime >> is going to be coming from the same module whose .pxd we analyzed at >> compile-time? Is it at least safe to assume that the Bar we import at >> runtime will be the same *type* as the Bar we saw at compile-time? >> (The next obvious question: what other information can we assume is >> the same?) >> >> > I can't help you, but: If not much is assumed at the moment, my vote is > to be able to start assuming stuff (and that the backwards compatability > breakage is a sacrifice one can make). > > Basically, if a pxd and a Python module has the same name, assume that > the pxd file is written with detailed knowledge of what goes on in the > Python module, and exists to provide hints to Cython about faster execution. > One thing that is relevant: If you do cdef extern from ...: cdef class numpy.ndarray... or something like that (see numpy.pxd), then "numpy.ndarray" will be assumed to have the right type object (and this is checked at module import time). But this is through the "extern cdef class" statement, and not because the pxd has the same name as the Python module. Dag Sverre From craigcitro at gmail.com Fri Mar 26 08:05:25 2010 From: craigcitro at gmail.com (Craig Citro) Date: Fri, 26 Mar 2010 00:05:25 -0700 Subject: [Cython] semantics of import and cimport In-Reply-To: <4BAC597F.1090702@student.matnat.uio.no> References: <4BAC597F.1090702@student.matnat.uio.no> Message-ID: > I can't help you, but: If not much is assumed at the moment, my vote is > to be able to start assuming stuff (and that the backwards compatability > breakage is a sacrifice one can make). > > Basically, if a pxd and a Python module has the same name, assume that > the pxd file is written with detailed knowledge of what goes on in the > Python module, and exists to provide hints to Cython about faster execution. This is what I'm hoping everyone agrees on. :) > But one can still do import tricks etc., as long as the pxd file written > is written with knowledge about it. > True. I was more worried about the case that we were overstepping our bounds: I was imagining a "stub" implementation of some class (i.e. all functions just return None), along with the corresponding .pxd, and that (based on some flag or the like) which module was actually loaded would change at runtime. In this case, if we analyzed the stub module, we'd get a bunch of type information that would be *incorrect* at runtime. I don't know of a system using Cython that does this, but it sounds perfectly plausible ... > Hmm. In my experience the return value of functions is much more > important though, and how can one possibly get those through > introspection? So I'm unsure about the gain... > I guess I'm not sure what you mean by introspection here. I was thinking of being able to decorate imports in a .pyx file (or even .py or .pxd, really) to say "go ahead and analyze these at compile-time," specifically to do things like determine return types. It's true that in many cases you couldn't come up with much ... it's definitely something we'd want as an "opt-in" thing, because it could be extremely expensive for little gain. > This is more in a grey zone, because there's a long tradition of making > standalone C files which can be compiled on any system independent of > the exact installed version of the Python library (at least, with a pxd > file, there's a human to make conscious decisions about everything). > True -- this would mean that certain changes to the .py files that get imported would require calling Cython again to regenerate the C files. > But I'm usually not opposed to directives that are off by default... > This would *definitely* be off by default. Accidentally analyzing the whole python standard library because of an "import urllib, distutils" would *not* make us a lot of friends. ;) -cc From dagss at student.matnat.uio.no Fri Mar 26 08:13:43 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 26 Mar 2010 08:13:43 +0100 Subject: [Cython] semantics of import and cimport In-Reply-To: References: <4BAC597F.1090702@student.matnat.uio.no> Message-ID: <4BAC5EA7.7050707@student.matnat.uio.no> Craig Citro wrote: > >> Hmm. In my experience the return value of functions is much more >> important though, and how can one possibly get those through >> introspection? So I'm unsure about the gain... >> >> > > I guess I'm not sure what you mean by introspection here. I was > thinking of being able to decorate imports in a .pyx file (or even .py > or .pxd, really) to say "go ahead and analyze these at compile-time," > specifically to do things like determine return types. It's true that > in many cases you couldn't come up with much ... it's definitely > something we'd want as an "opt-in" thing, because it could be > extremely expensive for little gain. > With introspection I mean exactly what you say. I'm just not familiar with any methods to figure out return types of existing Python code. If I have foo.py containing def f(x): print x return 3 how do you analyze that the return type is int? I could have an if-test on x, so calling it won't help you, and the "inspect" module only gives you the number of arguments and so on... You could disassemble the byte-code or parse the Python source and then do analysis, but I'm assuming you're not volunteering for that... :-) Do mean providing a set of annotations for pure Python code, so that we can read def f(x: cython.int) -> cython.int in a pure Python file (using the Cython shadow module), and understand it? If so, +1. Dag Sverre From craigcitro at gmail.com Fri Mar 26 08:21:39 2010 From: craigcitro at gmail.com (Craig Citro) Date: Fri, 26 Mar 2010 00:21:39 -0700 Subject: [Cython] semantics of import and cimport In-Reply-To: <4BAC5EA7.7050707@student.matnat.uio.no> References: <4BAC597F.1090702@student.matnat.uio.no> <4BAC5EA7.7050707@student.matnat.uio.no> Message-ID: > You could disassemble the byte-code or parse the Python source and then > do analysis, but I'm assuming you're not volunteering for that... :-) > Not this week -- this is going to take a little bit longer. :) > Do mean providing a set of annotations for pure Python code, so that we > can read > > def f(x: cython.int) -> cython.int > > in a pure Python file (using the Cython shadow module), and understand it? > > If so, +1. > Yep, this is the obvious use case right now -- either simple functions (where we already can infer the type by analyzing the source code) or cases with explicit annotations. -cc From dalcinl at gmail.com Fri Mar 26 14:28:38 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 26 Mar 2010 10:28:38 -0300 Subject: [Cython] Deferring closures to 0.14? In-Reply-To: <4BAC3395.60203@canterbury.ac.nz> References: <4BAB213B.2010804@behnel.de> <41635156-B8DA-4154-B0FF-CF48A31008E5@math.washington.edu> <4BAB2A1B.2020603@behnel.de> <4BABB3D2.4040607@behnel.de> <4BABB4C5.5080402@student.matnat.uio.no> <4BABBCC0.7030904@behnel.de> <4BAC3395.60203@canterbury.ac.nz> Message-ID: On 26 March 2010 01:09, Greg Ewing wrote: > Stefan Behnel wrote: >> From the top of my >> head, I don't even know which Python operations are really dangerous in >> __dealloc__, and even those that are may not lead to an error in a >> particular case due to the way GC works. > > The only thing I can think of offhand that might > be a problem in the face of GC in particular is > that when __dealloc__ gets called, some of the > object's Python-valued attributes may have been > set to None as a result of cycle-breaking. If > you were to assume they were pointing to some > particular type of object, a crash could result. > > More generally, the main thing to be careful of > in __dealloc__ is that Python-valued attributes > defined by a subclass may no longer contain valid > references (not even to None) by the time the > base class's __dealloc__ gets called. So if it > does anything that directly or indirectly results > in a method of the subclass getting called, and > that method tries to use one of the subclass's > attributes, a crash could result. But this has > nothing to do with GC particularly. > So, Greg, see this: cdef class Foo: cdef object bar def __dealloc__(self): pass And this (Cython) generated code: static void __pyx_tp_dealloc_2qq_Foo(PyObject *o) { struct __pyx_obj_2qq_Foo *p = (struct __pyx_obj_2qq_Foo *)o; { PyObject *etype, *eval, *etb; PyErr_Fetch(&etype, &eval, &etb); ++Py_REFCNT(o); __pyx_pf_2qq_3Foo___dealloc__(o); if (PyErr_Occurred()) PyErr_WriteUnraisable(o); --Py_REFCNT(o); PyErr_Restore(etype, eval, etb); } Py_XDECREF(p->bar); (*Py_TYPE(o)->tp_free)(o); } Is the object "resurrection" (I mean, the ++Py_REFCNT(o) line) safe? -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From tjhnson at gmail.com Fri Mar 26 17:17:58 2010 From: tjhnson at gmail.com (T J) Date: Fri, 26 Mar 2010 09:17:58 -0700 Subject: [Cython] cdef function accepting numpy array In-Reply-To: <4BAC5B82.7060606@student.matnat.uio.no> References: <4BAC5B82.7060606@student.matnat.uio.no> Message-ID: On Fri, Mar 26, 2010 at 12:00 AM, Dag Sverre Seljebotn wrote: > > I expect you are doing lots and lots of really small summations? > Otherwise there's no real point. > Correct...lots of very small sums. > You unfortunately need to do > > cdef double sum(object x, unsigned int > start, unsigned int stop, int skip): > ? ?cdef np.ndarray[np.float64_t, ndim=1] x_buf = x > > Thanks! > then use x_buf. There's a slight speed penalty with that assignment which > > Also, please use Py_ssize_t instead of unsigned int. unsigned ints are > dangerous in Cython. > Whoops. I was following: http://wiki.cython.org/tutorials/numpy to avoid negative indexes, but I just found negative_indexes=False: http://wiki.cython.org/enhancements/buffer From dagss at student.matnat.uio.no Fri Mar 26 18:26:13 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 26 Mar 2010 18:26:13 +0100 Subject: [Cython] cdef function accepting numpy array In-Reply-To: References: <4BAC5B82.7060606@student.matnat.uio.no> Message-ID: <4BACEE35.7020605@student.matnat.uio.no> T J wrote: > On Fri, Mar 26, 2010 at 12:00 AM, Dag Sverre Seljebotn > wrote: >> I expect you are doing lots and lots of really small summations? >> Otherwise there's no real point. >> > > Correct...lots of very small sums. > >> You unfortunately need to do >> >> cdef double sum(object x, unsigned int >> start, unsigned int stop, int skip): >> cdef np.ndarray[np.float64_t, ndim=1] x_buf = x >> >> > > Thanks! > > >> then use x_buf. There's a slight speed penalty with that assignment which >> >> Also, please use Py_ssize_t instead of unsigned int. unsigned ints are >> dangerous in Cython. >> > > Whoops. I was following: > http://wiki.cython.org/tutorials/numpy > to avoid negative indexes, but I just found negative_indexes=False: > http://wiki.cython.org/enhancements/buffer And now the preferred method is @cython.wraparound(False) This is best explained in http://conference.scipy.org/proceedings/SciPy2009/paper_2/ Feel free to update the wiki. -- Dag Sverre From dagss at student.matnat.uio.no Fri Mar 26 20:50:19 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Fri, 26 Mar 2010 20:50:19 +0100 Subject: [Cython] Idea for speeding up testing of Cython: ccache Message-ID: <4BAD0FFB.9000400@student.matnat.uio.no> I just stumbled over this one: http://ccache.samba.org/ If we change Cython slightly to take an option not to output dates in the C file header, it could mean that some C files would be identical between test runs. And then ccache could significantly decrease the test times, without much more effort than installing and setting up ccache. I won't have time to try it out so I'm just jotting it down.. """ The basic idea is to detect when you are compiling exactly the same code a 2nd time and use the previously compiled output. You detect that it is the same code by forming a hash of: * the pre-processor output from running the compiler with -E * the command line options * the real compilers size and modification time * any stderr output generated by the compiler These are hashed using md4 (a strong hash) and a cache file is formed based on that hash result. When the same compilation is done a second time ccache is able to supply the correct compiler output (including all warnings etc) from the cache. """ -- Dag Sverre From dalcinl at gmail.com Fri Mar 26 21:30:39 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Fri, 26 Mar 2010 17:30:39 -0300 Subject: [Cython] Idea for speeding up testing of Cython: ccache In-Reply-To: <4BAD0FFB.9000400@student.matnat.uio.no> References: <4BAD0FFB.9000400@student.matnat.uio.no> Message-ID: On 26 March 2010 16:50, Dag Sverre Seljebotn wrote: > I just stumbled over this one: > > http://ccache.samba.org/ > I've been using it for ages... > If we change Cython slightly to take an option not to output dates in > the C file header, it could mean that some C files would be identical > between test runs. The data is in a C comment, no need to change anything... > > ? ? * the pre-processor output from running the compiler with -E > because the preprocessor removes comments. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From greg.ewing at canterbury.ac.nz Fri Mar 26 23:36:49 2010 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Sat, 27 Mar 2010 10:36:49 +1200 Subject: [Cython] semantics of import and cimport In-Reply-To: <4BAC597F.1090702@student.matnat.uio.no> References: <4BAC597F.1090702@student.matnat.uio.no> Message-ID: <4BAD3701.2060604@canterbury.ac.nz> Dag Sverre Seljebotn wrote: > Craig Citro wrote: > >>is it safe to assume that the Bar we import at runtime >>is going to be coming from the same module whose .pxd we analyzed at >>compile-time? Is it at least safe to assume that the Bar we import at >>runtime will be the same *type* as the Bar we saw at compile-time? The assumption in Pyrex is that they're compatible with the interface described in the .pyx file. >>In general, it would be nice ... to be able to provide Cython with as much >>information as possible about the module it's going to see at runtime >>-- not just what's in the .pxd. That would be a departure from the Pyrex view of things. The idea is that the .pxd describes the interface, and the .pyx contains implementation details that can change without having to recompile anything that uses the module. -- Greg From tjhnson at gmail.com Sat Mar 27 02:54:24 2010 From: tjhnson at gmail.com (T J) Date: Fri, 26 Mar 2010 18:54:24 -0700 Subject: [Cython] array module Message-ID: Can Cython's buffer interface work with the 'array' module in Python's standard library? I need a growable array of floats and would like to do some computations on them. Converting to a NumPy array before computation is one option, but if I can write a cython function which can access those elements directly, this seems like a better route. If this is possible, can someone provide a quick example? From ellisonbg.net at gmail.com Sat Mar 27 05:19:42 2010 From: ellisonbg.net at gmail.com (Brian Granger) Date: Fri, 26 Mar 2010 21:19:42 -0700 Subject: [Cython] SciPy 2010 Tutorials: brainstorming and call for proposals Message-ID: <6ce0ac131003262119r17c113f7l3449f89d899eb8@mail.gmail.com> Greetings everyone, This year, there will be two days of tutorials (June 28th and 29th) before the main SciPy 2010 conference. Each of the two tutorial tracks (intro, advanced) will have a 3-4 hour morning and afternoon session both days, for a total of 4 intro sessions and 4 advanced sessions. The main tutorial web page for SciPy 2010 is here: http://conference.scipy.org/scipy2010/tutorials.html We are currently in the process of planning the tutorial sessions. You can help us in two ways: Brainstorm/vote on potential tutorial topics ============================================ To help us plan the tutorials, we have setup a web site that allow everyone in the community to brainstorm and vote on tutorial ideas/topics. The website for brainstorming/voting is here: http://conference.scipy.org/scipy2010/tutorialsUV.html The tutorial committee will use this information to help select the tutorials. Please jump in and let us know what tutorial topics you would like to see. Tutorial proposal submissions ============================= We are now accepting tutorial proposals from individuals or teams that would like to present a tutorial. Tutorials should be focused on covering a well defined topic in a hands on manner. We want to see tutorial attendees coding! We are pleased to offer tutorial presenters stipends this year for the first time: * 1 Session: $1,000 (half day) * 2 Sessions: $1,500 (full day) Optionally, part of this stipend can be applied to the presenter's registration costs. To submit a tutorial proposal please submit the following materials to 2010tutorials at scipy.org by April 15: * A short bio of the presenter or team members. * Which track the tutorial would be in (intro or advanced). * A short description and/or outline of the tutorial content. * A list of Python packages that attendees will need to have installed to follow along. From dagss at student.matnat.uio.no Sat Mar 27 08:33:51 2010 From: dagss at student.matnat.uio.no (Dag Sverre Seljebotn) Date: Sat, 27 Mar 2010 08:33:51 +0100 Subject: [Cython] array module In-Reply-To: References: Message-ID: <4BADB4DF.6030203@student.matnat.uio.no> T J wrote: > Can Cython's buffer interface work with the 'array' module in Python's > standard library? I need a growable array of floats and would like to > do some computations on them. Converting to a NumPy array before > computation is one option, but if I can write a cython function which > can access those elements directly, this seems like a better route. > If this is possible, can someone provide a quick example? There's some support here: http://trac.cython.org/cython_trac/ticket/314 Search for the related email discussion for examples if they are not in the patches. However, this is exactly the same as using NumPy arrays (with the [dtype, ndim]-notation). You don't gain anything except that your code will also work without NumPy installed. -- Dag Sverre From baihaoyu at gmail.com Sat Mar 27 16:13:55 2010 From: baihaoyu at gmail.com (Haoyu Bai) Date: Sat, 27 Mar 2010 23:13:55 +0800 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> Message-ID: <3eb99a591003270813x38a6d554u54419e693a345efc@mail.gmail.com> On Tue, Mar 23, 2010 at 3:07 AM, Craig Citro wrote: >> Thanks for your advice, Dag. Your suggestion about Python 3 support >> also lead to an interesting idea. That is, to make use of the Python 3 >> function annotation syntax to have a more convenient pure Python mode >> in Cython. >> > > I really like this idea -- I just ran into PEP 3107 (function > annotations) for the first time recently, and I was thinking exactly > the same thing. I'd definitely be interested in mentoring a project on > this front, and the Py3 angle probably gives us a good shot at getting > accepted by PSF. > > -cc Thanks for all the encouragements and suggestions. I have go through the relevant PEPs, CEPs, Cython issue tracker in these days. To getting familiar with Cython codebase, I even made a small patch for ticket #399 (but seems there is no way to submit it on trac?). As Stefan suggested, annotation support alone may not sufficient for a GSoC project. So I have go through the issue tracker to find other related things that could be included in a py3k project. So far, the works could fall into 4 groups: 1) Minor tickets related to Py3; 2) Pure Python mode with Py3 features; 3) m_clear() and per interpreter support; 4) Buffer related tickets. However, to include all of them may exceed the workload of GSoC, so maybe pick 2 or 3 of them is ok. Below is some details of the 4 groups. 1. Minor tickets related to Py3 This includes: * 'cython3' command directives for py3 * Exception chaining (#423) * Emulate Py3 print() for python <2.6 (#69) * Support 'nonlocal' (#490) * 'with' with multiple managers (#487) * future division not respected by C int (#259) (However, I cannot reproduce this, any detail about this?) * support for Ellipsis ('...') (#488) 2. Pure Python mode with py3 features I agree that we need to support tuple as annotation for a convention to be compatible with others. Besides function annotation, I'm also thinking to make use of decorators. As decorators now supported for classes, we could have @cdef @cpdef as decorator. This is a possible solution for get rid of .pxd files (ticket #112). A demo: import cython @cython.cdef class Foo: @cython.cdef def bar(self, x: cython.int) -> cython.p_char: ... Also, one could always use "from cython import *" to make the code more concise. 3. m_clear() and per interpreter state I tried to investigate this. However, even Python built-in modules are not implemented these, so maybe there's no example about how this could be done yet. So at first we should have test cases to demonstrate the problem, eg. memory leak after Py_Finalize(), or module states shared between interpreter instances. Then, Cython may need a proper resource management framework - we need to know where an object is allocated to, and when we should release it. If we are interested in doing this, I'll investigate more. 4. Buffer related tickets The new buffer protocol is also related to Py3, and there are many tickets about it. Is there already someone working on these, or I could also take these as part of GSoC work? I think a combination of 1,2,3 or 1,2,4 could be a well enough GSoC project. To me, 4. is more doable than 3., and of course, 2. is the most interesting part. Any suggestion? Craig, if you are interested to mentor, how could I contact you? Is there a Cython IRC channel, or an IRC channel you guys usually hanging on? Thank you! -- Haoyu BAI School of Computing, National University of Singapore. From Chris.Barker at noaa.gov Sun Mar 28 01:22:22 2010 From: Chris.Barker at noaa.gov (Christopher Barker) Date: Sat, 27 Mar 2010 17:22:22 -0700 Subject: [Cython] array module In-Reply-To: <4BADB4DF.6030203@student.matnat.uio.no> References: <4BADB4DF.6030203@student.matnat.uio.no> Message-ID: <4BAEA13E.2050000@noaa.gov> Dag Sverre Seljebotn wrote: > Converting to a NumPy array before >> computation is one option, You should be able to create a numpy array from an array.array without copying the data, so this could be pretty lightweight. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker at noaa.gov From mattcbro at earthlink.net Sat Mar 27 19:24:19 2010 From: mattcbro at earthlink.net (Matt) Date: Sat, 27 Mar 2010 18:24:19 +0000 (UTC) Subject: [Cython] bug #520: scanner broken for DOS line endings References: <4BA9B894.2070701@behnel.de> <4BA9BD42.2090207@student.matnat.uio.no> <4BAA8292.3020806@canterbury.ac.nz> Message-ID: Greg Ewing writes: > > Dag Sverre Seljebotn wrote: > > > Perhaps newer Macs use Unix-style line endings, given the change to a > > BSD kernel? > > Native MacOSX applications normally use Unix line endings. > Apps carried over from the Classic era may still use \r, > but they're becoming increasingly rare, so it's quite likely > nobody has been tripped up by this yet. > I didn't notice it until I upgraded to Python 2.6.5 on windows. Now all my cython code is broken (and a few other things were as well). Is there a fix available? It seems I always forget the rule about not upgrading while I'm actually trying to get something done. From stefan_ml at behnel.de Sun Mar 28 15:39:03 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 28 Mar 2010 15:39:03 +0200 Subject: [Cython] [cython-users] Help Installing Cython In-Reply-To: <98c3f7c51003271207s678085a0x223926bc9c963d58@mail.gmail.com> References: <773aca57-64a9-4e26-966e-a1aba6d628bf@g11g2000yqe.googlegroups.com> <4BAD15B6.5030207@behnel.de> <98c3f7c51003271207s678085a0x223926bc9c963d58@mail.gmail.com> Message-ID: <4BAF5BF7.9030307@behnel.de> James Reynolds, 27.03.2010 20:07: > c:\Python31\hello.pyx:1:23: Unrecognized character Interesting that people keep running into this now and no-one seemed to find this earlier... Anyway, the quick fix is to convert your source file to Unix line endings. > c:\Python31\Lib\cython\Cython\Plex\Scanners.c(384) : warning C4244: >> 'function' : > > conversion from 'Py_ssize_t' to 'long', possible loss of data > [...] Might be a new bug, I haven't seen this before. It's in the GetItemInt utility code. > Scanners.obj : warning LNK4197: export 'PyInit_Scanners' specified multiple >> times; using first specification Also interesting. No idea where this could come from. Maybe an MSVC specialty? > error: command 'mt.exe' failed: No such file or directory No idea what "mt.exe" might be. Stefan From dalcinl at gmail.com Mon Mar 29 18:09:04 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Mon, 29 Mar 2010 13:09:04 -0300 Subject: [Cython] [cython-users] Help Installing Cython In-Reply-To: <4BAF5BF7.9030307@behnel.de> References: <773aca57-64a9-4e26-966e-a1aba6d628bf@g11g2000yqe.googlegroups.com> <4BAD15B6.5030207@behnel.de> <98c3f7c51003271207s678085a0x223926bc9c963d58@mail.gmail.com> <4BAF5BF7.9030307@behnel.de> Message-ID: On 28 March 2010 10:39, Stefan Behnel wrote: > James Reynolds, 27.03.2010 20:07: > >> c:\Python31\Lib\cython\Cython\Plex\Scanners.c(384) : warning C4244: >>> 'function' : >> >> ? conversion from 'Py_ssize_t' to 'long', possible loss of data > ?> [...] > > Might be a new bug, I haven't seen this before. It's in the GetItemInt > utility code. > No, it is not a bug, but a good chance for a bit of cleanup : http://hg.cython.org/cython-devel/rev/1c8bbd5edd18 -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From stefan_ml at behnel.de Mon Mar 29 21:42:33 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 29 Mar 2010 21:42:33 +0200 Subject: [Cython] [cython-users] Help Installing Cython In-Reply-To: References: <773aca57-64a9-4e26-966e-a1aba6d628bf@g11g2000yqe.googlegroups.com> <4BAD15B6.5030207@behnel.de> <98c3f7c51003271207s678085a0x223926bc9c963d58@mail.gmail.com> <4BAF5BF7.9030307@behnel.de> Message-ID: <4BB102A9.8070902@behnel.de> Lisandro Dalcin, 29.03.2010 18:09: > On 28 March 2010 10:39, Stefan Behnel wrote: >> James Reynolds, 27.03.2010 20:07: >> >>> c:\Python31\Lib\cython\Cython\Plex\Scanners.c(384) : warning C4244: >>>> 'function' : >>> >>> conversion from 'Py_ssize_t' to 'long', possible loss of data >> > [...] >> >> Might be a new bug, I haven't seen this before. It's in the GetItemInt >> utility code. > > No, it is not a bug, but a good chance for a bit of cleanup : > > http://hg.cython.org/cython-devel/rev/1c8bbd5edd18 Nice. Stefan From ondrej at certik.cz Tue Mar 30 02:12:39 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Mon, 29 Mar 2010 17:12:39 -0700 Subject: [Cython] fwrap status + source codes In-Reply-To: <4BAB081C.6000208@student.matnat.uio.no> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> Message-ID: <85b5c3131003291712u7dc5c8fbuf17b6d12a93d6f50@mail.gmail.com> Hi Kurt! On Wed, Mar 24, 2010 at 11:52 PM, Dag Sverre Seljebotn wrote: > Ondrej Certik wrote: >> Hi Kurt! >> >> On Wed, Mar 24, 2010 at 1:40 PM, Kurt Smith wrote: >>> On Wed, Mar 24, 2010 at 2:08 PM, Ondrej Certik wrote: >>>> Hi, >>>> >>>> do you think that fwrap would be usable over the summer? I will be >>>> working with some fortran codes (f95), so I am curious whether I >>>> should stay with f2py, or invest some time into fwrap and use Cython, >>>> that I use anyway for C/C++. >>>> >>> I'm working on the build system as we speak :-) >>> >>>> Is there some webpage for it? Bug tracker? >>>> >>> I've been putting my time in on coding lately, so fwrap's public face >>> is sorely lacking. ?I need to remedy this soon. ?Thanks for the >>> motivation -- you and others have made it clear that soon is 'now.' >>> >>>> I found this blog: >>>> >>>> http://fortrancython.wordpress.com/ >>>> >>> To be updated soon, too (I know, long on promises -- I'm very short on >>> time lately). ?But the project is active. >>> >>>> and I found this: >>>> >>>> http://hg.cython.org/fwrap-dev/ >>>> >>>> Is this the official source code? >>>> >>>> How do I compile it and use it? How do I run tests? I tried: >>>> >>>> $ python fwrap.py >>>> Traceback (most recent call last): >>>> ?File "fwrap.py", line 4, in >>>> ? ?main() >>>> ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 12, in main >>>> ? ?wrap(options, args) >>>> ?File "/home/ondrej/repos/fwrap-dev/fwrap_src/main.py", line 18, in wrap >>>> ? ?funcs_templ = [(fc_wrap.generate_fortran, "%s_c.f90"), >>>> AttributeError: 'module' object has no attribute 'generate_fortran' >>>> >>>> $ python runtests.py >>>> Traceback (most recent call last): >>>> ?File "runtests.py", line 799, in >>>> ? ?from fwrap_src.Main import wrap >>>> >>>> >>>> And I didn't find any setup.py, or README (I found NOTES.txt, but no >>>> build instructions). Sorry for the basic questions, I just want to >>>> give it a shot. >>>> >>>> >>>> Kurt and Dag seem to be working/interested in this, so if we can get >>>> something usable (90%) to get started and then once I can use it for >>>> my stuff (I just need simple wrappers for now), it'd be really cool. >>> I'm *really* aiming for something like 90% by the end of April, as >>> unlikely as the above makes it seem. ?I'll put a post on the blog >>> (fortrancython.wordpress.com/) about what 90% means. ?It will include >>> all the basic support stuff that you mention above (where hosted >>> (likely bitbucket), a mailing-list, and the features supported). >>> >>> So by summer (end of May) fwrap should have a couple of releases out, >>> and be usable. ?It will certainly work for simple wrappers (no >>> callback support). ?Although that depends on just what you mean by >>> 'simple.' >> >> That should be enough. I use cmake, so I'd appreciate if fwrap could >> just spit the C (or fortran or both) files, that I compile and link >> myself. Just like Cython does. (I read some issues with integrating > > Note that fwrap generates both C, Fortran and Cython code to make > everything work together in a standard-compliant way... > > Also, unless things have changed, a configuration script must be run as > part of the build process to detect which C types correspond to which > Fortran types. f2py just makes blatant assumptions in this area and you > could probably impose such blatant assumptions yourself too if you want > to avoid complicating your build. let me know if I can help somehow with frap --- in the meantime I tried to figure out how to link gfortran with gcc, so that I can just use Cython myself to wrap fortran things. cd fwrap-dev/tests/compile/ gfortran -o int_args.o -c int_args.f95 cd intargs_stuff gfortran -o wrap_int_args.o -c wrap_int_args.f95 gcc -o drive_int_args.o -c drive_int_args.c gcc -o drive drive_int_args.o wrap_int_args.o ../int_args.o And: $ ./drive before subr call in c 1 2 4 8 after subr call in c 1 2 4 7 before func call in c 1 2 4 7 after func call in c 1 2 4 7 result=10 So this is cool and works fine. Now I just wrap void *int_args_subr(signed char *, short *, int *, long *); long int_args_func(signed char *, short *, int *, long *); using Cython. Why is the file wrap_int_args.f95 needed at all? Is it not possible to link with int_args.f95 directly? It contains the following code: subroutine wrapsubr_int_args_subr(a,b,c,d) bind(c,name="int_args_subr") use iso_c_binding implicit none integer(kind=c_signed_char), intent(in) :: a integer(kind=c_short), intent(in) :: b integer(kind=c_int), intent(in) :: c integer(kind=c_long), intent(out) :: d interface subroutine int_args_subr(a,b,c,d) integer(kind=1), intent(in) :: a integer(kind=2), intent(in) :: b integer(kind=4), intent(in) :: c integer(kind=8), intent(out) :: d end subroutine int_args_subr end interface call int_args_subr(a,b,c,d) end subroutine wrapsubr_int_args_subr is it so that gfortran generates a C compatible .o file? So the idea is that fwrap will generate the .f95 interface file, then the C file (or possibly just a C header file) and a .pyx file with the wrappers? Indeed, that'd be cool. But for my simple wrappers, it should not be such a big deal to do this by hand, right? I have to try an example with double arrays. If I can wrap that, then I am fine. Ondrej From ondrej at certik.cz Tue Mar 30 05:53:13 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Mon, 29 Mar 2010 20:53:13 -0700 Subject: [Cython] fwrap status + source codes In-Reply-To: <85b5c3131003291712u7dc5c8fbuf17b6d12a93d6f50@mail.gmail.com> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> <85b5c3131003291712u7dc5c8fbuf17b6d12a93d6f50@mail.gmail.com> Message-ID: <85b5c3131003292053k31c2cf03y8ae0fb4a0283c1cc@mail.gmail.com> On Mon, Mar 29, 2010 at 5:12 PM, Ondrej Certik wrote: [...] > should not be such a big deal to do this by hand, right? I have to try > an example with double arrays. If I can wrap that, then I am fine. Ok, so if I wrap things by hand, I can just write a simple C header file like this: void int_args_subr_(signed char *, short *, int *, long *, int *); long int_args_func_(signed char *, short *, int *, long *); then wrap it like this in Cython: cdef extern from "arrayobject.h": ctypedef int intp ctypedef extern class numpy.ndarray [object PyArrayObject]: cdef char *data cdef int nd cdef intp *dimensions cdef intp *strides cdef int flags cdef extern from "int_args.h": void int_args_subr_(signed char *, short *, int *, long *, int *) long int_args_func_(signed char *, short *, int *, long *) def int_args_subr(signed char a, short b, int c, ndarray d, int n): int_args_subr_(&a, &b, &c, (d.data), &n) def int_args_func(signed char a, short b, int c, long d): print "d before:", d r = int_args_func_(&a, &b, &c, &d) print "d after:", d return r link it and it works just fine. Here is how to test it: from numpy import array from int_args import int_args_func, int_args_subr def test_args1(): assert int_args_func(1, 10, 100, 4) == 10 def test_args_arrays(): a = array([5, 5, 5, 5], dtype="int64") int_args_subr(1, 10, 100, a, 4) assert list(a) == [111, 2, 8, -8] int_args_subr(1, 2, 3, a, 4) assert list(a) == [6, 2, 8, -8] int_args_subr(1, -2, 3, a, 4) assert list(a) == [2, 2, 8, -8] If you want to play with the code, just do: git clone git://github.com/certik/qsnake.git cd qsnake/fortran_test/ cmake . make cd lib py.test So I am happy that it is actually really simple to wrap Fortran codes. The only possible problems are argument ordering, "_" mangling, and argument types (long vs int) across compilers and architectures and I think the only robust solution is to either 1) generate the wrappers and do some checks with the current compilers/platform 2) do things by hand as I do, but write tests for all argument types to make sure that the wrappers work I guess fwrap will help with 1), but even 2) is fine for me, as I have just couple functions to wrap, so it's not a big deal to update the C header file and the Cython file by hand. At least I can nicely see what is happening below the hood in case there are some problems. Ondrej From kwmsmith at gmail.com Tue Mar 30 17:37:42 2010 From: kwmsmith at gmail.com (Kurt Smith) Date: Tue, 30 Mar 2010 09:37:42 -0600 Subject: [Cython] fwrap status + source codes In-Reply-To: <85b5c3131003292053k31c2cf03y8ae0fb4a0283c1cc@mail.gmail.com> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> <85b5c3131003291712u7dc5c8fbuf17b6d12a93d6f50@mail.gmail.com> <85b5c3131003292053k31c2cf03y8ae0fb4a0283c1cc@mail.gmail.com> Message-ID: On Mon, Mar 29, 2010 at 9:53 PM, Ondrej Certik wrote: [massive snippage] > > Ondrej Hi Ondrej, Sorry -- been out of town for the last 5 days and away from email. I'll get to your posts one-by-one. Thanks for pitching in. Fwrap will be in a much better state for collaboration soon -- I've been working on its guts exclusively for a while, so its public interface has gone out of sync. But like I said, I'll get to your posts soon. Kurt From kwmsmith at gmail.com Tue Mar 30 18:04:49 2010 From: kwmsmith at gmail.com (Kurt Smith) Date: Tue, 30 Mar 2010 10:04:49 -0600 Subject: [Cython] fwrap status + source codes In-Reply-To: <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> Message-ID: On Thu, Mar 25, 2010 at 12:06 AM, Ondrej Certik wrote: > Hi Kurt! >>> >>> >>> Kurt and Dag seem to be working/interested in this, so if we can get >>> something usable (90%) to get started and then once I can use it for >>> my stuff (I just need simple wrappers for now), it'd be really cool. >> >> I'm *really* aiming for something like 90% by the end of April, as >> unlikely as the above makes it seem. ?I'll put a post on the blog >> (fortrancython.wordpress.com/) about what 90% means. ?It will include >> all the basic support stuff that you mention above (where hosted >> (likely bitbucket), a mailing-list, and the features supported). >> >> So by summer (end of May) fwrap should have a couple of releases out, >> and be usable. ?It will certainly work for simple wrappers (no >> callback support). ?Although that depends on just what you mean by >> 'simple.' > > That should be enough. I use cmake, so I'd appreciate if fwrap could > just spit the C (or fortran or both) files, that I compile and link > myself. Just like Cython does. (I read some issues with integrating > fwrap into build tools like distutils etc. ---- I really don't need > that at all). If I can wrap fortran subroutines, pass some arguments > to them from Python and get results back (as numpy arrays), that's all > I need. I will need passing numpy arrays in and out though --- I don't > know if this is "simple" or not. I don't need callbacks. Scalars and numpy arrays will be supported, certainly. No problem. The issue that will take some work (as always) is getting everything working together -- fwrap, cython, user-specified fortran compiler (with entailed compiler flags, etc.), gcc, python, etc. Here's what fwrap will do, for the default case: You have a fortran source file, fsource.f90, with a function in it, say, bessel(knd, m, x). The default command invocation would look something like this -- fairly obvious: $ fwrap fsource.f90 Which would generate a bunch of source files in a directory 'fwrap-default' (the name can be changed with a flag), all necessary for wrapping the fsource.f90 source. By default, fwrap will take these source files and generate an extension module 'fwrap_default.so', placing it in the directory. The source files generated will include the cython .pxds necessary to use the wrapper code from other Cython source code. (If this doesn't make sense I'll come up with a more exhaustive example.) The C headers will be generated to use the wrappers from C source, too. It won't be necessary to recompile the Fortran source to call the wrapped code from Cython or C -- you just have to link in the shared object file (in this case, 'fwrap_default.so'). > > It seems to me, that fwrap is really close. Can you please ping me, > when I can run some simple example with fwrap? I will need some time > to learn it, so if you could just fix fwrap so that it runs, it'd be > cool for now. :) Yes, it is really close -- thanks for the input. I'll ping you. > > I am excited, couldn't wait to see this working, at least a little bit. > > Thanks and looking forward! > > Ondrej > _______________________________________________ > Cython-dev mailing list > Cython-dev at codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev > From kwmsmith at gmail.com Tue Mar 30 18:17:13 2010 From: kwmsmith at gmail.com (Kurt Smith) Date: Tue, 30 Mar 2010 10:17:13 -0600 Subject: [Cython] fwrap status + source codes In-Reply-To: <4BAB081C.6000208@student.matnat.uio.no> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> Message-ID: On Thu, Mar 25, 2010 at 12:52 AM, Dag Sverre Seljebotn wrote: [snip] >> >> That should be enough. I use cmake, so I'd appreciate if fwrap could >> just spit the C (or fortran or both) files, that I compile and link >> myself. Just like Cython does. (I read some issues with integrating > > Note that fwrap generates both C, Fortran and Cython code to make > everything work together in a standard-compliant way... > > Also, unless things have changed, a configuration script must be run as > part of the build process to detect which C types correspond to which > Fortran types. f2py just makes blatant assumptions in this area and you > could probably impose such blatant assumptions yourself too if you want > to avoid complicating your build. I'm shaking out these details as time goes on -- I'm settling on a few solutions for different use scenarios (not all are implemented, though). 1) David Cournapeau indicated that generating, compiling and running a fortran 'genconfig' program is a problem for using fwrap with scipy -- I believe because of windows restrictions. This is the easiest way to get the config modules/headers/pxd's generated without an fwrap dependency. 2) Allow the user to specify the mapping between each fortran kind-type-parameter and C type, with a dictionary in a specification file. This is brain-dead simple to get working -- not as user-friendly, though. May lead to hard-to-debug runtime segfaults if the mappings are specified incorrectly. 3) Let fwrap do it all -- but this requires fwrap to do the compilation step. Safer & friendlier, but introduces a compile-time dependency. > > (If you are building Cython code via cmake in a reasonably robust manner > we would love to have the necesarry scripts contributed to the our > Tools/ dir. Have you just hardcoded the C compilation flags or do you > try to be portable and query distutils.sysconfig?) > > Dag Sverre > _______________________________________________ > Cython-dev mailing list > Cython-dev at codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev > From robertwb at math.washington.edu Tue Mar 30 19:36:22 2010 From: robertwb at math.washington.edu (Robert Bradshaw) Date: Tue, 30 Mar 2010 10:36:22 -0700 Subject: [Cython] GSoC project for improving Python compatibility In-Reply-To: <3eb99a591003270813x38a6d554u54419e693a345efc@mail.gmail.com> References: <3eb99a591003210431s12042237ubd1be5c73b24bfec@mail.gmail.com> <4BA72159.6010007@student.matnat.uio.no> <3eb99a591003220939r3c5a94c3ibdd30826f885bc7e@mail.gmail.com> <3eb99a591003270813x38a6d554u54419e693a345efc@mail.gmail.com> Message-ID: <4A9A1D0E-EB65-4838-A59B-9E1737390DDC@math.washington.edu> On Mar 27, 2010, at 8:13 AM, Haoyu Bai wrote: > On Tue, Mar 23, 2010 at 3:07 AM, Craig Citro > wrote: >>> Thanks for your advice, Dag. Your suggestion about Python 3 support >>> also lead to an interesting idea. That is, to make use of the >>> Python 3 >>> function annotation syntax to have a more convenient pure Python >>> mode >>> in Cython. >>> >> >> I really like this idea -- I just ran into PEP 3107 (function >> annotations) for the first time recently, and I was thinking exactly >> the same thing. I'd definitely be interested in mentoring a project >> on >> this front, and the Py3 angle probably gives us a good shot at >> getting >> accepted by PSF. >> >> -cc > > Thanks for all the encouragements and suggestions. I have go through > the relevant PEPs, CEPs, Cython issue tracker in these days. To > getting familiar with Cython codebase, I even made a small patch for > ticket #399 (but seems there is no way to submit it on trac?). > > As Stefan suggested, annotation support alone may not sufficient for a > GSoC project. So I have go through the issue tracker to find other > related things that could be included in a py3k project. So far, the > works could fall into 4 groups: 1) Minor tickets related to Py3; 2) > Pure Python mode with Py3 features; 3) m_clear() and per interpreter > support; 4) Buffer related tickets. However, to include all of them > may exceed the workload of GSoC, so maybe pick 2 or 3 of them is ok. > > Below is some details of the 4 groups. > > 1. Minor tickets related to Py3 > This includes: > * 'cython3' command directives for py3 > * Exception chaining (#423) > * Emulate Py3 print() for python <2.6 (#69) > * Support 'nonlocal' (#490) > * 'with' with multiple managers (#487) > * future division not respected by C int (#259) > (However, I cannot reproduce this, any detail about this?) > * support for Ellipsis ('...') (#488) There's also Py3 scope binding rules for list comprehensions, exception blocks, etc. > 2. Pure Python mode with py3 features > I agree that we need to support tuple as annotation for a convention > to be compatible with others. Besides function annotation, I'm also > thinking to make use of decorators. As decorators now supported for > classes, we could have @cdef @cpdef as decorator. This is a possible > solution for get rid of .pxd files (ticket #112). A demo: > > import cython > @cython.cdef > class Foo: > @cython.cdef > def bar(self, x: cython.int) -> cython.p_char: > ... > > Also, one could always use "from cython import *" to make the code > more concise. That's an interesting idea, but how would one resolve from cython import * from X import * ? Also cython.int != __builtin__.int is important. Perhaps this would just be disallowed... > 3. m_clear() and per interpreter state > I tried to investigate this. However, even Python built-in modules are > not implemented these, so maybe there's no example about how this > could be done yet. > > So at first we should have test cases to demonstrate the problem, eg. > memory leak after Py_Finalize(), or module states shared between > interpreter instances. Then, Cython may need a proper resource > management framework - we need to know where an object is allocated > to, and when we should release it. > > If we are interested in doing this, I'll investigate more. > > 4. Buffer related tickets > The new buffer protocol is also related to Py3, and there are many > tickets about it. Is there already someone working on these, or I > could also take these as part of GSoC work? > > > I think a combination of 1,2,3 or 1,2,4 could be a well enough GSoC > project. To me, 4. is more doable than 3., and of course, 2. is the > most interesting part. Any suggestion? > > Craig, if you are interested to mentor, how could I contact you? Is > there a Cython IRC channel, or an IRC channel you guys usually hanging > on? We have an IRC channel, but I don't think it's usually very heavily populated. Email works well for most people in the Cython community, but of course everyone has individual preferences. I think 1 & 2 above would make a great project, and be a very valuable contribution. Under the same heading (depending on how things are going timewise) there are numerous improvements non-Py3 improvements one could make for pure mode as well. - Robert From ondrej at certik.cz Tue Mar 30 20:52:13 2010 From: ondrej at certik.cz (Ondrej Certik) Date: Tue, 30 Mar 2010 11:52:13 -0700 Subject: [Cython] fwrap status + source codes In-Reply-To: References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> Message-ID: <85b5c3131003301152x3725a8bel550b7d2885c06e09@mail.gmail.com> On Tue, Mar 30, 2010 at 9:17 AM, Kurt Smith wrote: > On Thu, Mar 25, 2010 at 12:52 AM, Dag Sverre Seljebotn > wrote: > [snip] > >>> >>> That should be enough. I use cmake, so I'd appreciate if fwrap could >>> just spit the C (or fortran or both) files, that I compile and link >>> myself. Just like Cython does. (I read some issues with integrating >> >> Note that fwrap generates both C, Fortran and Cython code to make >> everything work together in a standard-compliant way... >> >> Also, unless things have changed, a configuration script must be run as >> part of the build process to detect which C types correspond to which >> Fortran types. f2py just makes blatant assumptions in this area and you >> could probably impose such blatant assumptions yourself too if you want >> to avoid complicating your build. > > I'm shaking out these details as time goes on -- I'm settling on a few > solutions for different use scenarios (not all are implemented, > though). > > 1) David Cournapeau indicated that generating, compiling and running a > fortran 'genconfig' program is a problem for using fwrap with scipy -- > I believe because of windows restrictions. ?This is the easiest way to > get the config modules/headers/pxd's generated without an fwrap > dependency. > > 2) Allow the user to specify the mapping between each fortran > kind-type-parameter and C type, with a dictionary in a specification > file. ?This is brain-dead simple to get working -- not as > user-friendly, though. ?May lead to hard-to-debug runtime segfaults if > the mappings are specified incorrectly. > > 3) Let fwrap do it all -- but this requires fwrap to do the > compilation step. ?Safer & friendlier, but introduces a compile-time > dependency. As I wrote in my earlier email above, I think a project (or fwrap) has to have runtime tests, that the mappings work. In particular, it should test every type (in and out) and make sure it works. Such a test imho has to be included with the project and the idea is that if the tests pass, then the wrappers work (and there will be no runtime segfaults). And fwrap can make a good job at trying to guess the types right, but imho the tests are necessary. Ondrej From kwmsmith at gmail.com Tue Mar 30 21:33:50 2010 From: kwmsmith at gmail.com (Kurt Smith) Date: Tue, 30 Mar 2010 13:33:50 -0600 Subject: [Cython] fwrap status + source codes In-Reply-To: <85b5c3131003301152x3725a8bel550b7d2885c06e09@mail.gmail.com> References: <85b5c3131003241308i1ce8e424u453ef966efa5fbe8@mail.gmail.com> <85b5c3131003242306p799e2e1fibaf24f50c6f1d970@mail.gmail.com> <4BAB081C.6000208@student.matnat.uio.no> <85b5c3131003301152x3725a8bel550b7d2885c06e09@mail.gmail.com> Message-ID: On Tue, Mar 30, 2010 at 12:52 PM, Ondrej Certik wrote: > On Tue, Mar 30, 2010 at 9:17 AM, Kurt Smith wrote: >> On Thu, Mar 25, 2010 at 12:52 AM, Dag Sverre Seljebotn >> wrote: >> [snip] >> >>>> >>>> That should be enough. I use cmake, so I'd appreciate if fwrap could >>>> just spit the C (or fortran or both) files, that I compile and link >>>> myself. Just like Cython does. (I read some issues with integrating >>> >>> Note that fwrap generates both C, Fortran and Cython code to make >>> everything work together in a standard-compliant way... >>> >>> Also, unless things have changed, a configuration script must be run as >>> part of the build process to detect which C types correspond to which >>> Fortran types. f2py just makes blatant assumptions in this area and you >>> could probably impose such blatant assumptions yourself too if you want >>> to avoid complicating your build. >> >> I'm shaking out these details as time goes on -- I'm settling on a few >> solutions for different use scenarios (not all are implemented, >> though). >> >> 1) David Cournapeau indicated that generating, compiling and running a >> fortran 'genconfig' program is a problem for using fwrap with scipy -- >> I believe because of windows restrictions. ?This is the easiest way to >> get the config modules/headers/pxd's generated without an fwrap >> dependency. >> >> 2) Allow the user to specify the mapping between each fortran >> kind-type-parameter and C type, with a dictionary in a specification >> file. ?This is brain-dead simple to get working -- not as >> user-friendly, though. ?May lead to hard-to-debug runtime segfaults if >> the mappings are specified incorrectly. >> >> 3) Let fwrap do it all -- but this requires fwrap to do the >> compilation step. ?Safer & friendlier, but introduces a compile-time >> dependency. > > As I wrote in my earlier email above, I think a project (or fwrap) has > to have runtime tests, that the mappings work. In particular, it > should test every type (in and out) and make sure it works. Such a > test imho has to be included with the project and the idea is that if > the tests pass, then the wrappers work (and there will be no runtime > segfaults). And fwrap can make a good job at trying to guess the types > right, but imho the tests are necessary. I'm in complete agreement, and fwrap is very thorough about testing C-Fortran type compatibility. If it detects incompatibility it will raise an exception. The #2 option above should have emphasized the "may" qualifier. The difficulty is that there are some corner cases when fwrap might not have access to everything it needs to ensure type compatibility, and so the spec file would allow the user to fill in the missing info -- but this is a corner case and unlikely to occur. Kurt BTW -- I just set up an fwrap-users mailing list on googlegroups. Perhaps we should take the conversation there. http://groups.google.com/group/fwrap-users From kwmsmith at gmail.com Tue Mar 30 21:38:07 2010 From: kwmsmith at gmail.com (Kurt Smith) Date: Tue, 30 Mar 2010 13:38:07 -0600 Subject: [Cython] Announcing the creation of fwrap-users mailing list Message-ID: (I'm posting the announcement here, since fwrap has been piggybacking on cython's mailing lists.) This is to notify all interested in the soon-to-be-released fwrap utility of a new fwrap-users mailing list. http://groups.google.com/group/fwrap-users Please take all discussion of fwrap there, thanks. And thanks to cython for use of its mailing lists. Kurt From dsimmons.com at gmail.com Wed Mar 31 06:20:06 2010 From: dsimmons.com at gmail.com (David Simmons-Duffin) Date: Wed, 31 Mar 2010 00:20:06 -0400 Subject: [Cython] LiE in Cython In-Reply-To: References: <20091017115713.GC4492@zephyr> Message-ID: <17A344E1-C792-4D8B-9CA3-BEE580FB28EF@gmail.com> Dear All, I've finally gotten around to posting the code for my cython wrapper for the computer algebra package LiE: http://github.com/davidsd/lie Basically, the project contains a copy of the LiE source, some extra C files, and a cython file (lie.pyx) that references functions from LiE and defines extension classes and some more abstract python objects. It currently works well enough for what I made it for (http://arxiv.org/abs/0910.4585). However, there are many ways it could be improved. Since I'm no longer striving towards a specific goal, and I have other projects to work on, it's hard for me to prioritize one improvement over another. So I'd be grateful if those who are interested could take a look, let me know what improvements/features they think are important, and perhaps help me work towards them. Thanks, David On Oct 17, 2009, at 1:03 PM, Robert Bradshaw wrote: > Begin forwarded message: > >> From: "Nicolas M. Thiery" >> Date: October 17, 2009 4:57:13 AM PDT >> To: sage-devel at googlegroups.com, sage-combinat-devel at googlegroups.com, davfooidsd at physics.harvard.edu, Marc.van-Leeuwen at math.univ-poitiers.fr >> Cc: Cython-dev >> Subject: [sage-devel] Re: [Cython] LiE in Cython >> Reply-To: sage-devel at googlegroups.com >> >> >> Dear David, dear Marc, dear LiE fans, >> >> On Sat, Oct 17, 2009 at 12:44:12AM -0700, Robert Bradshaw wrote: >>> On Oct 16, 2009, at 6:59 PM, David Simmons-Duffin wrote: >>> >>>> A few months ago, I wrote the Cython list asking for some help writing >>>> a cython interface to the computer algebra package LiE. >>>> >>>> http://www-math.univ-poitiers.fr/~maavl/LiE/ >>>> >>>> There was some mention of interest in such a thing among sage people, >>>> and possibly others. But I needed the interface for a specific >>>> project, and it was recommended that I concentrate on writing it for >>>> that purpose and then think later about what to do publicly with the >>>> code. That project is now mostly finished, and I'd like to share the >>>> LiE python module. >>>> >>>> Any advice on the best way of making it accessible? It's not the most >>>> beautiful piece of code ever -- I was a cython novice when I started >>>> writing it. But it's quite functional, and capable of doing a wide >>>> range of lie group representation theory computations in python. >>>> Ideally, I want it to be easy for other people to contribute to and >>>> improve the code, without too much oversight from me. I don't know >>>> how much time I'll have personally to work on it, but I know it's >>>> useful, so I thought it was important to get it out there. >>>> >>>> Should I use github? Google code? Something else? Will it be easy to >>>> let others edit/patch/document the code? >> >> Excellent, thanks very much for your work on this! >> >> How does it overlap / interact with the Sage Lie spkg? >> >> By the way: what's the status of this spkg? Is there some >> documentation somewhere of what can be done with it from Sage? (I just >> tried lie-2.2.2.p3 but it does not seem compile on my 32bits i686 >> ubuntu box). >> >> In the longer run, it would be great to synchronise this project with >> the current development of root systems (and in particular Weyl >> characters) in Sage, to merge whatever can be merged and foster >> consistent interfaces and interoperability for the rest. >> >> You can browse through the (in development) documentation at: >> >> http://combinat.sagemath.org/doc/reference/combinat/root_systems.html >> >> (what's there will be integrated shortly into Sage) >> >> Best, >> Nicolas >> -- >> Nicolas M. Thi?ry "Isil" >> http://Nicolas.Thiery.name/ >> >> --~--~---------~--~----~------------~-------~--~----~ >> To post to this group, send an email to sage-devel at googlegroups.com >> To unsubscribe from this group, send an email to sage-devel-unsubscribe at googlegroups.com >> For more options, visit this group at http://groups.google.com/group/sage-devel >> URL: http://www.sagemath.org >> -~----------~----~----~----~------~----~------~--~--- >> > From tjhnson at gmail.com Wed Mar 31 19:32:02 2010 From: tjhnson at gmail.com (T J) Date: Wed, 31 Mar 2010 10:32:02 -0700 Subject: [Cython] array module In-Reply-To: <4BAEA13E.2050000@noaa.gov> References: <4BADB4DF.6030203@student.matnat.uio.no> <4BAEA13E.2050000@noaa.gov> Message-ID: On Sat, Mar 27, 2010 at 5:22 PM, Christopher Barker wrote: > Dag Sverre Seljebotn wrote: >> ?Converting to a NumPy array before >>> computation is one option, > > You should be able to create a numpy array from an array.array without > copying the data, so this could be pretty lightweight. > Does np.asarray handle that? Or must I do something more? From Chris.Barker at noaa.gov Wed Mar 31 20:13:13 2010 From: Chris.Barker at noaa.gov (Christopher Barker) Date: Wed, 31 Mar 2010 11:13:13 -0700 Subject: [Cython] array module In-Reply-To: References: <4BADB4DF.6030203@student.matnat.uio.no> <4BAEA13E.2050000@noaa.gov> Message-ID: <4BB390B9.6070802@noaa.gov> T J wrote: >> You should be able to create a numpy array from an array.array without >> copying the data, so this could be pretty lightweight. >> > Does np.asarray handle that? Or must I do something more? You know, I'm not sure -- it's not clear to me from that docs, but it doesn't look like it shares data: In [48]: a1 = array.array('f', (1,2,3,4,5,6)) In [50]: npa = np.asarray(a1) In [51]: npa Out[51]: array([ 1., 2., 3., 4., 5., 6.]) In [52]: a1[2] = 34 In [53]: a1 Out[53]: array('f', [1.0, 2.0, 34.0, 4.0, 5.0, 6.0]) In [54]: npa Out[54]: array([ 1., 2., 3., 4., 5., 6.]) However, np.frombuffer should work: In [62]: a1 Out[62]: array('f', [13.0, 2.0, 34.0, 4.0, 5.0, 6.0]) In [63]: npa = np.frombuffer(a1, dtype=np.float32) In [64]: npa Out[64]: array([ 13., 2., 34., 4., 5., 6.], dtype=float32) In [65]: a1[1] = 56 In [66]: npa Out[66]: array([ 13., 56., 34., 4., 5., 6.], dtype=float32) -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker at noaa.gov From dalcinl at gmail.com Wed Mar 31 20:32:55 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 31 Mar 2010 15:32:55 -0300 Subject: [Cython] array module In-Reply-To: <4BB390B9.6070802@noaa.gov> References: <4BADB4DF.6030203@student.matnat.uio.no> <4BAEA13E.2050000@noaa.gov> <4BB390B9.6070802@noaa.gov> Message-ID: On 31 March 2010 15:13, Christopher Barker wrote: > > However, np.frombuffer should work: > Of course, one just need to take care of not causing a reallocation in the original array. -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dalcinl at gmail.com Wed Mar 31 20:59:17 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 31 Mar 2010 15:59:17 -0300 Subject: [Cython] c++ parsing failure in deque.pxd Message-ID: While working pyx/pxd and module/package handling, I wrote a litle test for cimport taking advantage of the shipped pxds. But... Error converting Pyrex file to C: ------------------------------------------------------------ ... pass cppclass const_reverse_iterator(iterator): pass deque() deque(deque&) deque(size_t, T& val = T()) ^ ------------------------------------------------------------ /u/dalcinl/Devel/Cython/cython-devel-pxd/Cython/Includes/libcpp/deque.pxd:18:22: default values cannot be specified in pxd files, use ? or * Error converting Pyrex file to C: ------------------------------------------------------------ ... pass cppclass const_reverse_iterator(iterator): pass deque() deque(deque&) deque(size_t, T& val = T()) ^ ------------------------------------------------------------ /u/dalcinl/Devel/Cython/cython-devel-pxd/Cython/Includes/libcpp/deque.pxd:18:32: Expected ')' -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 From dalcinl at gmail.com Wed Mar 31 21:10:32 2010 From: dalcinl at gmail.com (Lisandro Dalcin) Date: Wed, 31 Mar 2010 16:10:32 -0300 Subject: [Cython] c++ parsing failure in deque.pxd In-Reply-To: References: Message-ID: Sorry, I got confused... Forget about it. On 31 March 2010 15:59, Lisandro Dalcin wrote: > While working pyx/pxd and module/package handling, I wrote a litle > test for cimport taking advantage of the shipped pxds. But... > > Error converting Pyrex file to C: > ------------------------------------------------------------ > ... > ? ? ? ? ? ?pass > ? ? ? ?cppclass const_reverse_iterator(iterator): > ? ? ? ? ? ?pass > ? ? ? ?deque() > ? ? ? ?deque(deque&) > ? ? ? ?deque(size_t, T& val = T()) > ? ? ? ? ? ? ? ? ? ? ^ > ------------------------------------------------------------ > > /u/dalcinl/Devel/Cython/cython-devel-pxd/Cython/Includes/libcpp/deque.pxd:18:22: > default values cannot be specified in pxd files, use ? or * > > Error converting Pyrex file to C: > ------------------------------------------------------------ > ... > ? ? ? ? ? ?pass > ? ? ? ?cppclass const_reverse_iterator(iterator): > ? ? ? ? ? ?pass > ? ? ? ?deque() > ? ? ? ?deque(deque&) > ? ? ? ?deque(size_t, T& val = T()) > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^ > ------------------------------------------------------------ > > /u/dalcinl/Devel/Cython/cython-devel-pxd/Cython/Includes/libcpp/deque.pxd:18:32: > Expected ')' > > > -- > Lisandro Dalcin > --------------- > Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) > Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) > Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) > PTLC - G?emes 3450, (3000) Santa Fe, Argentina > Tel/Fax: +54-(0)342-451.1594 > -- Lisandro Dalcin --------------- Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC) Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC) Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET) PTLC - G?emes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594