From fe35c785e02b749162561f1d03a2689ffae75945 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Fri, 7 Aug 2015 13:11:23 +0200 Subject: [PATCH] gopy: attach the c-extention module as Change-Id: I07cf1b818bc5acd80f01a603ebc19c62918df6bb --- gopy.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/gopy.py b/gopy.py index 7dc94ea..c254bfe 100644 --- a/gopy.py +++ b/gopy.py @@ -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