Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make code compatible with both python2 and python3 #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/searpc-codegen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/usr/bin/env python2
#!/usr/bin/env python

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

"""
Generate function define macros.
Expand Down Expand Up @@ -103,7 +108,7 @@ def generate_marshal(ret_type, arg_types):
def gen_marshal_functions(f):
from rpc_table import func_table
for item in func_table:
print >>f, generate_marshal(item[0], item[1])
print(generate_marshal(item[0], item[1]), file=f)


marshal_register_item = r"""
Expand All @@ -130,11 +135,11 @@ def generate_marshal_register_item(ret_type, arg_types):

def gen_marshal_register_function(f):
from rpc_table import func_table
print >>f, "static void register_marshals()"""
print >>f, "{"
print("static void register_marshals()""", file=f)
print("{", file=f)
for item in func_table:
print >>f, generate_marshal_register_item(item[0], item[1]),
print >>f, "}"
print(generate_marshal_register_item(item[0], item[1]), end=' ', file=f)
print("}", file=f)

signature_template = r"""
inline static gchar *
Expand Down Expand Up @@ -164,7 +169,7 @@ def generate_signature(ret_type, arg_types):
def gen_signature_list():
f = open('searpc-signature.h', 'w')
for item in func_table:
print >>f,generate_signature(item[0], item[1])
print(generate_signature(item[0], item[1]), file=f)
f.close()


Expand All @@ -178,8 +183,8 @@ def gen_signature_list():
f = os.path.basename(abspath)
mod = f[:f.rfind('.')]
sys.path.append(d)
rpc_mod = __import__(mod, globals(), locals(), [], -1)
print "loaded func_table from %s" % (rpc_mod.__file__)
rpc_mod = __import__(mod, globals(), locals(), [], 0)
print("loaded func_table from %s" % (rpc_mod.__file__))
func_table = rpc_mod.func_table
else:
# load from default rpc_table.py
Expand Down
6 changes: 3 additions & 3 deletions pysearpc/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from common import SearpcError
from .common import SearpcError

class SearpcService(object):
def __init__(self, name):
Expand All @@ -25,7 +25,7 @@ def _call_function(self, svcname, fcallstr):
"""input str -> output str"""
try:
argv = json.loads(fcallstr)
except Exception, e:
except Exception as e:
raise SearpcError('bad call str: ' + str(e))

service = self.services[svcname]
Expand All @@ -41,7 +41,7 @@ def _call_function(self, svcname, fcallstr):
def call_function(self, svcname, fcallstr):
try:
retVal = self._call_function(svcname, fcallstr)
except Exception, e:
except Exception as e:
ret = {'err_code': 555, 'err_msg': str(e)}
else:
ret = {'ret': retVal}
Expand Down