Skip to content

Commit dad0024

Browse files
committed
make: Make it possible to use an environ var to define DEBUG constant
1 parent 92fb0fc commit dad0024

File tree

13 files changed

+248
-237
lines changed

13 files changed

+248
-237
lines changed

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@ distclean: clean clean-libuv
3131

3232

3333
compile: check-env clean
34-
echo "DEF DEBUG = 0" > uvloop/__debug.pxi
35-
$(PYTHON) -m cython -3 uvloop/loop.pyx; rm uvloop/__debug.*
34+
$(PYTHON) -m cython -3 uvloop/loop.pyx
3635
@echo "$$UVLOOP_BUILD_PATCH_SCRIPT" | $(PYTHON)
3736
$(PYTHON) setup.py build_ext --inplace
3837

3938

4039
debug: check-env clean
41-
echo "DEF DEBUG = 1" > uvloop/__debug.pxi
42-
$(PYTHON) -m cython -3 -a -p uvloop/loop.pyx; rm uvloop/__debug.*
40+
$(PYTHON) -m cython -3 -a -p uvloop/loop.pyx
4341
@echo "$$UVLOOP_BUILD_PATCH_SCRIPT" | $(PYTHON)
44-
$(PYTHON) setup.py build_ext --inplace
42+
$(PYTHON) setup.py build_ext --inplace \
43+
--define UVLOOP_DEBUG,CYTHON_TRACE,CYTHON_TRACE_NOGIL
4544

4645

4746
docs: compile

uvloop/cbhandles.pyx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ cdef class Handle:
88

99
cdef inline _set_loop(self, Loop loop):
1010
self.loop = loop
11-
IF DEBUG:
11+
if UVLOOP_DEBUG:
1212
loop._debug_cb_handles_total += 1
1313
loop._debug_cb_handles_count += 1
1414
if loop._debug:
1515
self._source_traceback = tb_extract_stack(sys_getframe(0))
1616

17-
IF DEBUG:
18-
def __dealloc__(self):
19-
if self.loop is not None:
20-
self.loop._debug_cb_handles_count -= 1
21-
else:
22-
raise RuntimeError('Handle.loop is None in Handle.__dealloc__')
17+
def __dealloc__(self):
18+
if UVLOOP_DEBUG and self.loop is not None:
19+
self.loop._debug_cb_handles_count -= 1
20+
if self.loop is None:
21+
raise RuntimeError('Handle.loop is None in Handle.__dealloc__')
2322

2423
def __init__(self):
2524
raise TypeError(
@@ -144,15 +143,15 @@ cdef class TimerHandle:
144143
# Only add to loop._timers when `self.timer` is successfully created
145144
loop._timers.add(self)
146145

147-
IF DEBUG:
146+
if UVLOOP_DEBUG:
148147
self.loop._debug_cb_timer_handles_total += 1
149148
self.loop._debug_cb_timer_handles_count += 1
150149

151-
IF DEBUG:
152-
def __dealloc__(self):
150+
def __dealloc__(self):
151+
if UVLOOP_DEBUG:
153152
self.loop._debug_cb_timer_handles_count -= 1
154-
if self.closed == 0:
155-
raise RuntimeError('active TimerHandle is deallacating')
153+
if self.closed == 0:
154+
raise RuntimeError('active TimerHandle is deallacating')
156155

157156
cdef _cancel(self):
158157
if self.closed == 1:

uvloop/handles/handle.pyx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ cdef class UVHandle:
2525
self.__class__.__name__))
2626

2727
def __dealloc__(self):
28-
IF DEBUG:
28+
if UVLOOP_DEBUG:
2929
if self._loop is not None:
3030
self._loop._debug_handles_current.subtract([
3131
self.__class__.__name__])
@@ -71,7 +71,7 @@ cdef class UVHandle:
7171
if self._handle == NULL:
7272
return
7373

74-
IF DEBUG:
74+
if UVLOOP_DEBUG:
7575
self._loop._debug_uv_handles_freed += 1
7676

7777
PyMem_RawFree(self._handle)
@@ -91,7 +91,7 @@ cdef class UVHandle:
9191
if self._handle is not NULL:
9292
self._free()
9393

94-
IF DEBUG:
94+
if UVLOOP_DEBUG:
9595
name = self.__class__.__name__
9696
if self._inited:
9797
raise RuntimeError(
@@ -107,11 +107,11 @@ cdef class UVHandle:
107107
self._handle.data = <void*>self
108108
if self._loop._debug:
109109
self._source_traceback = tb_extract_stack(sys_getframe(0))
110-
IF DEBUG:
110+
if UVLOOP_DEBUG:
111111
self._loop._debug_uv_handles_total += 1
112112

113113
cdef inline _start_init(self, Loop loop):
114-
IF DEBUG:
114+
if UVLOOP_DEBUG:
115115
if self._loop is not None:
116116
raise RuntimeError(
117117
'{}._start_init can only be called once'.format(
@@ -126,7 +126,7 @@ cdef class UVHandle:
126126
cdef inline bint _is_alive(self):
127127
cdef bint res
128128
res = self._closed != 1 and self._inited == 1
129-
IF DEBUG:
129+
if UVLOOP_DEBUG:
130130
if res:
131131
name = self.__class__.__name__
132132
if self._handle is NULL:
@@ -181,7 +181,7 @@ cdef class UVHandle:
181181
if self._handle is NULL:
182182
return
183183

184-
IF DEBUG:
184+
if UVLOOP_DEBUG:
185185
if self._handle.data is NULL:
186186
raise RuntimeError(
187187
'{}._close: _handle.data is NULL'.format(
@@ -239,7 +239,7 @@ cdef class UVSocketHandle(UVHandle):
239239
return None
240240

241241
self.__cached_socket = self._new_socket()
242-
IF DEBUG:
242+
if UVLOOP_DEBUG:
243243
# We don't "dup" for the "__cached_socket".
244244
assert self.__cached_socket.fileno() == self._fileno()
245245
return self.__cached_socket
@@ -280,7 +280,7 @@ cdef inline bint __ensure_handle_data(uv.uv_handle_t* handle,
280280

281281
cdef Loop loop
282282

283-
IF DEBUG:
283+
if UVLOOP_DEBUG:
284284
if handle.loop is NULL:
285285
raise RuntimeError(
286286
'handle.loop is NULL in __ensure_handle_data')
@@ -316,7 +316,7 @@ cdef void __uv_close_handle_cb(uv.uv_handle_t* handle) with gil:
316316
# The original UVHandle is long dead. Just free the mem of
317317
# the uv_handle_t* handler.
318318

319-
IF DEBUG:
319+
if UVLOOP_DEBUG:
320320
if handle.loop == NULL or handle.loop.data == NULL:
321321
raise RuntimeError(
322322
'__uv_close_handle_cb: handle.loop is invalid')
@@ -325,7 +325,7 @@ cdef void __uv_close_handle_cb(uv.uv_handle_t* handle) with gil:
325325
PyMem_RawFree(handle)
326326
else:
327327
h = <UVHandle>handle.data
328-
IF DEBUG:
328+
if UVLOOP_DEBUG:
329329
h._loop._debug_handles_closed.update([
330330
h.__class__.__name__])
331331
h._free()

uvloop/handles/poll.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ cdef void __on_uvpoll_event(uv.uv_poll_t* handle,
180180
poll.reading_handle is not None):
181181

182182
try:
183-
IF DEBUG:
183+
if UVLOOP_DEBUG:
184184
poll._loop._poll_read_events_total += 1
185185
poll.reading_handle._run()
186186
except BaseException as ex:
187-
IF DEBUG:
187+
if UVLOOP_DEBUG:
188188
poll._loop._poll_read_cb_errors_total += 1
189189
poll._error(ex, False)
190190
# continue code execution
@@ -193,10 +193,10 @@ cdef void __on_uvpoll_event(uv.uv_poll_t* handle,
193193
poll.writing_handle is not None):
194194

195195
try:
196-
IF DEBUG:
196+
if UVLOOP_DEBUG:
197197
poll._loop._poll_write_events_total += 1
198198
poll.writing_handle._run()
199199
except BaseException as ex:
200-
IF DEBUG:
200+
if UVLOOP_DEBUG:
201201
poll._loop._poll_write_cb_errors_total += 1
202202
poll._error(ex, False)

uvloop/handles/process.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ cdef class UVProcess(UVHandle):
194194

195195
char **ret
196196

197-
IF DEBUG:
197+
if UVLOOP_DEBUG:
198198
assert arr_len > 0
199199

200200
ret = <char **>PyMem_RawMalloc((arr_len + 1) * sizeof(char *))
@@ -634,7 +634,7 @@ cdef class UVProcessTransport(UVProcess):
634634
class WriteSubprocessPipeProto(aio_BaseProtocol):
635635

636636
def __init__(self, proc, fd):
637-
IF DEBUG:
637+
if UVLOOP_DEBUG:
638638
if type(proc) is not UVProcessTransport:
639639
raise TypeError
640640
if not isinstance(fd, int):

uvloop/handles/stream.pyx

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cdef class _StreamWriteContext:
3131
if self.uv_bufs is not NULL:
3232
PyMem_RawFree(self.uv_bufs)
3333
self.uv_bufs = NULL
34-
IF DEBUG:
34+
if UVLOOP_DEBUG:
3535
if self.py_bufs_sml_inuse:
3636
raise RuntimeError(
3737
'_StreamWriteContext.close: uv_bufs != NULL and '
@@ -42,7 +42,7 @@ cdef class _StreamWriteContext:
4242
PyBuffer_Release(&self.py_bufs[i])
4343
PyMem_RawFree(self.py_bufs)
4444
self.py_bufs = NULL
45-
IF DEBUG:
45+
if UVLOOP_DEBUG:
4646
if self.py_bufs_sml_inuse:
4747
raise RuntimeError(
4848
'_StreamWriteContext.close: py_bufs != NULL and '
@@ -87,7 +87,7 @@ cdef class _StreamWriteContext:
8787
else:
8888
sent -= self.uv_bufs_start[idx].len
8989

90-
IF DEBUG:
90+
if UVLOOP_DEBUG:
9191
if sent < 0:
9292
raise RuntimeError('fatal: sent < 0 in advance_uv_buf')
9393

@@ -123,7 +123,7 @@ cdef class _StreamWriteContext:
123123

124124
else:
125125
for buf in buffers:
126-
IF DEBUG:
126+
if UVLOOP_DEBUG:
127127
if not isinstance(buf, (bytes, bytearray, memoryview)):
128128
raise RuntimeError(
129129
'invalid data in writebuf: an instance of '
@@ -180,7 +180,7 @@ cdef class _StreamWriteContext:
180180
ctx.py_bufs_len = py_bufs_len
181181
ctx.req.data = <void*> ctx
182182

183-
IF DEBUG:
183+
if UVLOOP_DEBUG:
184184
stream._loop._debug_stream_write_ctx_total += 1
185185
stream._loop._debug_stream_write_ctx_cnt += 1
186186

@@ -191,15 +191,14 @@ cdef class _StreamWriteContext:
191191
ctx.closed = 0
192192
return ctx
193193

194-
IF DEBUG:
195-
def __dealloc__(self):
194+
def __dealloc__(self):
195+
if not self.closed:
196196
# Because we do an INCREF in _StreamWriteContext.new,
197-
# __dealloc__ shouldn't ever happen with `self.closed == 0`
198-
199-
if not self.closed:
200-
raise RuntimeError(
201-
'open _StreamWriteContext is being deallocated')
197+
# __dealloc__ shouldn't ever happen with `self.closed == 1`
198+
raise RuntimeError(
199+
'open _StreamWriteContext is being deallocated')
202200

201+
if UVLOOP_DEBUG:
203202
if self.stream is not None:
204203
self.stream._loop._debug_stream_write_ctx_cnt -= 1
205204
self.stream = None
@@ -360,7 +359,7 @@ cdef class UVStream(UVBaseTransport):
360359
self._fatal_error(exc, True)
361360
return
362361

363-
IF DEBUG:
362+
if UVLOOP_DEBUG:
364363
self._loop._debug_stream_write_tries += 1
365364

366365
if <size_t>written == blen:
@@ -392,7 +391,7 @@ cdef class UVStream(UVBaseTransport):
392391
# Then:
393392
# - Try to write all buffered data right now.
394393
all_sent = self._exec_write()
395-
IF DEBUG:
394+
if UVLOOP_DEBUG:
396395
if self._buffer_size != 0 or self._buffer != []:
397396
raise RuntimeError(
398397
'_buffer_size is not 0 after a successful _exec_write')
@@ -454,7 +453,7 @@ cdef class UVStream(UVBaseTransport):
454453
return True
455454

456455
if sent > 0:
457-
IF DEBUG:
456+
if UVLOOP_DEBUG:
458457
if sent == len(data):
459458
raise RuntimeError(
460459
'_try_write sent all data and returned '
@@ -704,7 +703,7 @@ cdef void __uv_stream_on_shutdown(uv.uv_shutdown_t* req,
704703
# v0.11. A possible reason for leaving it unchanged is that it
705704
# informs the callee that the handle has been destroyed.
706705

707-
IF DEBUG:
706+
if UVLOOP_DEBUG:
708707
stream._loop._debug_stream_shutdown_errors_total += 1
709708

710709
exc = convert_error(status)
@@ -736,13 +735,13 @@ cdef inline void __uv_stream_on_read_impl(uv.uv_stream_t* stream,
736735
# when an error happens by calling uv_read_stop() or uv_close().
737736
# Trying to read from the stream again is undefined.
738737
try:
739-
IF DEBUG:
738+
if UVLOOP_DEBUG:
740739
loop._debug_stream_read_eof_total += 1
741740

742741
sc._stop_reading()
743742
sc._on_eof()
744743
except BaseException as ex:
745-
IF DEBUG:
744+
if UVLOOP_DEBUG:
746745
loop._debug_stream_read_eof_cb_errors_total += 1
747746

748747
sc._error(ex, False)
@@ -765,7 +764,7 @@ cdef inline void __uv_stream_on_read_impl(uv.uv_stream_t* stream,
765764
# doesn't raise exceptions unless uvloop is built with DEBUG=1,
766765
# we don't need try...finally here.
767766

768-
IF DEBUG:
767+
if UVLOOP_DEBUG:
769768
loop._debug_stream_read_errors_total += 1
770769

771770
if sc.__read_error_close:
@@ -780,12 +779,12 @@ cdef inline void __uv_stream_on_read_impl(uv.uv_stream_t* stream,
780779
return
781780

782781
try:
783-
IF DEBUG:
782+
if UVLOOP_DEBUG:
784783
loop._debug_stream_read_cb_total += 1
785784

786785
sc._on_read(loop._recv_buffer[:nread])
787786
except BaseException as exc:
788-
IF DEBUG:
787+
if UVLOOP_DEBUG:
789788
loop._debug_stream_read_cb_errors_total += 1
790789

791790
sc._error(exc, False)
@@ -805,7 +804,7 @@ cdef inline void __uv_stream_on_write_impl(uv.uv_write_t* req, int status):
805804
return
806805

807806
if status < 0:
808-
IF DEBUG:
807+
if UVLOOP_DEBUG:
809808
stream._loop._debug_stream_write_errors_total += 1
810809

811810
exc = convert_error(status)
@@ -816,7 +815,7 @@ cdef inline void __uv_stream_on_write_impl(uv.uv_write_t* req, int status):
816815
try:
817816
stream._on_write()
818817
except BaseException as exc:
819-
IF DEBUG:
818+
if UVLOOP_DEBUG:
820819
stream._loop._debug_stream_write_cb_errors_total += 1
821820

822821
stream._error(exc, False)
@@ -839,7 +838,7 @@ cdef void __uv_stream_on_read(uv.uv_stream_t* stream,
839838

840839
cdef void __uv_stream_on_write(uv.uv_write_t* req, int status) with gil:
841840

842-
IF DEBUG:
841+
if UVLOOP_DEBUG:
843842
if req.data is NULL:
844843
aio_logger.error(
845844
'UVStream.write callback called with NULL req.data, status=%r',

uvloop/handles/streamserver.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ cdef void __uv_streamserver_on_listen(uv.uv_stream_t* handle,
114114
UVStreamServer stream = <UVStreamServer> handle.data
115115

116116
if status < 0:
117-
IF DEBUG:
117+
if UVLOOP_DEBUG:
118118
stream._loop._debug_stream_listen_errors_total += 1
119119

120120
exc = convert_error(status)

0 commit comments

Comments
 (0)