SyntaxError uncaught while using py_compile - python

I have defined the following function inside a class in python, where I'm trying to compile python code from external source.
The python code comes in, is written to a file and then the file is sent to the below function.
When I'm trying to call the function as:
self._check_code_for_errors(source_file)
It does not execute the except block, where I'm catching the SyntaxError exception.
def _check_code_for_errors(self, source_file):
try:
file_ = open(source_file.name, 'r')
py_compile.compile(file_.name)
except SyntaxError:
return {'errors': 'You have a problem in your syntax'}
except (OSError, IOError):
return {'errors': 'Some error has occurred, please try again'}
Update:
class ValidatePythonCodeViewSet(generics.CreateAPIView):
parser_classes = (PlainTextParser, )
"""
The view set below accepts code from post request, executes it and then
returns the appropriate results (error or output)
"""
def _write_code_to_file(self, source):
# file is a reserved word in python 2.x, so using file_
with open('tempPythonCode.py', 'w') as file_:
file_.write(source)
return file_
def _check_code_for_errors(self, source_file):
try:
file_ = open(source_file.name, 'r')
py_compile.compile(file_.name, doraise=True)
except py_compile.PyCompileError:
return {'errors': 'You have a problem in your syntax'}
def post(self, request, *args, **kwargs):
source = request.data
if not source:
raise InformationMissingInRequestError()
else:
source_file = self._write_code_to_file(source)
response = self._check_code_for_errors(source_file)
if response.get('errors', None):
return Response(response, status=status.HTTP_400_BAD_REQUEST)
else:
#execute code here and return
pass
return Response(response, status=status.HTTP_200_OK)
The request that I'm making is:
TraceBack
File "tempPythonCode.py", line 1
import os\nprint 'hi
^
SyntaxError: unexpected character after line continuation character
Internal Server Error: /api/python/
Traceback (most recent call last):
File "/home/dhruuv/.virtualenvs/pythoneval/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/dhruuv/.virtualenvs/pythoneval/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/dhruuv/.virtualenvs/pythoneval/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/dhruuv/.virtualenvs/pythoneval/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/dhruuv/.virtualenvs/pythoneval/local/lib/python2.7/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/home/dhruuv/.virtualenvs/pythoneval/local/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/home/dhruuv/projects/PythonEval/api/views.py", line 44, in post
if response.get('errors', None):
AttributeError: 'NoneType' object has no attribute 'get'
[10/Feb/2016 09:45:44] "POST /api/python/ HTTP/1.1" 500 87401
Update 2
I tried it in ipdb, which worked just fine!
In [5]: try:
py_compile.compile('testing.py', doraise=True)
except py_compile.PyCompileError:
print 'dfsssssssssssss'
...:
dfsssssssssssss
Any help is appreciated.

SyntaxError is not a runtime error, and you cannot catch it in code. However, py_compile does not raise SyntaxError; as the documentation shows, it raises py_compile.PyCompileError.
Edit So there are a couple of things wrong with your code here. Firstly, again as the documentation shows, you need to pass doraise=True to compile in order to get it to raise an error.
And the other exception is happening because you're not returning anything from _check_code_for_errors if it succeeds. You probably should return an empty dict.

Related

Flask response.data raise error TypeError: 'NoneType' object is not callable

I use an online free api hoster and I have an issue with reponse.data, I use that on #app.after_request with should log path, args and server response, problem is that in console I get:
GET /favicon.ico HTTP/1.1" 500 - Error on request: Traceback (most recent call last): File "D:\Lib\site-packages\werkzeug\serving.py", line 319, in run_wsgi execute(self.server.app) File "D:\Lib\site-packages\werkzeug\serving.py", line 308, in execute application_iter = app(environ, start_response) File "D:\Lib\site-packages\flask\app.py", line 2095, in call return self.wsgi_app(environ, start_response) File "D:\Lib\site-packages\flask\app.py", line 2084, in wsgi_app return response(environ, start_response) TypeError: 'NoneType' object is not callable
But on discord logger I see the log:
The code is :
def log(f):
#wraps(f)
def decorated_function(*args, **kwargs):
url = discord_link
if dict(request.args) != {}:
values = {
'username': 'Api-Logger',
'content': f'User called path:`{request.path}` with arsg: `{dict(request.args)}`'
}
result=external_requests.post(url, json = values)
result.raise_for_status()
return f(*args, **kwargs)
return decorated_function
#app.after_request
#log
def after_request_func(response):
try:
SECURE.send_log(str(response.data).decode("utf-8"))
return None
except Exception as err:
if isinstance(err, TypeError):
pass
else:
SECURE.send_log(err)
I don't know how to fix it But once I remove #app.after_request it works fine
I managed to solve the problem.
The problem was that I was capturing the answer but not pushing further
FIXED:
#app.after_request
def after_request_func(response):
SECURE.send_log((response.data).decode('utf-8'))
return response

user_id = getattr(user, api_settings.USER_ID_FIELD) AttributeError: 'str' object has no attribute 'id

I am trying to authenticate a user through their email using DRF but so far it's only errors i've been getting.
This is the class that handles the email verification
class VerifyEmail(GenericAPIView):
def get(self, request):
token = request.GET.get('token')
try:
payload = jwt.decode(token, settings.SECRET_KEY) # Decodes the user token and the secret key to get the user ID
print(payload)
user = User.objects.get(id=payload['user_id']) # Gotten the user ID
if not user.is_verified: # Runs an if statement to see if the user has been verified already
user.is_verified = True
user.save()
data = {"confirmation_message": "Your account has been verified"}
return Response(data, status=status.HTTP_200_OK)
except jwt.ExpiredSignatureError as identifier:
error = {"expired_activation_link": "The activation link has expired"}
return Response(error, status=status.HTTP_400_BAD_REQUEST)
except jwt.DecodeError as identifier:
error = {"invalid_token": "The token is invalid request a new one"}
return Response(error, status=status.HTTP_400_BAD_REQUEST)
and this is the error i keep getting
Internal Server Error: /auth/register
Traceback (most recent call last):
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/SRC/users/views.py", l
ine 43, in post
token = RefreshToken.for_user(user_email).access_token
File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/sit
e-packages/rest_framework_simplejwt/tokens.py", line 161, in for_user
user_id = getattr(user, api_settings.USER_ID_FIELD)
AttributeError: 'str' object has no attribute 'id'
please how can i fix this?
In your views.py Line 43,
token = RefreshToken.for_user(user_email).access_token
This is wrong because the RefreshToken.for_user() method accepts a User object as argument and not a string and that's why you are getting that error.
Reference to for_user docs..
You can also see the relevant RefreshToken.for_user method's code on their github here.

In Django, how do you pass parameters from the URL to get_file() in VirtualDownloadView?

The below is parts of my Django project.
I've got the following URL configured.
urlpatterns = patterns(
url(r'^download/(?P<job>[0-9]+)/$', DownloadJobResults.as_view(), name='dm_download')
)
And the following is the DownloadJobResults class.
class DownloadJobResults(VirtualDownloadView):
def get_file(self, job):
print "Job is {}".format(job)
file = StringIO.StringIO()
file.write("test 1,2,3,4\n")
file.write("test 5,6,7,8\n")
file.seek(0)
return VirtualFile(file, name='sweet.txt')
I'll eventually be using the job parameter to look up job data and return a file with it. But I can't get the parameter passed. When I access that URL, I get:
get_file() takes exactly 2 arguments (1 given)
Here is the full stack trace.
Traceback: File "/Users/michael/app/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/michael/app/lib/python2.7/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs) File "/Users/michael/app/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
87. return handler(request, *args, **kwargs) File "/Users/michael/app/lib/python2.7/site-packages/django_downloadview/views/base.py" in get
170. return self.render_to_response() File "/Users/michael/app/lib/python2.7/site-packages/django_downloadview/views/base.py" in render_to_response
154. self.file_instance = self.get_file()
Exception Type: TypeError at /dm/download/5/ Exception Value: get_file() takes exactly 2 arguments (1 given)
I've got the problem figured out. Basically, Django puts those named URL parameters in kwargs.
class DownloadJobResults(VirtualDownloadView):
def get_file(self):
print "Job is {}".format(self.kwargs.get("job",None))
file = StringIO.StringIO()
file.write("test 1,2,3,4\n")
file.write("test 5,6,7,8\n")
file.seek(0)
return VirtualFile(file, name='sweet.txt')

'Thread' object has no attribute '_children' - django + scikit-learn

I'm having problems with a django application that uses a random forest classifier (http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html) to classify items. The error that I'm receiving says:
AttributeError at /items/
'Thread' object has no attribute '_children'
Request Method: POST
Request URL: http://localhost:8000/items/
Django Version: 1.7.6
Exception Type: AttributeError
Exception Value:
'Thread' object has no attribute '_children'
Exception Location: /usr/lib/python2.7/multiprocessing/dummy/__init__.py in start, line 73
Python Executable: /home/cristian/env/bin/python
Python Version: 2.7.3
Python Path:
['/home/cristian/filters',
'/home/cristian/env/lib/python2.7',
'/home/cristian/env/lib/python2.7/plat-linux2',
'/home/cristian/env/lib/python2.7/lib-tk',
'/home/cristian/env/lib/python2.7/lib-old',
'/home/cristian/env/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/home/cristian/env/local/lib/python2.7/site-packages']
Server time: Fri, 24 Apr 2015 16:08:20 +0000
The problem is that I'm not using threads at all. This is the code:
def item_to_dict(item):
item_dict = {}
for key in item:
value = item[key]
# fix encoding
if isinstance(value, unicode):
value = value.encode('utf-8')
item_dict[key] = [value]
return item_dict
def load_classifier(filter_name):
clf = joblib.load(os.path.join(CLASSIFIERS_PATH, filter_name, 'random_forest.100k.' + filter_name.lower() + '.pkl'))
return clf
#api_view(['POST'])
def classify_item(request):
"""
Classify item
"""
if request.method == 'POST':
serializer = ItemSerializer(data=request.data['item'])
if serializer.is_valid():
# get item and filter_name
item = serializer.data
filter_name = request.data['filter']
item_dict = item_to_dict(item)
clf = load_classifier(filter_name)
# score item
y_pred = clf.predict_proba(pd.DataFrame(item_dict))
item_score = y_pred[0][1]
# create and save classification
classification = Classification(classifier_name=filter_name,score=item_score,item_id=item['_id'])
classification_serializer = ClassificationSerializer(classification)
return Response(classification_serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
I'm able to print out the "clf" and "item_dict" variables and everything seems ok. The error raises when I call the method "predict_proba" of the classifier.
One important thing to add is that I don't recieve the error when I run the server and send the post method for the first time.
Here's the full traceback:
File "/home/cristian/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
line 111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/cristian/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
line 57. return view_func(*args, **kwargs)
File "/home/cristian/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
line 69. return self.dispatch(request, *args, **kwargs)
File "/home/cristian/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
line 452. response = self.handle_exception(exc)
File "/home/cristian/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
line 449. response = handler(request, *args, **kwargs)
File "/home/cristian/env/local/lib/python2.7/site-packages/rest_framework/decorators.py" in handler
line 50. return func(*args, **kwargs)
File "/home/cristian/filters/classifiers/views.py" in classify_item
line 70. y_pred = clf.predict_proba(pd.DataFrame(item_dict))
File "/home/cristian/env/local/lib/python2.7/site-packages/sklearn/pipeline.py" in predict_proba
line 159. return self.steps[-1][-1].predict_proba(Xt)
File "/home/cristian/env/local/lib/python2.7/site-packages/sklearn/ensemble/forest.py" in predict_proba
line 468. for i in range(n_jobs))
File "/home/cristian/env/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py" in __call__
line 568. self._pool = ThreadPool(n_jobs)
File "/usr/lib/python2.7/multiprocessing/pool.py" in __init__
line 685. Pool.__init__(self, processes, initializer, initargs)
File "/usr/lib/python2.7/multiprocessing/pool.py" in __init__
line 136. self._repopulate_pool()
File "/usr/lib/python2.7/multiprocessing/pool.py" in _repopulate_pool
line 199. w.start()
File "/usr/lib/python2.7/multiprocessing/dummy/__init__.py" in start
line 73. self._parent._children[self] = None
Exception Type: AttributeError at /items/
Exception Value: 'Thread' object has no attribute '_children'
As a workaround, you can disable the threading at prediction time with:
clf = load_classifier(filter_name)
clf.set_params(n_jobs=1)
y_pred = clf.predict_proba(pd.DataFrame(item_dict))
Also note, calling load_classifier at each request might be expensive it actually loads the model from the disk.
You can pass mmap_mode='r' to joblib.load to memory map the data from the disk. It will make it possible to load the model only once even if you have concurrent requests accessing the same model parameters concurrently (both with different threads and different Python processes if you use something like gunicorn).
It looks like that problem was fixed from Python 2.7.5. It was basically a bug in multiprocessing.

How to use AssertRaisesMessage() in Django tests

I followed the Django doc to write tests with assertRaisesMessage() but the problem is the exception itself is raised when executing test (so, the test is not executed).
Note that the exception called is an exception I voluntarily raise in a method of my model (not in the view).
class MyTestCase(TestCase):
def test_invertRewardState_view_fails_notmy_reward(self):
self.client.login(email='gislaine#toto.com', password='azerty')
resp = self.client.get(reverse(invertRewardState, args=(1,)))
self.assertRaisesMessage(
expected_exception=Exception,
expected_message=EXC_NOT_YOURS,
callable_obj=resp)
How Should I use AssertRaisesMessage() to let my test be executed without raising the Exception?
Thanks.
EDIT :
After trying falsetru 1st solution, the problem is still the same. As soon as my test enters in the resp = ... part, view is called, then related model method is called and raises the exception.
the full stack trace :
Traceback (most recent call last):
File "/Users/walt/Code/hellodjango/clientizr/tests.py", line 338, in test_invertRewardState_view_fails_notmy_reward
resp = self.client.get(reverse(invertRewardState, args=('1',)))
File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/test/client.py", line 473, in get
response = super(Client, self).get(path, data=data, **extra)
File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/test/client.py", line 280, in get
return self.request(**r)
File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/test/client.py", line 444, in request
six.reraise(*exc_info)
File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/walt/Code/hellodjango/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/walt/Code/hellodjango/clientizr/views.py", line 234, in invertRewardState
reward.invert_reward_state(request.user)
File "/Users/walt/Code/hellodjango/clientizr/models.py", line 606, in invert_reward_state
self.throw_error_if_not_owner_reward(cur_user)
File "/Users/walt/Code/hellodjango/clientizr/models.py", line 587, in throw_error_if_not_owner_reward
raise Exception(EXC_NOT_YOURS)
Exception: Cet objet n'est pas le v\xf4tre
You use assertRaisesMessage as a context manager around the code you expect to fail:
class MyTestCase(TestCase):
def test_invertRewardState_view_fails_notmy_reward(self):
self.client.login(email='gislaine#toto.com', password='azerty')
url = reverse(invertRewardState, args=(1,))
with self.assertRaisesMessage(Exception, EXC_NOT_YOURS):
self.client.get(url)
If you use self.client.get, you will not get an exception directly, but you can check status code.
def test_invertRewardState_view_fails_notmy_reward(self):
self.client.login(email='gislaine#toto.com', password='azerty')
resp = self.client.get(reverse(invertRewardState, args=('1',)))
self.assertEqual(resp.status_code, 500)
self.assertIn(EXC_NOT_YOURS in resp.content)
If you want to get an exception, call the view directly.
def test_invertRewardState_view_fails_notmy_reward(self):
request = HttpRequest()
request.user = User.objects.create(email='gislaine#toto.com') # login
self.assertRaisesMessage(Exception, EXC_NOT_YOURS, invertRewardState, '1')
You can use context manager form as Ned Batchelder suggested.

Categories

Resources