From hniksic at xemacs.org Wed May 7 17:27:15 2008 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 07 May 2008 17:27:15 +0200 Subject: [capi-sig] String to number conversion Message-ID: <87y76m3zjg.fsf@mulj.homelinux.net> I miss a function able to convert the string representation of a Python primitive number (int, long, float, complex) to an actual number, basically the reverse of repr for numbers. The only option I know of that doesn't involve inspecting the individual digits is eval, which doesn't cut it -- besides the obvious security implications, it tends to be slow when used en masse. What I have in mind is something along the lines of: >>> read_number('1') 1 >>> read_number('1.') 1.0 >>> read_number('3.14') 3.1400000000000001 >>> read_number('1000000000000000000') 1000000000000000000L >>> read_number('2+3j') (2+3j) >>> read_number('1e100') 1e+100 >>> read_number('bla') Traceback (most recent call last): File "", line 1, in ValueError: invalid numeric value 'bla' A sample implementation, a variation of which I use in an in-house project, follows. It does inspect the string to determine its type, but does so in a simple C loop which is quite fast for numeric input. It leaves the actual conversion to the Python's API code that knows what it's doing. Because of that it's simple and fast, but still (to my knowledge) correct. PyObject * read_number(PyObject *str) { int isfloat = 0, iscomplex = 0; /* First, intuit the type based on characters that appear in the string. */ const char *s = PyString_AS_STRING(str); const char *end = s + PyString_GET_SIZE(str); const char *q; for (q = s; q < end; q++) { char c = *q; if (c == '.' || c == 'e') isfloat = 1; else if (c == 'j') iscomplex = 1; else if (c != '-' && c != '+' && !isdigit(c) && !isspace(c)) { PyErr_Format(PyExc_ValueError, "invalid numeric value '%s'", s); return NULL; } } /* Now that the type is known, construct the number, leaving the actual error checking to the constructors. */ if (iscomplex) return PyObject_CallFunctionObjArgs((PyObject *) &PyComplex_Type, str); else if (isfloat) return PyFloat_FromString(str, NULL); else /* handles ints and longs */ return PyInt_FromString((char *) s, NULL, 10); } Would anyone else find this kind of function useful? From mal at egenix.com Wed May 7 17:46:09 2008 From: mal at egenix.com (M.-A. Lemburg) Date: Wed, 07 May 2008 17:46:09 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: <87y76m3zjg.fsf@mulj.homelinux.net> References: <87y76m3zjg.fsf@mulj.homelinux.net> Message-ID: <4821CEC1.4060806@egenix.com> On 2008-05-07 17:27, Hrvoje Niksic wrote: > I miss a function able to convert the string representation of a > Python primitive number (int, long, float, complex) to an actual > number, basically the reverse of repr for numbers. The only option I > know of that doesn't involve inspecting the individual digits is eval, > which doesn't cut it -- besides the obvious security implications, it > tends to be slow when used en masse. What I have in mind is something > along the lines of: > >>>> read_number('1') > 1 >>>> read_number('1.') > 1.0 >>>> read_number('3.14') > 3.1400000000000001 >>>> read_number('1000000000000000000') > 1000000000000000000L >>>> read_number('2+3j') > (2+3j) >>>> read_number('1e100') > 1e+100 >>>> read_number('bla') > Traceback (most recent call last): > File "", line 1, in > ValueError: invalid numeric value 'bla' > > A sample implementation, a variation of which I use in an in-house > project, follows. It does inspect the string to determine its type, > but does so in a simple C loop which is quite fast for numeric input. > It leaves the actual conversion to the Python's API code that knows > what it's doing. Because of that it's simple and fast, but still (to > my knowledge) correct. > > PyObject * > read_number(PyObject *str) > { > int isfloat = 0, iscomplex = 0; > > /* First, intuit the type based on characters that appear in the > string. */ > const char *s = PyString_AS_STRING(str); > const char *end = s + PyString_GET_SIZE(str); > const char *q; > for (q = s; q < end; q++) { > char c = *q; > if (c == '.' || c == 'e') > isfloat = 1; > else if (c == 'j') > iscomplex = 1; > else if (c != '-' && c != '+' && !isdigit(c) && !isspace(c)) { > PyErr_Format(PyExc_ValueError, "invalid numeric value '%s'", s); > return NULL; > } > } > > /* Now that the type is known, construct the number, leaving the > actual error checking to the constructors. */ > if (iscomplex) > return PyObject_CallFunctionObjArgs((PyObject *) &PyComplex_Type, str); > else if (isfloat) > return PyFloat_FromString(str, NULL); > else > /* handles ints and longs */ > return PyInt_FromString((char *) s, NULL, 10); > } > > Would anyone else find this kind of function useful? Yes. Perhaps as PyNumber_FromString() ?! -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, May 07 2008) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 From mal at egenix.com Wed May 7 18:40:51 2008 From: mal at egenix.com (M.-A. Lemburg) Date: Wed, 07 May 2008 18:40:51 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> References: <87y76m3zjg.fsf@mulj.homelinux.net> <4821CEC1.4060806@egenix.com> <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> Message-ID: <4821DB93.7060308@egenix.com> On 2008-05-07 18:28, Rasmus Andersson wrote: > > On 7 maj 2008, at 17.46, M.-A. Lemburg wrote: > >> On 2008-05-07 17:27, Hrvoje Niksic wrote: >>> I miss a function able to convert the string representation of a >>> Python primitive number (int, long, float, complex) to an actual >>> number, basically the reverse of repr for numbers. >>> >>> Would anyone else find this kind of function useful? >> >> Yes. >> >> Perhaps as PyNumber_FromString() ?! > > I rarely wrap primitives in objects, as this increases both memory > usage, complexity and overhead. (However, sometimes you _might_ need > to, but I can't come up with a scenario) > > So, in most cases this would probably happen: > > PyObject *num = PyNumber_FromString(num_s); > if(!PyLong_Check(num)) { > // Error... > } > else { > self->event_id = PyLong_AsLongLong(num); > } Not really. The point of the PyNumber_* API is to work on numbers without actually caring or knowing the specific number types (Include/abstract.h). You'd only do the final conversion to a specific number type at the very end of the calculation using e.g. PyNumber_Int(). In some cases, not even that, since all you're interested in is converting some object to a number object and then passing that to e.g. marshal. > And you lose, because this would be simpler: > > self->event_id = strtoll(PyString_AsString(num_s), (char *)NULL, 10); > if(errno == EINVAL) { > // Error... > } > > Meaning, you rarely need to take input which can be any kind of number > manifested as a string and return it as any number manifested as a > native number. (The examples above indicate the output type is known > (signed 64bit integer)) > > Could you maybe give a real-world scenario where this would be needed? > Where neither the input not the output number type is known. Parsing numeric data and converting it to some other format. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, May 07 2008) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 From rasmus at spotify.com Wed May 7 18:50:51 2008 From: rasmus at spotify.com (Rasmus Andersson) Date: Wed, 7 May 2008 18:50:51 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: <4821DB93.7060308@egenix.com> References: <87y76m3zjg.fsf@mulj.homelinux.net> <4821CEC1.4060806@egenix.com> <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> <4821DB93.7060308@egenix.com> Message-ID: <79B93F58-8153-4669-BD7B-CE19D62FF598@spotify.com> On 7 maj 2008, at 18.40, M.-A. Lemburg wrote: > On 2008-05-07 18:28, Rasmus Andersson wrote: >> On 7 maj 2008, at 17.46, M.-A. Lemburg wrote: >>> On 2008-05-07 17:27, Hrvoje Niksic wrote: >>>> I miss a function able to convert the string representation of a >>>> Python primitive number (int, long, float, complex) to an actual >>>> number, basically the reverse of repr for numbers. >>>> >>>> Would anyone else find this kind of function useful? >>> >>> Yes. >>> >>> Perhaps as PyNumber_FromString() ?! >> I rarely wrap primitives in objects, as this increases both memory >> usage, complexity and overhead. (However, sometimes you _might_ >> need to, but I can't come up with a scenario) >> So, in most cases this would probably happen: >> PyObject *num = PyNumber_FromString(num_s); >> if(!PyLong_Check(num)) { >> // Error... >> } >> else { >> self->event_id = PyLong_AsLongLong(num); >> } > > Not really. The point of the PyNumber_* API is to work on numbers > without actually caring or knowing the specific number types > (Include/abstract.h). Yes I know, but I can't really see a big need for this kind of functionality in practice, but please, enlighten me! (maybe with a scenario) By the way, great to see something happing on this list. Has been awfully quiet! :) > > You'd only do the final conversion to a specific number type > at the very end of the calculation using e.g. PyNumber_Int(). > > In some cases, not even that, since all you're interested in > is converting some object to a number object and then passing > that to e.g. marshal. > >> And you lose, because this would be simpler: >> self->event_id = strtoll(PyString_AsString(num_s), (char *)NULL, 10); >> if(errno == EINVAL) { >> // Error... >> } >> Meaning, you rarely need to take input which can be any kind of >> number manifested as a string and return it as any number >> manifested as a native number. (The examples above indicate the >> output type is known (signed 64bit integer)) >> Could you maybe give a real-world scenario where this would be >> needed? Where neither the input not the output number type is known. > > Parsing numeric data and converting it to some other format. > > -- > Marc-Andre Lemburg > eGenix.com > > Professional Python Services directly from the Source (#1, May 07 > 2008) > >>> Python/Zope Consulting and Support ... http://www.egenix.com/ > >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ > >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ > ________________________________________________________________________ > > :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for > free ! :::: > > > eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 > D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg > Registered at Amtsgericht Duesseldorf: HRB 46611 ???????????????? Rasmus Andersson Spotify +46 733 117 326 From rasmus at spotify.com Wed May 7 18:28:49 2008 From: rasmus at spotify.com (Rasmus Andersson) Date: Wed, 7 May 2008 18:28:49 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: <4821CEC1.4060806@egenix.com> References: <87y76m3zjg.fsf@mulj.homelinux.net> <4821CEC1.4060806@egenix.com> Message-ID: <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> On 7 maj 2008, at 17.46, M.-A. Lemburg wrote: > On 2008-05-07 17:27, Hrvoje Niksic wrote: >> I miss a function able to convert the string representation of a >> Python primitive number (int, long, float, complex) to an actual >> number, basically the reverse of repr for numbers. >> >> Would anyone else find this kind of function useful? > > Yes. > > Perhaps as PyNumber_FromString() ?! I rarely wrap primitives in objects, as this increases both memory usage, complexity and overhead. (However, sometimes you _might_ need to, but I can't come up with a scenario) So, in most cases this would probably happen: PyObject *num = PyNumber_FromString(num_s); if(!PyLong_Check(num)) { // Error... } else { self->event_id = PyLong_AsLongLong(num); } And you lose, because this would be simpler: self->event_id = strtoll(PyString_AsString(num_s), (char *)NULL, 10); if(errno == EINVAL) { // Error... } Meaning, you rarely need to take input which can be any kind of number manifested as a string and return it as any number manifested as a native number. (The examples above indicate the output type is known (signed 64bit integer)) Could you maybe give a real-world scenario where this would be needed? Where neither the input not the output number type is known. > > > -- > Marc-Andre Lemburg > eGenix.com > > Professional Python Services directly from the Source (#1, May 07 > 2008) > >>> Python/Zope Consulting and Support ... http://www.egenix.com/ > >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ > >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ > ________________________________________________________________________ > > :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for > free ! :::: > > > eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 > D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg > Registered at Amtsgericht Duesseldorf: HRB 46611 > _______________________________________________ > capi-sig mailing list > capi-sig at python.org > http://mail.python.org/mailman/listinfo/capi-sig ???????????????? Rasmus Andersson Spotify +46 733 117 326 From mal at egenix.com Wed May 7 19:03:37 2008 From: mal at egenix.com (M.-A. Lemburg) Date: Wed, 07 May 2008 19:03:37 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: <79B93F58-8153-4669-BD7B-CE19D62FF598@spotify.com> References: <87y76m3zjg.fsf@mulj.homelinux.net> <4821CEC1.4060806@egenix.com> <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> <4821DB93.7060308@egenix.com> <79B93F58-8153-4669-BD7B-CE19D62FF598@spotify.com> Message-ID: <4821E0E9.3060009@egenix.com> On 2008-05-07 18:50, Rasmus Andersson wrote: > > On 7 maj 2008, at 18.40, M.-A. Lemburg wrote: >>>> On 2008-05-07 17:27, Hrvoje Niksic wrote: >>>>> I miss a function able to convert the string representation of a >>>>> Python primitive number (int, long, float, complex) to an actual >>>>> number, basically the reverse of repr for numbers. >>>>> >>>>> Would anyone else find this kind of function useful? >>>> >>>> Yes. >>>> >>>> Perhaps as PyNumber_FromString() ?! >>> I rarely wrap primitives in objects, as this increases both memory >>> usage, complexity and overhead. (However, sometimes you _might_ need >>> to, but I can't come up with a scenario) >>> So, in most cases this would probably happen: >>> PyObject *num = PyNumber_FromString(num_s); >>> if(!PyLong_Check(num)) { >>> // Error... >>> } >>> else { >>> self->event_id = PyLong_AsLongLong(num); >>> } >> >> Not really. The point of the PyNumber_* API is to work on numbers >> without actually caring or knowing the specific number types >> (Include/abstract.h). > > Yes I know, but I can't really see a big need for this kind of > functionality in practice, but please, enlighten me! (maybe with a > scenario) Didn't I just give you a few ? > By the way, great to see something happing on this list. Has been > awfully quiet! :) > >> >> You'd only do the final conversion to a specific number type >> at the very end of the calculation using e.g. PyNumber_Int(). >> >> In some cases, not even that, since all you're interested in >> is converting some object to a number object and then passing >> that to e.g. marshal. >> >>> And you lose, because this would be simpler: >>> self->event_id = strtoll(PyString_AsString(num_s), (char *)NULL, 10); >>> if(errno == EINVAL) { >>> // Error... >>> } >>> Meaning, you rarely need to take input which can be any kind of >>> number manifested as a string and return it as any number manifested >>> as a native number. (The examples above indicate the output type is >>> known (signed 64bit integer)) >>> Could you maybe give a real-world scenario where this would be >>> needed? Where neither the input not the output number type is known. >> >> Parsing numeric data and converting it to some other format. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, May 07 2008) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 From rasmus at spotify.com Wed May 7 19:14:06 2008 From: rasmus at spotify.com (Rasmus Andersson) Date: Wed, 7 May 2008 19:14:06 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: <4821E0E9.3060009@egenix.com> References: <87y76m3zjg.fsf@mulj.homelinux.net> <4821CEC1.4060806@egenix.com> <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> <4821DB93.7060308@egenix.com> <79B93F58-8153-4669-BD7B-CE19D62FF598@spotify.com> <4821E0E9.3060009@egenix.com> Message-ID: On 7 maj 2008, at 19.03, M.-A. Lemburg wrote: > On 2008-05-07 18:50, Rasmus Andersson wrote: >> On 7 maj 2008, at 18.40, M.-A. Lemburg wrote: >>>>> On 2008-05-07 17:27, Hrvoje Niksic wrote: >>>>>> I miss a function able to convert the string representation of a >>>>>> Python primitive number (int, long, float, complex) to an actual >>>>>> number, basically the reverse of repr for numbers. >>>>>> >>>>>> Would anyone else find this kind of function useful? >>>>> >>>>> Yes. >>>>> >>>>> Perhaps as PyNumber_FromString() ?! >>>> I rarely wrap primitives in objects, as this increases both >>>> memory usage, complexity and overhead. (However, sometimes you >>>> _might_ need to, but I can't come up with a scenario) >>>> So, in most cases this would probably happen: >>>> PyObject *num = PyNumber_FromString(num_s); >>>> if(!PyLong_Check(num)) { >>>> // Error... >>>> } >>>> else { >>>> self->event_id = PyLong_AsLongLong(num); >>>> } >>> >>> Not really. The point of the PyNumber_* API is to work on numbers >>> without actually caring or knowing the specific number types >>> (Include/abstract.h). >> Yes I know, but I can't really see a big need for this kind of >> functionality in practice, but please, enlighten me! (maybe with a >> scenario) > > Didn't I just give you a few ? Either I'm unusually stupid today or I missed something ? where is the scenario (using this proposed functionality in real world)? (sorry) > > >> By the way, great to see something happing on this list. Has been >> awfully quiet! :) >>> >>> You'd only do the final conversion to a specific number type >>> at the very end of the calculation using e.g. PyNumber_Int(). >>> >>> In some cases, not even that, since all you're interested in >>> is converting some object to a number object and then passing >>> that to e.g. marshal. >>> >>>> And you lose, because this would be simpler: >>>> self->event_id = strtoll(PyString_AsString(num_s), (char *)NULL, >>>> 10); >>>> if(errno == EINVAL) { >>>> // Error... >>>> } >>>> Meaning, you rarely need to take input which can be any kind of >>>> number manifested as a string and return it as any number >>>> manifested as a native number. (The examples above indicate the >>>> output type is known (signed 64bit integer)) >>>> Could you maybe give a real-world scenario where this would be >>>> needed? Where neither the input not the output number type is >>>> known. >>> >>> Parsing numeric data and converting it to some other format. > > -- > Marc-Andre Lemburg > eGenix.com > > Professional Python Services directly from the Source (#1, May 07 > 2008) > >>> Python/Zope Consulting and Support ... http://www.egenix.com/ > >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ > >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ > ________________________________________________________________________ > > :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for > free ! :::: > > > eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 > D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg > Registered at Amtsgericht Duesseldorf: HRB 46611 ???????????????? Rasmus Andersson Spotify +46 733 117 326 From rasmus at spotify.com Wed May 7 18:55:24 2008 From: rasmus at spotify.com (Rasmus Andersson) Date: Wed, 7 May 2008 18:55:24 +0200 Subject: [capi-sig] =?windows-1252?q?Announcement=3A_Smisk_=96_web_service?= =?windows-1252?q?_framework_in_C_controlled_by_Python?= Message-ID: http://trac.hunch.se/smisk Please let me know what you think! -- Rasmus Andersson From mal at egenix.com Wed May 7 19:30:17 2008 From: mal at egenix.com (M.-A. Lemburg) Date: Wed, 07 May 2008 19:30:17 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: References: <87y76m3zjg.fsf@mulj.homelinux.net> <4821CEC1.4060806@egenix.com> <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> <4821DB93.7060308@egenix.com> <79B93F58-8153-4669-BD7B-CE19D62FF598@spotify.com> <4821E0E9.3060009@egenix.com> Message-ID: <4821E729.3010909@egenix.com> On 2008-05-07 19:14, Rasmus Andersson wrote: > > On 7 maj 2008, at 19.03, M.-A. Lemburg wrote: > >>>> Not really. The point of the PyNumber_* API is to work on numbers >>>> without actually caring or knowing the specific number types >>>> (Include/abstract.h). >>> Yes I know, but I can't really see a big need for this kind of >>> functionality in practice, but please, enlighten me! (maybe with a >>> scenario) >> >> Didn't I just give you a few ? > > Either I'm unusually stupid today or I missed something -- where is the > scenario (using this proposed functionality in real world)? (sorry) You asked for example of using the PyNumber_* API. All of these can use strings as input for the processing chain: * work with numbers regardless of type, do the final conversion at the end (you only care about the final type) * work with numbers, creating Python objects that can be passed to other Python mechanisms such as marshal (you only care about the fact that you're dealing with a number, not the type) * parsing numeric data and preparing it for conversion to some other format, e.g. using PyString_Format() -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, May 07 2008) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 From rasmus at flajm.se Wed May 7 19:48:55 2008 From: rasmus at flajm.se (Rasmus Andersson) Date: Wed, 7 May 2008 19:48:55 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: <4821E729.3010909@egenix.com> References: <87y76m3zjg.fsf@mulj.homelinux.net> <4821CEC1.4060806@egenix.com> <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> <4821DB93.7060308@egenix.com> <79B93F58-8153-4669-BD7B-CE19D62FF598@spotify.com> <4821E0E9.3060009@egenix.com> <4821E729.3010909@egenix.com> Message-ID: <2E92D810-DC24-43FA-A7A9-C3F99D936FE2@flajm.se> I see the point, but I'm generally sceptic to these kind of changes (ie extending the core) But why not make a patch and send it in for review?! Go for it! -- Rasmus Andersson Spotify +46 733 117 326 On 7 maj 2008, at 19.30, "M.-A. Lemburg" wrote: > On 2008-05-07 19:14, Rasmus Andersson wrote: >> On 7 maj 2008, at 19.03, M.-A. Lemburg wrote: > > >>>>> Not really. The point of the PyNumber_* API is to work on numbers >>>>> without actually caring or knowing the specific number types >>>>> (Include/abstract.h). >>>> Yes I know, but I can't really see a big need for this kind of >>>> functionality in practice, but please, enlighten me! (maybe with >>>> a scenario) >>> >>> Didn't I just give you a few ? >> Either I'm unusually stupid today or I missed something -- where is >> the scenario (using this proposed functionality in real world)? >> (sorry) > > You asked for example of using the PyNumber_* API. All of these > can use strings as input for the processing chain: > > * work with numbers regardless of type, do the final > conversion at the end > > (you only care about the final type) > > * work with numbers, creating Python objects that can be > passed to other Python mechanisms such as marshal > > (you only care about the fact that you're dealing with > a number, not the type) > > * parsing numeric data and preparing it for conversion to > some other format, e.g. using PyString_Format() > > -- > Marc-Andre Lemburg > eGenix.com > > Professional Python Services directly from the Source (#1, May 07 > 2008) > >>> Python/Zope Consulting and Support ... http://www.egenix.com/ > >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ > >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ > ________________________________________________________________________ > > :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for > free ! :::: > > > eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 > D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg > Registered at Amtsgericht Duesseldorf: HRB 46611 From hniksic at xemacs.org Wed May 7 23:42:06 2008 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 07 May 2008 23:42:06 +0200 Subject: [capi-sig] String to number conversion In-Reply-To: <42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> (Rasmus Andersson's message of "Wed\, 7 May 2008 18\:28\:49 +0200") References: <87y76m3zjg.fsf@mulj.homelinux.net> <4821CEC1.4060806@egenix.com><42543835-006D-44DF-9D91-41A879AD2CA3@spotify.com> Message-ID: <87prrxkd01.fsf@mulj.homelinux.net> Rasmus Andersson writes: > Could you maybe give a real-world scenario where this would be > needed? It is needed when writing Python numbers to a file and reading them back, reliably. In that case strtoll and friends won't work because, while they work on one data type, they fail on others (float, complex, and (Python) long in the case of strtoll). From jaf at meyersound.com Thu May 22 22:51:57 2008 From: jaf at meyersound.com (Jeremy Friesner) Date: Thu, 22 May 2008 13:51:57 -0700 Subject: [capi-sig] Problems linking python25.lib into my executable using MSVC2008 Express Message-ID: <200805221351.57734.jaf@meyersound.com> Hi all, Apologies if this is a common question, I googled for it and couldn't find any good answers. I've got a C++ program that includes an embedded Python interpreter, and under Linux and MacOS/X everything works fine. It's a Qt4-based program, so all I needed to do was add -lpython2.5 to the LIBS variable in my .pro file. Now I'm trying to get the same program working under Windows, but when I get to the link stage I run into trouble. I've compiled the Python 2.5 library using MSVC2008 Express (and the project files in the PCBuild8 sub-folder), but when I try to link my own app (adding python25.lib to my LIBS line, of course), I get the errors shown below. I notice that I only get link errors about the global objects exported in the Python25.lib library (e.g. _PyList_Type)... I don't get any errors related to exported Python functions. Is there something special I need to do in order to get MSVC2008's linker to resolved exported global objects? Thanks, Jeremy e:\msvc2008\vc\bin\nmake.exe -f Makefile.Release link /LIBPATH:"e: \Qt\4.4.0\lib" /NOLOGO /LIBPATH:../../../../../software/3rdparty/libsndfile/win32 /LIBPATH:../../../../../software/3rdparty/libsamplerate/win32 /LIBPATH:../../../../../software/3rdparty/libsamplerate /LIBPATH:../../../../../software/3rdparty/python/pcbuild8/win32 /LIBPATH:../../../../../software/libs/muscle/zlib/zlib /INCREMENTAL:NO /MANIFEST /MANIFESTFILE:"objects\CueStation.intermediate.manifest" /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /OUT:release\CueStation.exe @C: \DOCUME~1\JEREMY~1\LOCALS~1\Temp\nm48C.tmp PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyList_Type referenced in function "long __cdecl muscle::ParsePythonSequence(struct _object *,class muscle::Message &)" (?ParsePythonSequence at muscle@@YAJPAU_object@@AAVMessage at 1@@Z) PythonTemplateUtilityFunctions.obj : error LNK2001: unresolved external symbol _PyList_Type PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyDict_Type referenced in function "long __cdecl muscle::AddPyObjectToMessage(class muscle::String const &,struct _object *,class muscle::Message &)" (?AddPyObjectToMessage at muscle@@YAJABVString at 1@PAU_object@@AAVMessage at 1@@Z) PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyUnicode_Type referenced in function "long __cdecl muscle::AddPyObjectToMessage(class muscle::String const &,struct _object *,class muscle::Message &)" (?AddPyObjectToMessage at muscle@@YAJABVString at 1@PAU_object@@AAVMessage at 1@@Z) PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyComplex_Type referenced in function "long __cdecl muscle::AddPyObjectToMessage(class muscle::String const &,struct _object *,class muscle::Message &)" (?AddPyObjectToMessage at muscle@@YAJABVString at 1@PAU_object@@AAVMessage at 1@@Z) PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyString_Type referenced in function "long __cdecl muscle::AddPyObjectToMessage(class muscle::String const &,struct _object *,class muscle::Message &)" (?AddPyObjectToMessage at muscle@@YAJABVString at 1@PAU_object@@AAVMessage at 1@@Z) PythonTemplateUtilityFunctions.obj : error LNK2001: unresolved external symbol _PyString_Type SysexSubcueEditor.obj : error LNK2001: unresolved external symbol _PyString_Type PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyFloat_Type referenced in function "long __cdecl muscle::AddPyObjectToMessage(class muscle::String const &,struct _object *,class muscle::Message &)" (?AddPyObjectToMessage at muscle@@YAJABVString at 1@PAU_object@@AAVMessage at 1@@Z) PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyLong_Type referenced in function "long __cdecl muscle::AddPyObjectToMessage(class muscle::String const &,struct _object *,class muscle::Message &)" (?AddPyObjectToMessage at muscle@@YAJABVString at 1@PAU_object@@AAVMessage at 1@@Z) PythonTemplateUtilityFunctions.obj : error LNK2001: unresolved external symbol _PyLong_Type PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyInt_Type referenced in function "long __cdecl muscle::AddPyObjectToMessage(class muscle::String const &,struct _object *,class muscle::Message &)" (?AddPyObjectToMessage at muscle@@YAJABVString at 1@PAU_object@@AAVMessage at 1@@Z) PythonTemplateUtilityFunctions.obj : error LNK2001: unresolved external symbol _PyInt_Type PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol __Py_NoneStruct referenced in function "long __cdecl muscle::AddPyObjectToMessage(class muscle::String const &,struct _object *,class muscle::Message &)" (?AddPyObjectToMessage at muscle@@YAJABVString at 1@PAU_object@@AAVMessage at 1@@Z) PythonTemplateUtilityFunctions.obj : error LNK2001: unresolved external symbol __Py_NoneStruct SysexSubcueEditor.obj : error LNK2001: unresolved external symbol __Py_NoneStruct PythonUtilityFunctions.obj : error LNK2019: unresolved external symbol _PyExc_RuntimeError referenced in function "long __cdecl muscle::ParsePythonArgs(struct _object *,struct _object *,class muscle::Message &)" (?ParsePythonArgs at muscle@@YAJPAU_object@@0AAVMessage at 1@@Z) PythonTemplateUtilityFunctions.obj : error LNK2001: unresolved external symbol _PyExc_RuntimeError SysexSubcueEditor.obj : error LNK2001: unresolved external symbol _PyExc_RuntimeError PythonTemplateUtilityFunctions.obj : error LNK2019: unresolved external symbol __Py_TrueStruct referenced in function "long __cdecl qnet::RegenerateSysexDataFromTemplate(class qnet::IPythonTemplateEditor const &,class qnet::SysexEntry &)" (?RegenerateSysexDataFromTemplate at qnet@@YAJABVIPythonTemplateEditor at 1@AAVSysexEntry at 1@@Z) release\CueStation.exe : fatal error LNK1120: 11 unresolved externals From pablo.yabo at gmail.com Tue May 27 21:52:47 2008 From: pablo.yabo at gmail.com (Pablo Yabo) Date: Tue, 27 May 2008 16:52:47 -0300 Subject: [capi-sig] Python parser Message-ID: Hello, I embedded Python in an application to play with some objects. I want to add some features to my console and I don't find how to evaluate in the same way the Python console does. I want to let the user insert a: for a in [1,2,3]: and I want to put the '...' like the console but if I evaluate the 'for a in [1,2,3]:\n' using PyRun_SimpleString I get an error: File "", line 1 for a in [1,2,3]: ^ SyntaxError: unexpected EOF while parsing Is there any way to evaluate in the same way the console does? Thanks in advance, Pablo Yabo From jon at indelible.org Tue May 27 22:27:36 2008 From: jon at indelible.org (Jon Parise) Date: Tue, 27 May 2008 13:27:36 -0700 Subject: [capi-sig] Python parser In-Reply-To: References: Message-ID: <1aff89d70805271327m6b41aa8occ6d1a673f43f424@mail.gmail.com> On Tue, May 27, 2008 at 12:52 PM, Pablo Yabo wrote: > > I embedded Python in an application to play with some objects. > I want to add some features to my console and I don't find how to evaluate > in the same way the Python console does. > > I want to let the user insert a: > for a in [1,2,3]: > > and I want to put the '...' like the console but if I evaluate the 'for a in > [1,2,3]:\n' using PyRun_SimpleString I get an error: > > File "", line 1 > for a in [1,2,3]: > ^ > SyntaxError: unexpected EOF while parsing > > Is there any way to evaluate in the same way the console does? I don't think you'll be able to get that type of behavior out of PyRun_SimpleString(). Try taking a look at the PyRun_Interactive*() functions and PyRun_String() with the Py_single_input start symbol. That should get you pointed in the right direction. -- Jon Parise (jon of indelible.org) :: "Scientia potentia est"