walt3k schrieb am 16.03.22 um 23:02:
> On Wednesday, March 16, 2022 at 3:48:03 PM UTC-5 Stefan Behnel wrote:
>> walt3k schrieb am 16.03.22 um 21:22:
>>> but we get an error with cython. Here is the decorator up to the red line
>>> that produces the AttributeError which is copied below:
>>>
>>> def test():
>>> def call_coro(coro):
>>> currentTestName=coro.__name__
>>> @functools.wraps(coro)
>>> async def wrapper(*args, **kwargs):
>>> filename="UNKNOWN_BECAUSE_THIS_IS_CYTHON"
>>> # cython/cocofu_ext tests don't have this. Need to find another
>>> way.
>>> *filename=coro.__globals__['__file__']*
>>>
>>>
>>> ERROR Test Failed: test_force_pass_stops_busy_rtx (result was
>>> AttributeError)
>>> Traceback (most recent call last):
>>> File "__init__.py", line 83, in wrapper
>>> filename=coro.__globals__['__file__']
>>> AttributeError: 'builtin_function_or_method' object
>>> has no attribute '__globals__'
>>
>> You have to enable the "binding" directive. That's still disabled by
>> default in Cython 0.29.x. Cython 3.0 has it on automatically, so you can
>> also use that instead.
>
> I installed version 3.0a1 (BTW - Would that be the best choice if I want
> the best stable version?) and it works thx!!! -- but sort of.
>
> The problem is I am asking for __globals__['__file__'] which for python
> tests is the .py name of the file.
>
> Ideally I'd like to get the .pyx filename for cython tests but instead I
> get the name of the .so file which cython created when it compiled the .pyx
> file.
That's intentional. It gives you the file that implements the module. And
for a binary module, that's the .so file.
> Is there a way in my decorator to get the name of the .pyx file which was
> the source file used to create the .so file reported by
> __globals__['__file__'] ?
Is that really the file you need? It's not needed at runtime, just for the
translation. However, you'd probably still want to ship that file with your
binary package (read, wheel) in order to get proper traceback code lines
printed for exceptions.
If you keep the .py or .pyx file next to the .so file, then it should be
easy to find it from "__file__", by just removing everything behind the
first dot, and then appending the right extension. Even if you don't know
the right extension, you can probably just guess and look it up in the file
system (and cache the final path in a dict if that's really too slow).
Stefan