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 "<string>", 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
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@muscle@@YAJPAU_object@@AAVMessage@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@muscle@@YAJABVString@1@PAU_object@@AAVMessage@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@muscle@@YAJABVString@1@PAU_object@@AAVMessage@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@muscle@@YAJABVString@1@PAU_object@@AAVMessage@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@muscle@@YAJABVString@1@PAU_object@@AAVMessage@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@muscle@@YAJABVString@1@PAU_object@@AAVMessage@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@muscle@@YAJABVString@1@PAU_object@@AAVMessage@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@muscle@@YAJABVString@1@PAU_object@@AAVMessage@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@muscle@@YAJABVString@1@PAU_object@@AAVMessage@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@muscle@@YAJPAU_object@@0AAVMessage@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@qnet@@YAJABVIPythonTemplateEditor@1@AAVSysexEntry@1@@Z)
release\CueStation.exe : fatal error LNK1120: 11 unresolved externals
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 "<stdin>", line 1, in <module>
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?