Commit 0177b8bd authored by Stefan Behnel's avatar Stefan Behnel Committed by GitHub

Simplify the utility code loading by requiring the source file to be named...

Simplify the utility code loading by requiring the source file to be named explicitly. It was almost always passed anyway, so having a non-trivial search algorithm in place for a rare case of unnecessary laziness is just code bloat. (GH-3280)
parent c03afec7
...@@ -324,39 +324,14 @@ class UtilityCodeBase(object): ...@@ -324,39 +324,14 @@ class UtilityCodeBase(object):
return utilities return utilities
@classmethod @classmethod
def load(cls, util_code_name, from_file=None, **kwargs): def load(cls, util_code_name, from_file, **kwargs):
""" """
Load utility code from a file specified by from_file (relative to Load utility code from a file specified by from_file (relative to
Cython/Utility) and name util_code_name. If from_file is not given, Cython/Utility) and name util_code_name.
load it from the file util_code_name.*. There should be only one
file matched by this pattern.
""" """
if '::' in util_code_name: if '::' in util_code_name:
from_file, util_code_name = util_code_name.rsplit('::', 1) from_file, util_code_name = util_code_name.rsplit('::', 1)
if not from_file: assert from_file
utility_dir = get_utility_dir()
prefix = util_code_name + '.'
try:
listing = os.listdir(utility_dir)
except OSError:
# XXX the code below assumes as 'zipimport.zipimporter' instance
# XXX should be easy to generalize, but too lazy right now to write it
import zipfile
global __loader__
loader = __loader__
archive = loader.archive
with closing(zipfile.ZipFile(archive)) as fileobj:
listing = [os.path.basename(name)
for name in fileobj.namelist()
if os.path.join(archive, name).startswith(utility_dir)]
files = [filename for filename in listing
if filename.startswith(prefix)]
if not files:
raise ValueError("No match found for utility code " + util_code_name)
if len(files) > 1:
raise ValueError("More than one filename match found for utility code " + util_code_name)
from_file = files[0]
utilities = cls.load_utilities_from_file(from_file) utilities = cls.load_utilities_from_file(from_file)
proto, impl, tags = utilities[util_code_name] proto, impl, tags = utilities[util_code_name]
...@@ -393,7 +368,7 @@ class UtilityCodeBase(object): ...@@ -393,7 +368,7 @@ class UtilityCodeBase(object):
return cls(**kwargs) return cls(**kwargs)
@classmethod @classmethod
def load_cached(cls, utility_code_name, from_file=None, __cache={}): def load_cached(cls, utility_code_name, from_file, __cache={}):
""" """
Calls .load(), but using a per-type cache based on utility name and file name. Calls .load(), but using a per-type cache based on utility name and file name.
""" """
...@@ -406,7 +381,7 @@ class UtilityCodeBase(object): ...@@ -406,7 +381,7 @@ class UtilityCodeBase(object):
return code return code
@classmethod @classmethod
def load_as_string(cls, util_code_name, from_file=None, **kwargs): def load_as_string(cls, util_code_name, from_file, **kwargs):
""" """
Load a utility code as a string. Returns (proto, implementation) Load a utility code as a string. Returns (proto, implementation)
""" """
......
...@@ -3286,4 +3286,4 @@ packed_struct_utility_code = UtilityCode(proto=""" ...@@ -3286,4 +3286,4 @@ packed_struct_utility_code = UtilityCode(proto="""
#endif #endif
""", impl="", proto_block='utility_code_proto_before_types') """, impl="", proto_block='utility_code_proto_before_types')
capsule_utility_code = UtilityCode.load("Capsule") capsule_utility_code = UtilityCode.load("Capsule", "Capsule.c")
...@@ -22,14 +22,11 @@ class TestUtilityLoader(unittest.TestCase): ...@@ -22,14 +22,11 @@ class TestUtilityLoader(unittest.TestCase):
cls = Code.UtilityCode cls = Code.UtilityCode
def test_load_as_string(self): def test_load_as_string(self):
got = strip_2tup(self.cls.load_as_string(self.name))
self.assertEqual(got, self.expected)
got = strip_2tup(self.cls.load_as_string(self.name, self.filename)) got = strip_2tup(self.cls.load_as_string(self.name, self.filename))
self.assertEqual(got, self.expected) self.assertEqual(got, self.expected)
def test_load(self): def test_load(self):
utility = self.cls.load(self.name) utility = self.cls.load(self.name, from_file=self.filename)
got = strip_2tup((utility.proto, utility.impl)) got = strip_2tup((utility.proto, utility.impl))
self.assertEqual(got, self.expected) self.assertEqual(got, self.expected)
...@@ -37,10 +34,6 @@ class TestUtilityLoader(unittest.TestCase): ...@@ -37,10 +34,6 @@ class TestUtilityLoader(unittest.TestCase):
got = strip_2tup((required.proto, required.impl)) got = strip_2tup((required.proto, required.impl))
self.assertEqual(got, self.required) self.assertEqual(got, self.required)
utility = self.cls.load(self.name, from_file=self.filename)
got = strip_2tup((utility.proto, utility.impl))
self.assertEqual(got, self.expected)
utility = self.cls.load_cached(self.name, from_file=self.filename) utility = self.cls.load_cached(self.name, from_file=self.filename)
got = strip_2tup((utility.proto, utility.impl)) got = strip_2tup((utility.proto, utility.impl))
self.assertEqual(got, self.expected) self.assertEqual(got, self.expected)
...@@ -59,11 +52,11 @@ class TestTempitaUtilityLoader(TestUtilityLoader): ...@@ -59,11 +52,11 @@ class TestTempitaUtilityLoader(TestUtilityLoader):
cls = Code.TempitaUtilityCode cls = Code.TempitaUtilityCode
def test_load_as_string(self): def test_load_as_string(self):
got = strip_2tup(self.cls.load_as_string(self.name, context=self.context)) got = strip_2tup(self.cls.load_as_string(self.name, self.filename, context=self.context))
self.assertEqual(got, self.expected_tempita) self.assertEqual(got, self.expected_tempita)
def test_load(self): def test_load(self):
utility = self.cls.load(self.name, context=self.context) utility = self.cls.load(self.name, self.filename, context=self.context)
got = strip_2tup((utility.proto, utility.impl)) got = strip_2tup((utility.proto, utility.impl))
self.assertEqual(got, self.expected_tempita) self.assertEqual(got, self.expected_tempita)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment