1

If I want to get a PyObject for something like, say, sys.exc_info, I can write

PyObject *sys_module = PyImport_Import("sys");
PyObject *sys_exc_info = PyObject_GetAttr(sys_module, "exc_info");

What if I want to get a PyObject corresponding to a builtin such as list() or dir()? I've tried writing PyImport_Import("builtins") and PyImport_AddModule("builtins") and looking in the dictionary for that module, but it doesn't seem to be working as expected.

Zachary Turner
  • 738
  • 4
  • 24
  • Have you tried PyObject* PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals) ? https://docs.python.org/2/c-api/veryhigh.html#c.PyRun_String – Finch_Powers Nov 10 '15 at 22:27
  • Are you on Python 2 or Python 3? `builtins` is called `__builtin__` in Python 2. And if it's not working as expected, what actually happens? – user2357112 Nov 10 '15 at 22:35
  • @user2357112 if you respond as an answer i'll accept it, that was the problem. I'm using both Python 2 and Python 3 (i.e. I need to be cross-compatible), but I was using `builtins` instead of choosing based on the version. Works with I use `__builtin__` – Zachary Turner Nov 10 '15 at 22:39

1 Answers1

1

builtins is called __builtin__ in Python 2.

user2357112
  • 260,549
  • 28
  • 431
  • 505