
Hello.
I'd like to do some ML-like type inference for Python code. I'm not trying to resurrect the static typing vs. dynamic typing debate here. It's simply that I'd need to get static typing for Python for code-generation reasons.
Therefore, I'd like to ask: Does the Python C API support a way to inspect the bytecode or the source code of a given piece of Python code? What approach would be promising enough to type things in ML-way (I'm unfortunately not interested in cartesian-product type inference), and what help may the C API provide to meet this goal? (Maybe I haven't looked far enough, but I could not discern clearly what could be useful...)
(Sorry for posting in non-text-mode. Using a webmail because port 25 policies in this bloody country ar sooooo restrictive.)
Thanks.
Issyk Kol.

Hi,
The C API does not provide any code-inspection functionality like that directly, but you can access the byte-code objects of a python-function (the "func_code" attribute) and use the "inspect" module from the Python library.
If I understand you correctly, I don't think it will help you much to inspect the byte-code. It is nearly impossible in general to conclude, say, the return types of a function from the argument list given the python code for the function. I doubt that there is a feasible way to make type inference analysis on Python in a robust way since types are a bit vague in Python.
-- Ulf Worsøe
On Fri, May 21, 2010 at 7:47 PM, Issyk Kol <issyk.kol@gmx.com> wrote:

Issyk Kol, 21.05.2010 19:47:
I think your best bet is to look at an alternative implementation of Python, such as PyPy, Jython, IronPython or Cython. CPython doesn't do any type inference, but at least PyPy and Cython infer types to a certain extent, and I would expect the others to do it, too.
There's also ShedSkin which is supposed to have a pretty good type inferer for (static) Python-like code. Might be enough for your purpose.
In any case, the C-API won't help you here, so this is the wrong forum to discuss this.
Stefan

Hi,
The C API does not provide any code-inspection functionality like that directly, but you can access the byte-code objects of a python-function (the "func_code" attribute) and use the "inspect" module from the Python library.
If I understand you correctly, I don't think it will help you much to inspect the byte-code. It is nearly impossible in general to conclude, say, the return types of a function from the argument list given the python code for the function. I doubt that there is a feasible way to make type inference analysis on Python in a robust way since types are a bit vague in Python.
-- Ulf Worsøe
On Fri, May 21, 2010 at 7:47 PM, Issyk Kol <issyk.kol@gmx.com> wrote:

Issyk Kol, 21.05.2010 19:47:
I think your best bet is to look at an alternative implementation of Python, such as PyPy, Jython, IronPython or Cython. CPython doesn't do any type inference, but at least PyPy and Cython infer types to a certain extent, and I would expect the others to do it, too.
There's also ShedSkin which is supposed to have a pretty good type inferer for (static) Python-like code. Might be enough for your purpose.
In any case, the C-API won't help you here, so this is the wrong forum to discuss this.
Stefan
participants (3)
-
Issyk Kol
-
Stefan Behnel
-
Ulf Worsoe