Skip to content

Commit fa29953

Browse files
committed
quarantine einspect
1 parent 02906ed commit fa29953

File tree

6 files changed

+161
-17
lines changed

6 files changed

+161
-17
lines changed

mlir/extras/ast/py_type.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
from __future__ import annotations
2+
3+
from ctypes import (
4+
c_char,
5+
c_char_p,
6+
c_uint,
7+
c_ulong,
8+
pointer,
9+
)
10+
from typing import TypeVar
11+
12+
from einspect.structs.include.descrobject_h import PyGetSetDef, PyMemberDef
13+
from einspect.structs.include.methodobject_h import PyMethodDef
14+
from einspect.structs.include.object_h import (
15+
destructor,
16+
getattrfunc,
17+
setattrfunc,
18+
PyAsyncMethods,
19+
reprfunc,
20+
PyNumberMethods,
21+
PySequenceMethods,
22+
PyMappingMethods,
23+
hashfunc,
24+
ternaryfunc,
25+
getattrofunc,
26+
setattrofunc,
27+
PyBufferProcs,
28+
traverseproc,
29+
inquiry,
30+
richcmpfunc,
31+
getiterfunc,
32+
iternextfunc,
33+
descrgetfunc,
34+
descrsetfunc,
35+
initproc,
36+
allocfunc,
37+
newfunc,
38+
freefunc,
39+
vectorcallfunc,
40+
)
41+
from einspect.structs.py_object import PyObject, PyVarObject
42+
from einspect.types import ptr
43+
from typing_extensions import Annotated, Self
44+
45+
__all__ = ("PyTypeObject",)
46+
47+
_T = TypeVar("_T")
48+
49+
DEFAULT = object()
50+
51+
52+
# noinspection PyPep8Naming
53+
class PyTypeObject(PyVarObject[_T, None, None]):
54+
"""
55+
Defines a PyTypeObject Structure.
56+
57+
https://github.com/python/cpython/blob/3.11/Doc/includes/typestruct.h
58+
59+
..
60+
source: Include/cpython/object.h (struct _typeobject)
61+
"""
62+
63+
tp_name: Annotated[bytes, c_char_p]
64+
# For allocation
65+
tp_basicsize: int
66+
tp_itemsize: int
67+
# Methods to implement standard operations
68+
tp_dealloc: destructor
69+
tp_vectorcall_offset: int
70+
tp_getattr: getattrfunc
71+
tp_setattr: setattrfunc
72+
# formerly known as tp_compare (Python 2) or tp_reserved (Python 3)
73+
tp_as_async: ptr[PyAsyncMethods]
74+
75+
tp_repr: reprfunc
76+
77+
# Method suites for standard classes
78+
tp_as_number: ptr[PyNumberMethods]
79+
tp_as_sequence: ptr[PySequenceMethods]
80+
tp_as_mapping: ptr[PyMappingMethods]
81+
82+
# More standard operations (here for binary compatibility)
83+
tp_hash: hashfunc
84+
tp_call: ternaryfunc
85+
tp_str: reprfunc
86+
tp_getattro: getattrofunc
87+
tp_setattro: setattrofunc
88+
89+
# Functions to access object as input/output buffer
90+
tp_as_buffer: ptr[PyBufferProcs]
91+
92+
# Flags to define presence of optional/expanded features
93+
tp_flags: Annotated[int, c_ulong]
94+
95+
tp_doc: Annotated[bytes, c_char_p] # Documentation string
96+
97+
# Assigned meaning in release 2.0
98+
# call function for all accessible objects
99+
tp_traverse: traverseproc
100+
101+
tp_clear: inquiry # delete references to contained objects
102+
103+
# Assigned meaning in release 2.1
104+
# rich comparisons
105+
tp_richcompare: richcmpfunc
106+
107+
tp_weaklistoffset: int # weak reference enabler
108+
109+
# Iterators
110+
tp_iter: getiterfunc
111+
tp_iternext: iternextfunc
112+
113+
# Attribute descriptor and subclassing stuff
114+
tp_methods: ptr[PyMethodDef]
115+
tp_members: ptr[PyMemberDef]
116+
tp_getset: ptr[PyGetSetDef]
117+
118+
# Strong reference on a heap type, borrowed reference on a static type
119+
tp_base: pointer[Self]
120+
tp_dict: ptr[PyObject]
121+
tp_descr_get: descrgetfunc
122+
tp_descr_set: descrsetfunc
123+
tp_dictoffset: int
124+
tp_init: initproc
125+
tp_alloc: allocfunc
126+
tp_new: newfunc
127+
tp_free: freefunc # Low-level free-memory routine
128+
tp_is_gc: inquiry # For PyObject_IS_GC
129+
tp_bases: ptr[PyObject]
130+
tp_mro: ptr[PyObject] # method resolution order
131+
tp_cache: ptr[PyObject]
132+
tp_subclasses: ptr[PyObject] # for static builtin types this is an index
133+
tp_weaklist: ptr[PyObject]
134+
tp_del: destructor
135+
136+
# Type attribute cache version tag. Added in version 2.6
137+
tp_version_tag: Annotated[int, c_uint]
138+
139+
tp_finalize: destructor
140+
tp_vectorcall: vectorcallfunc
141+
142+
# bitset of which type-watchers care about this type
143+
tp_watched: c_char
144+
145+
146+
# https://github.com/python/cpython/blob/809aa9a682fc865f7502e7421da0a74d204aab6d/Objects/typevarobject.c#L29
147+
class PyTypeVarObject(PyVarObject[_T, None, None]):
148+
name: ptr[PyObject]
149+
# not sure why but this is the only thing that works but that's fine because it's the only thing we need
150+
bound: ptr[PyObject]
151+
152+
153+
if __name__ == "__main__":
154+
print(PyTypeObject.from_object(type(3.14)))

mlir/extras/ast/util.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import types
55
from opcode import opmap
66
from textwrap import dedent
7-
from typing import Dict, TypeVar
7+
from typing import Dict
88

99
from bytecode import ConcreteBytecode
1010
from cloudpickle import cloudpickle
11-
from einspect import ptr
12-
from einspect.structs import PyVarObject, PyObject
1311

1412

1513
def set_lineno(node, n=1):
@@ -164,13 +162,3 @@ def find_func_in_code_object(co, func_name):
164162
f = find_func_in_code_object(c, func_name)
165163
if f is not None:
166164
return f
167-
168-
169-
_T = TypeVar("_T")
170-
171-
172-
# https://github.com/python/cpython/blob/809aa9a682fc865f7502e7421da0a74d204aab6d/Objects/typevarobject.c#L29
173-
class PyTypeVarObject(PyVarObject[_T, None, None]):
174-
name: ptr[PyObject]
175-
# not sure why but this is the only thing that works but that's fine because it's the only thing we need
176-
bound: ptr[PyObject]

mlir/extras/dialects/ext/arith.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import numpy as np
1010
from bytecode import ConcreteBytecode
11-
from einspect.structs import PyTypeObject
11+
from ...ast.py_type import PyTypeObject
1212

1313
from ...ast.canonicalize import StrictTransformer, Canonicalizer, BytecodePatcher
1414
from ...util import get_user_code_loc, infer_mlir_type, mlir_type_to_np_dtype

mlir/extras/dialects/ext/func.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from functools import update_wrapper
66
from typing import Optional, List, Union, TypeVar
77

8-
from ...ast.util import copy_func, PyTypeVarObject
8+
from ...ast.util import copy_func
9+
from ...ast.py_type import PyTypeVarObject
910
from ...meta import op_region_builder
1011
from ... import types as T
1112
from ...util import get_user_code_loc, make_maybe_no_args_decorator
@@ -202,7 +203,7 @@ def __init__(
202203
def _is_decl(self):
203204
# magic constant found from looking at the code for an empty fn
204205
if sys.version_info.minor == 14:
205-
return self.body_builder.__code__.co_code == b'\x80\x00R\x00#\x00'
206+
return self.body_builder.__code__.co_code == b"\x80\x00R\x00#\x00"
206207
elif sys.version_info.minor == 13:
207208
return self.body_builder.__code__.co_code == b"\x95\x00g\x00"
208209
elif sys.version_info.minor == 12:

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
black
22
inflection
33
pytest
4+
astpretty

tests/test_gpu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def test_generic_type_var_closure_patching(ctx: MLIRContext):
601601
exec(
602602
dedent(
603603
"""\
604-
from mlir.extras.ast.util import PyTypeVarObject
604+
from mlir.extras.ast.py_type import PyTypeVarObject
605605
606606
def fun2[foo, bar, A: foo + bar]():
607607
print(A.__bound__)

0 commit comments

Comments
 (0)