junrushao commented on code in PR #8:
URL: https://github.com/apache/tvm-ffi/pull/8#discussion_r2365842713


##########
python/tvm_ffi/dataclasses/_utils.py:
##########
@@ -0,0 +1,207 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Utilities for constructing Python proxies of FFI types."""
+
+from __future__ import annotations
+
+import functools
+import inspect
+from dataclasses import MISSING
+from typing import Any, Callable, NamedTuple, TypeVar
+
+from ..core import (
+    Object,
+    TypeField,
+    TypeInfo,
+    _lookup_type_info_from_type_key,
+)
+
+_InputClsType = TypeVar("_InputClsType")
+
+
+def get_parent_type_info(type_cls: type) -> TypeInfo:
+    """Find the nearest ancestor with registered ``__tvm_ffi_type_info__``.
+
+    If none are found, return the base ``ffi.Object`` type info.
+    """
+    for base in type_cls.__bases__:
+        if (info := getattr(base, "__tvm_ffi_type_info__", None)) is not None:
+            return info
+    return _lookup_type_info_from_type_key("ffi.Object")
+
+
+def type_info_to_cls(
+    type_info: TypeInfo,
+    cls: type[_InputClsType],
+    methods: dict[str, Callable[..., Any] | None],
+) -> type[_InputClsType]:
+    assert type_info.type_cls is None, "Type class is already created"
+    # Step 1. Determine the base classes
+    cls_bases = cls.__bases__
+    if cls_bases == (object,):
+        # If the class inherits from `object`, we need to set the base class 
to `Object`
+        cls_bases = (Object,)
+
+    # Step 2. Define the new class attributes
+    attrs = dict(cls.__dict__)
+    attrs.pop("__dict__", None)
+    attrs.pop("__weakref__", None)
+    attrs["__slots__"] = ()
+    attrs["__tvm_ffi_type_info__"] = type_info
+
+    # Step 2. Add fields
+    for field in type_info.fields:
+        attrs[field.name] = field.as_property(cls)
+
+    # Step 3. Add methods
+    def _add_method(name: str, func: Callable) -> None:
+        func.__module__ = cls.__module__
+        func.__name__ = name
+        func.__qualname__ = f"{cls.__qualname__}.{name}"
+        func.__doc__ = f"Method `{name}` of class `{cls.__qualname__}`"
+        attrs[name] = func
+        setattr(cls, name, func)
+
+    for method in type_info.methods:
+        _add_method("_C_" + method.name, method.func)

Review Comment:
   Update: we decided to handle `__ffi_init__` as a special case, which is 
renamed to `__c_ffi_init__` when exposed to Python



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to