Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
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
35 changes: 35 additions & 0 deletions 35 Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,41 @@ def method_example(self): ...
self.assertEqual(_testcapi.eval_get_func_name(sum), "sum") # c function
self.assertEqual(_testcapi.eval_get_func_name(A), "type")

def test_function_get_code(self):
import types

def some():
pass

code = _testcapi.function_get_code(some)
self.assertIsInstance(code, types.CodeType)
self.assertEqual(code, some.__code__)

with self.assertRaises(SystemError):
_testcapi.function_get_code(None) # not a function

def test_function_get_globals(self):
def some():
pass

globals_ = _testcapi.function_get_globals(some)
self.assertIsInstance(globals_, dict)
self.assertEqual(globals_, some.__globals__)

with self.assertRaises(SystemError):
_testcapi.function_get_globals(None) # not a function

def test_function_get_module(self):
def some():
pass

module = _testcapi.function_get_module(some)
self.assertIsInstance(module, str)
self.assertEqual(module, some.__module__)

with self.assertRaises(SystemError):
_testcapi.function_get_module(None) # not a function


class TestPendingCalls(unittest.TestCase):

Expand Down
39 changes: 39 additions & 0 deletions 39 Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5658,6 +5658,42 @@ test_macros(PyObject *self, PyObject *Py_UNUSED(args))
Py_RETURN_NONE;
}

static PyObject *
function_get_code(PyObject *self, PyObject *func)
{
PyObject *code = PyFunction_GetCode(func);
if (code != NULL) {
Py_INCREF(code);

@sobolevn sobolevn Oct 10, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this Py_INCREF be here? Shouldn't callee do this? 🤔

Without it I have this error:

test_function_get_module (test.test_capi.CAPITest.test_function_get_module) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

== Tests result: SUCCESS ==

1 test OK.

Total duration: 242 ms
Tests result: SUCCESS
./Include/object.h:625: _Py_NegativeRefcount: Assertion failed: object has negative ref count
<object at 0x1082424d0 is freed>
Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
Python runtime state: finalizing (tstate=0x000000010759c6d0)

Current thread 0x000000011215d5c0 (most recent call first):
  <no Python frame>
[1]    92538 abort      ./python.exe -m test -v test_capi -m test_function_get_module

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cc: @markshannon as it pertains to code objects.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is correct, PyFunction_GetCode is documented as returning a borrowed ref: https://docs.python.org/3/c-api/function.html#c.PyFunction_GetCode

return code;
} else {
return NULL;
}
}

static PyObject *
function_get_globals(PyObject *self, PyObject *func)
{
PyObject *globals = PyFunction_GetGlobals(func);
if (globals != NULL) {
Py_INCREF(globals);
return globals;
} else {
return NULL;
}
}

static PyObject *
function_get_module(PyObject *self, PyObject *func)
{
PyObject *module = PyFunction_GetModule(func);
if (module != NULL) {
Py_INCREF(module);
return module;
} else {
return NULL;
}
}


static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
Expand Down Expand Up @@ -5942,6 +5978,9 @@ static PyMethodDef TestMethods[] = {
{"watch_dict", watch_dict, METH_VARARGS, NULL},
{"unwatch_dict", unwatch_dict, METH_VARARGS, NULL},
{"get_dict_watcher_events", get_dict_watcher_events, METH_NOARGS, NULL},
{"function_get_code", function_get_code, METH_O, NULL},
{"function_get_globals", function_get_globals, METH_O, NULL},
{"function_get_module", function_get_module, METH_O, NULL},
{NULL, NULL} /* sentinel */
};

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.