Include Pyton in cobol

who as experience and have included pyhton?

thanks for more Information!

Parents
  • Hi

    Use call system, but I think you want to know how to call cobol from python.

    Here is a example see doit.sh, you need a c wrapper, and need to use LD_PRELOAD.

    Regards

    Tony

    Here is the output from the test script.

    >>>>>>>>>> 

    /home/tonyt/test >mkdir tmp

    /home/tonyt/test >cd tmp

    /home/tonyt/test/tmp >ls

    /home/tonyt/test/tmp >cp ../2813490python/doit.sh .

    /home/tonyt/test/tmp >ls

    doit.sh

    /home/tonyt/test/tmp >. ./doit.sh

    COBDIR set to /home/products/vcdevhub22upd2preGA

    cob64 -C nolist -Zv helloworld.c program1.cbl

    helloworld.c:

    program1.cbl:

    * Micro Focus COBOL                  V2.2 revision 002           Compiler

    * Copyright (C) Micro Focus 1984-2014. All rights reserved.

    * Accepted - verbose

    * Accepted - nolist

    * Compiling program1.cbl

    * Total Messages:     0

    * Data:         320     Code:          78

    * Micro Focus COBOL Code Generator

    * Copyright (C) Micro Focus 1984-2014. All rights reserved.

    * Accepted - verbose

    * Accepted - pic

    * Generating program1

    * Data:          88     Code:         544     Literals:          32

     

    LD_LIBRARY_PATH     </home/products/vcdevhub22upd2preGA/lib>

    LD_PRELOAD          </home/products/vcdevhub22upd2preGA/lib/libcobcrtn64.so:/home/products/vcdevhub22upd2preGA/lib/libcobrts64.so:/home/products/vcdevhub22upd2preGA/lib/libcobmisc64.so:/home/products/vcdevhub22upd2preGA/lib/libcobscreen64.so:/home/products/vcdevhub22upd2preGA/lib/libcobtrace64.so>

    sys.path            <

    ['/home/tonyt/test/tmp', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']

     

    before c call

    Started Hello World

    program 1 has started

    after c call

    >>>>>>>>>>.

    Here is my test script.

    >>>>>>> doit.sh

    /home/tonyt/test/tmp >

    cat doit.sh

    #

    # set cobol environment

    #

    unset LD_LIBRARY_PATH

    . /home/products/vcdevhub22upd2preGA/bin/cobsetenv

    COBMODE=64

    export COBMODE

    #

    # create c program

    #

    cat >helloworld.c <<EOF

    /* Hello World program */

     

    #include<stdio.h>

     

    main()

    {

        cobinit();

        printf("Started Hello World\n");

        program1();

        cobtidy();

    }

    EOF

    #

    # create cobol program

    #

    cat >program1.cbl <<EOF

           program-id. program1 as "program1".

     

           environment division.

           configuration section.

     

           data division.

           working-storage section.

           01 in_data        pic x(10) value "prog1 data".

     

           procedure division.

     

               display "program 1 has started"

          *     call "Program2" using in_data.

              

               goback.

     

           end program program1.

     

    EOF

    #

    # build the .so file

    #

    cob -Zv helloworld.c program1.cbl

    #

    # create python script

    #

    cat >mytest.py <<EOF

    import ctypes

    import sys, os

    print

    print "LD_LIBRARY_PATH     <" os.environ['LD_LIBRARY_PATH'] ">"

    print "LD_PRELOAD          <" os.environ['LD_PRELOAD'] ">"

    print "sys.path            <"

    print sys.path

    print ">"

    print

    #

    mylib = ctypes.CDLL("./libhelloworld.so")

    print "before c call"

    mylib.main()

    print "after c call"

    exit()

    EOF

    #

    # run the python file

    #

    # get these modules loaded

    #

    export LD_PRELOAD=$COBDIR/lib/libcobcrtn64.so:$COBDIR/lib/libcobrts64.so:$COBDIR/lib/libcobmisc64.so:$COBDIR/lib/libcobscreen64.so:$COBDIR/lib/libcobtrace64.so

    #

    # run the python script

    #

    python mytest.py

    #

    # remove the LD_PRELOAD

    #

    unset LD_PRELOAD

    #

    # end

    #

     

    >>>>>>>>> 

Reply
  • Hi

    Use call system, but I think you want to know how to call cobol from python.

    Here is a example see doit.sh, you need a c wrapper, and need to use LD_PRELOAD.

    Regards

    Tony

    Here is the output from the test script.

    >>>>>>>>>> 

    /home/tonyt/test >mkdir tmp

    /home/tonyt/test >cd tmp

    /home/tonyt/test/tmp >ls

    /home/tonyt/test/tmp >cp ../2813490python/doit.sh .

    /home/tonyt/test/tmp >ls

    doit.sh

    /home/tonyt/test/tmp >. ./doit.sh

    COBDIR set to /home/products/vcdevhub22upd2preGA

    cob64 -C nolist -Zv helloworld.c program1.cbl

    helloworld.c:

    program1.cbl:

    * Micro Focus COBOL                  V2.2 revision 002           Compiler

    * Copyright (C) Micro Focus 1984-2014. All rights reserved.

    * Accepted - verbose

    * Accepted - nolist

    * Compiling program1.cbl

    * Total Messages:     0

    * Data:         320     Code:          78

    * Micro Focus COBOL Code Generator

    * Copyright (C) Micro Focus 1984-2014. All rights reserved.

    * Accepted - verbose

    * Accepted - pic

    * Generating program1

    * Data:          88     Code:         544     Literals:          32

     

    LD_LIBRARY_PATH     </home/products/vcdevhub22upd2preGA/lib>

    LD_PRELOAD          </home/products/vcdevhub22upd2preGA/lib/libcobcrtn64.so:/home/products/vcdevhub22upd2preGA/lib/libcobrts64.so:/home/products/vcdevhub22upd2preGA/lib/libcobmisc64.so:/home/products/vcdevhub22upd2preGA/lib/libcobscreen64.so:/home/products/vcdevhub22upd2preGA/lib/libcobtrace64.so>

    sys.path            <

    ['/home/tonyt/test/tmp', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']

     

    before c call

    Started Hello World

    program 1 has started

    after c call

    >>>>>>>>>>.

    Here is my test script.

    >>>>>>> doit.sh

    /home/tonyt/test/tmp >

    cat doit.sh

    #

    # set cobol environment

    #

    unset LD_LIBRARY_PATH

    . /home/products/vcdevhub22upd2preGA/bin/cobsetenv

    COBMODE=64

    export COBMODE

    #

    # create c program

    #

    cat >helloworld.c <<EOF

    /* Hello World program */

     

    #include<stdio.h>

     

    main()

    {

        cobinit();

        printf("Started Hello World\n");

        program1();

        cobtidy();

    }

    EOF

    #

    # create cobol program

    #

    cat >program1.cbl <<EOF

           program-id. program1 as "program1".

     

           environment division.

           configuration section.

     

           data division.

           working-storage section.

           01 in_data        pic x(10) value "prog1 data".

     

           procedure division.

     

               display "program 1 has started"

          *     call "Program2" using in_data.

              

               goback.

     

           end program program1.

     

    EOF

    #

    # build the .so file

    #

    cob -Zv helloworld.c program1.cbl

    #

    # create python script

    #

    cat >mytest.py <<EOF

    import ctypes

    import sys, os

    print

    print "LD_LIBRARY_PATH     <" os.environ['LD_LIBRARY_PATH'] ">"

    print "LD_PRELOAD          <" os.environ['LD_PRELOAD'] ">"

    print "sys.path            <"

    print sys.path

    print ">"

    print

    #

    mylib = ctypes.CDLL("./libhelloworld.so")

    print "before c call"

    mylib.main()

    print "after c call"

    exit()

    EOF

    #

    # run the python file

    #

    # get these modules loaded

    #

    export LD_PRELOAD=$COBDIR/lib/libcobcrtn64.so:$COBDIR/lib/libcobrts64.so:$COBDIR/lib/libcobmisc64.so:$COBDIR/lib/libcobscreen64.so:$COBDIR/lib/libcobtrace64.so

    #

    # run the python script

    #

    python mytest.py

    #

    # remove the LD_PRELOAD

    #

    unset LD_PRELOAD

    #

    # end

    #

     

    >>>>>>>>> 

Children
No Data