Include Pyton in cobol

who as experience and have included pyhton?

thanks for more Information!

  • 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

    #

     

    >>>>>>>>> 

  • CALL "SYSTEM" is in general not a good idea. It's difficult to creating security vulnerabilities, for one thing. And as an integration mechanism it's limited to pipes and filesystem objects for exchanging data, and to in-band signaling (commands embedded in data) and process termination for control flow.

    What problem are you trying to solve? Don't start by asking about technology - ask about problems and solutions.

    Python is an interpreted language with its own runtime. Since Micro Focus doesn't provide a backend that compiles COBOL to Python bytecode, you can't run COBOL under the Python interpreter. So in order to run Python and COBOL in the same process you have essentially four choices:

    • Embed the Python interpreter and runtime in a COBOL process.
    • Embed one of the COBOL runtimes in a Python process.
    • Use a Python variant that compiles to one of the execution environments for COBOL. For example, you could use Managed COBOL for .NET and IronPython, which compiles Python to .NET. Then you would have separate COBOL and Python assemblies which could interact in the same process.
    • Implement your own Python interpreter and runtime which you can embed in COBOL. This would not be trivial.

    These approaches have different trade-offs. Since we don't know what exactly you want to achieve or why, I can't say which might be best.

    If you want to mix Python and COBOL in the same source file ... well, that's not going to happen, unless you want to write a precompiler of some sort.