gopy: attach the c-extention module as <go-pkg-importpath>

Change-Id: I07cf1b818bc5acd80f01a603ebc19c62918df6bb
This commit is contained in:
Sebastien Binet 2015-08-07 13:11:23 +02:00
parent c8b2a0576b
commit fe35c785e0
1 changed files with 16 additions and 10 deletions

26
gopy.py
View File

@ -2,6 +2,9 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
### py2/py3 compat
from __future__ import print_function
__doc__ = """gopy is a convenience module to wrap and bind a Go package"""
__author__ = "The go-python authors"
@ -14,32 +17,35 @@ import imp
import os
import sys
def printf(args):
sys.stdout.write(args)
pass
def load(pkg, output=""):
"""
`load` takes a fully qualified Go package name and runs `gopy bind` on it.
@returns the C-extension module object
"""
printf("::: gopy.loading '%s'...\n" % pkg)
from subprocess import check_call
from subprocess import check_call, check_output
if output == "":
output = os.getcwd()
pass
print("gopy> inferring package name...")
pkg = check_output(["go", "list", pkg]).strip()
print("gopy> loading '%s'..." % pkg)
check_call(["gopy","bind", "-output=%s" % output, pkg])
n = os.path.basename(pkg)
printf("::: gopy.module=gopy.%s\n" % n)
print("gopy> importing '%s'" % (pkg,))
ok = imp.find_module(n, [output])
if not ok:
raise RuntimeError("could not find module 'gopy.%s'" % n)
raise RuntimeError("could not find module '%s'" % pkg)
fname, path, descr = ok
return imp.load_module("gopy."+n, fname, path, descr)
mod = imp.load_module('__gopy__.'+n, fname, path, descr)
mod.__name__ = pkg
sys.modules[pkg] = mod
del sys.modules['__gopy__.'+n]
return mod