Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions tensorflow_graphics/util/export_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,29 @@
from __future__ import print_function

import inspect
import sys

import six


def _get_caller_module():
"""Returns the module of the caller of the public export_api function."""
# Avoid inspect.stack() (O(depth * len(sys.modules))) and inspect.getmodule()
# on uncached newly-imported modules, which scans sys.modules and realpath().
frame = sys._getframe(2) # pylint: disable=protected-access
module = sys.modules.get(frame.f_globals.get("__name__", ""))
if getattr(module, "__dict__", None) is frame.f_globals:
return module
return inspect.getmodule(frame)


def get_functions_and_classes():
"""Extracts a list of public functions and classes for the API generation.

Returns:
A list of function and class names.
"""
caller = inspect.stack()[1]
module = inspect.getmodule(caller[0])
module = _get_caller_module()
return [
obj_name for obj_name, obj in inspect.getmembers(module)
if inspect.isfunction(obj) or
Expand All @@ -43,8 +54,7 @@ def get_modules():
Returns:
A list of module names.
"""
caller = inspect.stack()[1]
module = inspect.getmodule(caller[0])
module = _get_caller_module()
return [
obj_name for obj_name, obj in inspect.getmembers(module)
if inspect.ismodule(obj) and six.ensure_str(obj.__name__).rsplit(".", 1)
Expand Down
43 changes: 34 additions & 9 deletions tensorflow_graphics/util/tests/export_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,50 @@
# limitations under the License.
"""Tests for export API functionalities."""

import sys
import types

from tensorflow_graphics.util import export_api
from tensorflow_graphics.util import test_case


def sample_public_function():
"""Sample public function for export_api testing."""


class _SamplePrivateClass:
"""Sample private class for export_api testing."""


class ExportAPITest(test_case.TestCase):

def test_get_functions_and_classes(self):
"""Tests that get_functions_and_classes does not raise an exception."""
try:
export_api.get_functions_and_classes()
except Exception as e: # pylint: disable=broad-except
self.fail("Exception raised: %s" % str(e))
"""Tests that get_functions_and_classes returns public symbols."""
exported = export_api.get_functions_and_classes()
self.assertIn("ExportAPITest", exported)
self.assertIn("sample_public_function", exported)
self.assertNotIn("_SamplePrivateClass", exported)

def test_get_modules(self):
"""Tests that get_modules does not raise an exception."""
"""Tests that get_modules returns public direct submodules."""
self.assertEqual(export_api.get_modules(), [])

pkg_name = "_tfg_export_api_test_pkg"
pkg = types.ModuleType(pkg_name)
pkg.__dict__["export_api"] = export_api
pkg.__dict__["child"] = types.ModuleType(pkg_name + ".child")
pkg.__dict__["_private"] = types.ModuleType(pkg_name + "._private")
pkg.__dict__["other"] = types.ModuleType("unrelated_pkg.other")

def _call():
return export_api.get_modules()

sys.modules[pkg_name] = pkg
try:
export_api.get_modules()
except Exception as e: # pylint: disable=broad-except
self.fail("Exception raised: %s" % str(e))
call_in_pkg = types.FunctionType(_call.__code__, pkg.__dict__)
self.assertEqual(call_in_pkg(), ["child"])
finally:
sys.modules.pop(pkg_name, None)


if __name__ == "__main__":
Expand Down
Loading