| 1 |
from rpython.rlib import rutf8, jit |
| 2 |
|
| 3 |
from pypy.interpreter.gateway import unwrap_spec, WrappedDefault |
| 4 |
from pypy.interpreter.error import OperationError, oefmt |
| 5 |
|
| 6 |
def create_filter(space, w_category, action, modname): |
| 7 |
w_modname = space.newtext(modname) if modname is not None else space.w_None |
| 8 |
return space.newtuple([ |
| 9 |
space.newtext(action), space.w_None, w_category, |
| 10 |
w_modname, space.newint(0)]) |
| 11 |
|
| 12 |
class State: |
| 13 |
def __init__(self, space): |
| 14 |
self.init_filters(space) |
| 15 |
self.w_once_registry = space.newdict() |
| 16 |
self.w_default_action = space.newtext("default") |
| 17 |
self.filters_mutated(space) |
| 18 |
|
| 19 |
def filters_mutated(self, space): |
| 20 |
self.w_filters_version = space.call_function(space.w_object) |
| 21 |
|
| 22 |
def init_filters(self, space): |
| 23 |
filters_w = [] |
| 24 |
|
| 25 |
|
| 26 |
filters_w.append(create_filter( |
| 27 |
space, space.w_DeprecationWarning, "default", "__main__")) |
| 28 |
filters_w.append(create_filter( |
| 29 |
space, space.w_DeprecationWarning, "ignore", None)) |
| 30 |
filters_w.append(create_filter( |
| 31 |
space, space.w_PendingDeprecationWarning, "ignore", None)) |
| 32 |
filters_w.append(create_filter( |
| 33 |
space, space.w_ImportWarning, "ignore", None)) |
| 34 |
filters_w.append(create_filter( |
| 35 |
space, space.w_ResourceWarning, "ignore", None)) |
| 36 |
|
| 37 |
self.w_filters = space.newlist(filters_w) |
| 38 |
|
| 39 |
def get_warnings_attr(space, name): |
| 40 |
try: |
| 41 |
w_module = space.getitem(space.sys.get('modules'), |
| 42 |
space.newtext('warnings')) |
| 43 |
except OperationError as e: |
| 44 |
if not e.match(space, space.w_KeyError): |
| 45 |
raise |
| 46 |
return None |
| 47 |
|
| 48 |
try: |
| 49 |
return space.getattr(w_module, space.newtext(name)) |
| 50 |
except OperationError as e: |
| 51 |
if not e.match(space, space.w_AttributeError): |
| 52 |
raise |
| 53 |
return None |
| 54 |
|
| 55 |
def get_category(space, w_message, w_category): |
| 56 |
|
| 57 |
if space.isinstance_w(w_message, space.w_Warning): |
| 58 |
w_category = space.type(w_message) |
| 59 |
elif space.is_none(w_category): |
| 60 |
w_category = space.w_UserWarning |
| 61 |
|
| 62 |
|
| 63 |
try: |
| 64 |
if not space.abstract_issubclass_w(w_category, space.w_Warning): |
| 65 |
raise oefmt(space.w_TypeError, |
| 66 |
"category is not a subclass of Warning") |
| 67 |
except OperationError as e: |
| 68 |
if e.async(space): |
| 69 |
raise |
| 70 |
raise oefmt(space.w_TypeError, |
| 71 |
"category must be a Warning subclass, not '%T'", |
| 72 |
w_category) |
| 73 |
|
| 74 |
return w_category |
| 75 |
|
| 76 |
def is_internal_frame(space, frame): |
| 77 |
if frame is None: |
| 78 |
return False |
| 79 |
code = frame.getcode() |
| 80 |
if code is None or code.co_filename is None: |
| 81 |
return False |
| 82 |
|
| 83 |
|
| 84 |
return "importlib" in code.co_filename and "_bootstrap" in code.co_filename |
| 85 |
|
| 86 |
@jit.unroll_safe |
| 87 |
def next_external_frame(space, frame): |
| 88 |
ec = space.getexecutioncontext() |
| 89 |
while True: |
| 90 |
frame = ec.getnextframe_nohidden(frame) |
| 91 |
if frame is None or not is_internal_frame(space, frame): |
| 92 |
return frame |
| 93 |
|
| 94 |
@jit.look_inside_iff(lambda space, stacklevel: jit.isconstant(stacklevel)) |
| 95 |
def _get_frame(space, stacklevel): |
| 96 |
ec = space.getexecutioncontext() |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
frame = ec.gettopframe_nohidden() |
| 101 |
if stacklevel <= 0 or is_internal_frame(space, frame): |
| 102 |
while stacklevel > 1 and frame: |
| 103 |
frame = ec.getnextframe_nohidden(frame) |
| 104 |
stacklevel -= 1 |
| 105 |
else: |
| 106 |
while stacklevel > 1 and frame: |
| 107 |
frame = next_external_frame(space, frame) |
| 108 |
stacklevel -= 1 |
| 109 |
return frame |
| 110 |
|
| 111 |
def setup_context(space, stacklevel): |
| 112 |
|
| 113 |
frame = _get_frame(space, stacklevel) |
| 114 |
|
| 115 |
if frame: |
| 116 |
w_globals = frame.get_w_globals() |
| 117 |
w_filename = space.newtext(frame.pycode.co_filename) |
| 118 |
lineno = frame.get_last_lineno() |
| 119 |
else: |
| 120 |
w_globals = space.sys.w_dict |
| 121 |
w_filename = space.newtext("sys") |
| 122 |
lineno = 1 |
| 123 |
|
| 124 |
|
| 125 |
try: |
| 126 |
w_registry = space.getitem(w_globals, space.newtext("__warningregistry__")) |
| 127 |
except OperationError as e: |
| 128 |
if not e.match(space, space.w_KeyError): |
| 129 |
raise |
| 130 |
w_registry = space.newdict() |
| 131 |
space.setitem(w_globals, space.newtext("__warningregistry__"), w_registry) |
| 132 |
|
| 133 |
|
| 134 |
try: |
| 135 |
w_module = space.getitem(w_globals, space.newtext("__name__")) |
| 136 |
except OperationError as e: |
| 137 |
if not e.match(space, space.w_KeyError): |
| 138 |
raise |
| 139 |
w_module = space.newtext("<string>") |
| 140 |
|
| 141 |
return (w_filename, lineno, w_module, w_registry) |
| 142 |
|
| 143 |
def check_matched(space, w_obj, w_arg): |
| 144 |
|
| 145 |
if space.is_w(w_obj, space.w_None): |
| 146 |
return True |
| 147 |
|
| 148 |
if space.is_w(space.type(w_obj), space.w_unicode): |
| 149 |
return space.eq_w(w_obj, w_arg) |
| 150 |
|
| 151 |
return space.is_true(space.call_method(w_obj, "match", w_arg)) |
| 152 |
|
| 153 |
def get_filter(space, w_category, w_text, lineno, w_module): |
| 154 |
w_filters = get_warnings_attr(space, "filters") |
| 155 |
if w_filters: |
| 156 |
space.fromcache(State).w_filters = w_filters |
| 157 |
else: |
| 158 |
w_filters = space.fromcache(State).w_filters |
| 159 |
|
| 160 |
|
| 161 |
for w_item in space.fixedview(w_filters): |
| 162 |
w_action, w_msg, w_cat, w_mod, w_lineno = space.fixedview( |
| 163 |
w_item, 5) |
| 164 |
ln = space.int_w(w_lineno) |
| 165 |
|
| 166 |
if (check_matched(space, w_msg, w_text) and |
| 167 |
check_matched(space, w_mod, w_module) and |
| 168 |
space.abstract_issubclass_w(w_category, w_cat) and |
| 169 |
(ln == 0 or ln == lineno)): |
| 170 |
return space.text_w(w_action), w_item |
| 171 |
|
| 172 |
action = get_default_action(space) |
| 173 |
if not action: |
| 174 |
raise oefmt(space.w_ValueError, "warnings.defaultaction not found") |
| 175 |
return action, None |
| 176 |
|
| 177 |
def get_default_action(space): |
| 178 |
w_action = get_warnings_attr(space, "defaultaction"); |
| 179 |
if w_action is None: |
| 180 |
return space.text_w(space.fromcache(State).w_default_action) |
| 181 |
|
| 182 |
space.fromcache(State).w_default_action = w_action |
| 183 |
return space.text_w(w_action) |
| 184 |
|
| 185 |
def get_once_registry(space): |
| 186 |
w_registry = get_warnings_attr(space, "onceregistry"); |
| 187 |
if w_registry is None: |
| 188 |
return space.fromcache(State).w_once_registry |
| 189 |
|
| 190 |
space.fromcache(State).w_once_registry = w_registry |
| 191 |
return w_registry |
| 192 |
|
| 193 |
def update_registry(space, w_registry, w_text, w_category): |
| 194 |
w_key = space.newtuple2(w_text, w_category) |
| 195 |
return already_warned(space, w_registry, w_key, should_set=True) |
| 196 |
|
| 197 |
def already_warned(space, w_registry, w_key, should_set=False): |
| 198 |
w_version_obj = space.finditem_str(w_registry, "version") |
| 199 |
state = space.fromcache(State) |
| 200 |
if w_version_obj is not state.w_filters_version: |
| 201 |
space.call_method(w_registry, "clear") |
| 202 |
space.setitem_str(w_registry, "version", state.w_filters_version) |
| 203 |
else: |
| 204 |
w_already_warned = space.finditem(w_registry, w_key) |
| 205 |
if w_already_warned is not None and space.is_true(w_already_warned): |
| 206 |
return True |
| 207 |
|
| 208 |
if should_set: |
| 209 |
space.setitem(w_registry, w_key, space.w_True) |
| 210 |
return False |
| 211 |
|
| 212 |
def normalize_module(space, w_filename): |
| 213 |
|
| 214 |
|
| 215 |
filename = space.fsencode_w(w_filename) |
| 216 |
if len(filename) == 0: |
| 217 |
return space.newtext("<unknown>") |
| 218 |
if filename.endswith(".py"): |
| 219 |
n = len(filename) - 3 |
| 220 |
assert n >= 0 |
| 221 |
filename = filename[:n] |
| 222 |
return space.newfilename(filename) |
| 223 |
return w_filename |
| 224 |
|
| 225 |
def show_warning(space, w_filename, lineno, w_text, w_category, |
| 226 |
w_sourceline=None): |
| 227 |
w_name = space.getattr(w_category, space.newtext("__name__")) |
| 228 |
w_stderr = space.sys.get("stderr") |
| 229 |
|
| 230 |
|
| 231 |
message = b"%s:%d: %s: %s\n" % (space.utf8_w(w_filename), lineno, |
| 232 |
space.utf8_w(w_name), |
| 233 |
space.utf8_w(w_text)) |
| 234 |
space.call_method(w_stderr, "write", space.newtext(message)) |
| 235 |
|
| 236 |
|
| 237 |
if not w_sourceline: |
| 238 |
try: |
| 239 |
|
| 240 |
w_builtins = space.getbuiltinmodule('builtins') |
| 241 |
w_linecachemodule = space.call_method(w_builtins, '__import__', |
| 242 |
space.newtext("linecache")) |
| 243 |
w_sourceline = space.call_method(w_linecachemodule, "getline", |
| 244 |
w_filename, space.newint(lineno)) |
| 245 |
w_sourceline = space.call_method(w_sourceline, "strip") |
| 246 |
except OperationError: |
| 247 |
w_sourceline = None |
| 248 |
|
| 249 |
if not w_sourceline: |
| 250 |
return |
| 251 |
line = space.utf8_w(w_sourceline) |
| 252 |
if not line: |
| 253 |
return |
| 254 |
|
| 255 |
message = "\n" |
| 256 |
for i in range(len(line)): |
| 257 |
c = line[i] |
| 258 |
if c not in ' \t\014': |
| 259 |
message = " %s\n" % (line[i:],) |
| 260 |
break |
| 261 |
space.call_method(w_stderr, "write", space.newtext(message)) |
| 262 |
|
| 263 |
def do_warn(space, w_message, w_category, stacklevel, w_source=None): |
| 264 |
context_w = setup_context(space, stacklevel) |
| 265 |
do_warn_explicit(space, w_category, w_message, context_w, w_source=w_source) |
| 266 |
|
| 267 |
def do_warn_explicit(space, w_category, w_message, context_w, |
| 268 |
w_sourceline=None, w_source=None): |
| 269 |
w_filename, lineno, w_module, w_registry = context_w |
| 270 |
|
| 271 |
|
| 272 |
if space.is_w(w_module, space.w_None): |
| 273 |
w_module = normalize_module(space, w_filename) |
| 274 |
|
| 275 |
|
| 276 |
if space.isinstance_w(w_message, space.w_Warning): |
| 277 |
w_text = space.str(w_message) |
| 278 |
w_category = space.type(w_message) |
| 279 |
elif (not space.isinstance_w(w_message, space.w_unicode) and |
| 280 |
not space.isinstance_w(w_message, space.w_bytes)): |
| 281 |
w_text = space.str(w_message) |
| 282 |
w_message = space.call_function(w_category, w_message) |
| 283 |
else: |
| 284 |
w_text = w_message |
| 285 |
w_message = space.call_function(w_category, w_message) |
| 286 |
|
| 287 |
w_lineno = space.newint(lineno) |
| 288 |
|
| 289 |
|
| 290 |
w_key = space.newtuple([w_text, w_category, w_lineno]) |
| 291 |
|
| 292 |
if not space.is_w(w_registry, space.w_None): |
| 293 |
if already_warned(space, w_registry, w_key): |
| 294 |
return |
| 295 |
|
| 296 |
|
| 297 |
action, w_item = get_filter(space, w_category, w_text, lineno, w_module) |
| 298 |
|
| 299 |
if action == "error": |
| 300 |
raise OperationError(w_category, w_message) |
| 301 |
|
| 302 |
if action == 'ignore': |
| 303 |
return |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
warned = False |
| 308 |
if action != 'always': |
| 309 |
if not space.is_w(w_registry, space.w_None): |
| 310 |
space.setitem(w_registry, w_key, space.w_True) |
| 311 |
|
| 312 |
if action == 'once': |
| 313 |
if space.is_w(w_registry, space.w_None): |
| 314 |
w_registry = get_once_registry(space) |
| 315 |
warned = update_registry(space, w_registry, w_text, w_category) |
| 316 |
elif action == 'module': |
| 317 |
if not space.is_w(w_registry, space.w_None): |
| 318 |
warned = update_registry(space, w_registry, w_text, w_category) |
| 319 |
elif action != 'default': |
| 320 |
try: |
| 321 |
err = space.text_w(space.str(w_item)) |
| 322 |
except OperationError: |
| 323 |
err = "???" |
| 324 |
raise oefmt(space.w_RuntimeError, |
| 325 |
"Unrecognized action (%s) in warnings.filters:\n %s", |
| 326 |
action, err) |
| 327 |
|
| 328 |
if warned: |
| 329 |
|
| 330 |
return |
| 331 |
|
| 332 |
w_show_fn = get_warnings_attr(space, "_showwarnmsg") |
| 333 |
if w_show_fn is None: |
| 334 |
show_warning(space, w_filename, lineno, w_text, w_category, |
| 335 |
w_sourceline) |
| 336 |
return |
| 337 |
|
| 338 |
if not space.is_true(space.callable(w_show_fn)): |
| 339 |
raise oefmt(space.w_TypeError, |
| 340 |
"warnings._showwarnmsg() must be set to a callable") |
| 341 |
w_message_cls = get_warnings_attr(space, "WarningMessage") |
| 342 |
if w_message_cls is None: |
| 343 |
raise oefmt(space.w_RuntimeError, |
| 344 |
"unable to get warnings.WarningMessage") |
| 345 |
w_source = w_source or space.w_None |
| 346 |
w_msg = space.call_function( |
| 347 |
w_message_cls, w_message, w_category, |
| 348 |
w_filename, w_lineno, space.w_None, space.w_None, w_source) |
| 349 |
space.call_function(w_show_fn, w_msg) |
| 350 |
|
| 351 |
|
| 352 |
@unwrap_spec(stacklevel=int) |
| 353 |
def warn(space, w_message, w_category=None, stacklevel=1, w_source=None): |
| 354 |
"Issue a warning, or maybe ignore it or raise an exception." |
| 355 |
w_category = get_category(space, w_message, w_category); |
| 356 |
do_warn(space, w_message, w_category, stacklevel, w_source) |
| 357 |
|
| 358 |
|
| 359 |
def get_source_line(space, w_globals, lineno): |
| 360 |
if space.is_none(w_globals): |
| 361 |
return None |
| 362 |
|
| 363 |
|
| 364 |
try: |
| 365 |
w_loader = space.getitem(w_globals, space.newtext("__loader__")) |
| 366 |
w_module_name = space.getitem(w_globals, space.newtext("__name__")) |
| 367 |
except OperationError as e: |
| 368 |
if not e.match(space, space.w_KeyError): |
| 369 |
raise |
| 370 |
return None |
| 371 |
|
| 372 |
|
| 373 |
try: |
| 374 |
w_get_source = space.getattr(w_loader, space.newtext("get_source")) |
| 375 |
except OperationError as e: |
| 376 |
if not e.match(space, space.w_AttributeError): |
| 377 |
raise |
| 378 |
return None |
| 379 |
|
| 380 |
|
| 381 |
w_source = space.call_function(w_get_source, w_module_name) |
| 382 |
if space.is_w(w_source, space.w_None): |
| 383 |
return None |
| 384 |
|
| 385 |
|
| 386 |
w_source_list = space.call_method(space.w_text, "splitlines", w_source) |
| 387 |
|
| 388 |
|
| 389 |
w_source_line = space.getitem(w_source_list, space.newint(lineno - 1)) |
| 390 |
return w_source_line |
| 391 |
|
| 392 |
@unwrap_spec(lineno=int, w_module = WrappedDefault(None), |
| 393 |
w_registry = WrappedDefault(None), |
| 394 |
w_module_globals = WrappedDefault(None)) |
| 395 |
def warn_explicit(space, w_message, w_category, w_filename, lineno, |
| 396 |
w_module=None, w_registry=None, w_module_globals=None, |
| 397 |
w_source=None): |
| 398 |
"Low-level inferface to warnings functionality." |
| 399 |
|
| 400 |
w_source_line = get_source_line(space, w_module_globals, lineno) |
| 401 |
|
| 402 |
do_warn_explicit(space, w_category, w_message, |
| 403 |
(w_filename, lineno, w_module, w_registry), |
| 404 |
w_source_line, w_source) |
| 405 |
|
| 406 |
def filters_mutated(space): |
| 407 |
space.fromcache(State).filters_mutated(space) |