Calling COBOL from Python

Python is the shortest road to a COBOL program that already exists: no C file, no link line, and two ways to do it. The first loads the module and calls it in the same process, the second starts the program and talks to it through standard input and output.

ctypes over a module

The module is the one of the previous lesson, built with cobc -m:

DATA DIVISION. LINKAGE SECTION. 01 L-AMOUNT PIC S9(9) USAGE COMP-5. 01 L-RESULT PIC S9(9) USAGE COMP-5. PROCEDURE DIVISION USING L-AMOUNT L-RESULT. COMPUTE L-RESULT = L-AMOUNT * 13 / 100 GOBACK. END PROGRAM TAXCALC.

ctypes loads the shared object, and the COBOL runtime has to be started before the first call, exactly as in C:

import ctypes lib = ctypes.CDLL("./TAXCALC.so") lib.cob_init(0, None) amount = ctypes.c_int(300) result = ctypes.c_int(0) lib.TAXCALC(ctypes.byref(amount), ctypes.byref(result)) print("tax", result.value, "of", amount.value)

cobc -m -free -Wall -o TAXCALC.so callee.cob
python3 use.py

tax 39 of 300

c_int is the Python name of the C integer that PIC S9(9) USAGE COMP-5 stands for, and byref is the address the COBOL side expects. cob_init(0, None) is the one line of the driver that cannot be left out: without it the runtime stops the process with the message the previous lesson showed.

Nothing is loaded twice and nothing is copied: the Python process and the COBOL module share the memory of the two integers, which is why this road is the fast one. It is also the road that ties the Python program to the architecture of the module and to the shape of every parameter.

A subprocess over stdin

The other road does not care about either. A COBOL program that reads lines from standard input and writes to standard output is a program any language can run:

IDENTIFICATION DIVISION. PROGRAM-ID. ECHO1. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-LINE PIC X(80). 01 WS-N PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM UNTIL WS-N IS GREATER THAN 2 ACCEPT WS-LINE IF WS-LINE = SPACES EXIT PERFORM END-IF ADD 1 TO WS-N DISPLAY "read [" FUNCTION TRIM(WS-LINE) "]" END-PERFORM. DISPLAY "lines " WS-N. STOP RUN.

The Python side gives it four lines and takes the answer:

import subprocess proc = subprocess.run( ["./echo1"], input="alpha\nbeta\ngamma\ndelta\n", text=True, capture_output=True, check=True, ) print(proc.stdout, end="")

cobc -x -free -Wall -o echo1 echo1.cob
python3 run.py

read [alpha]
read [beta]
read [gamma]
lines 003

Three of the four lines were read and the fourth was left for the next reader, because the loop stops after three passes. The EXIT PERFORM is the guard for an empty line: the program would otherwise have read a blank and counted it.

The exit status of the COBOL program comes back as proc.returncode and a non-zero one raises CalledProcessError because of check=True, so the RETURN-CODE convention of the COBOL side is the error channel of the Python side. That is the whole contract, and it needs no copybook.

What the two roads cost

The direct call is fast and fragile: the parameters are raw memory, a wrong type in Python is a wrong number in COBOL with no check on either side, and a COBOL program that ends the run takes the Python process with it. The subprocess is slow and safe: one process per request, a text format between the two sides, and a crash on the COBOL side is a return code.

A batch job that runs once a night takes the second road without thinking about it. A web service that answers a thousand requests a second needs the first, and then the field layout is a contract that belongs in a copybook on the COBOL side and a ctypes.Structure on the Python side, kept in one file with a version field.

Exercises

  1. Change the amount in use.py to 1000 and run it again.
  2. Remove the cob_init call and read what Python reports.
  3. Give the subprocess program five lines and see how many it reads.
  4. Make the COBOL reader stop at the first line that starts with # and have Python send such a line.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does ctypes.CDLL load?

    Answer

    The shared module built with cobc -m

    The script loaded TAXCALC.so and called its entry point.

  2. Which call starts the COBOL runtime from Python?

    Answer

    lib.cob_init(0, None)

    It is the same call the C driver makes before its first COBOL call.

  3. Which Python type holds a COBOL PIC S9(9) USAGE COMP-5?

    Answer

    ctypes.c_int

    The two fields of the page are c_int values passed by reference.

  4. What does subprocess.run(... check=True) do with a non-zero exit status?

    Answer

    It raises CalledProcessError

    The exit status of the COBOL program is the error channel of the Python side.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM 3 TIMES ADD 1 TO W-N END-PERFORM. DISPLAY W-N. STOP RUN.

    Answer

    003

    Three passes count three.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-L PIC X(10) VALUE " abc ". PROCEDURE DIVISION. DISPLAY "[" FUNCTION TRIM(W-L) "]". STOP RUN.

    Answer

    [abc]

    The field holds the letters in the middle, and TRIM removes both sides.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC S9(4) USAGE COMP-5 VALUE 100. 01 W-R PIC S9(9) USAGE COMP-5. PROCEDURE DIVISION. COMPUTE W-R = W-A * 13 / 100 DISPLAY W-R. STOP RUN.

    Answer

    +0000000013

    Thirteen per cent of a hundred is thirteen.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM UNTIL W-N IS GREATER THAN 2 ADD 1 TO W-N END-PERFORM. DISPLAY W-N. STOP RUN.

    Answer

    3

    The loop adds until the counter passes two, which is three passes.

  9. Which road between Python and COBOL is safe against a wrong parameter type?

    Answer

    The subprocess, because the format between the sides is text

    A raw pointer carries no type on either side of the direct call.

  10. How does a COBOL failure reach the Python side of the subprocess?

    Answer

    As the exit status of the process, which becomes proc.returncode

    RETURN-CODE of the COBOL program is the exit status the Python side reads.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run with Python 3.14.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev