What is the SyntaxError builtin practically used for in python?
What is the point of the SyntaxError
builtin in python? It doesn't seem to have any practical use because SyntaxError
s are found by the python interpreter before the code is run. Example:
try:
&@!5_+ #SyntaxError
except: pass
This fails with a SyntaxError
because the SyntaxError
is found before it can be handled. Now this works and fails silently, like intended:
try:
raise SyntaxError
except: pass
However, I have never seen a function
or class
or anything raise a SyntaxError
. So why does this exist so easy to use and catch when the only use seems to be raising it pointlessly? Is there someplace that python raises a SyntaxError
that it can be caught? Or does it just exits in the builtin scope just to be there?
python error-handling syntax syntax-error
add a comment |
What is the point of the SyntaxError
builtin in python? It doesn't seem to have any practical use because SyntaxError
s are found by the python interpreter before the code is run. Example:
try:
&@!5_+ #SyntaxError
except: pass
This fails with a SyntaxError
because the SyntaxError
is found before it can be handled. Now this works and fails silently, like intended:
try:
raise SyntaxError
except: pass
However, I have never seen a function
or class
or anything raise a SyntaxError
. So why does this exist so easy to use and catch when the only use seems to be raising it pointlessly? Is there someplace that python raises a SyntaxError
that it can be caught? Or does it just exits in the builtin scope just to be there?
python error-handling syntax syntax-error
1
It can be used to handle items parsed usingast.literal_eval
for example.
– Ashwini Chaudhary
Jan 1 at 21:12
add a comment |
What is the point of the SyntaxError
builtin in python? It doesn't seem to have any practical use because SyntaxError
s are found by the python interpreter before the code is run. Example:
try:
&@!5_+ #SyntaxError
except: pass
This fails with a SyntaxError
because the SyntaxError
is found before it can be handled. Now this works and fails silently, like intended:
try:
raise SyntaxError
except: pass
However, I have never seen a function
or class
or anything raise a SyntaxError
. So why does this exist so easy to use and catch when the only use seems to be raising it pointlessly? Is there someplace that python raises a SyntaxError
that it can be caught? Or does it just exits in the builtin scope just to be there?
python error-handling syntax syntax-error
What is the point of the SyntaxError
builtin in python? It doesn't seem to have any practical use because SyntaxError
s are found by the python interpreter before the code is run. Example:
try:
&@!5_+ #SyntaxError
except: pass
This fails with a SyntaxError
because the SyntaxError
is found before it can be handled. Now this works and fails silently, like intended:
try:
raise SyntaxError
except: pass
However, I have never seen a function
or class
or anything raise a SyntaxError
. So why does this exist so easy to use and catch when the only use seems to be raising it pointlessly? Is there someplace that python raises a SyntaxError
that it can be caught? Or does it just exits in the builtin scope just to be there?
python error-handling syntax syntax-error
python error-handling syntax syntax-error
asked Jan 1 at 21:09
Ethan K888Ethan K888
563323
563323
1
It can be used to handle items parsed usingast.literal_eval
for example.
– Ashwini Chaudhary
Jan 1 at 21:12
add a comment |
1
It can be used to handle items parsed usingast.literal_eval
for example.
– Ashwini Chaudhary
Jan 1 at 21:12
1
1
It can be used to handle items parsed using
ast.literal_eval
for example.– Ashwini Chaudhary
Jan 1 at 21:12
It can be used to handle items parsed using
ast.literal_eval
for example.– Ashwini Chaudhary
Jan 1 at 21:12
add a comment |
3 Answers
3
active
oldest
votes
I believe that documentation is self-explanatory:
Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions
exec()
oreval()
, or when reading the initial script or standard input (also interactively).
Soimport ...
,__import__(...)
,exec(...)
andeval(...)
? Is that it?
– Ethan K888
Jan 1 at 21:14
@EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...
– vishes_shell
Jan 1 at 21:18
But can you catch them?
– Ethan K888
Jan 1 at 21:19
1
@EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.
– vishes_shell
Jan 1 at 21:21
add a comment |
There's very rarely a use for using SyntaxError
in your program. It's usually only used for the python interpreter when it encounters invalid syntax.
The only case I can think of that you would ever catch a SyntaxError
is if you are using exec
or eval
on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError
.
add a comment |
The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did
find . -type f -name "*.py" | xargs grep "raise SyntaxError"
We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml
library uses them in parsing XPath expressions.
This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.
I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval
etc. have to raise something, as the docs and the previous answer state.
Nice technical answer! Thanks for doing so much looking around!
– Ethan K888
Jan 8 at 21:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998960%2fwhat-is-the-syntaxerror-builtin-practically-used-for-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I believe that documentation is self-explanatory:
Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions
exec()
oreval()
, or when reading the initial script or standard input (also interactively).
Soimport ...
,__import__(...)
,exec(...)
andeval(...)
? Is that it?
– Ethan K888
Jan 1 at 21:14
@EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...
– vishes_shell
Jan 1 at 21:18
But can you catch them?
– Ethan K888
Jan 1 at 21:19
1
@EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.
– vishes_shell
Jan 1 at 21:21
add a comment |
I believe that documentation is self-explanatory:
Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions
exec()
oreval()
, or when reading the initial script or standard input (also interactively).
Soimport ...
,__import__(...)
,exec(...)
andeval(...)
? Is that it?
– Ethan K888
Jan 1 at 21:14
@EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...
– vishes_shell
Jan 1 at 21:18
But can you catch them?
– Ethan K888
Jan 1 at 21:19
1
@EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.
– vishes_shell
Jan 1 at 21:21
add a comment |
I believe that documentation is self-explanatory:
Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions
exec()
oreval()
, or when reading the initial script or standard input (also interactively).
I believe that documentation is self-explanatory:
Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions
exec()
oreval()
, or when reading the initial script or standard input (also interactively).
answered Jan 1 at 21:13
vishes_shellvishes_shell
10.7k34046
10.7k34046
Soimport ...
,__import__(...)
,exec(...)
andeval(...)
? Is that it?
– Ethan K888
Jan 1 at 21:14
@EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...
– vishes_shell
Jan 1 at 21:18
But can you catch them?
– Ethan K888
Jan 1 at 21:19
1
@EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.
– vishes_shell
Jan 1 at 21:21
add a comment |
Soimport ...
,__import__(...)
,exec(...)
andeval(...)
? Is that it?
– Ethan K888
Jan 1 at 21:14
@EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...
– vishes_shell
Jan 1 at 21:18
But can you catch them?
– Ethan K888
Jan 1 at 21:19
1
@EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.
– vishes_shell
Jan 1 at 21:21
So
import ...
, __import__(...)
, exec(...)
and eval(...)
? Is that it?– Ethan K888
Jan 1 at 21:14
So
import ...
, __import__(...)
, exec(...)
and eval(...)
? Is that it?– Ethan K888
Jan 1 at 21:14
@EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...
– vishes_shell
Jan 1 at 21:18
@EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...
– vishes_shell
Jan 1 at 21:18
But can you catch them?
– Ethan K888
Jan 1 at 21:19
But can you catch them?
– Ethan K888
Jan 1 at 21:19
1
1
@EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.
– vishes_shell
Jan 1 at 21:21
@EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.
– vishes_shell
Jan 1 at 21:21
add a comment |
There's very rarely a use for using SyntaxError
in your program. It's usually only used for the python interpreter when it encounters invalid syntax.
The only case I can think of that you would ever catch a SyntaxError
is if you are using exec
or eval
on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError
.
add a comment |
There's very rarely a use for using SyntaxError
in your program. It's usually only used for the python interpreter when it encounters invalid syntax.
The only case I can think of that you would ever catch a SyntaxError
is if you are using exec
or eval
on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError
.
add a comment |
There's very rarely a use for using SyntaxError
in your program. It's usually only used for the python interpreter when it encounters invalid syntax.
The only case I can think of that you would ever catch a SyntaxError
is if you are using exec
or eval
on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError
.
There's very rarely a use for using SyntaxError
in your program. It's usually only used for the python interpreter when it encounters invalid syntax.
The only case I can think of that you would ever catch a SyntaxError
is if you are using exec
or eval
on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError
.
answered Jan 1 at 21:57
Supa Mega Ducky Momo da WaffleSupa Mega Ducky Momo da Waffle
2,01351229
2,01351229
add a comment |
add a comment |
The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did
find . -type f -name "*.py" | xargs grep "raise SyntaxError"
We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml
library uses them in parsing XPath expressions.
This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.
I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval
etc. have to raise something, as the docs and the previous answer state.
Nice technical answer! Thanks for doing so much looking around!
– Ethan K888
Jan 8 at 21:51
add a comment |
The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did
find . -type f -name "*.py" | xargs grep "raise SyntaxError"
We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml
library uses them in parsing XPath expressions.
This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.
I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval
etc. have to raise something, as the docs and the previous answer state.
Nice technical answer! Thanks for doing so much looking around!
– Ethan K888
Jan 8 at 21:51
add a comment |
The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did
find . -type f -name "*.py" | xargs grep "raise SyntaxError"
We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml
library uses them in parsing XPath expressions.
This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.
I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval
etc. have to raise something, as the docs and the previous answer state.
The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did
find . -type f -name "*.py" | xargs grep "raise SyntaxError"
We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml
library uses them in parsing XPath expressions.
This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.
I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval
etc. have to raise something, as the docs and the previous answer state.
edited Jan 8 at 21:54
Ethan K888
563323
563323
answered Jan 1 at 21:29
JETMJETM
2,04341629
2,04341629
Nice technical answer! Thanks for doing so much looking around!
– Ethan K888
Jan 8 at 21:51
add a comment |
Nice technical answer! Thanks for doing so much looking around!
– Ethan K888
Jan 8 at 21:51
Nice technical answer! Thanks for doing so much looking around!
– Ethan K888
Jan 8 at 21:51
Nice technical answer! Thanks for doing so much looking around!
– Ethan K888
Jan 8 at 21:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998960%2fwhat-is-the-syntaxerror-builtin-practically-used-for-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It can be used to handle items parsed using
ast.literal_eval
for example.– Ashwini Chaudhary
Jan 1 at 21:12