Commit eb5fc339 authored by Robert Bradshaw's avatar Robert Bradshaw

Don't require typeinfo cimport for typeid.

parent 89723dc5
...@@ -27,6 +27,8 @@ Features added ...@@ -27,6 +27,8 @@ Features added
* ``libc/math.pxd`` provides ``e`` and ``pi`` as alias constants to simplify * ``libc/math.pxd`` provides ``e`` and ``pi`` as alias constants to simplify
usage as a drop-in replacement for Python's math module. usage as a drop-in replacement for Python's math module.
* Support for the C++ ``typeid`` operator.
Bugs fixed Bugs fixed
---------- ----------
......
...@@ -10204,16 +10204,12 @@ class TypeidNode(ExprNode): ...@@ -10204,16 +10204,12 @@ class TypeidNode(ExprNode):
is_temp = 1 is_temp = 1
def get_type_info_type(self, env): def get_type_info_type(self, env):
if env.is_module_scope:
env_module = env env_module = env
else: while not env_module.is_module_scope:
env_module = env.outer_scope env_module = env_module.outer_scope
for module in env_module.cimported_modules: typeinfo_module = env_module.find_module('libcpp.typeinfo', self.pos)
if module.qualified_name == 'libcpp.typeinfo': typeinfo_entry = typeinfo_module.lookup('type_info')
type_info = module.lookup('type_info') return PyrexTypes.CFakeReferenceType(PyrexTypes.c_const_type(typeinfo_entry.type))
type_info = PyrexTypes.CFakeReferenceType(PyrexTypes.c_const_type(type_info.type))
return type_info
return None
def analyse_types(self, env): def analyse_types(self, env):
type_info = self.get_type_info_type(env) type_info = self.get_type_info_type(env)
...@@ -10253,11 +10249,6 @@ class TypeidNode(ExprNode): ...@@ -10253,11 +10249,6 @@ class TypeidNode(ExprNode):
def generate_result_code(self, code): def generate_result_code(self, code):
if self.is_type: if self.is_type:
if self.arg_type.is_extension_type:
# the size of the pointer is boring
# we want the size of the actual struct
arg_code = self.arg_type.declaration_code("", deref=1)
else:
arg_code = self.arg_type.empty_declaration_code() arg_code = self.arg_type.empty_declaration_code()
else: else:
arg_code = self.arg_type.result() arg_code = self.arg_type.result()
......
...@@ -606,17 +606,16 @@ you can declare it using the Python @staticmethod decorator, i.e.:: ...@@ -606,17 +606,16 @@ you can declare it using the Python @staticmethod decorator, i.e.::
RTTI and typeid() RTTI and typeid()
================= =================
Cython has support for the ``typeid(...)`` operator. To use it, you need to import Cython has support for the ``typeid(...)`` operator.
it and the ``libcpp.typeinfo`` module first:
from cython.operator cimport typeid from cython.operator cimport typeid
from libcpp.typeinfo cimport type_info
The ``typeid(...)`` operator returns an object of the type ``const type_info &``. The ``typeid(...)`` operator returns an object of the type ``const type_info &``.
If you want to store a type_info value in a C variable, you will need to store it If you want to store a type_info value in a C variable, you will need to store it
as a pointer rather than a reference: as a pointer rather than a reference:
from libcpp.typeinfo cimport type_info
cdef const type_info* info = &typeid(MyClass) cdef const type_info* info = &typeid(MyClass)
If an invalid type is passed to ``typeid``, it will throw an ``std::bad_typeid`` If an invalid type is passed to ``typeid``, it will throw an ``std::bad_typeid``
......
...@@ -8,7 +8,6 @@ from cython.operator cimport typeid, dereference as deref ...@@ -8,7 +8,6 @@ from cython.operator cimport typeid, dereference as deref
from libc.string cimport const_char from libc.string cimport const_char
from libcpp cimport bool from libcpp cimport bool
cimport libcpp.typeinfo
cdef out(s, result_type=None): cdef out(s, result_type=None):
print '%s [%s]' % (s.decode('ascii'), result_type) print '%s [%s]' % (s.decode('ascii'), result_type)
......
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