this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "builtins.h"
3#include "frame.h"
4#include "handles.h"
5#include "modules.h"
6#include "runtime.h"
7#include "thread.h"
8#include "type-builtins.h"
9
10namespace py {
11
12namespace {
13RawObject getCategory(Thread* thread, const Object& message,
14 const Object& category) {
15 Runtime* runtime = thread->runtime();
16 HandleScope scope(thread);
17
18 Type result(&scope, runtime->typeOf(*message));
19 Type warning(&scope, runtime->typeAt(LayoutId::kWarning));
20 // TODO(bsimmers): Use our equivalent of PyObject_IsInstance once we have it.
21 if (!typeIsSubclass(*result, *warning)) {
22 if (category.isNoneType()) {
23 result = *warning;
24 } else if (runtime->isInstanceOfType(*category)) {
25 result = *category;
26 }
27 if (!typeIsSubclass(*result, *warning)) {
28 return thread->raiseWithFmt(LayoutId::kTypeError,
29 "category must be a Warning subclass");
30 }
31 }
32
33 return *result;
34}
35} // namespace
36
37RawObject FUNC(_warnings, warn)(Thread* thread, Arguments args) {
38 Runtime* runtime = thread->runtime();
39 HandleScope scope(thread);
40 Object message(&scope, args.get(0));
41 Object category(&scope, args.get(1));
42 Object stacklevel(&scope, args.get(2));
43
44 if (!runtime->isInstanceOfInt(*stacklevel)) {
45 return thread->raiseWithFmt(LayoutId::kTypeError,
46 "integer argument expected");
47 }
48 auto result = intUnderlying(*stacklevel).asInt<word>();
49 if (result.error != CastError::None) {
50 return thread->raiseWithFmt(LayoutId::kOverflowError,
51 "Python int too large to convert to C ssize_t");
52 }
53
54 Object real_category(&scope, getCategory(thread, message, category));
55 if (real_category.isError()) return *real_category;
56
57 // TODO(T39431178): Implement proper filtering/escalation.
58 return NoneType::object();
59}
60
61} // namespace py