RE: function causing core dump

Discussion in 'Python' started by Robert Brewer, May 10, 2004.

  1. Xaver Hinterhuber wrote:
    > I build a function with the following code segment:
    >
    > codeObject = new.code(
    > 0, # argcount
    > 0, # nlocals
    > 0, # stacksize
    > 0, # flags
    > codeString, # code
    > (), # consts
    > (), # names
    > (), # varnames
    > 'content', # filename
    > 'content', # name
    > 3, # first line number
    > codeString # lnotab
    > )
    > f = new.function(codeObject, dict, 'f')
    > f()
    >
    > Everything runs fine, until the function is called with f().
    > When python tries to execute f(), the core dump happens.
    > I don't have any clue why python core dumps.
    > The codeString is nothing complex, its a one-liner.
    > Could you plz give me some tips what I have to do?


    As usual, the tip is: include the one piece of information you left out
    of your post. What is the value of codeString?

    My first guess is that codeString is something like "return None", or
    some other string of uncompiled Python code, in which case, you should
    use eval() instead of new.code and new.function. Those two are used for
    creating code objects and functions from a _compiled_ codestring, like
    '\x88\x00\x00Sd\x00\x00S'. In addition, nearly all of the args passed to
    new.code are *very* important, and cannot simply be set to 0.

    If you want more info, show us the codeString! :)


    Robert Brewer
    MIS
    Amor Ministries
     
    Robert Brewer, May 10, 2004
    #1
    1. Advertising

  2. Hi Robert,

    I don't show you codeString because it is a real string variable.
    And yes, its uncompiled.
    The user in my program enters a python program in this variable without a
    def statement
    and then the code is executed and the result returned.

    Maybe you can help me to add a "def f():" to the codeString, take care of
    the
    indentation and then execute the code?

    I really don't know how to handle the indentation thing. A "def
    f():\n"+codeString is easy, but this doesn't solve
    this issue.

    Greets
    Xaver

    "Robert Brewer" <> schrieb im Newsbeitrag
    news:...
    Xaver Hinterhuber wrote:
    > I build a function with the following code segment:
    >
    > codeObject = new.code(
    > 0, # argcount
    > 0, # nlocals
    > 0, # stacksize
    > 0, # flags
    > codeString, # code
    > (), # consts
    > (), # names
    > (), # varnames
    > 'content', # filename
    > 'content', # name
    > 3, # first line number
    > codeString # lnotab
    > )
    > f = new.function(codeObject, dict, 'f')
    > f()
    >
    > Everything runs fine, until the function is called with f().
    > When python tries to execute f(), the core dump happens.
    > I don't have any clue why python core dumps.
    > The codeString is nothing complex, its a one-liner.
    > Could you plz give me some tips what I have to do?


    As usual, the tip is: include the one piece of information you left out
    of your post. What is the value of codeString?

    My first guess is that codeString is something like "return None", or
    some other string of uncompiled Python code, in which case, you should
    use eval() instead of new.code and new.function. Those two are used for
    creating code objects and functions from a _compiled_ codestring, like
    '\x88\x00\x00Sd\x00\x00S'. In addition, nearly all of the args passed to
    new.code are *very* important, and cannot simply be set to 0.

    If you want more info, show us the codeString! :)


    Robert Brewer
    MIS
    Amor Ministries
     
    Xaver Hinterhuber, May 11, 2004
    #2
    1. Advertising

  3. Robert Brewer

    Peter Otten Guest

    Xaver Hinterhuber wrote:

    > I don't show you codeString because it is a real string variable.


    You could show an _example_ codeString, though. That would have greatly
    simplified the diagnosis.

    > And yes, its uncompiled.
    > The user in my program enters a python program in this variable without a
    > def statement
    > and then the code is executed and the result returned.
    >
    > Maybe you can help me to add a "def f():" to the codeString, take care of
    > the
    > indentation and then execute the code?
    >
    > I really don't know how to handle the indentation thing. A "def
    > f():\n"+codeString is easy, but this doesn't solve
    > this issue.


    Maybe you can get away with a simple expression:

    >>> from math import *
    >>> codeString = "sin(0.5) + 23"
    >>> eval(codeString)

    23.479425538604204

    If not, try

    >>> codeString2 = "a=0.5\nb=23\nreturn sin(a) + b"
    >>> funcString = "def f():\n " + "\n ".join(codeString2.split("\n"))
    >>> funcString

    'def f():\n a=0.5\n b=23\n return sin(a) + b'
    >>> exec funcString
    >>> f()

    23.479425538604204
    >>>


    Peter
     
    Peter Otten, May 11, 2004
    #3
  4. Hi Peter,

    "Peter Otten" <> schrieb im Newsbeitrag
    news:c7q1vs$oph$04$-online.com...
    > Xaver Hinterhuber wrote:
    >
    > > I don't show you codeString because it is a real string variable.

    >
    > You could show an _example_ codeString, though. That would have greatly
    > simplified the diagnosis.


    For clarification: The codeString contains up to 500 lines of code. It's a
    complete program to render a pdf page with
    reportlab. It's really a huge monster. But in future I'll try give an
    example with all variables declared.
    >
    > > And yes, its uncompiled.
    > > The user in my program enters a python program in this variable without

    a
    > > def statement
    > > and then the code is executed and the result returned.
    > >
    > > Maybe you can help me to add a "def f():" to the codeString, take care

    of
    > > the
    > > indentation and then execute the code?
    > >
    > > I really don't know how to handle the indentation thing. A "def
    > > f():\n"+codeString is easy, but this doesn't solve
    > > this issue.

    >
    > Maybe you can get away with a simple expression:
    >
    > >>> from math import *
    > >>> codeString = "sin(0.5) + 23"
    > >>> eval(codeString)

    > 23.479425538604204
    >
    > If not, try
    >
    > >>> codeString2 = "a=0.5\nb=23\nreturn sin(a) + b"
    > >>> funcString = "def f():\n " + "\n ".join(codeString2.split("\n"))
    > >>> funcString

    > 'def f():\n a=0.5\n b=23\n return sin(a) + b'
    > >>> exec funcString
    > >>> f()

    > 23.479425538604204
    > >>>


    Thats a good idea. I'll try it as soon as possible.

    Thanx
     
    Xaver Hinterhuber, May 11, 2004
    #4
  5. Hi Peter,

    I now have tried it.
    My problem is now, that I get unicode encoding errors.
    I am working on a german windoze machine.
    Could you plz give me a tip what encoding I should use (iso8859-1 and utf-8
    dont't work)?

    --
    with kind regards
    Xaver Hinterhuber

    "Xaver Hinterhuber" <> schrieb im Newsbeitrag
    news:c7qger$mml$...
    > Hi Peter,
    >
    > "Peter Otten" <> schrieb im Newsbeitrag
    > news:c7q1vs$oph$04$-online.com...
    > > Xaver Hinterhuber wrote:
    > >
    > > > I don't show you codeString because it is a real string variable.

    > >
    > > You could show an _example_ codeString, though. That would have greatly
    > > simplified the diagnosis.

    >
    > For clarification: The codeString contains up to 500 lines of code. It's a
    > complete program to render a pdf page with
    > reportlab. It's really a huge monster. But in future I'll try give an
    > example with all variables declared.
    > >
    > > > And yes, its uncompiled.
    > > > The user in my program enters a python program in this variable

    without
    > a
    > > > def statement
    > > > and then the code is executed and the result returned.
    > > >
    > > > Maybe you can help me to add a "def f():" to the codeString, take care

    > of
    > > > the
    > > > indentation and then execute the code?
    > > >
    > > > I really don't know how to handle the indentation thing. A "def
    > > > f():\n"+codeString is easy, but this doesn't solve
    > > > this issue.

    > >
    > > Maybe you can get away with a simple expression:
    > >
    > > >>> from math import *
    > > >>> codeString = "sin(0.5) + 23"
    > > >>> eval(codeString)

    > > 23.479425538604204
    > >
    > > If not, try
    > >
    > > >>> codeString2 = "a=0.5\nb=23\nreturn sin(a) + b"
    > > >>> funcString = "def f():\n " + "\n

    ".join(codeString2.split("\n"))
    > > >>> funcString

    > > 'def f():\n a=0.5\n b=23\n return sin(a) + b'
    > > >>> exec funcString
    > > >>> f()

    > > 23.479425538604204
    > > >>>

    >
    > Thats a good idea. I'll try it as soon as possible.
    >
    > Thanx
    >
    >
     
    Xaver Hinterhuber, May 17, 2004
    #5
  6. Robert Brewer

    Peter Otten Guest

    Xaver Hinterhuber wrote:

    > My problem is now, that I get unicode encoding errors.
    > I am working on a german windoze machine.
    > Could you plz give me a tip what encoding I should use (iso8859-1 and
    > utf-8 dont't work)?


    Other popular encodings are cp1252 and ISO-8859-15.

    Since your new problem is totally unrelated to code objects and core dumps,
    you should start a new thread. Remember to include as many hints that could
    help solve your problem as possible. Providing a piece of code and a
    traceback is always a good idea, since you could be misusing the api rather
    than have picked the wrong encoding. Having this newsgroup suggest random
    encodings is certainly not the way to go.

    Good luck,
    Peter
     
    Peter Otten, May 17, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    0
    Views:
    738
  2. BlueDoze
    Replies:
    2
    Views:
    1,217
    Gordon Beaton
    May 4, 2004
  3. halfdog
    Replies:
    12
    Views:
    12,582
  4. Xaver Hinterhuber

    function causing core dump

    Xaver Hinterhuber, May 10, 2004, in forum: Python
    Replies:
    4
    Views:
    332
    Terry Reedy
    May 10, 2004
  5. Mark Brackett

    Sig handler causing core dump

    Mark Brackett, Nov 3, 2004, in forum: C Programming
    Replies:
    3
    Views:
    554
    Mark Brackett
    Nov 4, 2004
Loading...

Share This Page