Expose __pyx_f variable

25 views
Skip to first unread message

Ray Chen

unread,
Aug 27, 2021, 1:59:20 AM8/27/21
to cython-users
Hello everyone

I am using cythonize to make .c files from my pure python files. Sometimes the __pyx_f variable isn't the same as the file path so how can i get the value from pure python?

I do not want to modify the .c source code, and the value seems to be used in tracebacks too (for the filename in each frame).

D Woods

unread,
Aug 28, 2021, 1:17:36 AM8/28/21
to cython-users
I don't know why you need it, but this sounds like the wrong solution to your problem. However:

__pyx_f isn't a single filename but an array of filenames - Cython source code can come from a number of places (a pyx file, a pxd file, a file that's been included in another, utility code that Cython itself generates). As far as I can tell __pyx_f is only used for generating tracebacks and thus which element is picked depends on where the traceback comes from. I don't know how or if they are ordered.

You could access it by modifying the source code of your Cython file. I would probably declare it as an extern variable:

cdef extern from *:
    const char* __pyx_f[]

You can then create a Cython function to access the variable:

def get_from_pyx_f(n):
    return __pyx_f[n]

I don't think there's any way of telling how big the array is so this risks a segmentation fault if you try to access too far. None of this is tested so it may need small modifications to work.

Alternatively you could write a function that generates a traceback and then extract the file name from that.

Stefan Behnel

unread,
Aug 28, 2021, 1:22:33 AM8/28/21
to cython...@googlegroups.com
Am 26. August 2021 22:54:18 MESZ schrieb Ray Chen:
>I am using cythonize to make .c files from my pure python files.
>Sometimes
>the __pyx_f variable isn't the same as the file path so how can i get
>the value from pure python?

You can use the normal Python variables like "__file__" etc.

Stefan

Ray Chen

unread,
Aug 28, 2021, 1:54:46 PM8/28/21
to cython...@googlegroups.com
Oh, is this how you access C variables from Cython? I'll check it out!

--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/PJbW4cauWqI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/077740ba-cecd-4f2e-acd2-679f33328f0en%40googlegroups.com.

Stefan Behnel

unread,
Aug 31, 2021, 5:22:10 AM8/31/21
to cython...@googlegroups.com
Am 27. August 2021 21:26:46 MESZ schrieb D Woods:
> I don't know why you need it, but this sounds like the wrong solution
> to your problem.

Yes.


>would probably declare it as an extern variable:
>
>cdef extern from *:
> const char* __pyx_f[]
>
>You can then create a Cython function to access the variable:
>
>def get_from_pyx_f(n):
> return __pyx_f[n]

Note, though, that anything prefixed with "__pyx_" and friends is internal to Cython and subject to change at any time, without warning. Use at your own risk.

Stefan

Reply all
Reply to author
Forward
0 new messages