Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 799435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:07:35+00:00 2026-05-14T23:07:35+00:00

I have a python script that I run with ‘exec’. When a function is

  • 0

I have a python script that I run with ‘exec’.
When a function is called by the script, I would like it to know the line number and offset in line for that call.

Here is an example. If my script is:

foo1(); foo2(); foo1()
foo3()

And if I have code that prints (line,offset) in every function, I should get

(0,0), (0,8), (0,16), (1,0)

In most cases this can be easily done by getting the stack frame, because it contains the line number and the function name. The only problem is when there are two functions with the same name in a certain line. Unfortunately this is a common case for me. Any ideas?


Ok it seems that changing the original code is the simplest solution.

How would you solve things like

if foo1(7) or foo1(6):

or

foo2(foo1(), foo1())

There are some not very elegant solutions for this, for example, automatically turning the previous example to:

def curpos(pos, func):
  record_curpos(pos)
  return func

curpos(foo2,0)(curpos(foo1,5)(), curpos(foo1,13)())

Let me know if you have simpler ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:07:36+00:00Added an answer on May 14, 2026 at 11:07 pm

    Python doesn’t provide a lot of information about character offsets in a line.

    If you are using exec to execute the Python, then you could re-write the code mechanically before executing it to tell you what you want to know. For example, you could change the original code:

    foo1(); foo2(); foo1()
    foo3()
    

    into annotated code:

    curpos(1,0); foo1(); curpos(1,8); foo2(); curpos(1,16); foo1()
    curpos(2,0); foo3()
    

    and then exec the annotated code.

    Where curpos(line,char) records or prints the line and character information of that point in the original code. This will be much simpler than mucking around with stack frames.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that i want to run from the command line
I have a long-running Python script that I run from the command-line. The script
I have a python script that I want always to run in the background.
I have a Iron Python script that I want to run and then have
I have a python script on a vps that I run a simple command
I have a Python script that needs to issue a number of shell commands.
I have a Python script that uses built-in modules but also imports a number
I have a Python script that I'd like to compile into a Windows executable.
I have a python script that uses selenium RC; specifically webdriver. I would love
I have a python script that I run to populate my database. I usually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.