3

I am trying to execute the below python code on z/OS Mainframe USS. The problem I'm facing is that when I run the code, I get the below error message. It appears the square brackets are not recognized in my code.

  File "/u/q123/python/pyfilr.py", line 11
    print(lineï..0:4ï..)                   
              ¬                            
SyntaxError: invalid syntax 

Below is my code:

#!/usr/local/bin/rocket/python/python27                                 
# -*- coding: utf-8 -*-                                                 
import os                                                               
import json                                                             
def main():                                                             
    curpath = os.path.abspath(os.curdir)                                                      
    inp_file_path = os.path.join(curpath, os.path.join("python","inp.txt")
    file1 = open(inp_file_path,"r") 
    line = file1.readline().strip() 
    while line!="":
        print(line[0:4])
        jsonstr = json.dumps(line)
        line = file1.readline().strip()
        print(jsonstr)
    file1.close()
if __name__ == "__main__":
    main()   

If my remove the 2nd line "# -- coding: utf-8 -- " in my code then it errors out for below error message.

SyntaxError: Non-ASCII character '\xdd' in file /u/q123/python/pyfilr.py on line 11, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

I am not sure how to fix the error. I am using python 2.7.

Can you suggest a solution to this problem so that I can use square brackets in my code?.

nbk
  • 45,398
  • 8
  • 30
  • 47
sanddunes
  • 31
  • 2
  • 3
    `coding: utf-8` is not a valid thing to put in a source file that's not actually encoded in utf-8, as appears to be the case here. (Presumably EBCDIC encoding is involved at some point, as that appears to be the one encoding in the world that doesn't handle square brackets consistently.) You need to either find out what encoding your editor (or transfer method, if the files are being edited on another system) uses, and change the "coding" comment to match, or find a different editor/transfer method that bypasses EBCDIC completely. – jasonharper Oct 21 '19 at 03:41
  • I am not using any editor to develop my code. On the Mainframe we have USS (Unix Systems Services) region. I just get in there and create a new file with .py extension and add my code. If I don't code the "coding" line then it errors with the non-ASCII character error message that I mentioned in my post. – sanddunes Oct 21 '19 at 04:12
  • You could try setting the coding cookie to `# -- coding: cp037 --` - cp037 is often the default on IBM boxes. Also, this [list of encodings](https://docs.python.org/3/library/codecs.html#standard-encodings) includes various IBM encodings that might apply. You probably want to use the encoding that matches your environment's CCSID (cp037 is the same as *HEX, if IIRC). – snakecharmerb Oct 22 '19 at 04:49
  • This looks like you entered code using the wrong code page (37 or more likely 1140, which is 37 with €). In USS, source code needs to be CP 1047. If you are using a TN3270 emulator, you need to make sure that it is set to CP 1047, a.k.a. Open Systems. This is a very common problem with C and Java source as well. – zarchasmpgmr Oct 22 '19 at 22:09
  • I see this question was cross-posted to Rocket Software's forum, and there's another good answer there: https://forum.rocketsoftware.com/t/python-2-7-in-z-os-uss-unprintable-characters-displayed-in-the-code-while-executing/1698 There's a much newer release of Python for z/OS available that is likely quite helpful. – Timothy Sipples Oct 23 '19 at 09:17

2 Answers2

3

Ascii is very deeply built into Python, which is an issue on z/OS, which natively supports EBCDIC. You can use the chtag command to tell z/OS to treat a file as a specific encoding. To see if the file is tagged already, you can issue the command

ls -T <filename>

To tag the file, you can issue the command

chtag -tvc UTF-8 <filename>

I would suggest deleting the file, touching an empty file, tagging it, and then putting content in it, instead of just tagging the file.

Kevin McKenzie
  • 627
  • 3
  • 18
2

Since you are using python 2.7 the python script should be an ASCII file and your session should enable auto conversion. Here is what will work:

export _BPXK_AUTOCVT=ON
export _CEE_RUNOPTS="FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)"
iconv -f ibm-1047 -t utf-8 pyfilr.py >pyfilrA.py
chtag -t -c utf-8 pyfilrA.py
python pyfilrA.py

Also add a missing closing parenthesis on line 7 of your script.

Milos Lalovic
  • 554
  • 3
  • 10