Io Error Was Errno Strerror
Contents |
»learnpythoncommentsWant to join? Log in or sign up in seconds.|Englishlimit my search to /r/learnpythonuse the following search parameters to narrow your results:subreddit:subredditfind submissions in "subreddit"author:usernamefind submissions
Except Ioerror Invalid Syntax
by "username"site:example.comfind submissions from "example.com"url:textsearch for python errno "text" in urlselftext:textsearch for "text" in self post contentsself:yes
Python Error Types
(or self:no)include (or exclude) self postsnsfw:yes (or nsfw:no)include (or exclude) results marked as NSFWe.g. subreddit:aww python logging site:imgur.com dogsee the search faq for details.advanced search: by author, subreddit...this post was submitted on 24 Jun 20154 points (100% upvoted)shortlink: remember mereset passwordloginSubmit a new text postlearnpythonsubscribeunsubscribe66,650 readers100 users here now python open Welcome to /r/LearnPython! Please read the rules and guidelines below and search before posting. Learning resources All learning resources are in the wiki: /r/learnpython/w/index Frequently Asked Questions: /r/learnpython/w/FAQ IRC Chat Join us in the IRC channel: ##learnpython on irc.freenode.net Webchat link: webchat.freenode.net Guide on how to join and different IRC clients: /wiki/IRC Rules General Rules Posting only assignment/project goal is not allowed. Read posting guidelines. Easily googleable questions are not allowed. Posting screenshot of the code is (generally) not allowed. Read posting guidelines. I
User List Committer List Help Tracker Documentation Tracker Development Report Tracker Problem Issue6471 classification Title: errno
Python Sleep
and strerror attributes incorrectly set on socket errors wrapped by python requests urllib Type: behavior Stage: needs patch Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6 process Status: open Resolution: Dependencies: Superseder: Assigned To: Nosy List: amaury.forgeotdarc, catherine, chris.jerdonek, denkoren, ezio.melotti, georg.brandl, orsenthil, r.david.murray Priority: https://www.reddit.com/r/learnpython/comments/3ayqrg/except_ioerror_as_errno_strerror_throwing_syntax/ normal Keywords: patch Created on 2009-07-12 20:54 by ezio.melotti, last changed 2013-07-29 17:16 by r.david.murray. Files File name Uploaded Description Edit keeperrdata.patch catherine, 2012-07-29 02:52 patch to retain errno and strerror review Messages (8) msg90458 - (view) Author: Ezio Melotti (ezio.melotti) * Date: 2009-07-12 20:54 In Python 2.6, https://bugs.python.org/issue6471 socket.error was changed to be a child class of IOError [1]. IOError derives from EnvironmentError [2], and EnvironmentError accepts a 2-tuple used to set the values of the errno and strerror attributes respectively [3]. Apparently the IOError raised by the socket module are instantiated passing (always?) 'socket error' as first arg and an instance of socket.gaierror, socket.timeout or socket.herror (and maybe others) as second arg. The errno attributes ends up being a string (and not a number) and the strerror another exception (and not a str): >>> import socket >>> from urllib import urlopen >>> socket.setdefaulttimeout(0.01) >>> try: urlopen('http://www.python.org') ... except Exception, e: err1 = e ... >>> err1 IOError('socket error', timeout('timed out',)) >>> err1.errno 'socket error' >>> err1.strerror timeout('timed out',) >>> err1.strerror.errno >>> err1.strerror.strerror >>> >>> try: urlopen('http://www.pythonfoobarbaz.org') ... except Exception, e: err2 = e ... >>> err2 IOError('socke
and VariablesOperatorsinput and raw_input via the keyboardConditional StatementsWhile LoopsFor LoopsFormatted outputOutput with PrintSequential Data TypesDictionariesSets and Frozen SetsShallow and Deep CopyFunctionsRecursion and Recursive FunctionsTests, DocTests, UnitTestsMemoization with DecoratorsPassing ArgumentsNamespacesGlobal vs. Local VariablesFile ManagementModular Programming http://www.python-course.eu/exception_handling.php and ModulesIntroduction in Regular ExpressionsRegular Expressions, AdvancedLambda Operator, Filter, Reduce and MapList ComprehensionGeneratorsException HandlingObject Oriented ProgrammingInheritance ExampleSlotsClasses and Class CreationRoad to MetaclassesMetaclassesMetaclass Example: Count Function Calls Exception from the http://codereview.stackexchange.com/questions/57829/better-option-than-errno-for-file-io-error-handling Rule "There are two great rules of life, the one general and the other particular. The first is that everyone can in the end, get what he wants, if io error he only tries. That is the general rule. The particular rule is that every individual is, more or less, an exception to the rule." Samuel Butler Delicate Handling "The finest qualities of our nature, like the bloom on fruits, can be preserved only by the most delicate handling. Yet we do not treat ourselves nor one another thus io error was tenderly." Henry David Thoreau Supported by: Python Training Courses in Canada This topic in German / Deutsche Übersetzung: AusnahmebehandlungPython 2.7This tutorial deals with Python Version 2.7This chapter from our course is available in a version for Python3: Exception HandlingClassroom Training Courses This website contains a free and extensive online tutorial by Bernd Klein. If you are interested in an instructor-led classroom training in Canada or the US, you may have a look at the Python courses by Bernd Klein at Bodenseo © kabliczech - Fotolia.com Overview of Python courses by Bernd Klein. The Truth in Errors "The first step towards amendment is the recognition of error." Seneca "The world always makes the assumption that the exposure of an error is identical with the discovery of truth - that the error and truth are simply opposite. They are nothing of the sort. What the world turns to, when it is cured of one error, is usually simply another error, and maybe one worse than the first one." H.L. Mencken "Great services are not c
for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Code Review Questions Tags Users Badges Unanswered Ask Question _ Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Better option than “errno” for file IO error handling up vote 10 down vote favorite 3 I have the following method for opening a file: void TankFile::OpenForReading(const std::string & filename) { assert(!filename.empty()); errno = 0; file.exceptions(0); // Don't throw file.open(filename, (std::fstream::in | std::fstream::binary)); if (!file.is_open() || !file.good()) { const char * errorStr = strerror(errno); throw TankFileError(Format("Failed to open Tank file \"%s\": '%s'", filename.c_str(), errorStr)); } } The objective here is to attempt to open a file and throw TankFileError with a proper error description on failure. The caller will be expecting this exception type. Everything works fine and I get a nice error message like this if the exception is thrown: Failed to open Tank file "unexistent_file": 'No such file or directory' But what I don't like in that block is having to use the errno global and strerror(). A way around it would be to let the stream throw an exception, then catch it, get the error message from the what() member and re-throw with TankFileError, but I find this solution also a bit hackish, plus, in the tests I did, the resulting error message from std::fstream::failure was pretty cryptic: void TankFile::OpenForReading(const std::string & filename) { assert(!filename.empty()); try { file.exceptions(std::fstream::failbit); file.open(filename, (std::fstream::in | std::fstream::binary)); } catch (std::fstream::failure & err) { throw TankFileError(Format("Failed to open Tank file \"%s\": '%s'", filename.c_str(), err.what())); } } Produced the error message: Failed to open Tank file "unexistent_file": 'ios_base::clear: unspecified iostream_category error'. Is there a better way to implement this? I was hoping that the new C++11 system_error library would provide a way to query this kind of error messages, but from what I've seen, you still have to pass errno around to get an error string. c++ c++11 io error-handling share|improve this question asked Jul 23 '14 at 18:55