Skip to content

Commit d6f2e89

Browse files
niolxrmx
authored andcommitted
port pypy plugin to python3
[xrmx: add 36e045c to reduce delta with master]
1 parent 86f2499 commit d6f2e89

File tree

1 file changed

+88
-48
lines changed

1 file changed

+88
-48
lines changed

plugins/pypy/pypy_setup.py

Lines changed: 88 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import os
33
sys.path.insert(0, '.')
4-
sys.path.extend(os.environ.get('PYTHONPATH','').split(os.pathsep))
4+
sys.path.extend(os.environ.get('PYTHONPATH', '').split(os.pathsep))
55
import imp
66
import traceback
77

@@ -48,15 +48,15 @@
4848
uwsgi_defines = []
4949
uwsgi_cflags = ffi.string(lib0.uwsgi_get_cflags()).split()
5050
for cflag in uwsgi_cflags:
51-
if cflag.startswith('-D'):
52-
line = cflag[2:]
51+
if cflag.startswith(b'-D'):
52+
line = cflag[2:].decode()
5353
if '=' in line:
5454
(key, value) = line.split('=', 1)
5555
uwsgi_cdef.append('#define %s ...' % key)
56-
uwsgi_defines.append('#define %s %s' % (key, value.replace('\\"','"').replace('""','"')))
56+
uwsgi_defines.append('#define %s %s' % (key, value.replace('\\"','"').replace('""','"')))
5757
else:
5858
uwsgi_cdef.append('#define %s ...' % line)
59-
uwsgi_defines.append('#define %s 1' % line)
59+
uwsgi_defines.append('#define %s 1' % line)
6060
uwsgi_dot_h = ffi.string(lib0.uwsgi_get_dot_h())
6161

6262
# uwsgi definitions
@@ -166,16 +166,27 @@
166166
struct uwsgi_plugin *p[];
167167
...;
168168
};
169-
struct uwsgi_server uwsgi;
169+
extern struct uwsgi_server uwsgi;
170170
171-
struct uwsgi_plugin pypy_plugin;
171+
extern struct uwsgi_plugin pypy_plugin;
172172
173-
const char *uwsgi_pypy_version;
173+
extern const char *uwsgi_pypy_version;
174174
175175
char *uwsgi_binary_path();
176176
177177
void *uwsgi_malloc(size_t);
178178
179+
struct uwsgi_logvar {
180+
char key[256];
181+
uint8_t keylen;
182+
char val[256];
183+
uint8_t vallen;
184+
struct uwsgi_logvar *next;
185+
};
186+
187+
struct uwsgi_logvar *uwsgi_logvar_get(struct wsgi_request *, char *, uint8_t);
188+
void uwsgi_logvar_add(struct wsgi_request *, char *, uint8_t, char *, uint8_t);
189+
179190
int uwsgi_response_prepare_headers(struct wsgi_request *, char *, size_t);
180191
int uwsgi_response_add_header(struct wsgi_request *, char *, uint16_t, char *, uint16_t);
181192
int uwsgi_response_write_body_do(struct wsgi_request *, char *, size_t);
@@ -237,7 +248,7 @@
237248
238249
int uwsgi_ready_fd(struct wsgi_request *);
239250
240-
void set_user_harakiri(int);
251+
void set_user_harakiri(struct wsgi_request *, int);
241252
242253
int uwsgi_metric_set(char *, char *, int64_t);
243254
int uwsgi_metric_inc(char *, char *, int64_t);
@@ -260,14 +271,13 @@
260271
extern struct uwsgi_server uwsgi;
261272
extern struct uwsgi_plugin pypy_plugin;
262273
%s
263-
''' % ('\n'.join(uwsgi_defines), uwsgi_dot_h, hooks)
274+
''' % ('\n'.join(uwsgi_defines), uwsgi_dot_h.decode(), hooks)
264275

265276
ffi.cdef(cdefines)
266277
lib = ffi.verify(cverify)
267278
libc = ffi.dlopen(None)
268279

269280

270-
271281
"""
272282
this is a global object point the the WSGI callable
273283
it sucks, i will fix it in the near future...
@@ -276,7 +286,7 @@
276286

277287
# fix argv if needed
278288
if len(sys.argv) == 0:
279-
sys.argv.insert(0, ffi.string(lib.uwsgi_binary_path()))
289+
sys.argv.insert(0, ffi.string(lib.uwsgi_binary_path()).decode())
280290

281291
"""
282292
execute source, we expose it as cffi callback to avoid deadlocks
@@ -293,7 +303,7 @@ def uwsgi_pypy_execute_source(s):
293303
@ffi.callback("void(char *)")
294304
def uwsgi_pypy_loader(module):
295305
global wsgi_application
296-
m = ffi.string(module)
306+
m = ffi.string(module).decode()
297307
c = 'application'
298308
if ':' in m:
299309
m, c = m.split(':')
@@ -311,7 +321,7 @@ def uwsgi_pypy_file_loader(filename):
311321
global wsgi_application
312322
w = ffi.string(filename)
313323
c = 'application'
314-
mod = imp.load_source('uwsgi_file_wsgi', w)
324+
mod = imp.load_source('uwsgi_file_wsgi', w.decode())
315325
wsgi_application = getattr(mod, c)
316326

317327
"""
@@ -320,7 +330,7 @@ def uwsgi_pypy_file_loader(filename):
320330
@ffi.callback("void(char *)")
321331
def uwsgi_pypy_paste_loader(config):
322332
global wsgi_application
323-
c = ffi.string(config)
333+
c = ffi.string(config).decode()
324334
if c.startswith('config:'):
325335
c = c[7:]
326336
if c[0] != '/':
@@ -329,7 +339,7 @@ def uwsgi_pypy_paste_loader(config):
329339
from logging.config import fileConfig
330340
fileConfig(c)
331341
except ImportError:
332-
print "PyPy WARNING: unable to load logging.config"
342+
print("PyPy WARNING: unable to load logging.config")
333343
from paste.deploy import loadapp
334344
wsgi_application = loadapp('config:%s' % c)
335345

@@ -347,9 +357,9 @@ def uwsgi_pypy_post_fork_hook():
347357
"""
348358
@ffi.callback("void(char *)")
349359
def uwsgi_pypy_pythonpath(item):
350-
path = ffi.string(item)
360+
path = ffi.string(item).decode()
351361
sys.path.append(path)
352-
print "added %s to pythonpath" % path
362+
print("added %s to pythonpath" % path)
353363

354364

355365
"""
@@ -443,15 +453,17 @@ def writer(data):
443453
def start_response(status, headers, exc_info=None):
444454
if exc_info:
445455
traceback.print_exception(*exc_info)
456+
status = status.encode()
446457
lib.uwsgi_response_prepare_headers(wsgi_req, ffi.new("char[]", status), len(status))
447458
for hh in headers:
459+
hh = (hh[0].encode(), hh[1].encode())
448460
lib.uwsgi_response_add_header(wsgi_req, ffi.new("char[]", hh[0]), len(hh[0]), ffi.new("char[]", hh[1]), len(hh[1]))
449461
return writer
450462

451463
environ = {}
452464
iov = wsgi_req.hvec
453465
for i in range(0, wsgi_req.var_cnt, 2):
454-
environ[ffi.string(ffi.cast("char*", iov[i].iov_base), iov[i].iov_len)] = ffi.string(ffi.cast("char*", iov[i+1].iov_base), iov[i+1].iov_len)
466+
environ[ffi.string(ffi.cast("char*", iov[i].iov_base), iov[i].iov_len).decode()] = ffi.string(ffi.cast("char*", iov[i+1].iov_base), iov[i+1].iov_len).decode()
455467

456468
environ['wsgi.version'] = (1, 0)
457469
scheme = 'http'
@@ -568,8 +580,8 @@ def uwsgi_pypy_rpc(node, func, *args):
568580

569581
def uwsgi_pypy_call(func, *args):
570582
node = None
571-
if '@' in func:
572-
(func, node) = func.split('@')
583+
if b'@' in func:
584+
(func, node) = func.split(b'@')
573585
return uwsgi_pypy_rpc(node, func, *args)
574586
uwsgi.call = uwsgi_pypy_call
575587

@@ -708,7 +720,7 @@ def uwsgi_pypy_current_wsgi_req():
708720
def uwsgi_pypy_suspend():
709721
wsgi_req = uwsgi_pypy_current_wsgi_req()
710722
if lib.uwsgi.schedule_to_main:
711-
lib.uwsgi.schedule_to_main(wsgi_req);
723+
lib.uwsgi.schedule_to_main(wsgi_req)
712724
uwsgi.suspend = uwsgi_pypy_suspend
713725

714726
"""
@@ -723,7 +735,7 @@ def uwsgi_pypy_workers():
723735
worker['requests'] = lib.uwsgi.workers[i].requests
724736
worker['delta_requests'] = lib.uwsgi.workers[i].delta_requests
725737
worker['signals'] = lib.uwsgi.workers[i].signals
726-
worker['exceptions'] = lib.uwsgi_worker_exceptions(i);
738+
worker['exceptions'] = lib.uwsgi_worker_exceptions(i)
727739
worker['apps'] = []
728740
if lib.uwsgi.workers[i].cheaped:
729741
worker['status'] == 'cheap'
@@ -750,8 +762,8 @@ def uwsgi_pypy_workers():
750762
"""
751763
def uwsgi_pypy_async_sleep(timeout):
752764
if timeout > 0:
753-
wsgi_req = uwsgi_pypy_current_wsgi_req();
754-
lib.async_add_timeout(wsgi_req, timeout);
765+
wsgi_req = uwsgi_pypy_current_wsgi_req()
766+
lib.async_add_timeout(wsgi_req, timeout)
755767
uwsgi.async_sleep = uwsgi_pypy_async_sleep
756768

757769
"""
@@ -770,7 +782,7 @@ def uwsgi_pypy_async_connect(addr):
770782
uwsgi.wait_fd_read(fd, timeout=0)
771783
"""
772784
def uwsgi_pypy_wait_fd_read(fd, timeout=0):
773-
wsgi_req = uwsgi_pypy_current_wsgi_req();
785+
wsgi_req = uwsgi_pypy_current_wsgi_req()
774786
if lib.async_add_fd_read(wsgi_req, fd, timeout) < 0:
775787
raise Exception("unable to add fd %d to the event queue" % fd)
776788
uwsgi.wait_fd_read = uwsgi_pypy_wait_fd_read
@@ -779,7 +791,7 @@ def uwsgi_pypy_wait_fd_read(fd, timeout=0):
779791
uwsgi.wait_fd_write(fd, timeout=0)
780792
"""
781793
def uwsgi_pypy_wait_fd_write(fd, timeout=0):
782-
wsgi_req = uwsgi_pypy_current_wsgi_req();
794+
wsgi_req = uwsgi_pypy_current_wsgi_req()
783795
if lib.async_add_fd_write(wsgi_req, fd, timeout) < 0:
784796
raise Exception("unable to add fd %d to the event queue" % fd)
785797
uwsgi.wait_fd_write = uwsgi_pypy_wait_fd_write
@@ -788,7 +800,7 @@ def uwsgi_pypy_wait_fd_write(fd, timeout=0):
788800
uwsgi.ready_fd()
789801
"""
790802
def uwsgi_pypy_ready_fd():
791-
wsgi_req = uwsgi_pypy_current_wsgi_req();
803+
wsgi_req = uwsgi_pypy_current_wsgi_req()
792804
return lib.uwsgi_ready_fd(wsgi_req)
793805
uwsgi.ready_fd = uwsgi_pypy_ready_fd
794806

@@ -800,7 +812,7 @@ def uwsgi_pypy_send(*args):
800812
if len(args) == 0:
801813
raise ValueError("uwsgi.send() takes at least 1 argument")
802814
elif len(args) == 1:
803-
wsgi_req = uwsgi_pypy_current_wsgi_req();
815+
wsgi_req = uwsgi_pypy_current_wsgi_req()
804816
fd = wsgi_req.fd
805817
data = args[0]
806818
else:
@@ -819,7 +831,7 @@ def uwsgi_pypy_recv(*args):
819831
if len(args) == 0:
820832
raise ValueError("uwsgi.recv() takes at least 1 argument")
821833
elif len(args) == 1:
822-
wsgi_req = uwsgi_pypy_current_wsgi_req();
834+
wsgi_req = uwsgi_pypy_current_wsgi_req()
823835
fd = wsgi_req.fd
824836
l = args[0]
825837
else:
@@ -846,8 +858,8 @@ def uwsgi_pypy_recv(*args):
846858
uwsgi.websocket_recv()
847859
"""
848860
def uwsgi_pypy_websocket_recv():
849-
wsgi_req = uwsgi_pypy_current_wsgi_req();
850-
ub = lib.uwsgi_websocket_recv(wsgi_req);
861+
wsgi_req = uwsgi_pypy_current_wsgi_req()
862+
ub = lib.uwsgi_websocket_recv(wsgi_req)
851863
if ub == ffi.NULL:
852864
raise IOError("unable to receive websocket message")
853865
ret = ffi.buffer(ub.buf, ub.pos)[:]
@@ -859,8 +871,8 @@ def uwsgi_pypy_websocket_recv():
859871
uwsgi.websocket_recv_nb()
860872
"""
861873
def uwsgi_pypy_websocket_recv_nb():
862-
wsgi_req = uwsgi_pypy_current_wsgi_req();
863-
ub = lib.uwsgi_websocket_recv_nb(wsgi_req);
874+
wsgi_req = uwsgi_pypy_current_wsgi_req()
875+
ub = lib.uwsgi_websocket_recv_nb(wsgi_req)
864876
if ub == ffi.NULL:
865877
raise IOError("unable to receive websocket message")
866878
ret = ffi.buffer(ub.buf, ub.pos)[:]
@@ -872,7 +884,7 @@ def uwsgi_pypy_websocket_recv_nb():
872884
uwsgi.websocket_handshake(key, origin)
873885
"""
874886
def uwsgi_pypy_websocket_handshake(key='', origin='', proto=''):
875-
wsgi_req = uwsgi_pypy_current_wsgi_req();
887+
wsgi_req = uwsgi_pypy_current_wsgi_req()
876888
c_key = ffi.new('char[]', key)
877889
c_origin = ffi.new('char[]', origin)
878890
c_proto = ffi.new('char[]', proto)
@@ -884,7 +896,7 @@ def uwsgi_pypy_websocket_handshake(key='', origin='', proto=''):
884896
uwsgi.websocket_send(msg)
885897
"""
886898
def uwsgi_pypy_websocket_send(msg):
887-
wsgi_req = uwsgi_pypy_current_wsgi_req();
899+
wsgi_req = uwsgi_pypy_current_wsgi_req()
888900
if lib.uwsgi_websocket_send(wsgi_req, ffi.new('char[]', msg), len(msg)) < 0:
889901
raise IOError("unable to send websocket message")
890902
uwsgi.websocket_send = uwsgi_pypy_websocket_send
@@ -893,7 +905,7 @@ def uwsgi_pypy_websocket_send(msg):
893905
uwsgi.chunked_read(timeout=0)
894906
"""
895907
def uwsgi_pypy_chunked_read(timeout=0):
896-
wsgi_req = uwsgi_pypy_current_wsgi_req();
908+
wsgi_req = uwsgi_pypy_current_wsgi_req()
897909
rlen = ffi.new("size_t*")
898910
chunk = lib.uwsgi_chunked_read(wsgi_req, rlen, timeout, 0)
899911
if chunk == ffi.NULL:
@@ -905,7 +917,7 @@ def uwsgi_pypy_chunked_read(timeout=0):
905917
uwsgi.chunked_read_nb()
906918
"""
907919
def uwsgi_pypy_chunked_read_nb():
908-
wsgi_req = uwsgi_pypy_current_wsgi_req();
920+
wsgi_req = uwsgi_pypy_current_wsgi_req()
909921
rlen = ffi.new("size_t*")
910922
chunk = lib.uwsgi_chunked_read(wsgi_req, rlen, 0, 1)
911923
if chunk == ffi.NULL:
@@ -915,14 +927,42 @@ def uwsgi_pypy_chunked_read_nb():
915927
return ffi.buffer(chunk, rlen[0])[:]
916928
uwsgi.chunked_read_nb = uwsgi_pypy_chunked_read_nb
917929

918-
"""
919-
uwsgi.set_user_harakiri(sec)
920-
"""
921-
uwsgi.set_user_harakiri = lambda x: lib.set_user_harakiri(x)
930+
931+
def uwsgi_pypy_set_user_harakiri(x):
932+
"""
933+
uwsgi.set_user_harakiri(sec)
934+
"""
935+
wsgi_req = uwsgi_pypy_current_wsgi_req()
936+
lib.set_user_harakiri(wsgi_req, x)
937+
uwsgi.set_user_harakiri = uwsgi_pypy_set_user_harakiri
938+
939+
940+
def uwsgi_pypy_get_logvar(key):
941+
"""
942+
uwsgi.get_logvar(key)
943+
"""
944+
wsgi_req = uwsgi_pypy_current_wsgi_req()
945+
c_key = ffi.new('char[]', key)
946+
lv = lib.uwsgi_logvar_get(wsgi_req, c_key, len(key))
947+
if lv:
948+
return ffi.string(lv.val[0:lv.vallen])
949+
return None
950+
uwsgi.get_logvar = uwsgi_pypy_get_logvar
951+
952+
953+
def uwsgi_pypy_set_logvar(key, val):
954+
"""
955+
uwsgi.set_logvar(key, value)
956+
"""
957+
wsgi_req = uwsgi_pypy_current_wsgi_req()
958+
c_key = ffi.new('char[]', key)
959+
c_val = ffi.new('char[]', val)
960+
lib.uwsgi_logvar_add(wsgi_req, c_key, len(key), c_val, len(val))
961+
uwsgi.set_logvar = uwsgi_pypy_set_logvar
922962

923963

924-
print "Initialized PyPy with Python", sys.version
925-
print "PyPy Home:", sys.prefix
964+
print("Initialized PyPy with Python %s" % sys.version)
965+
print("PyPy Home: %s" % sys.prefix)
926966

927967

928968
"""
@@ -939,7 +979,7 @@ def uwsgi_pypy_continulet_wrapper(cont):
939979
@ffi.callback("void()")
940980
def uwsgi_pypy_continulet_schedule():
941981
id = lib.uwsgi.wsgi_req.async_id
942-
modifier1 = lib.uwsgi.wsgi_req.uh.modifier1;
982+
modifier1 = lib.uwsgi.wsgi_req.uh.modifier1
943983

944984
# generate a new continulet
945985
if not lib.uwsgi.wsgi_req.suspended:
@@ -961,7 +1001,7 @@ def uwsgi_pypy_continulet_schedule():
9611001
@ffi.callback("void(struct wsgi_request *)")
9621002
def uwsgi_pypy_continulet_switch(wsgi_req):
9631003
id = wsgi_req.async_id
964-
modifier1 = wsgi_req.uh.modifier1;
1004+
modifier1 = wsgi_req.uh.modifier1
9651005

9661006
# this is called in the current continulet
9671007
if lib.uwsgi.p[modifier1].suspend:
@@ -977,8 +1017,8 @@ def uwsgi_pypy_continulet_switch(wsgi_req):
9771017
lib.uwsgi.wsgi_req = wsgi_req
9781018

9791019
def uwsgi_pypy_setup_continulets():
980-
if lib.uwsgi.async <= 1:
1020+
if lib.uwsgi["async"] < 1:
9811021
raise Exception("pypy continulets require async mode !!!")
9821022
lib.uwsgi.schedule_to_main = uwsgi_pypy_continulet_switch
9831023
lib.uwsgi.schedule_to_req = uwsgi_pypy_continulet_schedule
984-
print "*** PyPy Continulets engine loaded ***"
1024+
print("*** PyPy Continulets engine loaded ***")

0 commit comments

Comments
 (0)