0

I'm trying to do something for a project that I cannot find the answer to after searching and trying some different code.

What I'm trying to do is receive a string from a client(sent through via post, and I have to use this method, teacher's orders), and then compile this string using exec() in php file. Basically I'm "grading" a python code snippet sent to me as a string. I'm also doing this on the Andrew file system server(afs).

I'm not very experienced with python, mostly used other languages, but I'm able to make a python executable with php exec and also just compile it using cython(it's what the linux machines on our school have, not sure if I should use this). I got this to work with files.

My problem is I can't simply compile a string without running it. I can do

python -c "print('Hello World')" for just executing the string where "print('Hello World')" would be a php string variable

but I'm not sure how to simply just check for syntax errors.

I tried python -m py_compile -c "print('Hello World')"but it doesn't like that for what looks like the -m on py_compile doesn't like strings, only files

So outside of basically making a new .py file for every string the client sends me and working on that, is there a better way without making new files or making a separate python script to handle this. I'm trying to do this in one php file. This might be a big nono doing python command line scripts on strings so I might as well ask.

5Ermacs
  • 15
  • 5
  • may you rewrite the whole thing with just what you want to achieve? add what you tried as a different section so that we can know what not to try. Remove extra story mode for easier understanding – Harry Oct 01 '17 at 17:11
  • For some reason I can't edit my question. Basically what I'm trying to do is do a linux command line argument that compiles a string argument in python code. I want to see any syntax errors or lack thereof in the string. So `python -m py_compile -c "A string of python code"` – 5Ermacs Oct 01 '17 at 17:18
  • Just to note that the reason I cant just use `python -c "A string"` is because if that has any errors it simply returns nothing, I need something that will return a string with the syntax errors" – 5Ermacs Oct 01 '17 at 17:24
  • may you try python -c "print \"hello" it returns error – Harry Oct 01 '17 at 17:42
  • Harry the big problem is when I'm doing `python -c "print\'Hello'" I'm doing that in a php file using exec(). I'm not actually manually inputting that into a command line prompt. I only see what php exec() returns. So if there is a syntax error exec() returns nothing – 5Ermacs Oct 01 '17 at 18:08

1 Answers1

0

You can use the parser-Module. Write a script like this:

import sys
import parser


data = sys.argv[1]

try:
    parser.suite(data)
except SyntaxError as e:
    print(e)

Invoke like this

python3.6 /tmp/test.py "import math"
deets
  • 6,285
  • 29
  • 28
  • This works great, I got this to work with my php file rather easily. I guess this makes more sense that trying to do some weird argument on the cl and just use a script – 5Ermacs Oct 01 '17 at 18:26