Hello.
I'm currently reworking the existing software embedding Python (v2.5 for now) into Objective Caml.
You can already do some trivial things with it:
yziquel@seldon:~$ ocaml Objective Caml version 3.11.1
# #use "topfind";;
: unit = () Findlib has been successfully loaded. Additional directives: #require "package";; to load a package #list;; to list the available packages #camlp4o;; to load camlp4 (standard syntax) #camlp4r;; to load camlp4 (revised syntax) #predicates "p,q,...";; to set these predicates Topfind.reset();; to force that packages will be reloaded #thread;; to enable threads
: unit = ()
#require "python.interpreter";;
/usr/lib/ocaml/python: added to search path /usr/lib/ocaml/python/python.cma: loaded /usr/lib/ocaml/python/oCamlPython.cmo: loaded # let q = Python.Module.import "dolfin";; val q : Python.obj = <abstr> # let w = Python.Module.import "modulethatdoesnotexist";; Exception: Python.Exception.Python_failure <abstr>. #
So it catches exceptions when trying to import a module that does not exist, and it succeeds when importing dolfin, a module that does exist on my box.
But then I'd like to inspect the contet of this module. So, here's my question: How do you access the content of this module, or the content of the python object, preferably with the C API.
All apologies if these questions seem trivial to you, but I'm quite new to python and its internals. Pointers to relevant (tutorial?) documentation are appreciated.
All the best,
-- Guillaume Yziquel http://yziquel.homelinux.org/
Guillaume Yziquel wrote:
Hello.
I'm currently reworking the existing software embedding Python (v2.5 for now) into Objective Caml.
You can already do some trivial things with it:
yziquel@seldon:~$ ocaml Objective Caml version 3.11.1
# #use "topfind";;
: unit = () Findlib has been successfully loaded. Additional directives: #require "package";; to load a package #list;; to list the available packages #camlp4o;; to load camlp4 (standard syntax) #camlp4r;; to load camlp4 (revised syntax) #predicates "p,q,...";; to set these predicates Topfind.reset();; to force that packages will be reloaded #thread;; to enable threads
: unit = ()
#require "python.interpreter";;
/usr/lib/ocaml/python: added to search path /usr/lib/ocaml/python/python.cma: loaded /usr/lib/ocaml/python/oCamlPython.cmo: loaded # let q = Python.Module.import "dolfin";; val q : Python.obj = <abstr> # let w = Python.Module.import "modulethatdoesnotexist";; Exception: Python.Exception.Python_failure <abstr>. #
So it catches exceptions when trying to import a module that does not exist, and it succeeds when importing dolfin, a module that does exist on my box.
But then I'd like to inspect the contet of this module. So, here's my question: How do you access the content of this module, or the content of the python object, preferably with the C API.
All apologies if these questions seem trivial to you, but I'm quite new to python and its internals. Pointers to relevant (tutorial?) documentation are appreciated.
Some pointers:
http://docs.python.org/c-api/module.html#PyModule_GetDict http://docs.python.org/c-api/dict.html
The C-API documentation should help you dig further into Python objects:
http://docs.python.org/c-api/index.html
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Feb 14 2010)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
M.-A. Lemburg a écrit :
All apologies if these questions seem trivial to you, but I'm quite new to python and its internals. Pointers to relevant (tutorial?) documentation are appreciated.
Some pointers:
http://docs.python.org/c-api/module.html#PyModule_GetDict http://docs.python.org/c-api/dict.html
Thanks. Exactly what I was looking for.
The C-API documentation should help you dig further into Python objects:
I already knew about this C API documentation. But getting familiar quickly with such a documentation is not an easy task. Thank you for your help.
-- Guillaume Yziquel http://yziquel.homelinux.org/
M.-A. Lemburg a écrit :
Some pointers:
http://docs.python.org/c-api/module.html#PyModule_GetDict http://docs.python.org/c-api/dict.html
The C-API documentation should help you dig further into Python objects:
Here's a question on which I find little information on the web. About Py_ssize_t.
First, some quick background about OCaml values: OCaml values are essentially of two sorts. There are integers, or integer-like OCaml values. And pointers of blocks in the OCaml heap. Both live in the same 32-bit or 64-bit type, called "value".
Blocks in the OCaml heap are aligned, and the lowest 2 or 3 bits of a pointer are therefore always nulled. Taking advantage of this, an OCaml integer always ends with a lowest bit set to 1. To get the real / semantic integer, you just do a >> 1. And therefore OCaml ints are stored as 31 bits or 63 bits, depending on your architecture.
The lowest bit thingy is used for OCaml's garbage collector, in order to know whether or not it should follow the value/pointer when doing its job traversing the OCaml heap.
So my question is the following: is there any alignment voodoo in Python that would imply that Py_ssize_t values have their lowest bit cleared?
If yes, I could map it to an integral OCaml type by adding 1. (The GC would not follow the value/pointer). If not, I have no other choice but to box the Py_ssize_t value.
Thoughts welcome.
-- Guillaume Yziquel http://yziquel.homelinux.org/
Guillaume Yziquel wrote:
M.-A. Lemburg a écrit :
Some pointers:
http://docs.python.org/c-api/module.html#PyModule_GetDict http://docs.python.org/c-api/dict.html
The C-API documentation should help you dig further into Python objects:
Here's a question on which I find little information on the web. About Py_ssize_t.
First, some quick background about OCaml values: OCaml values are essentially of two sorts. There are integers, or integer-like OCaml values. And pointers of blocks in the OCaml heap. Both live in the same 32-bit or 64-bit type, called "value".
Blocks in the OCaml heap are aligned, and the lowest 2 or 3 bits of a pointer are therefore always nulled. Taking advantage of this, an OCaml integer always ends with a lowest bit set to 1. To get the real / semantic integer, you just do a >> 1. And therefore OCaml ints are stored as 31 bits or 63 bits, depending on your architecture.
The lowest bit thingy is used for OCaml's garbage collector, in order to know whether or not it should follow the value/pointer when doing its job traversing the OCaml heap.
So my question is the following: is there any alignment voodoo in Python that would imply that Py_ssize_t values have their lowest bit cleared?
No, Py_ssize_t values can use the full (signed) range provided by the underlying type. They are used for positive-only values such as length information as well as positive and negative values such as indexes. In some cases -1 is used to flag an error for APIs that normally only return non-negative values.
If yes, I could map it to an integral OCaml type by adding 1. (The GC would not follow the value/pointer). If not, I have no other choice but to box the Py_ssize_t value.
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Feb 15 2010)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
participants (2)
-
Guillaume Yziquel
-
M.-A. Lemburg