Skip to content

Commit 807e988

Browse files
marian-pritsaklguohan
authored andcommitted
[flexsai]: Make code compatible with python3 (opencomputeproject#857)
Signed-off-by: Marian Pritsak <[email protected]>
1 parent 9cca393 commit 807e988

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

flexsai/p4/backend/json_stage/sai.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,19 @@ int main_wrapper(int argc, char *const argv[]) {
124124
#include "p4c_python.cpp"
125125

126126
static int py_argc = 8; //TODO find a better way to use this
127-
char * py_argv[py_argc];
128-
py_argv[0] = (char*) "P4_compiler.py";
129-
py_argv[1] = (char*) "-b";
130-
py_argv[2] = (char*) compiler_path.c_str();
127+
wchar_t * py_argv[py_argc];
128+
py_argv[0] = Py_DecodeLocale("P4_compiler.py", NULL);
129+
py_argv[1] = Py_DecodeLocale("-b", NULL);
130+
py_argv[2] = Py_DecodeLocale(compiler_path.c_str(), NULL);
131131
// py_argv[3] = (char*) "-o";
132132
// py_argv[4] = (char*) "output"; //TODO: add possibility to change output path(?)
133-
py_argv[3] = (char*) "-p";
134-
py_argv[4] = (char*) options.p4RuntimeJsonFile.c_str();
135-
py_argv[5] = (char*) "--api";
136-
py_argv[6] = (char*) "SAI";
137-
py_argv[7] = (char*) options.outputFile.c_str();
133+
py_argv[3] = Py_DecodeLocale("-p", NULL);
134+
py_argv[4] = Py_DecodeLocale(options.p4RuntimeJsonFile.c_str(), NULL);
135+
py_argv[5] = Py_DecodeLocale("--api", NULL);
136+
py_argv[6] = Py_DecodeLocale("SAI", NULL);
137+
py_argv[7] = Py_DecodeLocale(options.outputFile.c_str(), NULL);
138138

139-
Py_SetProgramName((char*)"sai_json_compiler");
139+
Py_SetProgramName(Py_DecodeLocale("sai_json_compiler", NULL));
140140
Py_Initialize();
141141
PySys_SetArgv(py_argc, py_argv);
142142
std::string compiler = compiler_path + "/output_stage/P4_compiler.py";

flexsai/p4/backend/output_stage/P4_aux.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Auxillary functions
44
def ensure_dir(directory):
55
if not os.path.exists(directory):
6-
os.makedirs(directory)
6+
os.makedirs(directory)
77

88
def get_canonical_c_name(name):
99
return name.replace('.','_')

flexsai/p4/backend/output_stage/P4_compiler.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
args.output_path
4040
)
4141

42-
print 'argv: '
43-
print sys.argv
44-
print 'args:'
45-
print args
42+
print('argv: ')
43+
print(sys.argv)
44+
print('args:')
45+
print(args)
4646
################################################################
4747
# local imports
4848
sys.path.append(os.path.join(args.backend_path,'output_stage'))
@@ -138,7 +138,7 @@ def create_visio(self): # support up to two tables per flow:
138138
try:
139139
new_line = visio_dicts[i][(table.flexPipe,pipe_index[table.flexPipe])].replace('__TEXT__',table_text)
140140
except KeyError:
141-
print 'Warning: [visio: %s] table skipped, no place in template'%visio
141+
print('Warning: [visio: %s] table skipped, no place in template'%visio)
142142
pass
143143
o.write(new_line)
144144
pipe_index[table.flexPipe] +=1
@@ -147,7 +147,7 @@ def create_visio(self): # support up to two tables per flow:
147147
o.write(line)
148148

149149
def get_header_key(self,key0,key1):
150-
if args.verbose : print ('getting key: %s.%s'%(key0,key1))
150+
if args.verbose : print ('getting key: %s.%s'%(key0,key1))
151151
# if key0 in self.sai_fixed_headers:
152152
# inner = self.sai_fixed_headers[key0]
153153
# if key1 in inner:
@@ -245,7 +245,7 @@ def interp_p4c_json_stage_output(lib,json_path):
245245
# bmv2 json doesn't save tables in order, need to search by 'base_default_next' field
246246

247247
print('interp ' + json_path)
248-
with open(json_path,'rb') as json_file:
248+
with open(json_path,'r') as json_file:
249249
j = json.load(json_file)
250250
for pipe in lib.flexPipes:
251251
# TODO (readability) make dict prio to prevent the below loop
@@ -264,7 +264,7 @@ def interp_p4c_json_stage_output(lib,json_path):
264264
while inserted_tables < len(tables) and next_table_name != None:
265265
for table in tables:
266266
table_name = table['name']
267-
if args.verbose : print "pipe %s. table %s" % (pipe, table_name)
267+
if args.verbose : print("pipe %s. table %s" % (pipe, table_name))
268268
if table_name != next_table_name and next_table_name != None:
269269
continue
270270
table_size = table['max_size']
@@ -294,7 +294,7 @@ def interp_p4c_json_stage_output(lib,json_path):
294294
# key_params.append(key['mlnx_extract']);
295295
# custom_key_offset.append(i)
296296
# if args.verbose : print 'getting custom key ' + json_target
297-
if args.verbose : print 'getting predefined key ' + json_target
297+
if args.verbose : print('getting predefined key ' + json_target)
298298

299299
#print key_params
300300
if (len(custom_key_offset) > 1):
@@ -308,10 +308,10 @@ def interp_p4c_json_stage_output(lib,json_path):
308308
#TODO- process static entries
309309
if args.verbose and 'entries' in table:
310310
for ent in table['entries']:
311-
print ent['match_key']
311+
print(ent['match_key'])
312312
# add to table
313313
next_table_name = table['base_default_next']
314-
if args.verbose : print 'inserted tables: %d'%inserted_tables
314+
if args.verbose : print ('inserted tables: %d'%inserted_tables)
315315
lib.pipe_table_count[pipe]=inserted_tables
316316
return
317317

@@ -336,8 +336,8 @@ def is_field_pre_extracted(json_obj, header, field_name):
336336
if len(fldlist) == 4:
337337
return fldlist[3]
338338
else:
339-
print 'Error parsing header field: ' + fld
340-
print fldlist
339+
print('Error parsing header field: ' + fld)
340+
print(fldlist)
341341
exit(0)
342342
break
343343
break
@@ -361,11 +361,11 @@ def is_field_pre_extracted(json_obj, header, field_name):
361361
print('\n==================================\nCreating %s API \n=================================='%args.api)
362362
if args.api == 'P4runtime':
363363
# import P4_api_grta as api
364-
print "This version doesn't support P4rt api"
364+
print ("This version doesn't support P4rt api")
365365
exit(1)
366366
if args.api == 'SDK':
367367
# import P4_api_SDK as api
368-
print "This version doesn't support SDK api"
368+
print ("This version doesn't support SDK api")
369369
exit(1)
370370
if args.api == 'SAI':
371371
import P4_api_SAI as api

0 commit comments

Comments
 (0)